diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/dashboard.rs | 2 | ||||
-rw-r--r-- | src/server/engines.rs | 137 | ||||
-rw-r--r-- | src/server/mining.rs | 119 | ||||
-rw-r--r-- | src/server/navigation.rs | 65 |
4 files changed, 184 insertions, 139 deletions
diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs index 82f73f9..81ac522 100644 --- a/src/server/dashboard.rs +++ b/src/server/dashboard.rs @@ -9,7 +9,7 @@ use connection::Connection; impl Connection { pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Mass>) -> bool { let ship = masses.get(&self.name).unwrap(); - let send = serde_json::to_string(&ship).unwrap(); + let send = serde_json::to_string(&ship).unwrap() + "\n"; match self.stream.write(send.as_bytes()) { Ok(_result) => true, Err(_error) => false, diff --git a/src/server/engines.rs b/src/server/engines.rs index aa0e703..85346f6 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -5,82 +5,89 @@ use std::io::BufRead; use std::collections::HashMap; use mass::{Mass, MassType}; +use module::{ModuleType}; use connection::Connection; -use targeting::TargetingStatus; +use module::NavigationStatus; impl Connection { pub fn server_engines(&mut self, masses : &mut HashMap<String, Mass>) -> bool { let masses_clone = masses.clone(); let ship = masses.get_mut(&self.name).unwrap(); - let targeting = match ship.mass_type { - MassType::Ship{ref targeting, ..} => Some(targeting.clone()), - _ => None, - }.unwrap(); + let mut acceleration = (0.0, 0.0, 0.0); - let targeted = targeting.status == TargetingStatus::Targeted; - let send = serde_json::to_string(&targeted).unwrap() + "\n"; + match ship.mass_type { + MassType::Ship{ref modules, ..} => { + match modules.get("Navigation").unwrap().module_type { + ModuleType::Navigation{ref status, ref target_name, ..} => { + let targeted = status == &NavigationStatus::Targeted; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, - } + let send = serde_json::to_string(&targeted).unwrap() + "\n"; + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, + } - let mut acceleration = (0.0, 0.0, 0.0); - let mut recv = String::new(); - match self.buff_r.read_line(&mut recv) { - Ok(result) => match recv.as_bytes() { - b"5\n" => acceleration.0 += 0.1, - b"0\n" => acceleration.0 -= 0.1, - b"8\n" => acceleration.1 += 0.1, - b"2\n" => acceleration.1 -= 0.1, - b"4\n" => acceleration.2 += 0.1, - b"6\n" => acceleration.2 -= 0.1, - b"+\n" => { - let m_v = ship.velocity; - acceleration = (m_v.0 * 0.05, - m_v.1 * 0.05, - m_v.2 * 0.05); - }, - b"-\n" => { - let m_v = ship.velocity; - acceleration = (-1.0 * m_v.0 * 0.05, - -1.0 * m_v.1 * 0.05, - -1.0 * m_v.2 * 0.05); - }, - b"c\n" => { - match targeting.target { - Some(name) => { - let target = masses_clone.get(&name).unwrap(); - let d_v = target.velocity; - let m_v = ship.velocity; - acceleration = (d_v.0 - m_v.0, - d_v.1 - m_v.1, - d_v.2 - m_v.2); - }, - None => (), - } - }, - b"t\n" => { - match targeting.target { - Some(name) => { - let target = masses_clone.get(&name).unwrap(); - let d_p = target.position; - let m_p = ship.position; - acceleration = ((d_p.0 - m_p.0) * 0.01, - (d_p.1 - m_p.1) * 0.01, - (d_p.2 - m_p.2) * 0.01); - }, - None => (), - } - }, - _ => { - if result == 0 { - return false - } - }, + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => match recv.as_bytes() { + b"5\n" => acceleration.0 += 0.1, + b"0\n" => acceleration.0 -= 0.1, + b"8\n" => acceleration.1 += 0.1, + b"2\n" => acceleration.1 -= 0.1, + b"4\n" => acceleration.2 += 0.1, + b"6\n" => acceleration.2 -= 0.1, + b"+\n" => { + let m_v = ship.velocity; + acceleration = (m_v.0 * 0.05, + m_v.1 * 0.05, + m_v.2 * 0.05); + }, + b"-\n" => { + let m_v = ship.velocity; + acceleration = (-1.0 * m_v.0 * 0.05, + -1.0 * m_v.1 * 0.05, + -1.0 * m_v.2 * 0.05); + }, + b"c\n" => { + match target_name { + &Some(ref name) => { + let target = masses_clone.get(name).unwrap(); + let d_v = target.velocity; + let m_v = ship.velocity; + acceleration = (d_v.0 - m_v.0, + d_v.1 - m_v.1, + d_v.2 - m_v.2); + }, + &None => (), + } + }, + b"t\n" => { + match target_name { + &Some(ref name) => { + let target = masses_clone.get(name).unwrap(); + let d_p = target.position; + let m_p = ship.position; + acceleration = ((d_p.0 - m_p.0) * 0.01, + (d_p.1 - m_p.1) * 0.01, + (d_p.2 - m_p.2) * 0.01); + }, + &None => (), + } + }, + _ => { + if result == 0 { + return false + } + }, + }, + Err(_error) => (), + } + }, + _ => (), + } }, - Err(_error) => (), + _ => (), } ship.accelerate(acceleration); diff --git a/src/server/mining.rs b/src/server/mining.rs index ff26840..98803c5 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -6,6 +6,7 @@ use std::collections::HashMap; use math::distance; use mass::{Mass, MassType}; +use module::ModuleType; use connection::Connection; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -21,60 +22,90 @@ impl Connection { let masses_clone = masses.clone(); let ship = masses.get_mut(&self.name).unwrap(); - let (mining, targeting) = match ship.mass_type { - MassType::Ship{ref targeting, ref mut mining, ..} => (Some(mining), Some(targeting.clone())), - _ => (None, None), - }; - let mining = mining.unwrap(); - let target = masses_clone.get(&targeting.unwrap().target.unwrap()); - let has_astroid_target = match target { - Some(target) => match target.mass_type { - MassType::Astroid{..} => true, - _ => false, - }, - None => false, - }; + match ship.mass_type { + MassType::Ship{ref modules, ..} => { + let mut mining_range = 0.0; + let mut mining_status = false; - let is_within_range = match has_astroid_target { - true => match target { - Some(target) => mining.range > distance(ship.position, target.position), - _ => false, - } - _ => false, - }; + match modules.get("Mining").unwrap().module_type { + ModuleType::Mining{ref range, ref status, ..} => { + mining_range = range.clone(); + mining_status = status.clone(); + } + _ => (), + } - let send = serde_json::to_string(&ServerData { - has_astroid_target : has_astroid_target, - is_within_range : is_within_range, - mining_range : mining.range, - mining_status : mining.status, - }).unwrap() + "\n"; + match modules.get("Navigation").unwrap().module_type { + ModuleType::Navigation{ref target_name, ..} => { + 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, + }; + } + _ => (), + } - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, - } + let send = serde_json::to_string(&ServerData { + has_astroid_target : has_astroid_target, + is_within_range : is_within_range, + mining_range : mining_range, + mining_status : mining_status, + }).unwrap() + "\n"; - 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(), + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, } } - }, - _ => { - if result == 0 { - return false + _ => (), + } + + match modules.get("Mining").unwrap().module_type { + ModuleType::Mining{ref range, ref status, ..} => { + 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 result == 0 { + return false + } + }, + } + Err(_error) => (), + } } - }, + _ => (), + } } - Err(_error) => (), + _ => (), } + true } } diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 3813732..58d88e7 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -2,10 +2,12 @@ extern crate serde_json; use std::io::Write; use std::io::BufRead; +use std::time::SystemTime; use std::collections::HashMap; -use mass::{Mass, MassType}; use math::distance; +use module::{ModuleType, NavigationStatus}; +use mass::{Mass, MassType}; use connection::Connection; impl Connection { @@ -15,40 +17,45 @@ impl Connection { let ship_position = ship.position; match ship.mass_type { - MassType::Ship{ref mut targeting, ..} => { - let target_name = targeting.clone().target; - match target_name { - Some(target_name) => { - let target = masses_clone.get(&target_name).unwrap(); - if distance(target.position, ship.position) > targeting.range { - targeting.target = None; + MassType::Ship{ref mut modules, ..} => { + match modules.get_mut("Navigation").unwrap().module_type { + ModuleType::Navigation{ref mut target_name, ref mut start, ref mut status, ref range, ..} => { + match target_name.clone() { + Some(name) => { + let target = masses_clone.get(&name).unwrap(); + if distance(target.position, ship.position) > *range { + *target_name = None; + } + }, + _ => (), } - }, - _ => (), - } - let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)| - distance(ship_position, mass.position) < targeting.range) - .collect(); + let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)| + distance(ship_position, mass.position) < *range) + .collect(); - let send = serde_json::to_string(&within_range).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, - } + let send = serde_json::to_string(&within_range).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) => (), - Err(_error) => (), - } - if !recv.is_empty() { - targeting.give_target(Some(recv.replace("\n", ""))); + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(_result) => (), + Err(_error) => (), + } + if !recv.is_empty() { + *target_name = Some(recv.replace("\n", "")); + *start = Some(SystemTime::now()); + *status = NavigationStatus::Targeting; + } + }, + _ => (), } - }, - _ => (), + } + _ => (), } - true } } |