From 7798ebba1d8762e8b4c6eaf1efcd610fa030375a Mon Sep 17 00:00:00 2001 From: tom barrett Date: Fri, 23 Feb 2018 01:10:14 -0600 Subject: -found way to downcast and created new struct --- Cargo.lock | 7 +++++++ Cargo.toml | 1 + src/astroid.rs | 33 +++++++++++++++++++++++++++++++++ src/bin/client.rs | 3 ++- src/bin/server.rs | 12 +++++++++++- src/connection.rs | 30 +++++++++++++++++++++++++----- src/lib.rs | 6 +++++- src/mass.rs | 12 ++++++++---- src/navigation.rs | 10 ++++++++++ src/ship.rs | 14 +++++++++++--- 10 files changed, 113 insertions(+), 15 deletions(-) create mode 100644 src/astroid.rs create mode 100644 src/navigation.rs diff --git a/Cargo.lock b/Cargo.lock index 748be5c..8172406 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,8 @@ +[[package]] +name = "downcast" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "dtoa" version = "0.4.2" @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 689c76f..ce4e48e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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> { + let mut masses : Vec> = 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>= 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>) { 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::().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){ + 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; } -- cgit v1.2.3