From 3d0efb20ac17ae18c495ffbb1b7d28b22851cb28 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 28 Feb 2018 10:05:22 -0600 Subject: -got rid of erased-serde, set up navigation better --- src/astroid.rs | 6 ++++-- src/connection.rs | 9 ++------- src/dashboard.rs | 1 - src/engines.rs | 1 - src/lib.rs | 1 - src/navigation.rs | 50 +++++++++++++++++++------------------------------- src/ship.rs | 4 ++++ 7 files changed, 29 insertions(+), 43 deletions(-) (limited to 'src') diff --git a/src/astroid.rs b/src/astroid.rs index ab00e6e..1e7c99c 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -1,8 +1,10 @@ -use mass::{Mass, Type}; -extern crate rand; use self::rand::distributions::Range; use astroid::rand::distributions::Sample; + +extern crate rand; extern crate serde_json; + +use mass::{Mass, Type}; use astroid::rand::Rng; #[derive(Serialize, Deserialize, Debug)] diff --git a/src/connection.rs b/src/connection.rs index 2f51920..3ca3b08 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -3,18 +3,13 @@ use std::io::BufReader; use std::net::TcpStream; extern crate serde_json; -extern crate erased_serde; - -use erased_serde::Deserializer; use ship::Ship; use mass::Mass; -use astroid::Astroid; -use module::{Module, from_primitive}; -use math::distance; -use dashboard::server_dashboard; use engines::server_engines; +use dashboard::server_dashboard; use navigation::server_navigation; +use module::{Module, from_primitive}; pub struct Connection { index : usize, diff --git a/src/dashboard.rs b/src/dashboard.rs index 99d5331..bfad542 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -6,7 +6,6 @@ use std::io::Write; extern crate serde_json; use ship::Ship; -use mass::Mass; pub fn client_dashboard(mut buff_r : BufReader) { loop { diff --git a/src/engines.rs b/src/engines.rs index 64d80bc..9d8841d 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -7,7 +7,6 @@ use std::time::Duration; use std::io::BufReader; use std::io::BufRead; -use ship::Ship; use mass::Mass; pub fn client_engines(mut stream : TcpStream) { diff --git a/src/lib.rs b/src/lib.rs index 29b3616..b7a3305 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,3 @@ -extern crate erased_serde; #[macro_use] extern crate serde_derive; #[macro_use] diff --git a/src/navigation.rs b/src/navigation.rs index 430ebc0..e55b65a 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -1,15 +1,12 @@ use std::net::TcpStream; use std::io::{BufRead, BufReader}; -use std::io::{stdout, Read, Write, stdin}; +use std::io::{stdout, Read, Write}; use termion::raw::IntoRawMode; use termion::async_stdin; -extern crate erased_serde; extern crate serde_json; extern crate termion; -use erased_serde::Deserializer; - use mass::Mass; use ship::Ship; use math::distance; @@ -26,29 +23,15 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf let string_masses = data.split(";"); let mut masses : Vec> = Vec::new(); - let mut ship : Option = None; for string_mass in string_masses { if string_mass.len() == 1 { break; } - let json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes()); - let mut deserialized : Box = Box::new(Deserializer::erase(json)); - - if string_mass.contains("Ship") { - let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); - if mass.name() == &name { - ship = Some(mass); - } - else { - masses.push(Box::new(mass)); - } - } - else { - let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap(); - masses.push(Box::new(mass)); - } + masses.push(build_mass(string_mass)); } + let ship = masses.iter().find(|ship| ship.name() == &name); + write!(stdout, "{}{}Targets:", termion::clear::All, termion::cursor::Goto(1,1)).unwrap(); @@ -109,19 +92,24 @@ pub fn server_navigation(masses : &mut Vec>, index : usize, mut stream Err(_error) => return false, } - /* let mut string_mass = String::new(); buff_r.read_line(&mut string_mass).unwrap(); if string_mass.len() > 0 { - let json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes()); - let mut deserialized : Box = Box::new(Deserializer::erase(json)); - if string_mass.contains("Ship") { - let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); - } - else { - let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap(); - } + let target = build_mass(&string_mass); + //ship.give_target(masses.iter().position(|mass| + // mass.name() == target.name())); } - */ + true } + +fn build_mass(string_mass : &str) -> Box { + let mut mass = Astroid::new(); + if string_mass.contains("Ship") { + let mass : Ship = serde_json::from_str(&string_mass).unwrap(); + } + else { + let mass : Astroid = serde_json::from_str(&string_mass).unwrap(); + } + Box::new(mass) +} diff --git a/src/ship.rs b/src/ship.rs index 16a3d01..08b4d93 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -23,6 +23,10 @@ impl Ship { } } + pub fn give_target(&mut self, target : Option) { + self.target = target; + } + pub fn range(&self) -> f64 { self.r } -- cgit v1.2.3