From a4efade392aa7127c373b0247d39274cb0decd10 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 19 Feb 2019 13:24:25 -0600 Subject: unified all server->client connection and brought logic to modules --- src/modules/construction.rs | 94 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 71 insertions(+), 23 deletions(-) (limited to 'src/modules/construction.rs') diff --git a/src/modules/construction.rs b/src/modules/construction.rs index 0f7f8f2..ca4dd2d 100644 --- a/src/modules/construction.rs +++ b/src/modules/construction.rs @@ -1,21 +1,12 @@ +use std::collections::HashMap; use std::time::SystemTime; use crate::constants; +use crate::item::ItemType; +use crate::mass::Mass; +use crate::math::Vector; use crate::modules::types::ModuleType; -use crate::server::construction::ConstructionData; - -#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] -pub enum ConstructionStatus { - None, - Constructing, - Constructed, -} - -impl Default for ConstructionStatus { - fn default() -> Self { - ConstructionStatus::None - } -} +use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct Construction { @@ -35,41 +26,98 @@ impl Construction { } } - pub fn process(&mut self) { + pub fn process( + &mut self, + ship_velocity: Vector, + ship_position: Vector, + masses: &mut HashMap, + storage: &mut Storage, + ) { + if !self.has_enough(storage) { + self.off(); + } if let Some(timer) = self.start { if timer.elapsed().unwrap().as_secs() > self.time { self.start = Some(SystemTime::now()); self.status = ConstructionStatus::Constructed; } } + if self.status == ConstructionStatus::Constructed { + storage + .take_items(ItemType::Iron, constants::SHIP_CONSTRUCTION_IRON_COST) + .unwrap(); + masses.insert( + "Station".to_string(), + Mass::new_station( + ModuleType::Refinery, + ship_position.clone(), + ship_velocity.clone(), + ), + ); + self.constructed(); + } } - pub fn give_recv(&mut self, recv: String, construction_data: &ConstructionData) { + pub fn get_client_data(&self, storage: &Storage) -> String { + let client_data = ConstructionClientData { + has_enough: self.has_enough(storage), + status: self.status.clone(), + }; + serde_json::to_string(&client_data).unwrap() + "\n" + } + + pub fn give_received_data(&mut self, recv: String) { if let "c" = recv.as_str() { - if construction_data.has_enough { - self.toggle() - } + self.toggle() } } - pub fn toggle(&mut self) { + fn has_enough(&self, storage: &Storage) -> bool { + storage + .items + .iter() + .filter(|item| item.item_type == ItemType::Iron) + .count() + >= constants::SHIP_CONSTRUCTION_IRON_COST + } + + fn toggle(&mut self) { match self.status { ConstructionStatus::None => self.on(), _ => self.off(), }; } - pub fn on(&mut self) { + fn on(&mut self) { self.start = Some(SystemTime::now()); self.status = ConstructionStatus::Constructing; } - pub fn off(&mut self) { + fn off(&mut self) { self.start = None; self.status = ConstructionStatus::None; } - pub fn constructed(&mut self) { + fn constructed(&mut self) { self.off() } } + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ConstructionClientData { + pub status: ConstructionStatus, + pub has_enough: bool, +} + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub enum ConstructionStatus { + None, + Constructing, + Constructed, +} + +impl Default for ConstructionStatus { + fn default() -> Self { + ConstructionStatus::None + } +} -- cgit v1.2.3