diff options
author | tom barrett <spalf0@gmail.com> | 2018-04-12 04:33:23 -0500 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-04-12 04:33:23 -0500 |
commit | 573ba69d810914c153a578747414b3d631e61bbc (patch) | |
tree | 4ce164db9d28ede9778d75ddb4f6922ea1dc1a91 /src/client/mining.rs | |
parent | ab797e7f30e5e8913faf73516346129b9a620550 (diff) |
completely restructured code and fixed navigation bug
Diffstat (limited to 'src/client/mining.rs')
-rw-r--r-- | src/client/mining.rs | 54 |
1 files changed, 54 insertions, 0 deletions
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(); + } +} |