From 87346ea9b91d441e92c476785c2cb979f9a23bf5 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 27 Feb 2018 03:36:04 -0600 Subject: -set up display for navigation system --- src/bin/client.rs | 12 ++++-------- src/connection.rs | 6 +----- src/lib.rs | 1 + src/math.rs | 3 +++ src/navigation.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++------ 5 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 src/math.rs diff --git a/src/bin/client.rs b/src/bin/client.rs index 2e8dd45..d327481 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -9,21 +9,17 @@ use space::engines::Engines; use space::navigation::Navigation; use space::module::{Module, from_primitive}; - -fn get_info() -> String { +fn main() { let mut name = String::new(); println!("Ship Name:"); io::stdin().read_line(&mut name).expect("Failed"); + name = name.replace("\n", ""); let mut password = String::new(); println!("Password:"); io::stdin().read_line(&mut password).expect("Failed"); - name.replace("\n", "") + ":" + &password -} - -fn main() { - let send = get_info(); + let send = name.clone() + ":" + &password; let mut stream = TcpStream::connect("localhost:6000").unwrap(); let mut buff_r = BufReader::new(stream.try_clone().unwrap()); @@ -47,6 +43,6 @@ fn main() { match module { Module::Dashboard => Dashboard(buff_r), Module::Engines => Engines(stream), - Module::Navigation => Navigation(stream, buff_r), + Module::Navigation => Navigation(name, stream, buff_r), } } diff --git a/src/connection.rs b/src/connection.rs index 9cf7923..db258e4 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -7,6 +7,7 @@ extern crate serde_json; use ship::Ship; use mass::Mass; use module::{Module, from_primitive}; +use math::distance; pub struct Connection { index : usize, @@ -102,9 +103,4 @@ impl Connection { } } } - -} - -fn distance(l0 : (f64, f64, f64), l1 : (f64, f64, f64)) -> f64 { - (((l1.0-l0.0).powf(2.0) + (l1.1-l0.1).powf(2.0) + (l1.2-l0.2).powf(2.0))).sqrt() } diff --git a/src/lib.rs b/src/lib.rs index b661cad..29b3616 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ extern crate termion; pub mod mass; pub mod ship; +pub mod math; pub mod module; pub mod astroid; pub mod engines; diff --git a/src/math.rs b/src/math.rs new file mode 100644 index 0000000..6766a60 --- /dev/null +++ b/src/math.rs @@ -0,0 +1,3 @@ +pub fn distance(l0 : (f64, f64, f64), l1 : (f64, f64, f64)) -> f64 { + (((l1.0-l0.0).powf(2.0) + (l1.1-l0.1).powf(2.0) + (l1.2-l0.2).powf(2.0))).sqrt() +} diff --git a/src/navigation.rs b/src/navigation.rs index bcb0c4d..cc2221a 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -1,25 +1,33 @@ use std::net::TcpStream; -use std::io::BufRead; -use std::io::BufReader; +use std::io::{BufRead, BufReader}; + +use std::io::{stdout, Read, Write, stdin}; +use termion::raw::IntoRawMode; +use termion::async_stdin; extern crate erased_serde; extern crate serde_json; +extern crate termion; -use std::collections::BTreeMap; +use erased_serde::Deserializer; use mass::Mass; use ship::Ship; +use math::distance; use astroid::Astroid; -use erased_serde::Deserializer; +pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader){ + let stdout = stdout(); + let mut stdout = stdout.lock().into_raw_mode().unwrap(); + let mut stdin = async_stdin().bytes(); -pub fn Navigation(mut stream :TcpStream, mut buff_r : BufReader){ loop { let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); 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; @@ -29,12 +37,48 @@ pub fn Navigation(mut stream :TcpStream, mut buff_r : BufReader){ if string_mass.contains("Ship") { let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); - masses.push(Box::new(mass)); + 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)); } } + + + write!(stdout, "{}{}Targets:", + termion::clear::All, + termion::cursor::Goto(1,1)).unwrap(); + + let location = ship.expect("zz").location(); + for (i, mass) in masses.iter().enumerate() { + write!(stdout, "{}{}) {} {:?} Distance : {}", + termion::cursor::Goto(1, 2 + i as u16), + i, + mass.name(), + mass.location(), + distance(mass.location(), location)).unwrap(); + } + + match stdin.next() { + Some(c) => { + let c = c.unwrap(); + let mut send = String::new(); + send.push(c as char); + if send.as_bytes() == b"q" { + break; + } + //send.push_str("\n"); + //stream.write(send.as_bytes()).unwrap(); + } + None => () + } + + stdout.flush().unwrap(); } } -- cgit v1.2.3