From 6fb0b0d918963a22aeb4050139e8d0000c74a9ff Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 21 Mar 2019 09:48:59 -0500 Subject: simplified client sending and added more tractorbeam options --- src/client/construction.rs | 17 +++++++--------- src/client/dashboard.rs | 12 ++++++------ src/client/engines.rs | 17 +++++++--------- src/client/mining.rs | 17 +++++++--------- src/client/navigation.rs | 25 ++++++++++++------------ src/client/refinery.rs | 17 +++++++--------- src/client/tractorbeam.rs | 48 +++++++++++++++++++++++++++++++--------------- 7 files changed, 79 insertions(+), 74 deletions(-) (limited to 'src') diff --git a/src/client/construction.rs b/src/client/construction.rs index 28a91be..b555601 100644 --- a/src/client/construction.rs +++ b/src/client/construction.rs @@ -13,7 +13,7 @@ use crate::modules::construction; pub fn client_construction(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(); + let mut stdin = async_stdin(); loop { let mut recv = String::new(); @@ -41,16 +41,13 @@ pub fn client_construction(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(); + let mut stdin = async_stdin(); loop { let mut recv = String::new(); @@ -27,12 +27,12 @@ pub fn client_dashboard(mut buff_r: BufReader) { ) .unwrap(); - if let Some(c) = stdin.next() { - let c = c.unwrap() as char; - if c == 'q' { - break; - } + let mut key = String::new(); + stdin.read_to_string(&mut key).unwrap(); + if key.as_str() == "q" { + break; } + stdout.flush().unwrap(); } } diff --git a/src/client/engines.rs b/src/client/engines.rs index 52af206..8cb709e 100644 --- a/src/client/engines.rs +++ b/src/client/engines.rs @@ -14,7 +14,7 @@ use crate::modules::engines; pub fn client_engines(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(); + let mut stdin = async_stdin(); loop { let mut recv = String::new(); @@ -50,16 +50,13 @@ pub fn client_engines(mut stream: TcpStream, mut buff_r: BufReader) { .unwrap(); } - if let Some(c) = stdin.next() { - 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_all(send.as_bytes()).unwrap(); + let mut key = String::new(); + stdin.read_to_string(&mut key).unwrap(); + if key.as_str() == "q" { + break; } + key.push_str("\n"); + stream.write_all(key.as_bytes()).unwrap(); stdout.flush().unwrap(); sleep(Duration::from_millis(100)); diff --git a/src/client/mining.rs b/src/client/mining.rs index c9ad3dc..5c69ef8 100644 --- a/src/client/mining.rs +++ b/src/client/mining.rs @@ -12,7 +12,7 @@ use crate::modules::mining; pub fn client_mining(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(); + let mut stdin = async_stdin(); loop { let mut recv = String::new(); @@ -47,16 +47,13 @@ pub fn client_mining(mut stream: TcpStream, mut buff_r: BufReader) { write!(stdout, "{}Ship has no astroid targeted.", clear).unwrap(); } - if let Some(c) = stdin.next() { - 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_all(send.as_bytes()).unwrap(); + let mut key = String::new(); + stdin.read_to_string(&mut key).unwrap(); + if key.as_str() == "q" { + break; } + key.push_str("\n"); + stream.write_all(key.as_bytes()).unwrap(); stdout.flush().unwrap(); } diff --git a/src/client/navigation.rs b/src/client/navigation.rs index 7e6959b..ba6d912 100644 --- a/src/client/navigation.rs +++ b/src/client/navigation.rs @@ -12,7 +12,7 @@ use crate::modules::navigation; pub fn client_navigation(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(); + let mut stdin = async_stdin(); loop { let mut recv = String::new(); @@ -51,20 +51,19 @@ pub fn client_navigation(mut stream: TcpStream, mut buff_r: BufReader .unwrap(); } - if let Some(c) = stdin.next() { - let c = c.unwrap() as char; - if c == 'q' { - break; - } else { - let i = c.to_digit(10).unwrap() as usize; - if i < navigation_data.available_targets.len() { - let mut send = String::new(); - send.push_str(&navigation_data.available_targets[i].0); - send.push_str("\n"); - stream.write_all(send.as_bytes()).unwrap(); - } + let mut key = String::new(); + stdin.read_to_string(&mut key).unwrap(); + if key.as_str() == "q" { + break; + } else { + let mut send = String::new(); + if let Ok(i) = key.parse::() { + send.push_str(&navigation_data.available_targets[i].0); + send.push_str("\n"); + stream.write_all(send.as_bytes()).unwrap(); } } + stdout.flush().unwrap(); } } diff --git a/src/client/refinery.rs b/src/client/refinery.rs index 99d4be7..9b16c94 100644 --- a/src/client/refinery.rs +++ b/src/client/refinery.rs @@ -12,7 +12,7 @@ use crate::modules::refinery; pub fn client_refinery(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(); + let mut stdin = async_stdin(); loop { let mut recv = String::new(); @@ -34,16 +34,13 @@ pub fn client_refinery(mut stream: TcpStream, mut buff_r: BufReader) write!(stdout, "{}You have no crude minerals.", clear).unwrap(); } - if let Some(c) = stdin.next() { - 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_all(send.as_bytes()).unwrap(); + let mut key = String::new(); + stdin.read_to_string(&mut key).unwrap(); + if key.as_str() == "q" { + break; } + key.push_str("\n"); + stream.write_all(key.as_bytes()).unwrap(); stdout.flush().unwrap(); } diff --git a/src/client/tractorbeam.rs b/src/client/tractorbeam.rs index c8a6d92..0274886 100644 --- a/src/client/tractorbeam.rs +++ b/src/client/tractorbeam.rs @@ -6,13 +6,19 @@ use self::termion::raw::IntoRawMode; use std::io::{stdout, Read, Write}; use std::io::{BufRead, BufReader}; use std::net::TcpStream; +use std::str::FromStr; use crate::modules::tractorbeam; pub fn client_tractorbeam(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(); + let mut async_in = async_stdin(); + + let mut server_recv_data = tractorbeam::ServerRecvData { + key: String::from_str("").unwrap(), + desired_distance: None, + }; loop { let mut recv = String::new(); @@ -22,30 +28,36 @@ pub fn client_tractorbeam(mut stream: TcpStream, mut buff_r: BufReader write!( stdout, - "{}Press o to pull, p to push, b to bring to 5m.", + "{}Press o to pull, p to push, b to bring to a specific distance, a to acquire item.", clear ) .unwrap(), tractorbeam::Status::Push => write!( stdout, - "{}Press o to pull, p to stop pushing, b to bring to 5m.", + "{}Press o to pull, p to stop pushing, b to bring to a specific distance, a to acquire item.", clear ) .unwrap(), tractorbeam::Status::Pull => write!( stdout, - "{}Press o to stop pulling, p to push, b to bring to 5m.", + "{}Press o to stop pulling, p to push, b to bring to a specific distance, a to acquire item.", clear ) .unwrap(), tractorbeam::Status::Bring => write!( stdout, - "{}Press o to pulling, p to push, b to stop bringing to 5m.", + "{}Press o to pull, p to push, b to stop bringing to {} m, a to acquire item.", + clear, + data.desired_distance.unwrap(), + ) + .unwrap(), + tractorbeam::Status::Acquire => write!( + stdout, + "{}Press o to pull, p to push, b to bring to a specific distance, a to stop acquiring the item.", clear ) .unwrap(), @@ -54,16 +66,22 @@ pub fn client_tractorbeam(mut stream: TcpStream, mut buff_r: BufReader