From 492b31ffd13d4def86050d1db31c5c2e59b670ab Mon Sep 17 00:00:00 2001 From: tom barrett Date: Wed, 21 Feb 2018 08:47:21 -0600 Subject: -moving engines shows change on dashboard --- src/connection.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) (limited to 'src/connection.rs') diff --git a/src/connection.rs b/src/connection.rs index 26993fd..cfe642d 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -8,8 +8,7 @@ use ship::Ship; use module::{Module, from_primitive}; pub struct Connection { -// name : String, - ship : Ship, + index : usize, module : Module, stream : TcpStream, buff_r : BufReader, @@ -17,12 +16,22 @@ pub struct Connection { } impl Connection { - pub fn new(mut stream : TcpStream) -> Connection { + pub fn new(mut stream : TcpStream, ships : &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 name = &data[..data.find(":").unwrap()]; + + let result = ships.into_iter().position(|ship| ship.name == name); + let index = match result { + Some(index) => index, + None => { + let mut ship = Ship::new(name); + ships.push(ship); + ships.len() - 1 + }, + }; let modules = b"dashboard,navigation,engine\n"; stream.write(modules).unwrap(); @@ -34,8 +43,7 @@ impl Connection { stream.set_nonblocking(true).unwrap(); Connection { -// name : String::from(name), - ship : Ship::new(), + index : index, module : module, stream : stream, buff_r : buff_r, @@ -43,10 +51,11 @@ impl Connection { } } - pub fn process(&mut self) { + + pub fn process(&mut self, ships : &mut Vec) { match self.module { Module::Dashboard => { - let mut send = serde_json::to_string(&self.ship).unwrap(); + let mut send = serde_json::to_string(&ships[self.index]).unwrap(); send.push_str("\n"); match self.stream.write(send.as_bytes()) { Ok(_result) => (), @@ -56,12 +65,14 @@ impl Connection { Module::Engines => { let mut data = String::new(); match self.buff_r.read_line(&mut data) { - Ok(_result) => println!("{}", data), - Err(_error) => println!("{}", _error) + Ok(_result) => match data.as_bytes() { + b"5\n" => ships[self.index].location.0 += 1, + _ => (), + }, + Err(_error) => println!("b{}", _error) } } _ => () } } } - -- cgit v1.2.3