diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/client/mining.rs | 10 | ||||
-rw-r--r-- | src/client/navigation.rs | 88 | ||||
-rw-r--r-- | src/server/engines.rs | 124 | ||||
-rw-r--r-- | src/server/mining.rs | 138 | ||||
-rw-r--r-- | src/server/navigation.rs | 63 |
5 files changed, 194 insertions, 229 deletions
diff --git a/src/client/mining.rs b/src/client/mining.rs index 44e7c30..5cb1240 100644 --- a/src/client/mining.rs +++ b/src/client/mining.rs @@ -7,13 +7,7 @@ use std::io::{BufReader, BufRead}; use std::io::{stdout, Read, Write}; use self::termion::raw::IntoRawMode; -#[derive(Serialize, Deserialize, Debug, Clone)] -struct ServerData { - has_astroid_target : bool, - is_within_range : bool, - mining_range : f64, - mining_status : bool, -} +use server::mining::MiningData; pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) { let stdout = stdout(); @@ -23,7 +17,7 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) loop { let mut recv = String::new(); buff_r.read_line(&mut recv).unwrap(); - let data : ServerData = serde_json::from_str(&recv.replace("\n", "")).unwrap(); + let data : MiningData = serde_json::from_str(&recv.replace("\n", "")).unwrap(); write!(stdout, "{}", termion::clear::All).unwrap(); diff --git a/src/client/navigation.rs b/src/client/navigation.rs index 59b6987..eb9a2c4 100644 --- a/src/client/navigation.rs +++ b/src/client/navigation.rs @@ -28,58 +28,52 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf let ship = within_range.remove(&name).unwrap(); - match ship.mass_type { - MassType::Ship{ref modules, ..} => { - match modules.get("Navigation").unwrap().module_type { - ModuleType::Navigation{ref status, ref target_name, ..} => { - for (i, (mass_name, mass)) in within_range.iter().enumerate() { - let target_data = match target_name.clone() { - Some(target_name) => { - if &target_name == mass_name { - serde_json::to_string(status).unwrap() - } - else { - String::new() - } - } - None => String::new(), - }; - - write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}", - termion::cursor::Goto(1, 2 + i as u16), - i, - mass_name, - mass.position.0, - mass.position.1, - mass.position.2, - distance(mass.position, ship.position), - target_data - ).unwrap(); + if let MassType::Ship{ref modules, ..} = ship.mass_type { + if let ModuleType::Navigation{ref status, ref target_name, ..} = modules.get("Navigation").unwrap().module_type { + for (i, (mass_name, mass)) in within_range.iter().enumerate() { + let target_data = match target_name.clone() { + Some(target_name) => { + if &target_name == mass_name { + serde_json::to_string(status).unwrap() + } + else { + String::new() + } } + None => String::new(), + }; - match stdin.next() { - Some(c) => { - let c = c.unwrap() as char; - if c == 'q' { - break; - } - else { - let i = c.to_digit(10).unwrap() as usize; - if i < within_range.len() { - let mut send = String::new(); - send.push_str(within_range.iter().nth(i).unwrap().0); - send.push_str("\n"); - stream.write(send.as_bytes()).unwrap(); - } - } + write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}", + termion::cursor::Goto(1, 2 + i as u16), + i, + mass_name, + mass.position.0, + mass.position.1, + mass.position.2, + distance(mass.position, ship.position), + target_data + ).unwrap(); + } + + match stdin.next() { + Some(c) => { + let c = c.unwrap() as char; + if c == 'q' { + break; + } + else { + let i = c.to_digit(10).unwrap() as usize; + if i < within_range.len() { + let mut send = String::new(); + send.push_str(within_range.iter().nth(i).unwrap().0); + send.push_str("\n"); + stream.write(send.as_bytes()).unwrap(); } - None => () } - }, - _ => (), + } + None => () } - }, - _ => (), + } } stdout.flush().unwrap(); 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 } |