summaryrefslogtreecommitdiff
path: root/src/client/navigation.rs
blob: eb9a2c48622eb659ceb06fac947597490c259e3d (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
extern crate termion;
extern crate serde_json;

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

use math::distance;
use mass::{Mass, MassType};
use module::ModuleType;

pub fn client_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 recv = String::new();
        buff_r.read_line(&mut recv).unwrap();
        let mut within_range : BTreeMap<String, Mass> = serde_json::from_str(&recv).unwrap();

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

        let ship = within_range.remove(&name).unwrap();

        if let MassType::Ship{ref modules, ..} = ship.mass_type {
            if let ModuleType::Navigation{ref status, ref target_name, ..} = modules.get("Navigation").unwrap().module_type {
                for (i, (mass_name, mass)) in within_range.iter().enumerate() {
                    let target_data = match target_name.clone() {
                        Some(target_name) => {
                            if &target_name == mass_name {
                                serde_json::to_string(status).unwrap()
                            }
                            else {
                                String::new()
                            }
                        }
                        None => String::new(),
                    };

                    write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}",
                           termion::cursor::Goto(1, 2 + i as u16),
                           i,
                           mass_name,
                           mass.position.0,
                           mass.position.1,
                           mass.position.2,
                           distance(mass.position, ship.position),
                           target_data
                           ).unwrap();
                }

                match stdin.next() {
                    Some(c) => {
                        let c = c.unwrap() as char;
                        if c == 'q' {
                            break;
                        }
                        else {
                            let i = c.to_digit(10).unwrap() as usize;
                            if i < within_range.len() {
                                let mut send = String::new();
                                send.push_str(within_range.iter().nth(i).unwrap().0);
                                send.push_str("\n");
                                stream.write(send.as_bytes()).unwrap();
                            }
                        }
                    }
                    None => ()
                }
            }
        }

        stdout.flush().unwrap();
    }
}