diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-12 13:20:06 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-12 13:20:06 -0600 |
commit | 070485e093dc540a5db9650d438cffe3d18d3883 (patch) | |
tree | a0ab1f42c844b3557c7f17b11f3c7eae334e853d /src/server/construction.rs | |
parent | 6a906ce663935e37d6dd79a2e2e31fb07ca7c2af (diff) |
added constants file, made so items are referred to by an enum, figured better ways than ship_clone
Diffstat (limited to 'src/server/construction.rs')
-rw-r--r-- | src/server/construction.rs | 39 |
1 files changed, 18 insertions, 21 deletions
diff --git a/src/server/construction.rs b/src/server/construction.rs index 70038ae..9c700ae 100644 --- a/src/server/construction.rs +++ b/src/server/construction.rs @@ -4,58 +4,52 @@ use std::collections::HashMap; use std::io::BufRead; use std::io::Write; +use crate::constants; +use crate::item::ItemType; use crate::mass::{Mass, MassType}; use crate::modules::construction::Construction; use crate::modules::construction::ConstructionStatus; use crate::modules::types::ModuleType; use crate::server::connection::ServerConnection; +use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ConstructionData { pub status: ConstructionStatus, - pub has_refined: bool, + pub has_enough: bool, } impl ServerConnection { pub fn server_construction(&mut self, masses: &mut HashMap<String, Mass>) { let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); - let mut construct = false; if let MassType::Ship { ref mut construction, + ref mut storage, .. } = ship.mass_type { let construction = construction.as_mut().unwrap(); - let construction_data = get_construction_data(ship_clone.clone(), construction); + let construction_data = get_construction_data(storage, construction); if self.open && self.txrx_construction(&construction_data) { construction.toggle(); } if construction_data.status == ConstructionStatus::Constructed { - construction.take(); + storage.take_items(ItemType::Iron, constants::SHIP_CONSTRUCTION_IRON_COST); masses.insert( "Station".to_string(), Mass::new_station( ModuleType::Refinery, - ship_clone.position, - ship_clone.velocity, + ship.position.clone(), + ship.velocity.clone(), ), ); - construct = true; + construction.taken(); } } - if construct { - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - } - masses.insert(self.name.clone(), ship); } @@ -69,7 +63,7 @@ impl ServerConnection { if let Ok(result) = self.buff_r.read_line(&mut recv) { match recv.as_bytes() { b"c\n" => { - if construction_data.has_refined { + if construction_data.has_enough { return true; } } @@ -85,11 +79,14 @@ impl ServerConnection { } } -fn get_construction_data(ship: Mass, construction: &Construction) -> ConstructionData { - let has_refined = ship.refined_count() >= 5; - +fn get_construction_data(storage: &Storage, construction: &Construction) -> ConstructionData { ConstructionData { status: construction.status.clone(), - has_refined, + has_enough: storage + .items + .iter() + .filter(|item| item.itemtype == ItemType::Iron) + .count() + >= constants::SHIP_CONSTRUCTION_IRON_COST, } } |