essences/
character_state.rs

1use crate::ad_usage::AdUsageMap;
2use crate::buffs::ActiveBuff;
3use crate::character_settings::CharacterSettings;
4use crate::class::CharacterClass;
5use crate::currency::{CurrencyId, CurrencyUnit};
6use crate::entity::EntityAttributes;
7use crate::talent_tree::TalentLevelsMap;
8
9use super::{abilities, bundles, characters, items, pets, skins, statue, users, vassals};
10
11use crate::prelude::*;
12
13#[derive(Clone, PartialEq, Eq, Default, Debug, JsonSchema, Tsify, Serialize, Deserialize)]
14pub struct CharacterState {
15    pub user: users::User,
16    pub character: characters::Character,
17    pub currencies: Vec<CurrencyUnit>,
18    pub inventory: Vec<items::Item>,
19    pub all_abilities: Vec<abilities::Ability>, // Every ability that player have with equipped abilities
20    pub equipped_abilities: abilities::EquippedAbilities, // Only player's equipped abilitie
21    pub all_pets: Vec<pets::Pet>,
22    pub equipped_pets: pets::EquippedPets,
23    pub suzerain: Option<vassals::Suzerain>,
24    pub vassals: Vec<vassals::Vassal>,
25    pub vassal_tasks: Vec<vassals::VassalTask>,
26    pub player_attributes: EntityAttributes,
27    pub bundle_step_generic: Vec<bundles::BundleStepGeneric>,
28    pub character_settings: CharacterSettings,
29    pub character_skins: skins::CharacterSkins,
30    pub talent_levels: TalentLevelsMap,
31    pub statue_state: statue::StatueState,
32    pub ad_usage: AdUsageMap,
33    pub active_buffs: Vec<ActiveBuff>,
34    pub character_classes: Vec<CharacterClass>,
35}
36
37impl CharacterState {
38    pub fn get_vassal(&self, vassal_id: uuid::Uuid) -> anyhow::Result<vassals::Vassal> {
39        match self
40            .vassals
41            .iter()
42            .find(|vassal| vassal.character_id == vassal_id)
43        {
44            Some(vassal) => Ok(vassal.clone()),
45            None => anyhow::bail!("No vassal with given id={}", vassal_id),
46        }
47    }
48
49    pub fn has_vassal(&self, vassal_id: uuid::Uuid) -> bool {
50        self.vassals
51            .iter()
52            .any(|vassal| vassal.character_id == vassal_id)
53    }
54
55    pub fn get_currency(&self, currency_id: CurrencyId) -> i64 {
56        if let Some(currency_unity) = self
57            .currencies
58            .iter()
59            .find(|currency| currency.currency_id == currency_id)
60        {
61            return currency_unity.amount;
62        };
63
64        0
65    }
66
67    pub fn get_arena_tickets(&self, currency_id: CurrencyId) -> i64 {
68        if let Some(arena_tickets_unit) = self
69            .currencies
70            .iter()
71            .find(|currency| currency.currency_id == currency_id)
72        {
73            return arena_tickets_unit.amount;
74        };
75
76        0
77    }
78
79    pub fn decrement_arena_tickets(&mut self, currency_id: CurrencyId) -> anyhow::Result<()> {
80        let Some(arena_tickets_unit) = self
81            .currencies
82            .iter_mut()
83            .find(|currency| currency.currency_id == currency_id)
84        else {
85            anyhow::bail!(
86                "Failed to get arena tickets currency unit with id={}",
87                currency_id
88            );
89        };
90
91        if arena_tickets_unit.amount <= 0 {
92            anyhow::bail!("No available arena tickets to decrement",);
93        }
94
95        arena_tickets_unit.amount -= 1;
96
97        Ok(())
98    }
99}
100
101#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema, Tsify)]
102#[tsify(into_wasm_abi, from_wasm_abi)]
103pub struct GetCharacterStateRequest {
104    pub character_id: uuid::Uuid,
105}
106
107#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema, Tsify)]
108#[tsify(into_wasm_abi, from_wasm_abi)]
109pub struct FullClearCharacterRequest {
110    pub username: String,
111}
112
113#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, JsonSchema, Tsify)]
114#[tsify(into_wasm_abi, from_wasm_abi)]
115pub enum GetCharacterStateResponse {
116    Ok {
117        character_state: Box<CharacterState>,
118    },
119    Error {
120        code: String,
121        message: String,
122    },
123}