-
-
Save pferreir/ca6e793d0167b3ad0779064135dd646b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/core/simulation/simulation.rs b/src/core/simulation/simulation.rs | |
index 414411e..3384367 100644 | |
--- a/src/core/simulation/simulation.rs | |
+++ b/src/core/simulation/simulation.rs | |
@@ -63,7 +63,7 @@ impl<'a> Simulation<'a> { | |
} | |
} | |
InputChange::Money(amount) => { | |
- *state.resources.get_mut(&Resource::Money).unwrap() += amount; | |
+ state.subtract_resources(&vec![(Resource::Money, *amount)]); | |
} | |
InputChange::UIMessage(UIMessage::Personnel(action)) => match action { | |
UIPersonnelAction::Hired(bldg_code, role_id, new_person) => { | |
diff --git a/src/core/simulation/state/state.rs b/src/core/simulation/state/state.rs | |
index 126728c..8322670 100644 | |
--- a/src/core/simulation/state/state.rs | |
+++ b/src/core/simulation/state/state.rs | |
@@ -2,9 +2,12 @@ use serde::{Deserialize, Serialize}; | |
use std::collections::HashMap; | |
use super::gen::gen_persons; | |
-use super::models::{BuildingInstance, UnderConstruction, Person}; | |
+use super::models::{BuildingInstance, Person, UnderConstruction}; | |
-use crate::data::{schemas::{BuildingType, Resource}, SeedData}; | |
+use crate::data::{ | |
+ schemas::{BuildingType, Resource}, | |
+ SeedData, | |
+}; | |
use crate::error::Error; | |
use crate::ui::UIMessage; | |
@@ -48,7 +51,7 @@ pub struct State { | |
pub resources: HashMap<Resource, i64>, | |
pub buildings: Vec<BuildingInstance>, | |
pub under_construction: Vec<UnderConstruction>, | |
- pub persons: HashMap<String, Person> | |
+ pub persons: HashMap<String, Person>, | |
} | |
impl State { | |
@@ -69,14 +72,14 @@ impl State { | |
}) | |
} | |
- fn subtract_resources(&mut self, costs: HashMap<Resource, i64>) -> Result<(), StateError> { | |
+ pub fn subtract_resources(&mut self, costs: &[(Resource, i64)]) -> Result<(), StateError> { | |
let mut not_met = Vec::new(); | |
for (res, cost) in costs { | |
let available = self.resources[&res]; | |
- if available >= cost { | |
- self.resources.insert(res, available - cost); | |
+ if available >= *cost { | |
+ self.resources.insert(*res, available - cost); | |
} else { | |
- not_met.push(res); | |
+ not_met.push(*res); | |
} | |
} | |
@@ -115,7 +118,9 @@ impl State { | |
pub fn enqueue_building(&mut self, bldg_type: &BuildingType) -> Result<(), StateError> { | |
let (id, build_time, costs) = bldg_type.as_construction_tuple(); | |
- if let Err(e) = self.subtract_resources(costs) { | |
+ if let Err(e) = | |
+ self.subtract_resources(&costs.iter().map(|(r, c)| (*r, *c)).collect::<Vec<(_, _)>>()) | |
+ { | |
Err(e) | |
} else { | |
self.under_construction | |
diff --git a/src/data/schemas.rs b/src/data/schemas.rs | |
index aa1c77c..4f73fe5 100644 | |
--- a/src/data/schemas.rs | |
+++ b/src/data/schemas.rs | |
@@ -3,7 +3,7 @@ use std::collections::HashMap; | |
use crate::ui::dialogs::{ExperimentPersonnelDialog, PersonHireDialog}; | |
-#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq, Hash)] | |
+#[derive(Debug, Serialize, Deserialize, Copy, Clone, PartialEq, Eq, Hash)] | |
pub enum Resource { | |
Research, | |
Money, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment