summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-02-23 01:10:14 -0600
committertom barrett <spalf0@gmail.com>2018-02-23 01:10:14 -0600
commit7798ebba1d8762e8b4c6eaf1efcd610fa030375a (patch)
treee5d01dfadf6fa8695c29f6ed4ba2a1c727ecd901 /src
parent839f8a7a260276973f41a2c5a6e9d138518d7072 (diff)
-found way to downcast and created new struct
Diffstat (limited to 'src')
-rw-r--r--src/astroid.rs33
-rw-r--r--src/bin/client.rs3
-rw-r--r--src/bin/server.rs12
-rw-r--r--src/connection.rs30
-rw-r--r--src/lib.rs6
-rw-r--r--src/mass.rs12
-rw-r--r--src/navigation.rs10
-rw-r--r--src/ship.rs14
8 files changed, 105 insertions, 15 deletions
diff --git a/src/astroid.rs b/src/astroid.rs
new file mode 100644
index 0000000..dd47123
--- /dev/null
+++ b/src/astroid.rs
@@ -0,0 +1,33 @@
+use mass::Mass;
+extern crate serde_json;
+
+#[derive(Serialize, Deserialize, Debug)]
+pub struct Astroid {
+ name : String,
+ location : (isize, isize, isize),
+}
+
+impl Mass for Astroid {
+ fn new(name : &str, location : (isize, isize, isize)) -> Astroid {
+ Astroid {
+ name : String::from(name),
+ location : location,
+ }
+ }
+
+ fn name(&self) -> &String {
+ &self.name
+ }
+
+ fn location(&self) -> (isize, isize, isize) {
+ self.location
+ }
+
+ fn set_location(&mut self, location : (isize, isize, isize)) {
+ self.location = location;
+ }
+
+ fn serialize(&self) ->String {
+ serde_json::to_string(self).unwrap()
+ }
+}
diff --git a/src/bin/client.rs b/src/bin/client.rs
index 6f4a8fc..2e8dd45 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -6,6 +6,7 @@ use std::net::TcpStream;
extern crate space;
use space::dashboard::Dashboard;
use space::engines::Engines;
+use space::navigation::Navigation;
use space::module::{Module, from_primitive};
@@ -46,6 +47,6 @@ fn main() {
match module {
Module::Dashboard => Dashboard(buff_r),
Module::Engines => Engines(stream),
- _ => (),
+ Module::Navigation => Navigation(stream, buff_r),
}
}
diff --git a/src/bin/server.rs b/src/bin/server.rs
index d2923d8..e042855 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -4,13 +4,23 @@ use std::net::TcpListener;
extern crate space;
use space::mass::Mass;
+use space::astroid::Astroid;
use space::connection::Connection;
+
+fn populate() -> Vec<Box<Mass>> {
+ let mut masses : Vec<Box<Mass>> = Vec::new();
+
+ masses.push(Box::new(Astroid::new("cZfAJ", (10, -5, 4))));
+
+ masses
+}
+
fn main() {
let listener = TcpListener::bind("localhost:6000").unwrap();
listener.set_nonblocking(true).unwrap();
- let mut masses : Vec<Box<Mass>>= Vec::new();
+ let mut masses = populate();
let mut connections = Vec::new();
for stream in listener.incoming() {
diff --git a/src/connection.rs b/src/connection.rs
index 5c086fa..b1a2056 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -24,7 +24,7 @@ impl Connection {
buff_r.read_line(&mut data).unwrap();
let name = &data[..data.find(":").unwrap()];
- let result = masses.into_iter().position(|ship| ship.get_name() == name);
+ let result = masses.into_iter().position(|ship| ship.name() == name);
let index = match result {
Some(index) => index,
None => {
@@ -52,7 +52,6 @@ impl Connection {
}
}
-
pub fn process(&mut self, masses : &mut Vec<Box<Mass>>) {
match self.module {
Module::Dashboard => {
@@ -64,7 +63,7 @@ impl Connection {
}
}
Module::Engines => {
- let mut location = masses[self.index].get_location();
+ let mut location = masses[self.index].location();
let mut data = String::new();
match self.buff_r.read_line(&mut data) {
Ok(result) => match data.as_bytes() {
@@ -82,11 +81,32 @@ impl Connection {
},
Err(_error) => (),
}
- masses[self.index].give_location(location);
+ masses[self.index].set_location(location);
}
Module::Navigation => {
- ()
+ let ship = &masses[self.index].downcast_ref::<Ship>().unwrap();
+
+ let mut within_range = Vec::new();
+ for mass in masses.iter() {
+ if distance(ship.location(), mass.location()) > ship.range() {
+ within_range.push(mass);
+ }
+ }
+ let mut send = String::new();
+ for mass in within_range {
+ send.push_str(&mass.serialize());
+ send.push_str("\n");
+ }
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => self.open = false,
+ }
}
}
}
+
+}
+
+fn distance(l0 : (isize, isize, isize), l1 : (isize, isize, isize)) -> f64 {
+ (((l1.0-l0.0).pow(2) + (l1.1-l0.1).pow(2) + (l1.2-l0.2).pow(2)) as f64).sqrt()
}
diff --git a/src/lib.rs b/src/lib.rs
index 90fd64b..0611f67 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,11 +1,15 @@
#[macro_use]
extern crate serde_derive;
+#[macro_use]
+extern crate downcast;
+
extern crate termion;
pub mod mass;
pub mod ship;
pub mod module;
+pub mod astroid;
pub mod engines;
pub mod dashboard;
pub mod connection;
-
+pub mod navigation;
diff --git a/src/mass.rs b/src/mass.rs
index d1f9d3e..955f84d 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,7 +1,11 @@
-pub trait Mass {
+use downcast::Any;
+
+pub trait Mass : Any {
fn new(name : &str, location : (isize, isize, isize)) -> Self where Self: Sized;
- fn get_name(&self) -> &String;
- fn get_location(&self) -> (isize, isize, isize);
- fn give_location(&mut self, location : (isize, isize, isize));
+ fn name(&self) -> &String;
+ fn location(&self) -> (isize, isize, isize);
+ fn set_location(&mut self, location : (isize, isize, isize));
fn serialize(&self) -> String;
}
+
+downcast!(Mass);
diff --git a/src/navigation.rs b/src/navigation.rs
new file mode 100644
index 0000000..b3cd9cf
--- /dev/null
+++ b/src/navigation.rs
@@ -0,0 +1,10 @@
+use std::net::TcpStream;
+use std::io::BufRead;
+use std::io::BufReader;
+
+pub fn Navigation(mut stream :TcpStream, mut buff_r : BufReader<TcpStream>){
+ loop {
+ let mut data = String::new();
+ buff_r.read_line(&mut data).unwrap();
+ }
+}
diff --git a/src/ship.rs b/src/ship.rs
index 715f019..30a27fb 100644
--- a/src/ship.rs
+++ b/src/ship.rs
@@ -5,6 +5,13 @@ extern crate serde_json;
pub struct Ship {
name : String,
location : (isize, isize, isize),
+ r : f64,
+}
+
+impl Ship {
+ pub fn range(&self) -> f64 {
+ self.r
+ }
}
impl Mass for Ship {
@@ -12,18 +19,19 @@ impl Mass for Ship {
Ship {
name : String::from(name),
location : location,
+ r : 10.0,
}
}
- fn get_name(&self) -> &String {
+ fn name(&self) -> &String {
&self.name
}
- fn get_location(&self) -> (isize, isize, isize) {
+ fn location(&self) -> (isize, isize, isize) {
self.location
}
- fn give_location(&mut self, location : (isize, isize, isize)) {
+ fn set_location(&mut self, location : (isize, isize, isize)) {
self.location = location;
}