diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/connection.rs | 21 | ||||
-rw-r--r-- | src/server/construction.rs | 15 | ||||
-rw-r--r-- | src/server/engines.rs | 25 | ||||
-rw-r--r-- | src/server/mining.rs | 20 | ||||
-rw-r--r-- | src/server/navigation.rs | 11 | ||||
-rw-r--r-- | src/server/refinery.rs | 20 | ||||
-rw-r--r-- | src/server/tractorbeam.rs | 16 |
7 files changed, 35 insertions, 93 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs index 9275b67..92a7531 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -56,18 +56,19 @@ impl ServerConnection { ModuleType::Construction => self.server_construction(&mut masses), } } -} -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", "")) + pub fn receive(&mut self) -> String { + let mut recv = String::new(); + match self.buff_r.read_line(&mut recv) { + Ok(result) => { + if result == 0 { + self.open = false; + String::new() + } else { + recv.replace("\n", "") + } } + Err(_) => String::new(), } - Err(_) => Some(String::new()), } } diff --git a/src/server/construction.rs b/src/server/construction.rs index d45135c..50a193a 100644 --- a/src/server/construction.rs +++ b/src/server/construction.rs @@ -9,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::{receive, ServerConnection}; +use crate::server::connection::ServerConnection; use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] @@ -28,22 +28,13 @@ impl ServerConnection { .. } = ship.mass_type { - let construction = construction.as_mut().unwrap(); let construction_data = get_construction_data(storage, construction); 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, - } + let recv = self.receive(); + construction.give_recv(recv, &construction_data); if construction_data.status == ConstructionStatus::Constructed { storage.take_items(ItemType::Iron, constants::SHIP_CONSTRUCTION_IRON_COST); diff --git a/src/server/engines.rs b/src/server/engines.rs index 86ef5ec..067acb5 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -5,7 +5,7 @@ use std::io::Write; use crate::mass::{Mass, MassType}; use crate::modules::navigation::NavigationStatus; -use crate::server::connection::{receive, ServerConnection}; +use crate::server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct EnginesData { @@ -24,30 +24,21 @@ impl ServerConnection { .. } = ship.mass_type { - let navigation = navigation.clone().unwrap(); - let engines = engines.as_mut().unwrap(); - 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), + let target = match &navigation.target_name { + Some(name) => masses.get(name), None => None, }; - match receive(&mut self.buff_r) { - Some(recv) => engines.give_client_data( - ship.position.clone(), - ship.velocity.clone(), - target, - recv, - ), - None => self.open = false, - } + let send = serde_json::to_string(&engines_data).unwrap() + "\n"; + self.open = self.stream.write(send.as_bytes()).is_ok(); + + let recv = self.receive(); + engines.give_recv(recv, ship.position.clone(), ship.velocity.clone(), target); } masses.insert(self.name.clone(), ship); diff --git a/src/server/mining.rs b/src/server/mining.rs index b9b0ca0..f5d8dd8 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -8,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::{receive, ServerConnection}; +use crate::server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct MiningData { @@ -30,27 +30,15 @@ impl ServerConnection { .. } = ship.mass_type { - let mining = mining.as_mut().unwrap(); - let navigation = navigation.as_ref().unwrap(); let mining_data = get_mining_data(ship.position.clone(), mining, navigation, masses); 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, - } + let recv = self.receive(); + mining.give_recv(recv, mining_data); - if !mining_data.is_within_range { - mining.off(); - } else if mining.status == MiningStatus::Mined { + if mining.status == MiningStatus::Mined { if let Some(name) = navigation.target_name.clone() { let target = masses.get_mut(&name).unwrap(); if let MassType::Astroid { diff --git a/src/server/navigation.rs b/src/server/navigation.rs index e14c340..67d6758 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -4,7 +4,7 @@ use std::collections::HashMap; use std::io::Write; use crate::mass::{Mass, MassType}; -use crate::server::connection::{receive, ServerConnection}; +use crate::server::connection::ServerConnection; impl ServerConnection { pub fn server_navigation(&mut self, masses: &mut HashMap<String, Mass>) { @@ -15,7 +15,6 @@ impl ServerConnection { ref mut navigation, .. } = ship.mass_type { - let navigation = navigation.as_mut().unwrap(); navigation.verify_target(ship.position.clone(), &masses); let mut within_range: HashMap<&String, &Mass> = masses .iter() @@ -29,12 +28,8 @@ impl ServerConnection { let send = serde_json::to_string(&within_range).unwrap() + "\n"; self.open = self.stream.write(send.as_bytes()).is_ok(); - match receive(&mut self.buff_r) { - Some(recv) => { - navigation.give_target(recv); - } - None => self.open = false, - } + let recv = self.receive(); + navigation.give_recv(recv); } } diff --git a/src/server/refinery.rs b/src/server/refinery.rs index 1a9a6ab..1b6c944 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::item::{Item, ItemType}; use crate::mass::{Mass, MassType}; use crate::modules::refinery::RefineryStatus; -use crate::server::connection::{receive, ServerConnection}; +use crate::server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RefineryData { @@ -24,8 +24,6 @@ impl ServerConnection { .. } = ship.mass_type { - let refinery = refinery.as_mut().unwrap(); - let refinery_data = RefineryData { has_crude_minerals: storage .items @@ -37,20 +35,8 @@ impl ServerConnection { 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 { - refinery.off(); - } + let recv = self.receive(); + refinery.give_recv(recv, refinery_data); if refinery.status == RefineryStatus::Refined { storage.take_item(ItemType::CrudeMinerals); diff --git a/src/server/tractorbeam.rs b/src/server/tractorbeam.rs index b7c11af..9591c0b 100644 --- a/src/server/tractorbeam.rs +++ b/src/server/tractorbeam.rs @@ -6,7 +6,7 @@ use std::io::Write; use crate::mass::{Mass, MassType}; use crate::modules::navigation::NavigationStatus; use crate::modules::tractorbeam::TractorbeamStatus; -use crate::server::connection::{receive, ServerConnection}; +use crate::server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct TractorbeamData { @@ -24,9 +24,6 @@ impl ServerConnection { .. } = ship.mass_type { - let tractorbeam = tractorbeam.as_mut().unwrap(); - let navigation = navigation.as_ref().unwrap(); - if self.open { let tractorbeam_data = TractorbeamData { has_target: navigation.status == NavigationStatus::Targeted, @@ -36,15 +33,8 @@ impl ServerConnection { let send = serde_json::to_string(&tractorbeam_data).unwrap() + "\n"; 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, - } + let recv = self.receive(); + tractorbeam.give_recv(recv); } if let Some(name) = navigation.target_name.clone() { |