From 160d7b6e50df6d818f6882c033398d49193bd804 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 5 Mar 2018 08:14:14 -0600 Subject: -brought back downcasting, moved connection functions inside the structure --- src/astroid.rs | 4 ---- src/connection.rs | 17 +++++++---------- src/dashboard.rs | 15 ++++++++++----- src/engines.rs | 46 ++++++++++++++++++++++++-------------------- src/mass.rs | 3 --- src/navigation.rs | 57 +++++++++++++++++++++++++++++++++---------------------- src/ship.rs | 50 ++++++++++++++++++++++++------------------------ 7 files changed, 101 insertions(+), 91 deletions(-) diff --git a/src/astroid.rs b/src/astroid.rs index c789b87..25029ca 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -48,10 +48,6 @@ impl Mass for Astroid { serde_json::to_string(self).unwrap() } - fn slow(&mut self) {} - fn range(&self) -> f64 {0.0} - fn give_target(&mut self, target : Option) {} - fn box_clone(&self) -> Box { Box::new((*self).clone()) } diff --git a/src/connection.rs b/src/connection.rs index 0bae3b2..2698be3 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -6,16 +6,13 @@ extern crate serde_json; use ship::Ship; use mass::Mass; -use engines::server_engines; -use dashboard::server_dashboard; -use navigation::server_navigation; use module::{Module, from_primitive}; pub struct Connection { - index : usize, - module : Module, - stream : TcpStream, - buff_r : BufReader, + pub index : usize, + pub module : Module, + pub stream : TcpStream, + pub buff_r : BufReader, pub open : bool, } @@ -57,9 +54,9 @@ impl Connection { pub fn process(&mut self, mut masses : &mut Vec>) { self.open = match self.module { - Module::Dashboard => server_dashboard(masses[self.index].serialize(), &self.stream), - Module::Engines => server_engines(&mut masses[self.index], &mut self.buff_r), - Module::Navigation => server_navigation(&mut masses.to_vec(), &mut masses[self.index], &self.stream, &mut self.buff_r), + Module::Engines => self.server_engines(&mut masses), + Module::Dashboard => self.server_dashboard(&mut masses), + Module::Navigation => self.server_navigation(&mut masses), }; } } diff --git a/src/dashboard.rs b/src/dashboard.rs index bfad542..c4c3200 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -5,7 +5,9 @@ use std::io::Write; extern crate serde_json; +use mass::Mass; use ship::Ship; +use connection::Connection; pub fn client_dashboard(mut buff_r : BufReader) { loop { @@ -16,10 +18,13 @@ pub fn client_dashboard(mut buff_r : BufReader) { } } -pub fn server_dashboard(mut ship_string : String, mut stream : &TcpStream) -> bool { - ship_string.push_str("\n"); - match stream.write(ship_string.as_bytes()) { - Ok(_result) => true, - Err(_error) => false, +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()) { + Ok(_result) => true, + Err(_error) => false, + } } } diff --git a/src/engines.rs b/src/engines.rs index d320c9d..e951856 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -4,10 +4,11 @@ use termion::async_stdin; use std::thread::sleep; use std::io::{Read, Write, stdout}; use std::time::Duration; -use std::io::BufReader; use std::io::BufRead; +use ship::Ship; use mass::Mass; +use connection::Connection; pub fn client_engines(mut stream : TcpStream) { let stdout = stdout(); @@ -34,26 +35,29 @@ pub fn client_engines(mut stream : TcpStream) { } } -pub fn server_engines(ship : &mut Box, buff_r : &mut BufReader) -> bool { - let mut acceleration = (0.0, 0.0, 0.0); - let mut data = String::new(); - match buff_r.read_line(&mut data) { - Ok(result) => match data.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" => ship.slow(), - _ => { - if result == 0 { - return false - } +impl Connection { + pub fn server_engines(&mut self, masses : &mut Vec>) -> bool { + let ship = masses[self.index].downcast_mut::().unwrap(); + let mut acceleration = (0.0, 0.0, 0.0); + let mut data = String::new(); + match self.buff_r.read_line(&mut data) { + Ok(result) => match data.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" => ship.slow(), + _ => { + if result == 0 { + return false + } + }, }, - }, - Err(_error) => (), + Err(_error) => (), + } + ship.give_acceleration(acceleration); + true } - ship.give_acceleration(acceleration); - true } diff --git a/src/mass.rs b/src/mass.rs index d6feb6c..22862b6 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -5,11 +5,8 @@ pub trait Mass : Any { fn position(&self) -> (f64, f64, f64); fn serialize(&self) -> String; fn process(&mut self); - fn slow(&mut self); fn give_acceleration(&mut self, acceleration : (f64, f64, f64)); fn box_clone(&self) -> Box; - fn range(&self) -> f64; - fn give_target(&mut self, target : Option); } impl Clone for Box { diff --git a/src/navigation.rs b/src/navigation.rs index a9c38b2..b615eaa 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -11,6 +11,7 @@ use mass::Mass; use ship::Ship; use math::distance; use astroid::Astroid; +use connection::Connection; pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader){ let stdout = stdout(); @@ -24,7 +25,7 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf let string_masses = data.split(";"); let mut masses : Vec> = Vec::new(); for string_mass in string_masses { - if string_mass.len() == 1 { + if string_mass.len() <= 1 { break; } masses.push(build_mass(string_mass)); @@ -76,30 +77,40 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf } } -pub fn server_navigation(masses : &mut Vec>, ship : &mut Box, mut stream : &TcpStream, buff_r : &mut BufReader) -> bool { - let within_range : Vec<&Box> = masses.iter().filter(|mass| - distance(ship.position(), mass.position()) < ship.range()).collect(); - - let mut send = String::new(); - for mass in within_range { - send.push_str(&mass.serialize()); - send.push_str(";"); - } - send.push_str("\n"); - match stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, - } - - let mut string_mass = String::new(); +impl Connection { + pub fn server_navigation(&mut self, masses : &mut Vec>) -> bool { + let position = masses[self.index].position(); + let range = masses[self.index].downcast_ref::().unwrap().range(); + + { + let within_range : Vec<&Box> = masses.iter().filter(|mass| + distance(position, mass.position()) < range) + .collect(); + let mut send = String::new(); + for mass in within_range { + send.push_str(&mass.serialize()); + send.push_str(";"); + } + send.push_str("\n"); + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, + } + } - if string_mass.len() > 0 { - let target = build_mass(&string_mass); - ship.give_target(masses.iter().position(|mass| - mass.name() == target.name())); + let mut string_mass = String::new(); + match self.buff_r.read_line(&mut string_mass) { + Ok(_result) => (), + Err(_error) => (), + } + if string_mass.len() > 0 { + let target = build_mass(&string_mass); + let t = masses.iter().position(|mass| + mass.name() == target.name()); + masses[self.index].downcast_mut::().unwrap().give_target(t); + } + true } - - true } fn build_mass(string_mass : &str) -> Box { diff --git a/src/ship.rs b/src/ship.rs index 2482db1..b4b32e7 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -22,30 +22,8 @@ impl Ship { target : None, } } -} - -impl Mass for Ship { - fn name(&self) -> &String { - &self.name - } - - fn position(&self) -> (f64, f64, f64) { - self.position - } - - fn serialize(&self) -> String { - serde_json::to_string(self).unwrap() - } - - fn range(&self) -> f64 { - self.r - } - fn give_target(&mut self, target : Option) { - self.target = target; - } - - fn slow(&mut self) { + pub fn slow(&mut self) { if self.velocity.0 > 0.01 { self.velocity.0 += -1.0 * self.velocity.0 * 0.1; } @@ -68,8 +46,18 @@ impl Mass for Ship { } } - fn box_clone(&self) -> Box { - Box::new((*self).clone()) + pub fn range(&self) -> f64 { + self.r + } + + pub fn give_target(&mut self, target : Option) { + self.target = target; + } +} + +impl Mass for Ship { + fn name(&self) -> &String { + &self.name } fn process(&mut self) { @@ -78,6 +66,18 @@ impl Mass for Ship { self.position.2 += self.velocity.2; } + fn position(&self) -> (f64, f64, f64) { + self.position + } + + fn serialize(&self) -> String { + serde_json::to_string(self).unwrap() + } + + fn box_clone(&self) -> Box { + Box::new((*self).clone()) + } + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { self.velocity.0 += acceleration.0; self.velocity.1 += acceleration.1; -- cgit v1.2.3