diff options
author | tom barrett <spalf0@gmail.com> | 2018-02-22 04:32:51 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2018-02-22 04:32:51 -0600 |
commit | c8d308a566c9e3e5be85e0e67082a06c3420a72b (patch) | |
tree | dbbd0ce6581a60a44b8dc978abc435829d874e0d /src | |
parent | 763653a32e62134a7e394c349d742636b0662a94 (diff) |
-got now a trait which resembles polymorphism for new objects
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/server.rs | 9 | ||||
-rw-r--r-- | src/connection.rs | 33 | ||||
-rw-r--r-- | src/dashboard.rs | 4 | ||||
-rw-r--r-- | src/lib.rs | 1 | ||||
-rw-r--r-- | src/mass.rs | 7 | ||||
-rw-r--r-- | src/ship.rs | 29 |
6 files changed, 57 insertions, 26 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs index 7c6a63e..d2923d8 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -3,23 +3,24 @@ use std::time::Duration; use std::net::TcpListener; extern crate space; +use space::mass::Mass; use space::connection::Connection; fn main() { let listener = TcpListener::bind("localhost:6000").unwrap(); listener.set_nonblocking(true).unwrap(); - let mut ships = Vec::new(); + let mut masses : Vec<Box<Mass>>= Vec::new(); let mut connections = Vec::new(); for stream in listener.incoming() { match stream { - Ok(stream) => connections.push(Connection::new(stream, &mut ships)), + Ok(stream) => connections.push(Connection::new(stream, &mut masses)), _ => { for i in 0..connections.len() { - connections[i].process(&mut ships); + connections[i].process(&mut masses); } - connections.retain(|connection| connection.open ); + connections.retain(|connection| connection.open); sleep(Duration::from_millis(100)); } 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 => { + () } - _ => () } } } diff --git a/src/dashboard.rs b/src/dashboard.rs index 3a3ec2b..2227251 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -11,8 +11,6 @@ pub fn Dashboard(mut buff_r : BufReader<TcpStream>) { let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); let ship : Ship = serde_json::from_str(&data).unwrap(); - println!("Location: ({},{},{})", ship.location.0, - ship.location.1, - ship.location.2); + println!("{:?}", ship); } } @@ -2,6 +2,7 @@ extern crate serde_derive; extern crate termion; +pub mod mass; pub mod ship; pub mod module; pub mod engines; diff --git a/src/mass.rs b/src/mass.rs new file mode 100644 index 0000000..d1f9d3e --- /dev/null +++ b/src/mass.rs @@ -0,0 +1,7 @@ +pub trait Mass { + fn new(name : &str, location : (isize, isize, isize)) -> Self where Self: Sized; + fn get_name(&self) -> &String; + fn get_location(&self) -> (isize, isize, isize); + fn give_location(&mut self, location : (isize, isize, isize)); + fn serialize(&self) -> String; +} diff --git a/src/ship.rs b/src/ship.rs index f791809..715f019 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -1,14 +1,33 @@ +use mass::Mass; +extern crate serde_json; + #[derive(Serialize, Deserialize, Debug)] pub struct Ship { - pub name : String, - pub location : (isize, isize, isize), + name : String, + location : (isize, isize, isize), } -impl Ship { - pub fn new(name : &str) -> Ship { +impl Mass for Ship { + fn new(name : &str, location : (isize, isize, isize)) -> Ship { Ship { name : String::from(name), - location : (0,0,0), + location : location, } } + + fn get_name(&self) -> &String { + &self.name + } + + fn get_location(&self) -> (isize, isize, isize) { + self.location + } + + fn give_location(&mut self, location : (isize, isize, isize)) { + self.location = location; + } + + fn serialize(&self) ->String { + serde_json::to_string(self).unwrap() + } } |