summaryrefslogtreecommitdiff
path: root/src/client/engines.rs
blob: 8cb709e6cbdf28e9e4f07b326a878dd75cea1529 (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
61
62
63
64
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 std::thread::sleep;
use std::time::Duration;

use crate::modules::engines;

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

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

        writeln!(
            stdout,
            "{}{}Fuel: {:.2}\nuse numpad to freely move",
            termion::clear::All,
            termion::cursor::Goto(1, 1),
            engines_data.fuel
        )
        .unwrap();
        write!(stdout, "{}+ : speedup", termion::cursor::Goto(1, 2)).unwrap();
        write!(stdout, "{}- : slowdown", termion::cursor::Goto(1, 3)).unwrap();
        write!(stdout, "{}s : stop", termion::cursor::Goto(1, 4)).unwrap();
        write!(stdout, "{}q : quit", termion::cursor::Goto(1, 5)).unwrap();

        if engines_data.has_target {
            write!(
                stdout,
                "{}c : mimic targets velocity vector",
                termion::cursor::Goto(1, 6)
            )
            .unwrap();
            write!(
                stdout,
                "{}t : accelerate torwards target",
                termion::cursor::Goto(1, 7)
            )
            .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));
    }
}