summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-13 03:39:24 -0500
committertom barrett <spalf0@gmail.com>2018-04-13 03:39:24 -0500
commit4d7a7e368a3f0a6b1261e1b9180a2647eb158047 (patch)
tree50ead62fdc5e22e839920dae44a02dc25b247678 /src/client
parent74c7e70096ebcb0cab10f24ce1d348198d583161 (diff)
moved over to mass struct with enum for types
Diffstat (limited to 'src/client')
-rw-r--r--src/client/dashboard.rs4
-rw-r--r--src/client/navigation.rs54
2 files changed, 19 insertions, 39 deletions
diff --git a/src/client/dashboard.rs b/src/client/dashboard.rs
index 3776223..b9f0a08 100644
--- a/src/client/dashboard.rs
+++ b/src/client/dashboard.rs
@@ -4,13 +4,13 @@ use std::io::BufRead;
use std::io::BufReader;
use std::net::TcpStream;
-use ship::Ship;
+use mass::Mass;
pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) {
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let ship : Ship = serde_json::from_str(&recv).unwrap();
+ let ship : Mass = serde_json::from_str(&recv).unwrap();
println!("{:?}", ship);
}
}
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
index e5a5069..171d050 100644
--- a/src/client/navigation.rs
+++ b/src/client/navigation.rs
@@ -1,19 +1,16 @@
extern crate termion;
-extern crate itertools;
+//extern crate itertools;
extern crate serde_json;
use std::net::TcpStream;
use std::collections::BTreeMap;
use self::termion::async_stdin;
-use self::itertools::Itertools;
use std::io::{BufRead, BufReader};
use std::io::{stdout, Read, Write};
use self::termion::raw::IntoRawMode;
-use mass::Mass;
-use ship::Ship;
use math::distance;
-use astroid::Astroid;
+use mass::{Mass, MassType};
pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){
let stdout = stdout();
@@ -23,30 +20,24 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
-
- let string_hashmap = recv.split(";");
- let mut masses : BTreeMap<String, Box<Mass>> = BTreeMap::new();
- for string_element in string_hashmap {
- if string_element.len() <= 1 {
- break;
- }
- let (string_name, string_mass) = string_element.split("@").next_tuple().unwrap();
- masses.insert(string_name.to_string(), build_mass(string_mass));
- }
-
+ let mut within_range : BTreeMap<String, Mass> = serde_json::from_str(&recv).unwrap();
write!(stdout, "{}{}Targets:",
termion::clear::All,
termion::cursor::Goto(1,1)).unwrap();
- let ship = masses.remove(&name).unwrap().downcast::<Ship>().unwrap();
+ let ship = within_range.remove(&name).unwrap();
+ let targeting = match ship.mass_type {
+ MassType::Ship{ref targeting, ..} => Some(targeting.clone()),
+ _ => None,
+ }.unwrap();
- for (i, (mass_name, mass)) in masses.iter().enumerate() {
+ for (i, (mass_name, mass)) in within_range.iter().enumerate() {
- let target_data = match ship.recv_target() {
+ let target_data = match targeting.target.clone() {
Some(target_name) => {
if &target_name == mass_name {
- serde_json::to_string(&ship.recv_targeting_status()).unwrap()
+ serde_json::to_string(&targeting.status).unwrap()
}
else {
String::new()
@@ -59,10 +50,10 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
termion::cursor::Goto(1, 2 + i as u16),
i,
mass_name,
- mass.position().0,
- mass.position().1,
- mass.position().2,
- distance(mass.position(), ship.position()),
+ mass.position.0,
+ mass.position.1,
+ mass.position.2,
+ distance(mass.position, ship.position),
target_data
).unwrap();
}
@@ -75,9 +66,9 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
}
else {
let i = c.to_digit(10).unwrap() as usize;
- if i < masses.len() {
+ if i < within_range.len() {
let mut send = String::new();
- send.push_str(masses.iter().nth(i).unwrap().0);
+ send.push_str(within_range.iter().nth(i).unwrap().0);
send.push_str("\n");
stream.write(send.as_bytes()).unwrap();
}
@@ -88,14 +79,3 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
stdout.flush().unwrap();
}
}
-
-fn build_mass(string_mass : &str) -> Box<Mass> {
- if string_mass.contains("Ship") {
- let mass : Ship = serde_json::from_str(&string_mass).unwrap();
- return Box::new(mass)
- }
- else {
- let mass : Astroid = serde_json::from_str(&string_mass).unwrap();
- return Box::new(mass)
- }
-}