From a6f21e236f86bc9a9bdcf687ba1086c464d079bc Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 20 Apr 2018 04:27:31 -0500 Subject: -moved mining_data init into function --- src/client/mining.rs | 7 +++- src/module.rs | 3 +- src/server/mining.rs | 107 +++++++++++++++++++++++++-------------------------- 3 files changed, 60 insertions(+), 57 deletions(-) diff --git a/src/client/mining.rs b/src/client/mining.rs index 5cb1240..17a7819 100644 --- a/src/client/mining.rs +++ b/src/client/mining.rs @@ -23,8 +23,11 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader) match data.has_astroid_target { true => match data.is_within_range { - true => write!(stdout, "{}Press F to begin mining.", termion::cursor::Goto(1,1)).unwrap(), - false => write!(stdout, "{}Astroid must be within range of {}.", termion::cursor::Goto(1,1), data.mining_range).unwrap(), + true => match data.status { + true => write!(stdout, "{}Press F to stop mining.", termion::cursor::Goto(1,1)).unwrap(), + false => write!(stdout, "{}Press F to begin mining.", termion::cursor::Goto(1,1)).unwrap(), + } + false => write!(stdout, "{}Astroid must be within range of {}.", termion::cursor::Goto(1,1), data.range).unwrap(), }, false => write!(stdout, "{}Ship has no astroid targeted.", termion::cursor::Goto(1,1)).unwrap(), } diff --git a/src/module.rs b/src/module.rs index 0318aad..028eebe 100644 --- a/src/module.rs +++ b/src/module.rs @@ -84,7 +84,8 @@ impl Module { _ => (), } }, - ModuleType::Mining{..} => (), + ModuleType::Mining{..} => { + }, _ => (), } } diff --git a/src/server/mining.rs b/src/server/mining.rs index 2904d51..06f2418 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -6,82 +6,41 @@ use std::collections::HashMap; use math::distance; use mass::{Mass, MassType}; -use module::ModuleType; +use module::{Module, ModuleType}; use server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MiningData { pub has_astroid_target : bool, pub is_within_range : bool, - pub mining_range : f64, - pub mining_status : bool, + pub range : f64, + pub status : bool, } impl ServerConnection { pub fn server_mining(&mut self, masses : &mut HashMap) -> bool { let masses_clone = masses.clone(); let ship = masses.get_mut(&self.name).unwrap(); + let ship_clone = ship.clone(); if let MassType::Ship{ref mut modules, ..} = ship.mass_type { - let mut mining_range = 0.0; - let mut mining_status = false; + let mining_data = get_mining_data(ship_clone, modules, masses_clone); - if let ModuleType::Mining{ref range, ref status, ..} = modules.get("Mining").unwrap().module_type { - mining_range = *range; - mining_status = *status; + let send = serde_json::to_string(&mining_data).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, } - if let ModuleType::Navigation{ref target_name, ..} = modules.get("Navigation").unwrap().module_type { - let mut has_astroid_target = false; - let mut is_within_range = false; - match target_name.clone() { - Some(name) => { - let target = masses_clone.get(&name); - has_astroid_target = match target { - Some(target) => match target.mass_type { - MassType::Astroid{..} => true, - _ => false, - }, - None => false, - }; - is_within_range = match has_astroid_target { - true => match target { - Some(target) => mining_range > distance(ship.position, target.position), - _ => false, - } - _ => false, - }; - } - _ => (), - } - - let send = serde_json::to_string(&MiningData { - has_astroid_target : has_astroid_target, - is_within_range : is_within_range, - mining_range : mining_range, - mining_status : mining_status, - }).unwrap() + "\n"; - - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, - } - } - - if let ModuleType::Mining{ref range, ref status, ..} = modules.get("Mining").unwrap().module_type { + if let ModuleType::Mining{ref mut status, ..} = modules.get_mut("Mining").unwrap().module_type { let mut recv = String::new(); match self.buff_r.read_line(&mut recv) { Ok(result) => match recv.as_bytes() { b"F\n" => { - /* - if is_within_range { - match mining.status { - true => mining.stop(), - false => mining.start(), - } + if mining_data.is_within_range { + *status = !*status; } - */ }, _ => { if result == 0 { @@ -93,7 +52,47 @@ impl ServerConnection { } } } - true } } + +fn get_mining_data(ship : Mass, modules : &mut HashMap, masses_clone : HashMap) -> MiningData { + let mut mining_range = 0.0; + let mut mining_status = false; + if let ModuleType::Mining{ref range, ref status, ..} = modules.get("Mining").unwrap().module_type { + mining_range = *range; + mining_status = *status; + } + + let mut has_astroid_target = false; + let mut is_within_range = false; + if let ModuleType::Navigation{ref target_name, ..} = modules.get("Navigation").unwrap().module_type { + match target_name.clone() { + Some(name) => { + let target = masses_clone.get(&name); + has_astroid_target = match target { + Some(target) => match target.mass_type { + MassType::Astroid{..} => true, + _ => false, + }, + None => false, + }; + is_within_range = match has_astroid_target { + true => match target { + Some(target) => mining_range > distance(ship.position, target.position), + _ => false, + } + _ => false, + }; + } + _ => (), + } + } + + MiningData { + has_astroid_target : has_astroid_target, + is_within_range : is_within_range, + range : mining_range, + status : mining_status, + } +} -- cgit v1.2.3