summaryrefslogtreecommitdiff
path: root/src/engines.rs
blob: d320c9d18d996c2e6449433c563780a6363413ad (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
use std::net::TcpStream;
use termion::raw::IntoRawMode;
use termion::async_stdin;
use std::thread::sleep;
use std::io::{Read, Write, stdout};
use std::time::Duration;
use std::io::BufReader;
use std::io::BufRead;

use mass::Mass;

pub fn client_engines(mut stream : TcpStream) {
    let stdout = stdout();
    let mut stdout = stdout.lock().into_raw_mode().unwrap();
    let mut stdin = async_stdin().bytes();

    loop {
        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();
        sleep(Duration::from_millis(100));
    }
}

pub fn server_engines(ship : &mut Box<Mass>, buff_r : &mut BufReader<TcpStream>) -> bool {
    let mut acceleration = (0.0, 0.0, 0.0);
    let mut data = String::new();
    match buff_r.read_line(&mut data) {
        Ok(result) => match data.as_bytes() {
            b"5\n" => acceleration.0 += 0.1,
            b"0\n" => acceleration.0 -= 0.1,
            b"8\n" => acceleration.1 += 0.1,
            b"2\n" => acceleration.1 -= 0.1,
            b"4\n" => acceleration.2 += 0.1,
            b"6\n" => acceleration.2 -= 0.1,
            b"-\n" => ship.slow(),
            _ => {
                if result == 0 {
                    return false
                }
            },
        },
        Err(_error) => (),
    }
    ship.give_acceleration(acceleration);
    true
}