summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/bin/client.rs12
-rw-r--r--src/connection.rs6
-rw-r--r--src/lib.rs1
-rw-r--r--src/math.rs3
-rw-r--r--src/navigation.rs56
5 files changed, 59 insertions, 19 deletions
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<TcpStream>){
+ 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<TcpStream>){
loop {
let mut data = String::new();
buff_r.read_line(&mut data).unwrap();
let string_masses = data.split(";");
let mut masses : Vec<Box<Mass>> = Vec::new();
+ let mut ship : Option<Ship> = 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<TcpStream>){
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();
}
}