summaryrefslogtreecommitdiff
path: root/src/client/mining.rs
blob: 5c69ef86bab45d3c346bf28d2c57cecea71c2b88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
extern crate serde_json;
extern crate termion;

use self::termion::async_stdin;
use self::termion::raw::IntoRawMode;
use std::io::{stdout, Read, Write};
use std::io::{BufRead, BufReader};
use std::net::TcpStream;

use crate::modules::mining;

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();

    loop {
        let mut recv = String::new();
        buff_r.read_line(&mut recv).unwrap();
        let data: mining::ClientData = serde_json::from_str(&recv.replace("\n", "")).unwrap();

        write!(stdout, "{}", termion::clear::All).unwrap();

        let clear = termion::cursor::Goto(1, 1);

        if data.has_astroid_target {
            if data.is_within_range {
                if data.astroid_has_minerals {
                    match data.status {
                        mining::Status::None => {
                            write!(stdout, "{}Press F to begin mining.", clear).unwrap()
                        }
                        _ => write!(stdout, "{}Press F to stop mining.", clear).unwrap(),
                    };
                } else {
                    write!(stdout, "{}Astroid has ran out of minerals.", clear).unwrap();
                }
            } else {
                write!(
                    stdout,
                    "{}Astroid must be within range of {}.",
                    clear, data.range
                )
                .unwrap();
            }
        } else {
            write!(stdout, "{}Ship has no astroid targeted.", clear).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();
    }
}