From c8d308a566c9e3e5be85e0e67082a06c3420a72b Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 22 Feb 2018 04:32:51 -0600 Subject: -got now a trait which resembles polymorphism for new objects --- src/connection.rs | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) (limited to 'src/connection.rs') 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) -> Connection { + pub fn new(mut stream : TcpStream, masses : &mut Vec>) -> 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) { + pub fn process(&mut self, masses : &mut Vec>) { 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 => { + () } - _ => () } } } -- cgit v1.2.3