summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-12 04:33:23 -0500
committertom barrett <spalf0@gmail.com>2018-04-12 04:33:23 -0500
commit573ba69d810914c153a578747414b3d631e61bbc (patch)
tree4ce164db9d28ede9778d75ddb4f6922ea1dc1a91 /src/client
parentab797e7f30e5e8913faf73516346129b9a620550 (diff)
completely restructured code and fixed navigation bug
Diffstat (limited to 'src/client')
-rw-r--r--src/client/dashboard.rs16
-rw-r--r--src/client/engines.rs49
-rw-r--r--src/client/mining.rs54
-rw-r--r--src/client/mod.rs4
-rw-r--r--src/client/navigation.rs101
5 files changed, 224 insertions, 0 deletions
diff --git a/src/client/dashboard.rs b/src/client/dashboard.rs
new file mode 100644
index 0000000..3776223
--- /dev/null
+++ b/src/client/dashboard.rs
@@ -0,0 +1,16 @@
+extern crate serde_json;
+
+use std::io::BufRead;
+use std::io::BufReader;
+use std::net::TcpStream;
+
+use ship::Ship;
+
+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();
+ println!("{:?}", ship);
+ }
+}
diff --git a/src/client/engines.rs b/src/client/engines.rs
new file mode 100644
index 0000000..b6f5f93
--- /dev/null
+++ b/src/client/engines.rs
@@ -0,0 +1,49 @@
+extern crate termion;
+extern crate serde_json;
+
+use std::thread::sleep;
+use std::time::Duration;
+use std::net::TcpStream;
+use self::termion::async_stdin;
+use std::io::{BufRead, BufReader};
+use std::io::{Read, Write, stdout};
+use self::termion::raw::IntoRawMode;
+
+pub fn client_engines(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();
+
+ loop {
+ let mut recv = String::new();
+ buff_r.read_line(&mut recv).unwrap();
+ let has_target = serde_json::from_str(&recv.replace("\n", "")).unwrap();
+
+ write!(stdout, "{}{}use numpad to freely move\n", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
+ write!(stdout, "{}+ : speedup", termion::cursor::Goto(1, 2)).unwrap();
+ write!(stdout, "{}- : slowdown", termion::cursor::Goto(1, 3)).unwrap();
+ write!(stdout, "{}q : quit", termion::cursor::Goto(1, 4)).unwrap();
+
+ if has_target {
+ write!(stdout, "{}c : mimic targets velocity vector", termion::cursor::Goto(1,5)).unwrap();
+ write!(stdout, "{}t : accelerate torwards target", termion::cursor::Goto(1,6)).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();
+ sleep(Duration::from_millis(100));
+ }
+}
diff --git a/src/client/mining.rs b/src/client/mining.rs
new file mode 100644
index 0000000..44e7c30
--- /dev/null
+++ b/src/client/mining.rs
@@ -0,0 +1,54 @@
+extern crate termion;
+extern crate serde_json;
+
+use std::net::TcpStream;
+use self::termion::async_stdin;
+use std::io::{BufReader, BufRead};
+use std::io::{stdout, Read, Write};
+use self::termion::raw::IntoRawMode;
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+struct ServerData {
+ has_astroid_target : bool,
+ is_within_range : bool,
+ mining_range : f64,
+ mining_status : bool,
+}
+
+pub fn client_mining(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();
+
+ loop {
+ let mut recv = String::new();
+ buff_r.read_line(&mut recv).unwrap();
+ let data : ServerData = serde_json::from_str(&recv.replace("\n", "")).unwrap();
+
+ write!(stdout, "{}", termion::clear::All).unwrap();
+
+ match data.has_astroid_target {
+ true => match data.is_within_range {
+ true => write!(stdout, "{}Press F to begin mining.", termion::cursor::Goto(1,1)).unwrap(),
+ false => write!(stdout, "{}Astroid must be within range of {}.", termion::cursor::Goto(1,1), data.mining_range).unwrap(),
+ },
+ false => write!(stdout, "{}Ship has no astroid targeted.", termion::cursor::Goto(1,1)).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();
+ }
+}
diff --git a/src/client/mod.rs b/src/client/mod.rs
new file mode 100644
index 0000000..baabf96
--- /dev/null
+++ b/src/client/mod.rs
@@ -0,0 +1,4 @@
+pub mod mining;
+pub mod engines;
+pub mod dashboard;
+pub mod navigation;
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
new file mode 100644
index 0000000..e5a5069
--- /dev/null
+++ b/src/client/navigation.rs
@@ -0,0 +1,101 @@
+extern crate termion;
+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;
+
+pub fn client_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();
+
+ 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));
+ }
+
+
+ write!(stdout, "{}{}Targets:",
+ termion::clear::All,
+ termion::cursor::Goto(1,1)).unwrap();
+
+ let ship = masses.remove(&name).unwrap().downcast::<Ship>().unwrap();
+
+ for (i, (mass_name, mass)) in masses.iter().enumerate() {
+
+ let target_data = match ship.recv_target() {
+ Some(target_name) => {
+ if &target_name == mass_name {
+ serde_json::to_string(&ship.recv_targeting_status()).unwrap()
+ }
+ else {
+ String::new()
+ }
+ }
+ None => String::new(),
+ };
+
+ write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}",
+ 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()),
+ target_data
+ ).unwrap();
+ }
+
+ match stdin.next() {
+ Some(c) => {
+ let c = c.unwrap() as char;
+ if c == 'q' {
+ break;
+ }
+ else {
+ let i = c.to_digit(10).unwrap() as usize;
+ if i < masses.len() {
+ let mut send = String::new();
+ send.push_str(masses.iter().nth(i).unwrap().0);
+ send.push_str("\n");
+ stream.write(send.as_bytes()).unwrap();
+ }
+ }
+ }
+ None => ()
+ }
+ 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)
+ }
+}