essences/
character_state.rs

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