From 11b51897dda7609b5a22a490bfb8cd2c269b5969 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 7 Jun 2018 08:30:35 -0500 Subject: -added crude refinery methods --- src/bin/client.rs | 6 ++-- src/client/mod.rs | 1 + src/client/refinery.rs | 50 +++++++++++++++++++++++++++++++++ src/mass.rs | 18 ++++++++++-- src/modules/mod.rs | 1 + src/modules/refinery.rs | 52 ++++++++++++++++++++++++++++++++++ src/modules/types.rs | 3 +- src/server/connection.rs | 5 ++-- src/server/mod.rs | 1 + src/server/refinery.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ src/storage.rs | 7 +++++ 11 files changed, 209 insertions(+), 8 deletions(-) create mode 100644 src/client/refinery.rs create mode 100644 src/modules/refinery.rs create mode 100644 src/server/refinery.rs diff --git a/src/bin/client.rs b/src/bin/client.rs index e5dec5a..f9aa038 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -6,9 +6,10 @@ use std::io::BufReader; use std::io::prelude::*; use std::net::TcpStream; +use space::modules::types::ModuleType; use space::client::mining::client_mining; use space::client::engines::client_engines; -use space::modules::types::ModuleType; +use space::client::refinery::client_refinery; use space::client::dashboard::client_dashboard; use space::client::navigation::client_navigation; @@ -47,8 +48,9 @@ fn main() { match module_type { ModuleType::Dashboard => client_dashboard(buff_r), - ModuleType::Engines => client_engines(stream, buff_r), ModuleType::Mining => client_mining(stream, buff_r), + ModuleType::Engines => client_engines(stream, buff_r), + ModuleType::Refinery => client_refinery(stream, buff_r), ModuleType::Navigation => client_navigation(name, stream, buff_r), } } diff --git a/src/client/mod.rs b/src/client/mod.rs index baabf96..8bb0eca 100644 --- a/src/client/mod.rs +++ b/src/client/mod.rs @@ -1,4 +1,5 @@ pub mod mining; pub mod engines; +pub mod refinery; pub mod dashboard; pub mod navigation; diff --git a/src/client/refinery.rs b/src/client/refinery.rs new file mode 100644 index 0000000..4f437df --- /dev/null +++ b/src/client/refinery.rs @@ -0,0 +1,50 @@ +extern crate termion; +extern crate serde_json; + +use std::net::TcpStream; +use self::termion::async_stdin; +use std::io::{BufRead, BufReader}; +use std::io::{stdout, Read, Write}; +use self::termion::raw::IntoRawMode; + +use server::refinery::RefineryData; + +pub fn client_refinery(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 : RefineryData = serde_json::from_str(&recv.replace("\n", "")).unwrap(); + + write!(stdout, "{}", termion::clear::All).unwrap(); + + let clear = termion::cursor::Goto(1,1); + + match data.has_minerals { + true => match data.status { + true => write!(stdout, "{}Press R to stop refining.", clear).unwrap(), + false => write!(stdout, "{}Press R to begin refining.", clear).unwrap(), + }, + false => write!(stdout, "{}You have no refinable minerals.", 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/mass.rs b/src/mass.rs index 4bb44b0..96244c0 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -8,6 +8,7 @@ use storage::Storage; use modules::mining::Mining; use modules::engines::Engines; use modules::types::ModuleType; +use modules::refinery::Refinery; use modules::dashboard::Dashboard; use modules::navigation::Navigation; @@ -21,11 +22,12 @@ pub struct Mass { #[derive(Serialize, Deserialize, Debug, Clone)] pub enum MassType { Ship { + storage : Storage, mining : Option, - navigation : Option, engines : Option, + refinery : Option, dashboard : Option, - storage : Storage, + navigation : Option, }, Astroid{ resources : Storage, @@ -63,6 +65,7 @@ impl Mass { let ship = MassType::Ship { mining : Some(Mining::new()), engines : Some(Engines::new()), + refinery : Some(Refinery::new()), dashboard : Some(Dashboard::new()), navigation : Some(Navigation::new()), storage : Storage::new(Vec::new()), @@ -79,6 +82,7 @@ impl Mass { let mut modules = Vec::new(); modules.push(ModuleType::Mining); modules.push(ModuleType::Engines); + modules.push(ModuleType::Refinery); modules.push(ModuleType::Dashboard); modules.push(ModuleType::Navigation); modules @@ -87,8 +91,9 @@ impl Mass { 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, ..} => { + MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ..} => { mining.as_mut().unwrap().process(); + refinery.as_mut().unwrap().process(); navigation.as_mut().unwrap().process(); acceleration = engines.as_mut().unwrap().recv_acceleration(); }, @@ -106,6 +111,13 @@ impl Mass { self.velocity.2 += acceleration.2; } + pub fn has_minerals(&self) -> bool { + match self.mass_type { + MassType::Ship{ref storage, ..} => storage.has_minerals(), + MassType::Astroid{ref resources, ..} => resources.has_minerals(), + } + } + 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/mod.rs b/src/modules/mod.rs index 0c19156..61e8133 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -2,4 +2,5 @@ pub mod mining; pub mod engines; pub mod dashboard; pub mod navigation; +pub mod refinery; pub mod types; diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs new file mode 100644 index 0000000..733fcd4 --- /dev/null +++ b/src/modules/refinery.rs @@ -0,0 +1,52 @@ +use std::time::SystemTime; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Refinery { + time : u64, + start : Option, + pub status : bool, + pub ready : bool, +} + +impl Refinery { + pub fn new() -> Refinery { + Refinery { + time : 5, + start : None, + status : false, + ready : false, + } + } + + 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.ready = true; + } + } + _ => (), + } + if !self.status { + self.start = None; + self.ready = false; + } + } + + pub fn toggle(&mut self) { + self.status = !self.status; + self.start = match self.status { + true => Some(SystemTime::now()), + false => None, + }; + } + + pub fn off(&mut self) { + self.status = false; + } + + pub fn take(&mut self) { + self.ready = false; + } +} diff --git a/src/modules/types.rs b/src/modules/types.rs index 1af99d6..48b3473 100644 --- a/src/modules/types.rs +++ b/src/modules/types.rs @@ -1,7 +1,8 @@ #[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] pub enum ModuleType { - Navigation, Mining, Engines, + Refinery, Dashboard, + Navigation, } diff --git a/src/server/connection.rs b/src/server/connection.rs index fd76661..d8e0eef 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -45,9 +45,10 @@ impl ServerConnection { pub fn process(&mut self, mut masses : &mut HashMap) { self.open = match self.module_type { - ModuleType::Dashboard => self.server_dashboard(&mut masses), - ModuleType::Engines => self.server_engines(&mut masses), ModuleType::Mining => self.server_mining(&mut masses), + ModuleType::Engines => self.server_engines(&mut masses), + ModuleType::Refinery => self.server_refinery(&mut masses), + ModuleType::Dashboard => self.server_dashboard(&mut masses), ModuleType::Navigation => self.server_navigation(&mut masses), }; } diff --git a/src/server/mod.rs b/src/server/mod.rs index 444e487..11dfdce 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,5 +1,6 @@ pub mod mining; pub mod engines; +pub mod refinery; pub mod dashboard; pub mod navigation; pub mod connection; diff --git a/src/server/refinery.rs b/src/server/refinery.rs new file mode 100644 index 0000000..bb0627f --- /dev/null +++ b/src/server/refinery.rs @@ -0,0 +1,73 @@ +extern crate serde_json; + +use std::io::Write; +use std::io::BufRead; +use std::collections::HashMap; + +use item::Item; +use mass::{Mass, MassType}; +use server::connection::ServerConnection; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct RefineryData { + pub has_minerals : bool, + pub status : bool, +} + +impl ServerConnection { + pub fn server_refinery(&mut self, masses : &mut HashMap) -> bool { + let mut ship = masses.remove(&self.name).unwrap(); + let ship_clone = ship.clone(); + let mut refine = false; + let mut connection_good = true; + + if let MassType::Ship{ref mut refinery, ..} = ship.mass_type { + let mut refinery = refinery.as_mut().unwrap(); + + let refinery_data = RefineryData { + has_minerals : ship_clone.has_minerals(), + status : refinery.status, + }; + + let send = serde_json::to_string(&refinery_data).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => connection_good = false, + } + + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => match recv.as_bytes() { + b"R\n" => { + if refinery_data.has_minerals { + refinery.toggle(); + } + }, + _ => { + if result == 0 { + connection_good = false; + } + }, + } + Err(_error) => (), + } + + if !refinery_data.has_minerals { + refinery.off(); + } + + if refinery.status && refinery.ready { + refinery.take(); + refine = true; + } + } + + if refine { + ship.take("Iron"); + ship.give(Item::new("Refined Iron", 1)); + } + + masses.insert(self.name.clone(), ship); + connection_good + } +} diff --git a/src/storage.rs b/src/storage.rs index 3e900b2..6aa8417 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -14,6 +14,13 @@ impl Storage { } } + pub fn has_minerals(&self) -> bool { + match self.items.iter().position(|item| item.name == "Iron") { + Some(_) => true, + None => false, + } + } + pub fn take(&mut self, name : &str) -> Option { match self.items.iter().position(|item| item.name == name) { Some(index) => Some(self.items.remove(index)), -- cgit v1.2.3