summaryrefslogtreecommitdiff
path: root/src/connection.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/connection.rs')
-rw-r--r--src/connection.rs33
1 files changed, 19 insertions, 14 deletions
diff --git a/src/connection.rs b/src/connection.rs
index 648e1e3..b84c4e8 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -5,6 +5,7 @@ use std::net::TcpStream;
extern crate serde_json;
use ship::Ship;
+use mass::Mass;
use module::{Module, from_primitive};
pub struct Connection {
@@ -16,20 +17,20 @@ pub struct Connection {
}
impl Connection {
- pub fn new(mut stream : TcpStream, ships : &mut Vec<Ship>) -> Connection {
+ pub fn new(mut stream : TcpStream, masses : &mut Vec<Box<Mass>>) -> Connection {
let mut buff_r = BufReader::new(stream.try_clone().unwrap());
let mut data = String::new();
buff_r.read_line(&mut data).unwrap();
let name = &data[..data.find(":").unwrap()];
- let result = ships.into_iter().position(|ship| ship.name == name);
+ let result = masses.into_iter().position(|ship| ship.get_name() == name);
let index = match result {
Some(index) => index,
None => {
- let ship = Ship::new(name);
- ships.push(ship);
- ships.len() - 1
+ let ship = Box::new(Ship::new(name, (0,0,0)));
+ masses.push(ship);
+ masses.len() - 1
},
};
@@ -52,10 +53,10 @@ impl Connection {
}
- pub fn process(&mut self, ships : &mut Vec<Ship>) {
+ pub fn process(&mut self, masses : &mut Vec<Box<Mass>>) {
match self.module {
Module::Dashboard => {
- let mut send = serde_json::to_string(&ships[self.index]).unwrap();
+ let mut send = masses[self.index].serialize();
send.push_str("\n");
match self.stream.write(send.as_bytes()) {
Ok(_result) => (),
@@ -63,21 +64,25 @@ impl Connection {
}
}
Module::Engines => {
+ let mut location = masses[self.index].get_location();
let mut data = String::new();
match self.buff_r.read_line(&mut data) {
Ok(_result) => match data.as_bytes() {
- b"5\n" => ships[self.index].location.0 += 1,
- b"0\n" => ships[self.index].location.0 -= 1,
- b"8\n" => ships[self.index].location.1 += 1,
- b"2\n" => ships[self.index].location.1 -= 1,
- b"4\n" => ships[self.index].location.2 += 1,
- b"6\n" => ships[self.index].location.2 -= 1,
+ b"5\n" => location.0 += 1,
+ b"0\n" => location.0 -= 1,
+ b"8\n" => location.1 += 1,
+ b"2\n" => location.1 -= 1,
+ b"4\n" => location.2 += 1,
+ b"6\n" => location.2 -= 1,
_ => (),
},
Err(_error) => println!("b{}", _error)
}
+ masses[self.index].give_location(location);
+ }
+ Module::Navigation => {
+ ()
}
- _ => ()
}
}
}