essences/
opponents.rs

1use crate::{abilities, bots, character_state, characters, class, items, users};
2
3use crate::prelude::*;
4
5#[derive(
6    Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, Tsify, CustomType, Default,
7)]
8#[tsify(into_wasm_abi, from_wasm_abi)]
9pub struct OpponentPreview {
10    pub id: uuid::Uuid,
11    pub username: String,
12    pub photo_url: String,
13    pub arena_rating: i64,
14    pub power: i64,
15    pub level: i64,
16    pub class_id: class::ClassId,
17    pub is_bot: bool,
18}
19
20impl OpponentPreview {
21    pub fn from_character_state(character_state: &character_state::CharacterState) -> Self {
22        Self {
23            id: character_state.character.id,
24            username: character_state.user.username.clone(),
25            photo_url: character_state.user.photo_url.clone().unwrap_or_default(),
26            arena_rating: character_state.character.arena_rating,
27            power: character_state.character.power,
28            level: character_state.character.character_level,
29            class_id: character_state.character.class,
30            is_bot: false,
31        }
32    }
33
34    pub fn from_bot(bot: &bots::Bot) -> Self {
35        Self {
36            id: bot.id,
37            username: bot.username.clone(),
38            photo_url: bot.photo_url.clone(),
39            arena_rating: bot.arena_rating,
40            power: bot.power,
41            level: bot.level,
42            class_id: bot.class_id,
43            is_bot: true,
44        }
45    }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
49pub struct OpponentHuman {
50    pub user: users::User,
51    pub character: characters::Character,
52}
53
54#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
55pub struct OpponentBot {
56    pub bot: bots::Bot,
57}
58
59#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq)]
60pub enum Opponent {
61    Human(Box<OpponentHuman>),
62    Bot(Box<OpponentBot>),
63}
64
65impl Opponent {
66    pub fn id(&self) -> uuid::Uuid {
67        match self {
68            Opponent::Human(human) => human.character.id,
69            Opponent::Bot(bot) => bot.bot.id,
70        }
71    }
72
73    pub fn rating_calculation_data(&self) -> RatingCalculationData {
74        match self {
75            Opponent::Human(human) => RatingCalculationData {
76                arena_rating: human.character.arena_rating,
77                power: human.character.power,
78            },
79            Opponent::Bot(bot) => RatingCalculationData {
80                arena_rating: bot.bot.arena_rating,
81                power: bot.bot.power,
82            },
83        }
84    }
85
86    pub fn rating(&self) -> i64 {
87        match self {
88            Opponent::Human(human) => human.character.arena_rating,
89            Opponent::Bot(bot) => bot.bot.arena_rating,
90        }
91    }
92}
93
94impl From<Opponent> for OpponentPreview {
95    fn from(opponent: Opponent) -> Self {
96        match opponent {
97            Opponent::Human(human) => OpponentPreview {
98                id: human.character.id,
99                username: human.user.username,
100                photo_url: human.user.photo_url.unwrap_or_default(),
101                arena_rating: human.character.arena_rating,
102                power: human.character.power,
103                level: human.character.character_level,
104                class_id: human.character.class,
105                is_bot: false,
106            },
107            Opponent::Bot(bot) => OpponentPreview {
108                id: bot.bot.id,
109                username: bot.bot.username,
110                photo_url: bot.bot.photo_url,
111                arena_rating: bot.bot.arena_rating,
112                power: bot.bot.power,
113                level: bot.bot.level,
114                class_id: bot.bot.class_id,
115                is_bot: true,
116            },
117        }
118    }
119}
120
121#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, JsonSchema, Tsify, CustomType)]
122pub struct RatingCalculationData {
123    pub arena_rating: i64,
124    pub power: i64,
125}
126
127#[derive(Debug, Clone, Serialize, Deserialize, Eq, JsonSchema, Tsify, PartialEq)]
128pub struct OpponentStateHuman {
129    pub character_state: character_state::CharacterState,
130}
131
132#[derive(Debug, Clone, Serialize, Deserialize, Eq, JsonSchema, Tsify, PartialEq)]
133pub struct OpponentStateBot {
134    pub bot: bots::Bot,
135    pub inventory: Vec<items::Item>,
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize, Eq, JsonSchema, Tsify, PartialEq)]
139pub enum OpponentState {
140    Human(Box<OpponentStateHuman>),
141    Bot(Box<OpponentStateBot>),
142}
143
144impl OpponentState {
145    pub fn id(&self) -> uuid::Uuid {
146        match self {
147            OpponentState::Human(human) => human.character_state.character.id,
148            OpponentState::Bot(bot) => bot.bot.id,
149        }
150    }
151
152    pub fn power(&self) -> i64 {
153        match self {
154            OpponentState::Human(human) => human.character_state.character.power,
155            OpponentState::Bot(bot) => bot.bot.power,
156        }
157    }
158
159    pub fn rating_calculation_data(&self) -> RatingCalculationData {
160        match self {
161            OpponentState::Human(human) => RatingCalculationData {
162                arena_rating: human.character_state.character.arena_rating,
163                power: human.character_state.character.power,
164            },
165            OpponentState::Bot(bot) => RatingCalculationData {
166                arena_rating: bot.bot.arena_rating,
167                power: bot.bot.power,
168            },
169        }
170    }
171
172    pub fn character_state(&self) -> Option<&character_state::CharacterState> {
173        match self {
174            OpponentState::Human(human) => Some(&human.character_state),
175            OpponentState::Bot(_) => None,
176        }
177    }
178
179    pub fn level(&self) -> i64 {
180        match self {
181            OpponentState::Human(human) => human.character_state.character.character_level,
182            OpponentState::Bot(bot) => bot.bot.level,
183        }
184    }
185
186    pub fn inventory(&self) -> &Vec<items::Item> {
187        match self {
188            OpponentState::Human(human) => &human.character_state.inventory,
189            OpponentState::Bot(bot) => &bot.inventory,
190        }
191    }
192
193    pub fn class(&self) -> class::ClassId {
194        match self {
195            OpponentState::Human(human) => human.character_state.character.class,
196            OpponentState::Bot(bot) => bot.bot.class_id,
197        }
198    }
199
200    pub fn equipped_abilities(&self) -> &abilities::EquippedAbilities {
201        match self {
202            OpponentState::Human(human) => &human.character_state.equipped_abilities,
203            OpponentState::Bot(bot) => &bot.bot.equipped_abilities,
204        }
205    }
206
207    pub fn equipped_pets(&self) -> Option<&super::pets::EquippedPets> {
208        match self {
209            OpponentState::Human(human) => Some(&human.character_state.equipped_pets),
210            OpponentState::Bot(_) => None,
211        }
212    }
213
214    pub fn is_bot(&self) -> bool {
215        match self {
216            OpponentState::Human(_) => false,
217            OpponentState::Bot(_) => true,
218        }
219    }
220}