summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-03-19 05:06:50 -0500
committertom barrett <spalf0@gmail.com>2018-03-19 05:06:50 -0500
commitc297e871b7ce8935dd3bb1c65ecae37000d9a331 (patch)
treed6371df117020da5381acec1024c4fcb35060c76
parent7a4403d2a26250c82b6f089de3b56e076c69e39a (diff)
-pass module information in a serializable way
-rw-r--r--src/bin/client.rs13
-rw-r--r--src/bin/server.rs6
-rw-r--r--src/connection.rs8
-rw-r--r--src/module.rs13
-rw-r--r--src/ship.rs21
5 files changed, 38 insertions, 23 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs
index 558f914..cf12e53 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -3,11 +3,13 @@ use std::io::prelude::*;
use std::io::BufReader;
use std::net::TcpStream;
+extern crate serde_json;
+
extern crate space;
use space::dashboard::client_dashboard;
use space::engines::client_engines;
use space::navigation::client_navigation;
-use space::module::{Module, from_primitive};
+use space::module::Module;
fn main() {
let mut name = String::new();
@@ -28,7 +30,8 @@ fn main() {
let mut data = String::new();
buff_r.read_line(&mut data).unwrap();
- let modules : Vec<&str> = data.split(",").collect();
+ let mut modules : Vec<&str> = data.split(";").collect();
+ modules.pop();
println!("Choose your module:");
for (i, module) in modules.iter().enumerate() {
@@ -37,9 +40,11 @@ fn main() {
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed");
- stream.write(choice.as_bytes()).unwrap();
+ let mut module = modules[choice.replace("\n","").parse::<usize>().unwrap()].to_owned();
+ module.push_str("\n");
+ stream.write(module.as_bytes()).unwrap();
- let module = from_primitive(choice);
+ let module : Module = serde_json::from_str(&module.replace("\n","")).unwrap();
match module {
Module::Dashboard => client_dashboard(buff_r),
Module::Engines => client_engines(stream, buff_r),
diff --git a/src/bin/server.rs b/src/bin/server.rs
index 36840c1..16186d7 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -11,9 +11,9 @@ use space::connection::Connection;
fn populate() -> Vec<Box<Mass>> {
let mut masses : Vec<Box<Mass>> = Vec::new();
- masses.push(Box::new(Astroid::new()));
- masses.push(Box::new(Astroid::new()));
- masses.push(Box::new(Astroid::new()));
+ for _ in 0..10 {
+ masses.push(Box::new(Astroid::new()));
+ }
masses
}
diff --git a/src/connection.rs b/src/connection.rs
index 2698be3..a71bced 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -6,7 +6,7 @@ extern crate serde_json;
use ship::Ship;
use mass::Mass;
-use module::{Module, from_primitive};
+use module::Module;
pub struct Connection {
pub index : usize,
@@ -34,12 +34,12 @@ impl Connection {
},
};
- let modules = b"dashboard,navigation,engine\n";
- stream.write(modules).unwrap();
+ let modules = masses[index].downcast_ref::<Ship>().unwrap().get_modules();
+ stream.write(modules.as_bytes()).unwrap();
let mut data = String::new();
buff_r.read_line(&mut data).unwrap();
- let module = from_primitive(data);
+ let module : Module = serde_json::from_str(&data.replace("\n","")).unwrap();
stream.set_nonblocking(true).unwrap();
diff --git a/src/module.rs b/src/module.rs
index 7e99329..b051e60 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -1,17 +1,6 @@
+#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Module {
Dashboard,
Navigation,
Engines,
}
-
-pub fn from_primitive(data : String) -> Module {
- let data = data.replace("\n", "");
- let num = data.parse::<isize>().unwrap();
-
- match num {
- 0 => Module::Dashboard,
- 1 => Module::Navigation,
- 2 => Module::Engines,
- _ => Module::Dashboard,
- }
-}
diff --git a/src/ship.rs b/src/ship.rs
index c33dc6c..e226936 100644
--- a/src/ship.rs
+++ b/src/ship.rs
@@ -1,3 +1,4 @@
+use module::Module;
use mass::{Mass, Type};
extern crate serde_json;
@@ -9,10 +10,17 @@ pub struct Ship {
mass_type : Type,
r : f64,
target : Option<usize>,
+ modules : Vec<Module>,
}
impl Ship {
pub fn new(name : &str, position : (f64, f64, f64)) -> Ship {
+ let mut modules = Vec::new();
+
+ modules.push(Module::Navigation);
+ modules.push(Module::Engines);
+ modules.push(Module::Dashboard);
+
Ship {
name : String::from(name),
position : position,
@@ -20,6 +28,7 @@ impl Ship {
mass_type : Type::Ship,
r : 100.0,
target : None,
+ modules : modules,
}
}
@@ -64,6 +73,18 @@ impl Ship {
pub fn recv_target(&self) -> Option<usize> {
self.target
}
+
+ pub fn get_modules(&self) -> String {
+ let mut data = String::new();
+
+ for module in self.modules.iter() {
+ data.push_str(&serde_json::to_string(&module).unwrap());
+ data.push_str(";");
+ }
+ data.push_str("\n");
+
+ data
+ }
}
impl Mass for Ship {