summaryrefslogtreecommitdiff
path: root/src/navigation.rs
blob: e55b65a9b5e9d139cd3b7845557c97194ea37944 (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use std::net::TcpStream;
use std::io::{BufRead, BufReader};
use std::io::{stdout, Read, Write};
use termion::raw::IntoRawMode;
use termion::async_stdin;

extern crate serde_json;
extern crate termion;

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

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

        let string_masses = data.split(";");
        let mut masses : Vec<Box<Mass>> = Vec::new();
        for string_mass in string_masses {
            if string_mass.len() == 1 {
                break;
            }
            masses.push(build_mass(string_mass));
        }

        let ship = masses.iter().find(|ship| ship.name() == &name);

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

        let position = ship.unwrap().position();
        for (i, mass) in masses.iter().enumerate() {
            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(), position)).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;
                }
                else {
                    let i = match send.parse::<usize>() {
                        Ok(num) => num,
                        Err(_err) => 100,
                    };
                    if i < masses.len() {
                        send = masses[i].serialize();
                        send.push_str("\n");
                        stream.write(send.as_bytes()).unwrap();
                    }
                }
            }
            None => ()
        }

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

pub fn server_navigation(masses : &mut Vec<Box<Mass>>, index : usize, mut stream : &TcpStream, buff_r : &mut BufReader<TcpStream>) -> bool {
    let ship = masses[index].downcast_ref::<Ship>().unwrap();

    let within_range : Vec<&Box<Mass>> = masses.iter().filter(|mass|
    distance(ship.position(), mass.position()) < ship.range()).collect();

    let mut send = String::new();
    for mass in within_range {
        send.push_str(&mass.serialize());
        send.push_str(";");
    }
    send.push_str("\n");
    match stream.write(send.as_bytes()) {
        Ok(_result) => (),
        Err(_error) => return false,
    }

    let mut string_mass = String::new();
    buff_r.read_line(&mut string_mass).unwrap();
    if string_mass.len() > 0 {
        let target = build_mass(&string_mass);
        //ship.give_target(masses.iter().position(|mass| 
        //                                        mass.name() == target.name()));
    }

    true
}

fn build_mass(string_mass : &str) -> Box<Mass> {
    let mut mass = Astroid::new();
    if string_mass.contains("Ship") {
        let mass : Ship = serde_json::from_str(&string_mass).unwrap();
    }
    else {
        let mass : Astroid = serde_json::from_str(&string_mass).unwrap();
    }
    Box::new(mass)
}