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 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")]
17pub struct InfrastructureStats {
18  building: HashMap<BuildingId, BuildingStatsTable>,
19  mine: HashMap<MineId, MineStatsTable>,
20  storage: HashMap<StorageId, StorageStatsTable>,
21  wall: WallStatsTable,
22}
23
24impl InfrastructureStats {
25  pub fn new(config: &WorldConfig) -> Self {
26    let infrastructure = Infrastructure::default();
27    let building = BuildingId::iter()
28      .map(|id| (id, BuildingStatsTable::new(infrastructure.building(id))))
29      .collect();
30
31    let mine = MineId::iter()
32      .map(|id| (id, MineStatsTable::new(config, infrastructure.mine(id))))
33      .collect();
34
35    let storage = StorageId::iter()
36      .map(|id| (id, StorageStatsTable::new(infrastructure.storage(id))))
37      .collect();
38
39    let wall = WallStatsTable::new();
40
41    Self { building, mine, storage, wall }
42  }
43
44  #[inline]
45  pub fn building(&self, id: BuildingId) -> Result<&BuildingStatsTable> {
46    self
47      .building
48      .get(&id)
49      .ok_or(Error::BuildingStatsNotFound(id))
50  }
51
52  #[inline]
53  pub fn mine(&self, id: MineId) -> Result<&MineStatsTable> {
54    self
55      .mine
56      .get(&id)
57      .ok_or(Error::MineStatsNotFound(id))
58  }
59
60  #[inline]
61  pub fn storage(&self, id: StorageId) -> Result<&StorageStatsTable> {
62    self
63      .storage
64      .get(&id)
65      .ok_or(Error::StorageStatsNotFound(id))
66  }
67
68  #[inline]
69  pub fn wall(&self) -> &WallStatsTable {
70    &self.wall
71  }
72}