diff options
author | tom barrett <spalf0@gmail.com> | 2018-02-23 01:10:14 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-02-23 01:10:14 -0600 |
commit | 7798ebba1d8762e8b4c6eaf1efcd610fa030375a (patch) | |
tree | e5d01dfadf6fa8695c29f6ed4ba2a1c727ecd901 | |
parent | 839f8a7a260276973f41a2c5a6e9d138518d7072 (diff) |
-found way to downcast and created new struct
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/astroid.rs | 33 | ||||
-rw-r--r-- | src/bin/client.rs | 3 | ||||
-rw-r--r-- | src/bin/server.rs | 12 | ||||
-rw-r--r-- | src/connection.rs | 30 | ||||
-rw-r--r-- | src/lib.rs | 6 | ||||
-rw-r--r-- | src/mass.rs | 12 | ||||
-rw-r--r-- | src/navigation.rs | 10 | ||||
-rw-r--r-- | src/ship.rs | 14 |
10 files changed, 113 insertions, 15 deletions
@@ -1,4 +1,9 @@ [[package]] +name = "downcast" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "dtoa" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -83,6 +88,7 @@ dependencies = [ name = "space" version = "0.1.0" dependencies = [ + "downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -123,6 +129,7 @@ version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" [metadata] +"checksum downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "242756f88358d71447a37780b71b1cb180a276bd214765ee2d81dfe7c90e59bb" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" @@ -8,3 +8,4 @@ serde = "1.0" serde_derive = "1.0" serde_json = "1.0" termion = "1.5.1" +downcast = "0.8" 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() } @@ -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; } |