Skip to main content

nil_core/
ruler.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::error::{Error, Result};
5use crate::ethic::Ethics;
6use crate::npc::bot::{Bot, BotId};
7use crate::npc::precursor::{Precursor, PrecursorId};
8use crate::player::{Player, PlayerId};
9use crate::resources::Resources;
10use crate::resources::gold::Gold;
11use crate::resources::influence::Influence;
12use derive_more::{TryUnwrap, Unwrap};
13use serde::{Deserialize, Serialize};
14use std::{cmp, fmt, mem};
15use strum::EnumIs;
16
17#[derive(Clone, Debug, PartialEq, Eq, Hash, Deserialize, Serialize, EnumIs)]
18#[serde(tag = "kind", rename_all = "kebab-case")]
19#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
20pub enum Ruler {
21  Bot { id: BotId },
22  Player { id: PlayerId },
23  Precursor { id: PrecursorId },
24}
25
26impl Ruler {
27  #[inline]
28  pub fn bot(&self) -> Option<&BotId> {
29    if let Self::Bot { id } = self { Some(id) } else { None }
30  }
31
32  #[inline]
33  pub fn player(&self) -> Option<&PlayerId> {
34    if let Self::Player { id } = self { Some(id) } else { None }
35  }
36
37  #[inline]
38  pub fn precursor(&self) -> Option<PrecursorId> {
39    if let Self::Precursor { id } = self { Some(*id) } else { None }
40  }
41
42  pub fn is_bot_and<F>(&self, f: F) -> bool
43  where
44    F: FnOnce(&BotId) -> bool,
45  {
46    self.bot().is_some_and(f)
47  }
48
49  pub fn is_player_and<F>(&self, f: F) -> bool
50  where
51    F: FnOnce(&PlayerId) -> bool,
52  {
53    self.player().is_some_and(f)
54  }
55
56  pub fn is_precursor_and<F>(&self, f: F) -> bool
57  where
58    F: FnOnce(PrecursorId) -> bool,
59  {
60    self.precursor().is_some_and(f)
61  }
62}
63
64impl AsRef<str> for Ruler {
65  fn as_ref(&self) -> &str {
66    match self {
67      Self::Bot { id } => id.as_str(),
68      Self::Player { id } => id.as_str(),
69      Self::Precursor { id } => id.as_ref(),
70    }
71  }
72}
73
74impl fmt::Display for Ruler {
75  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76    match self {
77      Self::Bot { id } => id.fmt(f),
78      Self::Player { id } => id.fmt(f),
79      Self::Precursor { id } => id.fmt(f),
80    }
81  }
82}
83
84impl From<&Bot> for Ruler {
85  fn from(bot: &Bot) -> Self {
86    Self::Bot { id: bot.id() }
87  }
88}
89
90impl From<BotId> for Ruler {
91  fn from(id: BotId) -> Self {
92    Self::Bot { id }
93  }
94}
95
96impl From<&BotId> for Ruler {
97  fn from(id: &BotId) -> Self {
98    Self::Bot { id: id.clone() }
99  }
100}
101
102impl From<&Player> for Ruler {
103  fn from(player: &Player) -> Self {
104    Self::Player { id: player.id() }
105  }
106}
107
108impl From<PlayerId> for Ruler {
109  fn from(id: PlayerId) -> Self {
110    Self::Player { id }
111  }
112}
113
114impl From<&PlayerId> for Ruler {
115  fn from(id: &PlayerId) -> Self {
116    Self::Player { id: id.clone() }
117  }
118}
119
120impl From<&dyn Precursor> for Ruler {
121  fn from(precursor: &dyn Precursor) -> Self {
122    Self::Precursor { id: precursor.id() }
123  }
124}
125
126impl<T: Precursor> From<&T> for Ruler {
127  fn from(precursor: &T) -> Self {
128    Self::Precursor { id: precursor.id() }
129  }
130}
131
132impl From<PrecursorId> for Ruler {
133  fn from(id: PrecursorId) -> Self {
134    Self::Precursor { id }
135  }
136}
137
138impl From<RulerRef<'_>> for Ruler {
139  fn from(ruler: RulerRef<'_>) -> Self {
140    match ruler {
141      RulerRef::Bot(bot) => Self::Bot { id: bot.id() },
142      RulerRef::Player(player) => Self::Player { id: player.id() },
143      RulerRef::Precursor(precursor) => Self::Precursor { id: precursor.id() },
144    }
145  }
146}
147
148impl From<RulerRefMut<'_>> for Ruler {
149  fn from(ruler: RulerRefMut<'_>) -> Self {
150    match ruler {
151      RulerRefMut::Bot(bot) => Self::Bot { id: bot.id() },
152      RulerRefMut::Player(player) => Self::Player { id: player.id() },
153      RulerRefMut::Precursor(precursor) => Self::Precursor { id: precursor.id() },
154    }
155  }
156}
157
158impl PartialOrd for Ruler {
159  fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
160    Some(self.cmp(other))
161  }
162}
163
164impl Ord for Ruler {
165  fn cmp(&self, other: &Self) -> cmp::Ordering {
166    match (self, other) {
167      (Self::Bot { id: a }, Self::Bot { id: b }) => a.cmp(b),
168      (Self::Player { id: a }, Self::Player { id: b }) => a.cmp(b),
169      (Self::Precursor { id: a }, Self::Precursor { id: b }) => a.cmp(b),
170      (Self::Bot { .. }, Self::Player { .. }) => cmp::Ordering::Less,
171      (Self::Bot { .. }, Self::Precursor { .. }) => cmp::Ordering::Greater,
172      (Self::Player { .. }, _) => cmp::Ordering::Greater,
173      (Self::Precursor { .. }, _) => cmp::Ordering::Less,
174    }
175  }
176}
177
178#[derive(EnumIs, TryUnwrap, Unwrap)]
179#[try_unwrap(ref)]
180#[unwrap(ref)]
181pub enum RulerRef<'a> {
182  Bot(&'a Bot),
183  Player(&'a Player),
184  Precursor(&'a dyn Precursor),
185}
186
187impl<'a> RulerRef<'a> {
188  pub fn ethics(&self) -> Option<&'a Ethics> {
189    match self {
190      Self::Bot(bot) => Some(bot.ethics()),
191      Self::Player(..) => None,
192      Self::Precursor(precursor) => Some(precursor.ethics()),
193    }
194  }
195
196  pub fn resources(&self) -> Resources {
197    match self {
198      Self::Bot(bot) => bot.resources(),
199      Self::Player(player) => player.resources(),
200      Self::Precursor(precursor) => precursor.resources(),
201    }
202  }
203
204  #[inline]
205  pub fn has_resources(&self, resources: Resources) -> bool {
206    self
207      .resources()
208      .checked_sub(resources)
209      .is_some()
210  }
211
212  pub fn gold(&self) -> Gold {
213    match self {
214      Self::Bot(bot) => bot.gold(),
215      Self::Player(player) => player.gold(),
216      Self::Precursor(precursor) => precursor.gold(),
217    }
218  }
219
220  pub fn influence(&self) -> Influence {
221    match self {
222      Self::Bot(bot) => bot.influence(),
223      Self::Player(player) => player.influence(),
224      Self::Precursor(precursor) => precursor.influence(),
225    }
226  }
227}
228
229impl<'a> From<&'a Bot> for RulerRef<'a> {
230  fn from(bot: &'a Bot) -> Self {
231    Self::Bot(bot)
232  }
233}
234
235impl<'a> From<&'a Player> for RulerRef<'a> {
236  fn from(player: &'a Player) -> Self {
237    Self::Player(player)
238  }
239}
240
241impl<'a> From<&'a dyn Precursor> for RulerRef<'a> {
242  fn from(precursor: &'a dyn Precursor) -> Self {
243    Self::Precursor(precursor)
244  }
245}
246
247#[derive(EnumIs, TryUnwrap, Unwrap)]
248#[try_unwrap(ref)]
249#[unwrap(ref)]
250pub enum RulerRefMut<'a> {
251  Bot(&'a mut Bot),
252  Player(&'a mut Player),
253  Precursor(&'a mut dyn Precursor),
254}
255
256impl<'a> RulerRefMut<'a> {
257  pub fn resources_mut(&'a mut self) -> &'a mut Resources {
258    match self {
259      Self::Bot(bot) => bot.resources_mut(),
260      Self::Player(player) => player.resources_mut(),
261      Self::Precursor(precursor) => precursor.resources_mut(),
262    }
263  }
264
265  /// Withdraws the specified resources from the ruler.
266  pub fn withdraw_resources(&'a mut self, resources: Resources) -> Result<()> {
267    let ruler_resources = self.resources_mut();
268    match ruler_resources.checked_sub(resources) {
269      Some(result) => *ruler_resources = result,
270      None => return Err(Error::InsufficientResources),
271    }
272
273    Ok(())
274  }
275
276  /// Takes all resources from the ruler, leaving them with nothing.
277  pub fn take_resources(&mut self) -> Resources {
278    match self {
279      Self::Bot(bot) => mem::take(bot.resources_mut()),
280      Self::Player(player) => mem::take(player.resources_mut()),
281      Self::Precursor(precursor) => mem::take(precursor.resources_mut()),
282    }
283  }
284
285  pub fn gold_mut(&mut self) -> &mut Gold {
286    match self {
287      Self::Bot(bot) => bot.gold_mut(),
288      Self::Player(player) => player.gold_mut(),
289      Self::Precursor(precursor) => precursor.gold_mut(),
290    }
291  }
292
293  /// Withdraws the specified amount of gold from the ruler.
294  pub fn withdraw_gold(&mut self, gold: Gold) -> Result<()> {
295    let ruler_gold = self.gold_mut();
296    match ruler_gold.checked_sub(gold) {
297      Some(result) => *ruler_gold = result,
298      None => return Err(Error::InsufficientGold),
299    }
300
301    Ok(())
302  }
303}
304
305impl<'a> From<&'a mut Bot> for RulerRefMut<'a> {
306  fn from(bot: &'a mut Bot) -> Self {
307    Self::Bot(bot)
308  }
309}
310
311impl<'a> From<&'a mut Player> for RulerRefMut<'a> {
312  fn from(player: &'a mut Player) -> Self {
313    Self::Player(player)
314  }
315}
316
317impl<'a> From<&'a mut dyn Precursor> for RulerRefMut<'a> {
318  fn from(precursor: &'a mut dyn Precursor) -> Self {
319    Self::Precursor(precursor)
320  }
321}