diff options
author | tom barrett <spalf0@gmail.com> | 2018-04-17 04:17:29 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-04-17 04:17:29 -0500 |
commit | aeaaefd2706479378a24c1ab6d1dc8f74d557e78 (patch) | |
tree | 82b1ed085a2c098fc92e83257f4cbfa52b0cad00 /src/server | |
parent | 07b0ba697bff4209907f6abb734fb52a889753e9 (diff) |
-moved over to if let to get less indentation
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/engines.rs | 124 | ||||
-rw-r--r-- | src/server/mining.rs | 138 | ||||
-rw-r--r-- | src/server/navigation.rs | 63 |
3 files changed, 151 insertions, 174 deletions
diff --git a/src/server/engines.rs b/src/server/engines.rs index c2a97ef..b837a90 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -16,78 +16,72 @@ impl ServerConnection { let ship = masses.get_mut(&self.name).unwrap(); let mut acceleration = (0.0, 0.0, 0.0); - 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; + if let MassType::Ship{ref modules, ..} = ship.mass_type { + if let ModuleType::Navigation{ref status, ref target_name, ..} = modules.get("Navigation").unwrap().module_type { + let targeted = status == &NavigationStatus::Targeted; - let send = serde_json::to_string(&targeted).unwrap() + "\n"; - 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 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 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 = (-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 => (), - } + acceleration = (d_v.0 - m_v.0, + d_v.1 - m_v.1, + d_v.2 - m_v.2); }, - _ => { - if result == 0 { - return false - } + &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); }, - }, - Err(_error) => (), - } + &None => (), + } + }, + _ => { + if result == 0 { + return false + } + }, }, - _ => (), + Err(_error) => (), } - }, - _ => (), + } } ship.accelerate(acceleration); diff --git a/src/server/mining.rs b/src/server/mining.rs index fd8b6ff..2904d51 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -7,14 +7,14 @@ use std::collections::HashMap; use math::distance; use mass::{Mass, MassType}; use module::ModuleType; -use::server::connection::ServerConnection; +use server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] -struct ServerData { - has_astroid_target : bool, - is_within_range : bool, - mining_range : f64, - mining_status : bool, +pub struct MiningData { + pub has_astroid_target : bool, + pub is_within_range : bool, + pub mining_range : f64, + pub mining_status : bool, } impl ServerConnection { @@ -23,87 +23,75 @@ impl ServerConnection { let ship = masses.get_mut(&self.name).unwrap(); - match ship.mass_type { - MassType::Ship{ref modules, ..} => { - let mut mining_range = 0.0; - let mut mining_status = false; + if let MassType::Ship{ref mut modules, ..} = ship.mass_type { + let mut mining_range = 0.0; + let mut mining_status = false; - match modules.get("Mining").unwrap().module_type { - ModuleType::Mining{ref range, ref status, ..} => { - mining_range = *range; - mining_status = *status; + if let ModuleType::Mining{ref range, ref status, ..} = modules.get("Mining").unwrap().module_type { + mining_range = *range; + mining_status = *status; + } + + 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, + }; } _ => (), } - 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, - }; - } - _ => (), - } - - 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 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, - } - } - _ => (), + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => 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 - } - }, + if let ModuleType::Mining{ref range, ref status, ..} = modules.get("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(), + } } - Err(_error) => (), - } + */ + }, + _ => { + if result == 0 { + return false + } + }, } - _ => (), + Err(_error) => (), } } - _ => (), } true diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 1d332f9..7f4a1bc 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -16,45 +16,40 @@ impl ServerConnection { let ship = masses.get_mut(&self.name).unwrap(); let ship_position = ship.position; - match ship.mass_type { - 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) < *range) - .collect(); + if let MassType::Ship{ref mut modules, ..} = ship.mass_type { + let mut navigation = modules.get_mut("Navigation").unwrap(); + if let ModuleType::Navigation{ref mut target_name, ref mut start, ref mut status, ref range, ..} = navigation.module_type { - let send = serde_json::to_string(&within_range).unwrap() + "\n"; - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, + match target_name.clone() { + Some(name) => { + let target = masses_clone.get(&name).unwrap(); + if distance(target.position, ship.position) > *range { + *target_name = None; } + } + _ => (), + } - 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; - } - }, - _ => (), + 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 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 } |