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/server/connection.rs | 5 ++-- src/server/mod.rs | 1 + src/server/refinery.rs | 73 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 src/server/refinery.rs (limited to 'src/server') 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 + } +} -- cgit v1.2.3