summaryrefslogtreecommitdiff
path: root/src/navigation.rs
blob: cc2221a7db8cbfec75f23f12a0a4e9b6c3bd7a9c (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::net::TcpStream;
use std::io::{BufRead, BufReader};

use std::io::{stdout, Read, Write, stdin};
use termion::raw::IntoRawMode;
use termion::async_stdin;

extern crate erased_serde;
extern crate serde_json;
extern crate termion;

use erased_serde::Deserializer;

use mass::Mass;
use ship::Ship;
use math::distance;
use astroid::Astroid;

pub fn Navigation(name : String, 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 data = String::new();
        buff_r.read_line(&mut data).unwrap();

        let string_masses = data.split(";");
        let mut masses : Vec<Box<Mass>> = Vec::new();
        let mut ship : Option<Ship> = None;
        for string_mass in string_masses {
            if string_mass.len() == 1 {
                break;
            }
            let json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes());
            let mut deserialized : Box<Deserializer> = Box::new(Deserializer::erase(json));

            if string_mass.contains("Ship") {
                let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap();
                if mass.name() == &name {
                    ship = Some(mass);
                }
                else {
                    masses.push(Box::new(mass));
                }
            }
            else {
                let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap();
                masses.push(Box::new(mass));
            }
        }


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

        let location = ship.expect("zz").location();
        for (i, mass) in masses.iter().enumerate() {
            write!(stdout, "{}{}) {} {:?} Distance : {}",
                   termion::cursor::Goto(1, 2 + i as u16),
                   i,
                   mass.name(),
                   mass.location(),
                   distance(mass.location(), location)).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();
    }
}