Skip to main content

nil_core/infrastructure/
stats.rs

1// Copyright (C) Call of Nil contributors
2// SPDX-License-Identifier: AGPL-3.0-only
3
4use super::Infrastructure;
5use super::building::wall::WallStatsTable;
6use super::building::{BuildingId, BuildingStatsTable, MineId, StorageId};
7use super::mine::MineStatsTable;
8use super::storage::StorageStatsTable;
9use crate::error::{Error, Result};
10use serde::{Deserialize, Serialize};
11use std::collections::HashMap;
12use strum::IntoEnumIterator;
13
14#[derive(Debug, Deserialize, Serialize)]
15#[serde(rename_all = "camelCase")]
16pub struct InfrastructureStats {
17  building: HashMap<BuildingId, BuildingStatsTable>,
18  mine: HashMap<MineId, MineStatsTable>,
19  storage: HashMap<StorageId, StorageStatsTable>,
20  wall: WallStatsTable,
21}
22
23impl InfrastructureStats {
24  pub fn new() -> Self {
25    let infrastructure = Infrastructure::default();
26    let building = BuildingId::iter()
27      .map(|id| (id, BuildingStatsTable::new(infrastructure.building(id))))
28      .collect();
29
30    let mine = MineId::iter()
31      .map(|id| (id, MineStatsTable::new(infrastructure.mine(id))))
32      .collect();
33
34    let storage = StorageId::iter()
35      .map(|id| (id, StorageStatsTable::new(infrastructure.storage(id))))
36      .collect();
37
38    let wall = WallStatsTable::new();
39
40    Self { building, mine, storage, wall }
41  }
42
43  #[inline]
44  pub fn building(&self, id: BuildingId) -> Result<&BuildingStatsTable> {
45    self
46      .building
47      .get(&id)
48      .ok_or(Error::BuildingStatsNotFound(id))
49  }
50
51  #[inline]
52  pub fn mine(&self, id: MineId) -> Result<&MineStatsTable> {
53    self
54      .mine
55      .get(&id)
56      .ok_or(Error::MineStatsNotFound(id))
57  }
58
59  #[inline]
60  pub fn storage(&self, id: StorageId) -> Result<&StorageStatsTable> {
61    self
62      .storage
63      .get(&id)
64      .ok_or(Error::StorageStatsNotFound(id))
65  }
66
67  #[inline]
68  pub fn wall(&self) -> &WallStatsTable {
69    &self.wall
70  }
71}
72
73impl Default for InfrastructureStats {
74  fn default() -> Self {
75    Self::new()
76  }
77}