diff options
author | tom barrett <spalf0@gmail.com> | 2019-03-21 09:48:59 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-03-21 09:48:59 -0500 |
commit | 6fb0b0d918963a22aeb4050139e8d0000c74a9ff (patch) | |
tree | 72c9ff1d51fbf8d599ad27730b5feab4d921c0a2 /src/client/tractorbeam.rs | |
parent | 69aa9f090d551c1ebb211f3a01273b28421fc9c0 (diff) |
simplified client sending and added more tractorbeam options
Diffstat (limited to 'src/client/tractorbeam.rs')
-rw-r--r-- | src/client/tractorbeam.rs | 48 |
1 files changed, 33 insertions, 15 deletions
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<TcpStream>) { 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<TcpStream write!(stdout, "{}", termion::clear::All).unwrap(); let clear = termion::cursor::Goto(1, 1); - if data.has_target { match data.status { tractorbeam::Status::None => 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<TcpStream write!(stdout, "{}You have no target.", 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(); + server_recv_data.desired_distance = None; + + let mut key = String::new(); + async_in.read_to_string(&mut key).unwrap(); + + if key.as_str() == "q" { + break; + } else if key.as_str() == "b" && data.has_target { + server_recv_data.desired_distance = Some(5.0); } + + server_recv_data.key = key.to_string(); + + let send = serde_json::to_string(&server_recv_data).unwrap() + "\n"; + stream.write_all(send.as_bytes()).unwrap(); + stdout.flush().unwrap(); } } |