From ae79327d4f0ee6de0ef6b8e3c51299aebfe3bc25 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 27 Jun 2018 07:51:47 -0500 Subject: -added elementry construction module, doesnt do anything other than exist as a mass --- src/bin/client.rs | 2 ++ src/client/construction.rs | 51 ++++++++++++++++++++++++++++ src/client/mod.rs | 1 + src/item.rs | 2 +- src/mass.rs | 33 ++++++++++++++++-- src/modules/construction.rs | 61 ++++++++++++++++++++++++++++++++++ src/modules/mining.rs | 22 +++++------- src/modules/mod.rs | 1 + src/modules/refinery.rs | 21 +++++------- src/modules/types.rs | 1 + src/server/connection.rs | 1 + src/server/construction.rs | 81 +++++++++++++++++++++++++++++++++++++++++++++ src/server/mining.rs | 8 ++--- src/server/mod.rs | 1 + src/server/refinery.rs | 4 +-- src/storage.rs | 6 ++++ 16 files changed, 261 insertions(+), 35 deletions(-) create mode 100644 src/client/construction.rs create mode 100644 src/modules/construction.rs create mode 100644 src/server/construction.rs diff --git a/src/bin/client.rs b/src/bin/client.rs index 24a87b8..3353e87 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -17,6 +17,7 @@ use space::client::engines::client_engines; use space::client::refinery::client_refinery; use space::client::dashboard::client_dashboard; use space::client::navigation::client_navigation; +use space::client::construction::client_construction; #[derive(Debug, Deserialize)] struct Config { @@ -81,5 +82,6 @@ fn main() { ModuleType::Engines => client_engines(stream, buff_r), ModuleType::Refinery => client_refinery(stream, buff_r), ModuleType::Navigation => client_navigation(name, stream, buff_r), + ModuleType::Construction => client_construction(stream, buff_r), } } diff --git a/src/client/construction.rs b/src/client/construction.rs new file mode 100644 index 0000000..7a316c6 --- /dev/null +++ b/src/client/construction.rs @@ -0,0 +1,51 @@ +extern crate termion; +extern crate serde_json; + +use std::net::TcpStream; +use self::termion::async_stdin; +use std::io::{BufReader, BufRead}; +use std::io::{stdout, Read, Write}; +use self::termion::raw::IntoRawMode; + +use server::construction::ConstructionData; +use modules::construction::ConstructionStatus; + +pub fn client_construction(mut stream : TcpStream, mut buff_r : BufReader) { + let stdout = stdout(); + let mut stdout = stdout.lock().into_raw_mode().unwrap(); + let mut stdin = async_stdin().bytes(); + + loop { + let mut recv = String::new(); + buff_r.read_line(&mut recv).unwrap(); + let data : ConstructionData = serde_json::from_str(&recv.replace("\n", "")).unwrap(); + + write!(stdout, "{}", termion::clear::All).unwrap(); + + let clear = termion::cursor::Goto(1,1); + + match data.has_refined { + true => match data.status { + ConstructionStatus::None => write!(stdout, "{}Press c to create a refinery.", clear).unwrap(), + _ => write!(stdout, "{}Press c to cancel..", clear).unwrap(), + }, + false => write!(stdout, "{}You need 5 refined minerals to create a refinery.", clear).unwrap(), + } + + match stdin.next() { + Some(c) => { + let c = c.unwrap(); + let mut send = String::new(); + send.push(c as char); + if send.as_bytes() == b"q" { + break; + } + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); + } + None => () + } + + stdout.flush().unwrap(); + } +} diff --git a/src/client/mod.rs b/src/client/mod.rs index 8bb0eca..421a394 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -3,3 +3,4 @@ pub mod engines; pub mod refinery; pub mod dashboard; pub mod navigation; +pub mod construction; diff --git a/src/item.rs b/src/item.rs index f9c7490..af5c2f8 100644 --- a/src/item.rs +++ b/src/item.rs @@ -13,7 +13,7 @@ impl Item { } pub fn is_mineral(&self) -> bool { - if self.name == "Iron" { + if self.name == "Mineral" { true } else { diff --git a/src/mass.rs b/src/mass.rs index 252de49..f09685f 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -11,6 +11,7 @@ use modules::types::ModuleType; use modules::refinery::Refinery; use modules::dashboard::Dashboard; use modules::navigation::Navigation; +use modules::construction::Construction; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Mass { @@ -28,13 +29,17 @@ pub enum MassType { refinery : Option, dashboard : Option, navigation : Option, + construction: Option, }, Astroid { resources : Storage, }, Item { item : Item, - } + }, + Station { + module_type : ModuleType, + }, } impl Mass { @@ -50,7 +55,7 @@ impl Mass { let mut rr = Range::new(0, 20); let mut resources = Vec::new(); for _ in 0..rr.sample(&mut rng) { - resources.push(Item::new("Iron", 1)); + resources.push(Item::new("Mineral", 1)); } let astroid = MassType::Astroid { @@ -71,6 +76,7 @@ impl Mass { refinery : Some(Refinery::new()), dashboard : Some(Dashboard::new()), navigation : Some(Navigation::new()), + construction: Some(Construction::new()), storage : Storage::new(Vec::new()), }; @@ -89,6 +95,18 @@ impl Mass { } } + pub fn new_station(module_type : ModuleType, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass { + let mass_type = MassType::Station { + module_type : module_type + }; + + Mass { + mass_type : mass_type, + position : position, + velocity : velocity, + } + } + pub fn get_modules(&self) -> Vec { let mut modules = Vec::new(); modules.push(ModuleType::Mining); @@ -96,16 +114,18 @@ impl Mass { modules.push(ModuleType::Refinery); modules.push(ModuleType::Dashboard); modules.push(ModuleType::Navigation); + modules.push(ModuleType::Construction); modules } pub fn process(&mut self) { let mut acceleration = (0.0, 0.0, 0.0); match self.mass_type { - MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ..} => { + MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ref mut construction, ..} => { mining.as_mut().unwrap().process(); refinery.as_mut().unwrap().process(); navigation.as_mut().unwrap().process(); + construction.as_mut().unwrap().process(); acceleration = engines.as_mut().unwrap().recv_acceleration(); }, _ => (), @@ -130,6 +150,13 @@ impl Mass { } } + pub fn refined_count(&self) -> usize { + match self.mass_type { + MassType::Ship{ref storage, ..} => storage.refined_count(), + _ => 0, + } + } + pub fn take(&mut self, name : &str) -> Option { match self.mass_type { MassType::Ship{ref mut storage, ..} => storage.take(name), diff --git a/src/modules/construction.rs b/src/modules/construction.rs new file mode 100644 index 0000000..22086a5 --- /dev/null +++ b/src/modules/construction.rs @@ -0,0 +1,61 @@ +use std::time::SystemTime; +use modules::types::ModuleType; + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub enum ConstructionStatus { + None, + Constructing, + Constructed, +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Construction { + pub status : ConstructionStatus, + construction : Option, + time : u64, + start : Option, +} + +impl Construction { + pub fn new() -> Construction { + Construction { + status : ConstructionStatus::None, + construction : None, + time : 5, + start : None, + } + } + + pub fn process(&mut self) { + match self.start.clone() { + Some(timer) => { + if timer.elapsed().unwrap().as_secs() > self.time { + self.start = Some(SystemTime::now()); + self.status = ConstructionStatus::Constructed; + } + } + _ => (), + } + } + + pub fn toggle(&mut self) { + match self.status { + ConstructionStatus::None => self.on(), + _ => self.off(), + }; + } + + pub fn on(&mut self) { + self.start = Some(SystemTime::now()); + self.status = ConstructionStatus::Constructing; + } + + pub fn off(&mut self) { + self.start = None; + self.status = ConstructionStatus::None; + } + + pub fn take(&mut self) { + self.status = ConstructionStatus::None; + } +} diff --git a/src/modules/mining.rs b/src/modules/mining.rs index 4a1a37f..7a6461a 100644 --- a/src/modules/mining.rs +++ b/src/modules/mining.rs @@ -35,30 +35,26 @@ impl Mining { } _ => (), } - if self.status == MiningStatus::None { - self.start = None; - } } pub fn toggle(&mut self) { - self.status = match self.status { - MiningStatus::None => { - self.start = Some(SystemTime::now()); - MiningStatus::Mining - } - _ => { - self.start = None; - MiningStatus::None - } + match self.status { + MiningStatus::None => self.on(), + _ => self.off(), }; } + pub fn on(&mut self) { + self.start = Some(SystemTime::now()); + self.status = MiningStatus::Mining; + } + pub fn off(&mut self) { + self.start = None; self.status = MiningStatus::None; } pub fn take(&mut self) { self.status = MiningStatus::Mining; } - } diff --git a/src/modules/mod.rs b/src/modules/mod.rs index 18cba81..d0ffd38 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -4,3 +4,4 @@ pub mod engines; pub mod refinery; pub mod dashboard; pub mod navigation; +pub mod construction; diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs index 177f2c5..cdb5de0 100644 --- a/src/modules/refinery.rs +++ b/src/modules/refinery.rs @@ -33,25 +33,22 @@ impl Refinery { } _ => (), } - if self.status == RefineryStatus::None { - self.start = None; - } } pub fn toggle(&mut self) { - self.status = match self.status { - RefineryStatus::None => { - self.start = Some(SystemTime::now()); - RefineryStatus::Refining - }, - _ => { - self.start = None; - RefineryStatus::None - } + match self.status { + RefineryStatus::None => self.on(), + _ => self.off(), }; } + pub fn on(&mut self) { + self.start = Some(SystemTime::now()); + self.status = RefineryStatus::Refining; + } + pub fn off(&mut self) { + self.start = None; self.status = RefineryStatus::None; } diff --git a/src/modules/types.rs b/src/modules/types.rs index 48b3473..17e7c6c 100644 --- a/src/modules/types.rs +++ b/src/modules/types.rs @@ -5,4 +5,5 @@ pub enum ModuleType { Refinery, Dashboard, Navigation, + Construction, } diff --git a/src/server/connection.rs b/src/server/connection.rs index 80ecde8..973c5d1 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -50,6 +50,7 @@ impl ServerConnection { ModuleType::Refinery => self.server_refinery(&mut masses), ModuleType::Dashboard => self.server_dashboard(&mut masses), ModuleType::Navigation => self.server_navigation(&mut masses), + ModuleType::Construction => self.server_construction(&mut masses), } } } diff --git a/src/server/construction.rs b/src/server/construction.rs new file mode 100644 index 0000000..0eb2179 --- /dev/null +++ b/src/server/construction.rs @@ -0,0 +1,81 @@ +extern crate serde_json; + +use std::io::BufRead; +use std::io::Write; +use std::collections::HashMap; + +use mass::{Mass, MassType}; +use modules::construction::Construction; +use server::connection::ServerConnection; +use modules::construction::ConstructionStatus; +use modules::types::ModuleType; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct ConstructionData { + pub status : ConstructionStatus, + pub has_refined : 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(); + + if let MassType::Ship{ref mut construction, ..} = ship.mass_type { + let mut construction = construction.as_mut().unwrap(); + let construction_data = get_construction_data(ship_clone.clone(), construction); + + if self.open { + if self.txrx_construction(&construction_data) { + construction.toggle(); + } + } + + if construction_data.status == ConstructionStatus::Constructed { + construction.take(); + masses.insert("Station".to_string(), Mass::new_station(ModuleType::Refinery, ship_clone.position, ship_clone.velocity)); + } + } + + masses.insert(self.name.clone(), ship); + } + + fn txrx_construction(&mut self, construction_data : &ConstructionData) -> bool { + let send = serde_json::to_string(construction_data).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Err(_error) => self.open = false, + _ => (), + } + + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => match recv.as_bytes() { + b"c\n" => { + if construction_data.has_refined { + return true + } + }, + _ => { + if result == 0 { + self.open = false; + } + }, + } + _ => (), + } + + false + } +} + +fn get_construction_data(ship : Mass, construction : &Construction) -> ConstructionData { + let mut has_refined = false; + if ship.refined_count() >= 5 { + has_refined = true; + } + + ConstructionData { + status : construction.status.clone(), + has_refined : has_refined, + } +} diff --git a/src/server/mining.rs b/src/server/mining.rs index 190ca24..115c034 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -32,9 +32,7 @@ impl ServerConnection { if self.open { if self.txrx_mining(&mining_data) { - if mining_data.is_within_range { - mining.toggle(); - } + mining.toggle(); } } @@ -47,7 +45,7 @@ impl ServerConnection { match navigation.target_name.clone() { Some(name) => { let target = masses.get_mut(&name).unwrap(); - item = target.take("Iron"); + item = target.take("Mineral"); } _ => (), } @@ -80,7 +78,9 @@ impl ServerConnection { match self.buff_r.read_line(&mut recv) { Ok(result) => match recv.as_bytes() { b"F\n" => { + if mining_data.is_within_range { return true; + } }, _ => { if result == 0 { diff --git a/src/server/mod.rs b/src/server/mod.rs index 11dfdce..a5c359d 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -4,3 +4,4 @@ pub mod refinery; pub mod dashboard; pub mod navigation; pub mod connection; +pub mod construction; diff --git a/src/server/refinery.rs b/src/server/refinery.rs index 9df078c..1f7628f 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -46,8 +46,8 @@ impl ServerConnection { } if refine { - ship.take("Iron"); - ship.give(Item::new("Refined Iron", 1)); + ship.take("Mineral"); + ship.give(Item::new("Refined Mineral", 1)); } masses.insert(self.name.clone(), ship); diff --git a/src/storage.rs b/src/storage.rs index 5cae0ff..72a3b27 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -27,6 +27,12 @@ impl Storage { } } + pub fn refined_count(&self) -> usize { + let mut items = self.items.clone(); + items.retain(|item| item.name == "Refined Mineral"); + items.len() + } + pub fn take(&mut self, name : &str) -> Option { match self.items.iter().position(|item| item.name == name) { Some(index) => { -- cgit v1.2.3