diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-13 13:25:00 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-13 13:25:00 -0600 |
commit | 284cac8f4034f15e7edeba5c8232a770fc082e20 (patch) | |
tree | 8f73230bae89c3bf796e47f7c52ac84f7015bd09 /src/server | |
parent | 070485e093dc540a5db9650d438cffe3d18d3883 (diff) |
added engine fuel and simplified/unified txrx from server
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/connection.rs | 14 | ||||
-rw-r--r-- | src/server/construction.rs | 42 | ||||
-rw-r--r-- | src/server/dashboard.rs | 6 | ||||
-rw-r--r-- | src/server/engines.rs | 31 | ||||
-rw-r--r-- | src/server/mining.rs | 41 | ||||
-rw-r--r-- | src/server/navigation.rs | 19 | ||||
-rw-r--r-- | src/server/refinery.rs | 43 | ||||
-rw-r--r-- | src/server/tractorbeam.rs | 30 |
8 files changed, 89 insertions, 137 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs index 662e33d..9275b67 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -57,3 +57,17 @@ impl ServerConnection { } } } + +pub fn receive(buff_r: &mut BufReader<TcpStream>) -> Option<String> { + let mut recv = String::new(); + match buff_r.read_line(&mut recv) { + Ok(result) => { + if result == 0 { + None + } else { + Some(recv.replace("\n", "")) + } + } + Err(_) => Some(String::new()), + } +} diff --git a/src/server/construction.rs b/src/server/construction.rs index 9c700ae..d45135c 100644 --- a/src/server/construction.rs +++ b/src/server/construction.rs @@ -1,7 +1,6 @@ extern crate serde_json; use std::collections::HashMap; -use std::io::BufRead; use std::io::Write; use crate::constants; @@ -10,7 +9,7 @@ use crate::mass::{Mass, MassType}; use crate::modules::construction::Construction; use crate::modules::construction::ConstructionStatus; use crate::modules::types::ModuleType; -use crate::server::connection::ServerConnection; +use crate::server::connection::{receive, ServerConnection}; use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -32,8 +31,18 @@ impl ServerConnection { let construction = construction.as_mut().unwrap(); let construction_data = get_construction_data(storage, construction); - if self.open && self.txrx_construction(&construction_data) { - construction.toggle(); + let send = serde_json::to_string(&construction_data).unwrap() + "\n"; + self.open = self.stream.write(send.as_bytes()).is_ok(); + + match receive(&mut self.buff_r) { + Some(recv) => { + if let "c" = recv.as_str() { + if construction_data.has_enough { + construction.toggle(); + } + } + } + None => self.open = false, } if construction_data.status == ConstructionStatus::Constructed { @@ -52,31 +61,6 @@ impl ServerConnection { masses.insert(self.name.clone(), ship); } - - fn txrx_construction(&mut self, construction_data: &ConstructionData) -> bool { - let send = serde_json::to_string(construction_data).unwrap() + "\n"; - if let Err(_err) = self.stream.write(send.as_bytes()) { - self.open = false; - } - - let mut recv = String::new(); - if let Ok(result) = self.buff_r.read_line(&mut recv) { - match recv.as_bytes() { - b"c\n" => { - if construction_data.has_enough { - return true; - } - } - _ => { - if result == 0 { - self.open = false; - } - } - } - } - - false - } } fn get_construction_data(storage: &Storage, construction: &Construction) -> ConstructionData { diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs index 981158b..cff2398 100644 --- a/src/server/dashboard.rs +++ b/src/server/dashboard.rs @@ -11,11 +11,7 @@ impl ServerConnection { if self.open { let ship = masses.get(&self.name).unwrap(); let send = serde_json::to_string(&ship).unwrap() + "\n"; - - self.open = match self.stream.write(send.as_bytes()) { - Ok(_result) => true, - Err(_error) => false, - }; + self.open = self.stream.write(send.as_bytes()).is_ok(); } } } diff --git a/src/server/engines.rs b/src/server/engines.rs index f3e68f0..86ef5ec 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -1,12 +1,17 @@ extern crate serde_json; use std::collections::HashMap; -use std::io::BufRead; use std::io::Write; use crate::mass::{Mass, MassType}; use crate::modules::navigation::NavigationStatus; -use crate::server::connection::ServerConnection; +use crate::server::connection::{receive, ServerConnection}; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct EnginesData { + pub has_target: bool, + pub fuel: f64, +} impl ServerConnection { pub fn server_engines(&mut self, masses: &mut HashMap<String, Mass>) { @@ -21,29 +26,27 @@ impl ServerConnection { { let navigation = navigation.clone().unwrap(); let engines = engines.as_mut().unwrap(); - let targeted = navigation.status == NavigationStatus::Targeted; - let send = serde_json::to_string(&targeted).unwrap() + "\n"; - if let Err(_err) = self.stream.write(send.as_bytes()) { - self.open = false; - } + let engines_data = EnginesData { + has_target: navigation.status == NavigationStatus::Targeted, + fuel: engines.fuel, + }; + let send = serde_json::to_string(&engines_data).unwrap() + "\n"; + self.open = self.stream.write(send.as_bytes()).is_ok(); let target = match navigation.target_name { Some(name) => masses.get(&name), None => None, }; - let mut recv = String::new(); - if let Ok(result) = self.buff_r.read_line(&mut recv) { - engines.give_client_data( + match receive(&mut self.buff_r) { + Some(recv) => engines.give_client_data( ship.position.clone(), ship.velocity.clone(), target, recv, - ); - if result == 0 { - self.open = false; - } + ), + None => self.open = false, } } diff --git a/src/server/mining.rs b/src/server/mining.rs index 4c1d3f6..b9b0ca0 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -1,7 +1,6 @@ extern crate serde_json; use std::collections::HashMap; -use std::io::BufRead; use std::io::Write; use crate::item::ItemType; @@ -9,7 +8,7 @@ use crate::mass::{Mass, MassType}; use crate::math::Vector; use crate::modules::mining::{Mining, MiningStatus}; use crate::modules::navigation::Navigation; -use crate::server::connection::ServerConnection; +use crate::server::connection::{receive, ServerConnection}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MiningData { @@ -35,8 +34,18 @@ impl ServerConnection { let navigation = navigation.as_ref().unwrap(); let mining_data = get_mining_data(ship.position.clone(), mining, navigation, masses); - if self.open && self.txrx_mining(&mining_data) { - mining.toggle(); + let send = serde_json::to_string(&mining_data).unwrap() + "\n"; + self.open = self.stream.write(send.as_bytes()).is_ok(); + + match receive(&mut self.buff_r) { + Some(recv) => { + if let "F" = recv.as_str() { + if mining_data.is_within_range { + mining.toggle(); + } + } + } + None => self.open = false, } if !mining_data.is_within_range { @@ -69,30 +78,6 @@ impl ServerConnection { masses.insert(self.name.clone(), ship); } - - fn txrx_mining(&mut self, mining_data: &MiningData) -> bool { - let send = serde_json::to_string(mining_data).unwrap() + "\n"; - if let Err(_err) = self.stream.write(send.as_bytes()) { - self.open = false; - } - - let mut recv = String::new(); - if let Ok(result) = self.buff_r.read_line(&mut recv) { - match recv.as_bytes() { - b"F\n" => { - if mining_data.is_within_range { - return true; - } - } - _ => { - if result == 0 { - self.open = false; - } - } - } - } - false - } } fn get_mining_data( diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 8fc7731..e14c340 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -1,11 +1,10 @@ extern crate serde_json; use std::collections::HashMap; -use std::io::BufRead; use std::io::Write; use crate::mass::{Mass, MassType}; -use crate::server::connection::ServerConnection; +use crate::server::connection::{receive, ServerConnection}; impl ServerConnection { pub fn server_navigation(&mut self, masses: &mut HashMap<String, Mass>) { @@ -28,19 +27,13 @@ impl ServerConnection { if self.open { let send = serde_json::to_string(&within_range).unwrap() + "\n"; + self.open = self.stream.write(send.as_bytes()).is_ok(); - if let Err(_err) = self.stream.write(send.as_bytes()) { - self.open = false; - }; - - let mut recv = String::new(); - if let Ok(result) = self.buff_r.read_line(&mut recv) { - if result == 0 { - self.open = false; - } - if !recv.is_empty() { - navigation.give_target(recv.replace("\n", "")); + match receive(&mut self.buff_r) { + Some(recv) => { + navigation.give_target(recv); } + None => self.open = false, } } } diff --git a/src/server/refinery.rs b/src/server/refinery.rs index 7cec23e..1a9a6ab 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -1,13 +1,12 @@ extern crate serde_json; use std::collections::HashMap; -use std::io::BufRead; use std::io::Write; use crate::item::{Item, ItemType}; use crate::mass::{Mass, MassType}; use crate::modules::refinery::RefineryStatus; -use crate::server::connection::ServerConnection; +use crate::server::connection::{receive, ServerConnection}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RefineryData { @@ -35,8 +34,18 @@ impl ServerConnection { status: refinery.status.clone(), }; - if self.open && self.txrx_refinery(&refinery_data) { - refinery.toggle(); + let send = serde_json::to_string(&refinery_data).unwrap() + "\n"; + self.open = self.stream.write(send.as_bytes()).is_ok(); + + match receive(&mut self.buff_r) { + Some(recv) => { + if let "R" = recv.as_str() { + if refinery_data.has_crude_minerals { + refinery.toggle(); + } + } + } + None => self.open = false, } if !refinery_data.has_crude_minerals { @@ -46,35 +55,11 @@ impl ServerConnection { if refinery.status == RefineryStatus::Refined { storage.take_item(ItemType::CrudeMinerals); storage.give_item(Item::new(ItemType::Iron)); + storage.give_item(Item::new(ItemType::Hydrogen)); refinery.taken(); } } masses.insert(self.name.clone(), ship); } - - fn txrx_refinery(&mut self, refinery_data: &RefineryData) -> bool { - let send = serde_json::to_string(refinery_data).unwrap() + "\n"; - if let Err(_err) = self.stream.write(send.as_bytes()) { - self.open = false; - } - - let mut recv = String::new(); - if let Ok(result) = self.buff_r.read_line(&mut recv) { - match recv.as_bytes() { - b"R\n" => { - if refinery_data.has_crude_minerals { - return true; - } - } - _ => { - if result == 0 { - self.open = false; - } - } - } - } - - false - } } diff --git a/src/server/tractorbeam.rs b/src/server/tractorbeam.rs index fc76b7c..b7c11af 100644 --- a/src/server/tractorbeam.rs +++ b/src/server/tractorbeam.rs @@ -1,13 +1,12 @@ extern crate serde_json; use std::collections::HashMap; -use std::io::BufRead; use std::io::Write; use crate::mass::{Mass, MassType}; use crate::modules::navigation::NavigationStatus; use crate::modules::tractorbeam::TractorbeamStatus; -use crate::server::connection::ServerConnection; +use crate::server::connection::{receive, ServerConnection}; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct TractorbeamData { @@ -35,23 +34,16 @@ impl ServerConnection { }; let send = serde_json::to_string(&tractorbeam_data).unwrap() + "\n"; - self.open = match self.stream.write(send.as_bytes()) { - Ok(_result) => true, - Err(_error) => false, - }; - - let mut recv = String::new(); - if let Ok(result) = self.buff_r.read_line(&mut recv) { - match recv.as_bytes() { - b"o\n" => tractorbeam.toggle_pull(), - b"p\n" => tractorbeam.toggle_push(), - b"t\n" => tractorbeam.toggle_bring(5.0), - _ => { - if result == 0 { - self.open = false; - } - } - } + self.open = self.stream.write(send.as_bytes()).is_ok(); + + match receive(&mut self.buff_r) { + Some(recv) => match recv.as_str() { + "o" => tractorbeam.toggle_pull(), + "p" => tractorbeam.toggle_push(), + "t" => tractorbeam.toggle_bring(5.0), + _ => (), + }, + None => self.open = false, } } |