From 573ba69d810914c153a578747414b3d631e61bbc Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 12 Apr 2018 04:33:23 -0500 Subject: completely restructured code and fixed navigation bug --- src/server/mining.rs | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 src/server/mining.rs (limited to 'src/server/mining.rs') diff --git a/src/server/mining.rs b/src/server/mining.rs new file mode 100644 index 0000000..9aa054f --- /dev/null +++ b/src/server/mining.rs @@ -0,0 +1,84 @@ +extern crate serde_json; + +use std::io::BufRead; +use std::io::Write; +use std::collections::HashMap; + +use ship::Ship; +use math::distance; +use mass::{Mass, MassType}; +use connection::Connection; + +#[derive(Serialize, Deserialize, Debug, Clone)] +struct ServerData { + has_astroid_target : bool, + is_within_range : bool, + mining_range : f64, + mining_status : bool, +} + +impl Connection { + pub fn server_mining(&mut self, masses : &mut HashMap>) -> bool { + let masses_clone = masses.clone(); + let mass = masses.get_mut(&self.name).unwrap(); + let ship = mass.downcast_mut::().unwrap(); + + let target = match ship.recv_target() { + Some(name) => masses_clone.get(&name), + None => None, + }; + + let has_astroid_target = match target { + Some(target) => match target.recv_mass_type() { + MassType::Ship => false, + MassType::Astroid => true, + }, + None => false, + }; + + let is_within_range = match has_astroid_target { + true => match target { + Some(target) => match ship.recv_mining_range() > distance(ship.position(), target.position()) { + true => true, + false => false, + }, + None => false, + } + false => false, + }; + + let send = serde_json::to_string(&ServerData { + has_astroid_target : has_astroid_target, + is_within_range : is_within_range, + mining_range : ship.recv_mining_range(), + mining_status : ship.recv_mining_status(), + }).unwrap() + "\n"; + + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, + } + + 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 ship.recv_mining_status() { + true => ship.stop_mining(), + false => ship.start_mining(), + } + } + }, + _ => { + if result == 0 { + return false + } + }, + } + Err(_error) => (), + } + + true + } +} -- cgit v1.2.3