Skip to main content

nil_core/ranking/
score.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use crate::military::army::Army;
5use crate::military::army::personnel::ArmyPersonnel;
6use crate::military::squad::Squad;
7use derive_more::Display;
8use nil_num::impl_mul_ceil;
9use nil_util::{ConstDeref, F64Math};
10use serde::{Deserialize, Serialize};
11use std::cmp::Ordering;
12use std::iter::Sum;
13use std::ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign};
14
15#[derive(Copy, Debug, Display, Deserialize, Serialize, ConstDeref, F64Math)]
16#[derive_const(Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
17#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
18pub struct Score(u32);
19
20impl Score {
21  pub const ZERO: Score = Score(0);
22
23  #[inline]
24  pub const fn new(value: u32) -> Self {
25    Self(value)
26  }
27}
28
29impl const From<u32> for Score {
30  fn from(value: u32) -> Self {
31    Self::new(value)
32  }
33}
34
35impl const From<Score> for u32 {
36  fn from(value: Score) -> Self {
37    value.0
38  }
39}
40
41impl const From<f64> for Score {
42  fn from(value: f64) -> Self {
43    Self::new(value as u32)
44  }
45}
46
47impl const From<Score> for f64 {
48  fn from(value: Score) -> Self {
49    f64::from(value.0)
50  }
51}
52
53impl const PartialEq<u32> for Score {
54  fn eq(&self, other: &u32) -> bool {
55    self.0.eq(other)
56  }
57}
58
59impl const PartialOrd<u32> for Score {
60  fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
61    self.0.partial_cmp(other)
62  }
63}
64
65impl<'a> Sum<&'a Squad> for Score {
66  fn sum<I>(iter: I) -> Self
67  where
68    I: Iterator<Item = &'a Squad>,
69  {
70    iter.fold(Score::default(), |mut acc, squad| {
71      acc += squad.score();
72      acc
73    })
74  }
75}
76
77impl<'a> Sum<&'a ArmyPersonnel> for Score {
78  fn sum<I>(iter: I) -> Self
79  where
80    I: Iterator<Item = &'a ArmyPersonnel>,
81  {
82    iter.flat_map(ArmyPersonnel::iter).sum()
83  }
84}
85
86impl<'a> Sum<&'a Army> for Score {
87  fn sum<I>(iter: I) -> Self
88  where
89    I: Iterator<Item = &'a Army>,
90  {
91    iter.flat_map(Army::iter).sum()
92  }
93}
94
95impl const Add for Score {
96  type Output = Score;
97
98  fn add(self, rhs: Self) -> Self::Output {
99    Self(self.0.saturating_add(rhs.0))
100  }
101}
102
103impl const AddAssign for Score {
104  fn add_assign(&mut self, rhs: Self) {
105    *self = *self + rhs;
106  }
107}
108
109impl const Add<u32> for Score {
110  type Output = Score;
111
112  fn add(self, rhs: u32) -> Self::Output {
113    Self(self.0.saturating_add(rhs))
114  }
115}
116
117impl const AddAssign<u32> for Score {
118  fn add_assign(&mut self, rhs: u32) {
119    *self = *self + rhs;
120  }
121}
122
123impl const Sub for Score {
124  type Output = Score;
125
126  fn sub(self, rhs: Self) -> Self::Output {
127    Self(self.0.saturating_sub(rhs.0))
128  }
129}
130
131impl const SubAssign for Score {
132  fn sub_assign(&mut self, rhs: Self) {
133    *self = *self - rhs;
134  }
135}
136
137impl const Mul for Score {
138  type Output = Score;
139
140  fn mul(self, rhs: Score) -> Self::Output {
141    Self(self.0.saturating_mul(rhs.0))
142  }
143}
144
145impl const Mul<u32> for Score {
146  type Output = Score;
147
148  fn mul(self, rhs: u32) -> Self::Output {
149    Self(self.0.saturating_mul(rhs))
150  }
151}
152
153impl const MulAssign for Score {
154  fn mul_assign(&mut self, rhs: Self) {
155    *self = *self * rhs;
156  }
157}
158
159impl_mul_ceil!(Score);