summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-03-05 08:14:14 -0600
committertom barrett <spalf0@gmail.com>2018-03-05 08:14:14 -0600
commit160d7b6e50df6d818f6882c033398d49193bd804 (patch)
treeb211d052dce89c9c69012c8e6c7f9de648322086 /src
parent233aa5d803e8015f78f663bc9af6bb33e56eb96e (diff)
-brought back downcasting, moved connection functions inside the structure
Diffstat (limited to 'src')
-rw-r--r--src/astroid.rs4
-rw-r--r--src/connection.rs17
-rw-r--r--src/dashboard.rs15
-rw-r--r--src/engines.rs46
-rw-r--r--src/mass.rs3
-rw-r--r--src/navigation.rs57
-rw-r--r--src/ship.rs50
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<usize>) {}
-
fn box_clone(&self) -> Box<Mass> {
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<TcpStream>,
+ pub index : usize,
+ pub module : Module,
+ pub stream : TcpStream,
+ pub buff_r : BufReader<TcpStream>,
pub open : bool,
}
@@ -57,9 +54,9 @@ impl Connection {
pub fn process(&mut self, mut masses : &mut Vec<Box<Mass>>) {
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<TcpStream>) {
loop {
@@ -16,10 +18,13 @@ pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) {
}
}
-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<Box<Mass>>) -> 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<Mass>, buff_r : &mut BufReader<TcpStream>) -> 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<Box<Mass>>) -> bool {
+ let ship = masses[self.index].downcast_mut::<Ship>().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<Mass>;
- fn range(&self) -> f64;
- fn give_target(&mut self, target : Option<usize>);
}
impl Clone for Box<Mass> {
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<TcpStream>){
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<Box<Mass>> = 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<Box<Mass>>, ship : &mut Box<Mass>, mut stream : &TcpStream, buff_r : &mut BufReader<TcpStream>) -> bool {
- let within_range : Vec<&Box<Mass>> = 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<Box<Mass>>) -> bool {
+ let position = masses[self.index].position();
+ let range = masses[self.index].downcast_ref::<Ship>().unwrap().range();
+
+ {
+ let within_range : Vec<&Box<Mass>> = 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::<Ship>().unwrap().give_target(t);
+ }
+ true
}
-
- true
}
fn build_mass(string_mass : &str) -> Box<Mass> {
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<usize>) {
- 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<Mass> {
- Box::new((*self).clone())
+ pub fn range(&self) -> f64 {
+ self.r
+ }
+
+ pub fn give_target(&mut self, target : Option<usize>) {
+ 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<Mass> {
+ Box::new((*self).clone())
+ }
+
fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) {
self.velocity.0 += acceleration.0;
self.velocity.1 += acceleration.1;