diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/bin/client.rs | 12 | ||||
-rw-r--r-- | src/connection.rs | 70 | ||||
-rw-r--r-- | src/dashboard.rs | 12 | ||||
-rw-r--r-- | src/engines.rs | 31 | ||||
-rw-r--r-- | src/navigation.rs | 37 |
5 files changed, 90 insertions, 72 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs index d327481..839c0be 100644 --- a/src/bin/client.rs +++ b/src/bin/client.rs @@ -4,9 +4,9 @@ use std::io::BufReader; use std::net::TcpStream; extern crate space; -use space::dashboard::Dashboard; -use space::engines::Engines; -use space::navigation::Navigation; +use space::dashboard::client_dashboard; +use space::engines::client_engines; +use space::navigation::client_navigation; use space::module::{Module, from_primitive}; fn main() { @@ -41,8 +41,8 @@ fn main() { let module = from_primitive(choice); match module { - Module::Dashboard => Dashboard(buff_r), - Module::Engines => Engines(stream), - Module::Navigation => Navigation(name, stream, buff_r), + Module::Dashboard => client_dashboard(buff_r), + Module::Engines => client_engines(stream), + Module::Navigation => client_navigation(name, stream, buff_r), } } diff --git a/src/connection.rs b/src/connection.rs index eacbb10..2f51920 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -12,6 +12,9 @@ use mass::Mass; use astroid::Astroid; use module::{Module, from_primitive}; use math::distance; +use dashboard::server_dashboard; +use engines::server_engines; +use navigation::server_navigation; pub struct Connection { index : usize, @@ -58,67 +61,10 @@ impl Connection { } pub fn process(&mut self, masses : &mut Vec<Box<Mass>>) { - match self.module { - Module::Dashboard => { - let mut send = masses[self.index].serialize(); - send.push_str("\n"); - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => self.open = false, - } - } - Module::Engines => { - let mut acceleration = (0.0, 0.0, 0.0); - let mut data = String::new(); - match self.buff_r.read_line(&mut data) { - Ok(result) => match data.as_bytes() { - b"5\n" => acceleration.0 += 0.1, - b"0\n" => acceleration.0 -= 0.1, - b"8\n" => acceleration.1 += 0.1, - b"2\n" => acceleration.1 -= 0.1, - b"4\n" => acceleration.2 += 0.1, - b"6\n" => acceleration.2 -= 0.1, - b"-\n" => masses[self.index].slow(), - _ => { - if result == 0 { - self.open = false; - } - }, - }, - Err(_error) => (), - } - masses[self.index].give_acceleration(acceleration); - } - Module::Navigation => { - let ship = &masses[self.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 self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => self.open = false, - } - - let mut string_mass = String::new(); - self.buff_r.read_line(&mut string_mass).unwrap(); - if string_mass.len() > 0 { - let json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes()); - let mut deserialized : Box<Deserializer> = Box::new(Deserializer::erase(json)); - if string_mass.contains("Ship") { - let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); - } - else { - let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap(); - } - } - } - } + self.open = match self.module { + Module::Dashboard => server_dashboard(masses[self.index].serialize(), &self.stream), + Module::Engines => server_engines(&mut self.buff_r, &mut masses[self.index]), + Module::Navigation => server_navigation(masses, self.index, &self.stream, &mut self.buff_r), + }; } } diff --git a/src/dashboard.rs b/src/dashboard.rs index 2227251..99d5331 100644 --- a/src/dashboard.rs +++ b/src/dashboard.rs @@ -1,12 +1,14 @@ use std::io::BufReader; use std::io::BufRead; use std::net::TcpStream; +use std::io::Write; extern crate serde_json; use ship::Ship; +use mass::Mass; -pub fn Dashboard(mut buff_r : BufReader<TcpStream>) { +pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) { loop { let mut data = String::new(); buff_r.read_line(&mut data).unwrap(); @@ -14,3 +16,11 @@ pub fn Dashboard(mut buff_r : BufReader<TcpStream>) { println!("{:?}", ship); } } + +pub fn server_dashboard(mut ship_string : String, mut stream : &TcpStream) -> bool { + ship_string.push_str("\n"); + match stream.write(ship_string.as_bytes()) { + Ok(_result) => true, + Err(_error) => false, + } +} diff --git a/src/engines.rs b/src/engines.rs index b50a6ae..64d80bc 100644 --- a/src/engines.rs +++ b/src/engines.rs @@ -4,8 +4,13 @@ use termion::async_stdin; use std::thread::sleep; use std::io::{Read, Write, stdout}; use std::time::Duration; +use std::io::BufReader; +use std::io::BufRead; -pub fn Engines(mut stream : TcpStream) { +use ship::Ship; +use mass::Mass; + +pub fn client_engines(mut stream : TcpStream) { let stdout = stdout(); let mut stdout = stdout.lock().into_raw_mode().unwrap(); let mut stdin = async_stdin().bytes(); @@ -29,3 +34,27 @@ pub fn Engines(mut stream : TcpStream) { sleep(Duration::from_millis(100)); } } + +pub fn server_engines(buff_r : &mut BufReader<TcpStream>, ship : &mut Box<Mass>) -> bool { + let mut acceleration = (0.0, 0.0, 0.0); + let mut data = String::new(); + match buff_r.read_line(&mut data) { + Ok(result) => match data.as_bytes() { + b"5\n" => acceleration.0 += 0.1, + b"0\n" => acceleration.0 -= 0.1, + b"8\n" => acceleration.1 += 0.1, + b"2\n" => acceleration.1 -= 0.1, + b"4\n" => acceleration.2 += 0.1, + b"6\n" => acceleration.2 -= 0.1, + b"-\n" => ship.slow(), + _ => { + if result == 0 { + return false + } + }, + }, + Err(_error) => (), + } + ship.give_acceleration(acceleration); + true +} diff --git a/src/navigation.rs b/src/navigation.rs index 978c154..430ebc0 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -1,6 +1,5 @@ use std::net::TcpStream; use std::io::{BufRead, BufReader}; - use std::io::{stdout, Read, Write, stdin}; use termion::raw::IntoRawMode; use termion::async_stdin; @@ -16,7 +15,7 @@ use ship::Ship; use math::distance; use astroid::Astroid; -pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){ +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(); @@ -92,3 +91,37 @@ pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader< 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 json = &mut serde_json::de::Deserializer::from_slice(string_mass.as_bytes()); + let mut deserialized : Box<Deserializer> = Box::new(Deserializer::erase(json)); + if string_mass.contains("Ship") { + let mass : Ship = erased_serde::deserialize(&mut deserialized).unwrap(); + } + else { + let mass : Astroid = erased_serde::deserialize(&mut deserialized).unwrap(); + } + } + */ + true +} |