From 070485e093dc540a5db9650d438cffe3d18d3883 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 12 Feb 2019 13:20:06 -0600 Subject: added constants file, made so items are referred to by an enum, figured better ways than ship_clone --- src/server/construction.rs | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) (limited to 'src/server/construction.rs') 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) { 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, } } -- cgit v1.2.3