diff options
author | tom barrett <spalf0@gmail.com> | 2018-06-26 04:49:31 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-06-26 04:49:31 -0500 |
commit | 25d556ad995a83c0c783f0bf9ca555b17cb0bfb0 (patch) | |
tree | dd18a8c287e3a397ee3ab8281643ee4a48ce9125 /src | |
parent | 2d01ede909281e8c412924ba524b7406c2d760ee (diff) |
-moved to enum states for mining and refinery, added mining module knowing if astroid has no minerals
Diffstat (limited to 'src')
-rw-r--r-- | src/client/mining.rs | 12 | ||||
-rw-r--r-- | src/client/refinery.rs | 5 | ||||
-rw-r--r-- | src/modules/mining.rs | 57 | ||||
-rw-r--r-- | src/modules/mod.rs | 4 | ||||
-rw-r--r-- | src/modules/refinery.rs | 35 | ||||
-rw-r--r-- | src/server/mining.rs | 26 | ||||
-rw-r--r-- | src/server/refinery.rs | 7 |
7 files changed, 89 insertions, 57 deletions
diff --git a/src/client/mining.rs b/src/client/mining.rs index f0fcbd0..80cd611 100644 --- a/src/client/mining.rs +++ b/src/client/mining.rs @@ -8,6 +8,7 @@ use std::io::{stdout, Read, Write}; use self::termion::raw::IntoRawMode; use server::mining::MiningData; +use modules::mining::MiningStatus; pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) { let stdout = stdout(); @@ -25,10 +26,13 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) match data.has_astroid_target { true => match data.is_within_range { - true => match data.status { - true => write!(stdout, "{}Press F to stop mining.", clear).unwrap(), - false => write!(stdout, "{}Press F to begin mining.", clear).unwrap(), - } + true => match data.astroid_has_minerals { + true => match data.status { + MiningStatus::None => write!(stdout, "{}Press F to begin mining.", clear).unwrap(), + _ => write!(stdout, "{}Press F to stop mining.", clear).unwrap(), + }, + false => write!(stdout, "{}Astroid has ran out of minerals.", clear).unwrap(), + } false => write!(stdout, "{}Astroid must be within range of {}.", clear, data.range).unwrap(), }, false => write!(stdout, "{}Ship has no astroid targeted.", clear).unwrap(), diff --git a/src/client/refinery.rs b/src/client/refinery.rs index 4f437df..5f126bf 100644 --- a/src/client/refinery.rs +++ b/src/client/refinery.rs @@ -8,6 +8,7 @@ use std::io::{stdout, Read, Write}; use self::termion::raw::IntoRawMode; use server::refinery::RefineryData; +use modules::refinery::RefineryStatus; pub fn client_refinery(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) { let stdout = stdout(); @@ -25,8 +26,8 @@ pub fn client_refinery(mut stream : TcpStream, mut buff_r : BufReader<TcpStream> 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(), + RefineryStatus::None => write!(stdout, "{}Press R to begin refining.", clear).unwrap(), + _ => write!(stdout, "{}Press R to stop refining.", clear).unwrap(), }, false => write!(stdout, "{}You have no refinable minerals.", clear).unwrap(), } diff --git a/src/modules/mining.rs b/src/modules/mining.rs index 7f11f05..4a1a37f 100644 --- a/src/modules/mining.rs +++ b/src/modules/mining.rs @@ -1,55 +1,64 @@ use std::time::SystemTime; +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub enum MiningStatus { + None, + Mining, + Mined, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Mining { pub range : f64, - pub status : bool, + pub status : MiningStatus, time : u64, start : Option<SystemTime>, - pub ready : bool, } - impl Mining { pub fn new() -> Mining { Mining { range : 10.0, - status : false, + status : MiningStatus::None, time : 5, start : None, - 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; - } - pub fn process(&mut self) { match self.start.clone() { Some(timer) => { if timer.elapsed().unwrap().as_secs() > self.time { + self.status = MiningStatus::Mined; self.start = Some(SystemTime::now()); - self.ready = true; } } _ => (), } - if !self.status { + if self.status == MiningStatus::None { self.start = None; - self.ready = false; } } + + pub fn toggle(&mut self) { + self.status = match self.status { + MiningStatus::None => { + self.start = Some(SystemTime::now()); + MiningStatus::Mining + } + _ => { + self.start = None; + MiningStatus::None + } + }; + } + + pub fn off(&mut self) { + 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 61e8133..18cba81 100644 --- a/src/modules/mod.rs +++ b/src/modules/mod.rs @@ -1,6 +1,6 @@ +pub mod types; pub mod mining; pub mod engines; +pub mod refinery; pub mod dashboard; pub mod navigation; -pub mod refinery; -pub mod types; diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs index 733fcd4..177f2c5 100644 --- a/src/modules/refinery.rs +++ b/src/modules/refinery.rs @@ -1,11 +1,17 @@ use std::time::SystemTime; +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub enum RefineryStatus { + None, + Refining, + Refined, +} + #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Refinery { time : u64, start : Option<SystemTime>, - pub status : bool, - pub ready : bool, + pub status : RefineryStatus, } impl Refinery { @@ -13,8 +19,7 @@ impl Refinery { Refinery { time : 5, start : None, - status : false, - ready : false, + status : RefineryStatus::None, } } @@ -22,31 +27,35 @@ impl Refinery { match self.start.clone() { Some(timer) => { if timer.elapsed().unwrap().as_secs() > self.time { + self.status = RefineryStatus::Refined; self.start = Some(SystemTime::now()); - self.ready = true; } } _ => (), } - if !self.status { + if self.status == RefineryStatus::None { 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, + self.status = match self.status { + RefineryStatus::None => { + self.start = Some(SystemTime::now()); + RefineryStatus::Refining + }, + _ => { + self.start = None; + RefineryStatus::None + } }; } pub fn off(&mut self) { - self.status = false; + self.status = RefineryStatus::None; } pub fn take(&mut self) { - self.ready = false; + self.status = RefineryStatus::Refining; } } diff --git a/src/server/mining.rs b/src/server/mining.rs index 3cf5f0b..190ca24 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -6,15 +6,16 @@ use std::collections::HashMap; use math::distance; use mass::{Mass, MassType}; -use modules::mining::Mining; use modules::navigation::Navigation; use server::connection::ServerConnection; +use modules::mining::{Mining, MiningStatus}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MiningData { pub has_astroid_target : bool, + pub astroid_has_minerals: bool, pub is_within_range : bool, - pub status : bool, + pub status : MiningStatus, pub range : f64, } @@ -29,7 +30,6 @@ impl ServerConnection { let mut navigation = navigation.as_ref().unwrap(); let mining_data = get_mining_data(ship_clone, mining, navigation, masses); - if self.open { if self.txrx_mining(&mining_data) { if mining_data.is_within_range { @@ -42,7 +42,7 @@ impl ServerConnection { mining.off(); } else { - if mining.status && mining.ready { + if mining.status == MiningStatus::Mined { mining.take(); match navigation.target_name.clone() { Some(name) => { @@ -98,13 +98,19 @@ fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, mass match navigation.target_name.clone() { Some(name) => { let target = masses.get(&name); + + let mut astroid_has_minerals = false; let has_astroid_target = match target { - Some(target) => match target.mass_type { - MassType::Astroid{..} => true, - _ => false, + Some(target) => { + astroid_has_minerals = target.has_minerals(); + match target.mass_type { + MassType::Astroid{..} => true, + _ => false, + } }, None => false, }; + let is_within_range = match has_astroid_target { true => match target { Some(target) => mining.range > distance(ship.position, target.position), @@ -115,17 +121,19 @@ fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, mass MiningData { has_astroid_target : has_astroid_target, + astroid_has_minerals: astroid_has_minerals, is_within_range : is_within_range, range : mining.range, - status : mining.status, + status : mining.status.clone(), } } _ => { MiningData { has_astroid_target : false, + astroid_has_minerals: false, is_within_range : false, range : mining.range, - status : mining.status, + status : mining.status.clone(), } } } diff --git a/src/server/refinery.rs b/src/server/refinery.rs index b1af971..9df078c 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -7,11 +7,12 @@ use std::collections::HashMap; use item::Item; use mass::{Mass, MassType}; use server::connection::ServerConnection; +use modules::refinery::RefineryStatus; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RefineryData { pub has_minerals : bool, - pub status : bool, + pub status : RefineryStatus, } impl ServerConnection { @@ -25,7 +26,7 @@ impl ServerConnection { let refinery_data = RefineryData { has_minerals : ship_clone.has_minerals(), - status : refinery.status, + status : refinery.status.clone(), }; if self.open { @@ -38,7 +39,7 @@ impl ServerConnection { refinery.off(); } - if refinery.status && refinery.ready { + if refinery.status == RefineryStatus::Refined { refinery.take(); refine = true; } |