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::r#impl::wall::WallStatsTable;
6use super::building::{BuildingId, BuildingStatsTable, MineId, StorageId};
7use super::mine::MineStatsTable;
8use super::storage::StorageStatsTable;
9use crate::error::{Error, Result};
10use crate::world::config::WorldConfig;
11use serde::{Deserialize, Serialize};
12use std::collections::HashMap;
13use strum::IntoEnumIterator;
14
15#[derive(Debug, Deserialize, Serialize)]
16#[serde(rename_all = "camelCase")]
17#[cfg_attr(feature = "typescript", derive(ts_rs::TS))]
18pub struct InfrastructureStats {
19  building: HashMap<BuildingId, BuildingStatsTable>,
20  mine: HashMap<MineId, MineStatsTable>,
21  storage: HashMap<StorageId, StorageStatsTable>,
22  wall: WallStatsTable,
23}
24
25impl InfrastructureStats {
26  pub fn new(config: &WorldConfig) -> Self {
27    let infrastructure = Infrastructure::default();
28    let building = BuildingId::iter()
29      .map(|id| (id, BuildingStatsTable::new(infrastructure.building(id))))
30      .collect();
31
32    let mine = MineId::iter()
33      .map(|id| (id, MineStatsTable::new(config, infrastructure.mine(id))))
34      .collect();
35
36    let storage = StorageId::iter()
37      .map(|id| (id, StorageStatsTable::new(infrastructure.storage(id))))
38      .collect();
39
40    let wall = WallStatsTable::new();
41
42    Self { building, mine, storage, wall }
43  }
44
45  #[inline]
46  pub fn building(&self, id: BuildingId) -> Result<&BuildingStatsTable> {
47    self
48      .building
49      .get(&id)
50      .ok_or(Error::BuildingStatsNotFound(id))
51  }
52
53  #[inline]
54  pub fn mine(&self, id: MineId) -> Result<&MineStatsTable> {
55    self
56      .mine
57      .get(&id)
58      .ok_or(Error::MineStatsNotFound(id))
59  }
60
61  #[inline]
62  pub fn storage(&self, id: StorageId) -> Result<&StorageStatsTable> {
63    self
64      .storage
65      .get(&id)
66      .ok_or(Error::StorageStatsNotFound(id))
67  }
68
69  #[inline]
70  pub fn wall(&self) -> &WallStatsTable {
71    &self.wall
72  }
73}