From 20d2ab63c46d97a9dcc4df848dd728419d0031f9 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 5 Apr 2018 08:58:01 -0500 Subject: -changed how everything is referenced by its name now, instead of the index, -also added targeting status to the navigation panel --- src/connection.rs | 22 ++++++++++------------ src/dashboard.rs | 6 +++--- src/engines.rs | 13 ++++++++----- src/navigation.rs | 35 +++++++++++++++++++++++++---------- src/ship.rs | 17 +++++++++-------- 5 files changed, 55 insertions(+), 38 deletions(-) (limited to 'src') diff --git a/src/connection.rs b/src/connection.rs index f0b3c25..954cd27 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -9,7 +9,7 @@ use mass::Mass; use module::Module; pub struct Connection { - pub index : usize, + pub name : String, pub module : Module, pub stream : TcpStream, pub buff_r : BufReader, @@ -24,17 +24,15 @@ impl Connection { buff_r.read_line(&mut data).unwrap(); let name = &data[..data.find(":").unwrap()]; - let result = masses.into_iter().position(|ship| ship.name() == name); - let index = match result { - Some(index) => index, - None => { - let ship = Box::new(Ship::new(name, (0.0, 0.0, 0.0))); - masses.push(ship); - masses.len() - 1 - }, - }; + match masses.iter().find(|ship| ship.name() == name).is_some() { + false => masses.push(Box::new(Ship::new(name, (0.0, 0.0, 0.0)))), + _ => (), + } + + let mass = masses.iter().find(|ship| ship.name() == name).unwrap(); + let ship = mass.downcast_ref::().unwrap(); - let modules = masses[index].downcast_ref::().unwrap().get_modules(); + let modules = ship.get_modules(); stream.write(modules.as_bytes()).unwrap(); let mut data = String::new(); @@ -44,7 +42,7 @@ impl Connection { stream.set_nonblocking(true).unwrap(); Connection { - index : index, + name : String::from(name), module : module, stream : stream, buff_r : buff_r, diff --git a/src/dashboard.rs b/src/dashboard.rs index c4c3200..e552127 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -20,9 +20,9 @@ pub fn client_dashboard(mut buff_r : BufReader) { impl Connection { pub fn server_dashboard(&mut self, masses : &mut Vec>) -> bool { - let mut ship_string = masses[self.index].serialize(); - ship_string.push_str("\n"); - match self.stream.write(ship_string.as_bytes()) { + let ship = masses.iter().find(|ship| ship.name() == &self.name).unwrap(); + let send = ship.serialize() + "\n"; + match self.stream.write(send.as_bytes()) { Ok(_result) => true, Err(_error) => false, } diff --git a/src/engines.rs b/src/engines.rs index 548b44b..d7ae865 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -58,7 +58,8 @@ pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader) impl Connection { pub fn server_engines(&mut self, masses : &mut Vec>) -> bool { let m = masses.to_vec(); - let ship = masses[self.index].downcast_mut::().unwrap(); + let mass = masses.into_iter().find(|ship| ship.name() == &self.name).unwrap(); + let ship = mass.downcast_mut::().unwrap(); let mut send = String::new(); match ship.recv_target().is_some() { @@ -84,8 +85,9 @@ impl Connection { b"-\n" => ship.slow(), b"c\n" => { match ship.recv_target() { - Some(index) => { - let d_v = m[index].recv_velocity(); + Some(name) => { + let target = m.into_iter().find(|target| target.name() == &name).unwrap(); + let d_v = target.recv_velocity(); let m_v = ship.recv_velocity(); acceleration = (d_v.0 - m_v.0, d_v.1 - m_v.1, @@ -96,8 +98,9 @@ impl Connection { }, b"t\n" => { match ship.recv_target() { - Some(index) => { - let d_p = m[index].position(); + Some(name) => { + let target = m.into_iter().find(|target| target.name() == &name).unwrap(); + let d_p = target.recv_velocity(); let m_p = ship.position(); acceleration = ((d_p.0 - m_p.0) * 0.01, (d_p.1 - m_p.1) * 0.01, diff --git a/src/navigation.rs b/src/navigation.rs index 67f53cb..84aca86 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -32,22 +32,36 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf } let index = masses.iter().position(|ship| ship.name() == &name).unwrap(); - let ship = masses.remove(index); + let ship = masses.remove(index).downcast::().unwrap(); write!(stdout, "{}{}Targets:", termion::clear::All, termion::cursor::Goto(1,1)).unwrap(); - let position = ship.position(); for (i, mass) in masses.iter().enumerate() { - write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2}", + + let target_data = match ship.recv_target() { + Some(name) => { + if &name == mass.name() { + serde_json::to_string(&ship.recv_target_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(), position)).unwrap(); + distance(mass.position(), ship.position()), + target_data + ).unwrap(); } match stdin.next() { @@ -80,11 +94,13 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf impl Connection { pub fn server_navigation(&mut self, masses : &mut Vec>) -> bool { let m = masses.to_vec(); - let ship = masses[self.index].downcast_mut::().unwrap(); + let mass = masses.into_iter().find(|ship| ship.name() == &self.name).unwrap(); + let ship = mass.downcast_mut::().unwrap(); match ship.recv_target() { - Some(index) => { - if distance(m[index].position(), ship.position()) > ship.range() { + Some(name) => { + let target = m.iter().find(|target| target.name() == &name).unwrap(); + if distance(target.position(), ship.position()) > ship.range() { ship.give_target(None); } } @@ -112,9 +128,8 @@ impl Connection { } if string_mass.len() > 0 { let target = build_mass(&string_mass); - let t = m.iter().position(|mass| - mass.name() == target.name()); - ship.give_target(t); + let name = target.name().clone(); + ship.give_target(Some(name)); } true } diff --git a/src/ship.rs b/src/ship.rs index f97e4d2..4f2a910 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -5,7 +5,7 @@ use mass::{Mass, Type}; extern crate serde_json; #[derive(Serialize, Deserialize, Debug, Clone)] -enum TargetingStatus { +pub enum TargetingStatus { None, Targeting, Targeted, @@ -13,7 +13,7 @@ enum TargetingStatus { #[derive(Serialize, Deserialize, Debug, Clone)] struct Targeting { - target : Option, + target : Option, status : TargetingStatus, time : u64, start : Option, @@ -87,7 +87,7 @@ impl Ship { self.r } - pub fn give_target(&mut self, target : Option) { + pub fn give_target(&mut self, target : Option) { self.targeting.target = target; match self.targeting.target { Some(_) => { @@ -101,11 +101,12 @@ impl Ship { } } - pub fn recv_target(&self) -> Option { - match self.targeting.status { - TargetingStatus::Targeted => self.targeting.target, - _ => None - } + pub fn recv_target(&self) -> Option { + self.targeting.target.clone() + } + + pub fn recv_target_status(&self) -> TargetingStatus { + self.targeting.status.clone() } pub fn get_modules(&self) -> String { -- cgit v1.2.3