essences/
opponents.rs

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