diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/connection.rs | 74 | ||||
-rw-r--r-- | src/server/construction.rs | 69 | ||||
-rw-r--r-- | src/server/dashboard.rs | 17 | ||||
-rw-r--r-- | src/server/engines.rs | 47 | ||||
-rw-r--r-- | src/server/mining.rs | 94 | ||||
-rw-r--r-- | src/server/mod.rs | 8 | ||||
-rw-r--r-- | src/server/navigation.rs | 38 | ||||
-rw-r--r-- | src/server/refinery.rs | 51 | ||||
-rw-r--r-- | src/server/tractorbeam.rs | 52 |
9 files changed, 0 insertions, 450 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs deleted file mode 100644 index 92a7531..0000000 --- a/src/server/connection.rs +++ /dev/null @@ -1,74 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::prelude::*; -use std::io::BufReader; -use std::net::TcpStream; - -use crate::mass::Mass; -use crate::modules::types::ModuleType; - -pub struct ServerConnection { - pub name: String, - pub module_type: ModuleType, - pub stream: TcpStream, - pub buff_r: BufReader<TcpStream>, - pub open: bool, -} - -impl ServerConnection { - pub fn new(mut stream: TcpStream, masses: &mut HashMap<String, Mass>) -> ServerConnection { - let mut buff_r = BufReader::new(stream.try_clone().unwrap()); - - let mut recv = String::new(); - buff_r.read_line(&mut recv).unwrap(); - let name = &recv[..recv.find(':').unwrap()]; - - let ship = masses - .entry(name.to_string()) - .or_insert_with(Mass::new_ship); - - let send = serde_json::to_string(&ship.get_modules()).unwrap() + "\n"; - stream.write_all(send.as_bytes()).unwrap(); - - let mut recv = String::new(); - buff_r.read_line(&mut recv).unwrap(); - let module_type: ModuleType = serde_json::from_str(&recv.replace("\n", "")).unwrap(); - - stream.set_nonblocking(true).unwrap(); - ServerConnection { - name: String::from(name), - module_type, - stream, - buff_r, - open: true, - } - } - - pub fn process(&mut self, mut masses: &mut HashMap<String, Mass>) { - match self.module_type { - ModuleType::Mining => self.server_mining(&mut masses), - ModuleType::Engines => self.server_engines(&mut masses), - ModuleType::Refinery => self.server_refinery(&mut masses), - ModuleType::Dashboard => self.server_dashboard(&mut masses), - ModuleType::Navigation => self.server_navigation(&mut masses), - ModuleType::Tractorbeam => self.server_tractorbeam(&mut masses), - ModuleType::Construction => self.server_construction(&mut masses), - } - } - - 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(), - } - } -} diff --git a/src/server/construction.rs b/src/server/construction.rs deleted file mode 100644 index 0e24952..0000000 --- a/src/server/construction.rs +++ /dev/null @@ -1,69 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::constants; -use crate::item::ItemType; -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::storage::Storage; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct ConstructionData { - pub status: ConstructionStatus, - pub has_enough: bool, -} - -impl ServerConnection { - pub fn server_construction(&mut self, masses: &mut HashMap<String, Mass>) { - let mut ship = masses.remove(&self.name).unwrap(); - - if let MassType::Ship { - ref mut construction, - ref mut storage, - .. - } = ship.mass_type - { - 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(); - - 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) - .unwrap(); - masses.insert( - "Station".to_string(), - Mass::new_station( - ModuleType::Refinery, - ship.position.clone(), - ship.velocity.clone(), - ), - ); - construction.constructed(); - } - } - - masses.insert(self.name.clone(), ship); - } -} - -fn get_construction_data(storage: &Storage, construction: &Construction) -> ConstructionData { - ConstructionData { - status: construction.status.clone(), - has_enough: storage - .items - .iter() - .filter(|item| item.itemtype == ItemType::Iron) - .count() - >= constants::SHIP_CONSTRUCTION_IRON_COST, - } -} diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs deleted file mode 100644 index cff2398..0000000 --- a/src/server/dashboard.rs +++ /dev/null @@ -1,17 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::mass::Mass; -use crate::server::connection::ServerConnection; - -impl ServerConnection { - pub fn server_dashboard(&mut self, masses: &mut HashMap<String, Mass>) { - if self.open { - let ship = masses.get(&self.name).unwrap(); - let send = serde_json::to_string(&ship).unwrap() + "\n"; - self.open = self.stream.write(send.as_bytes()).is_ok(); - } - } -} diff --git a/src/server/engines.rs b/src/server/engines.rs deleted file mode 100644 index 067acb5..0000000 --- a/src/server/engines.rs +++ /dev/null @@ -1,47 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::mass::{Mass, MassType}; -use crate::modules::navigation::NavigationStatus; -use crate::server::connection::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>) { - if self.open { - let mut ship = masses.remove(&self.name).unwrap(); - - if let MassType::Ship { - ref mut engines, - ref navigation, - .. - } = ship.mass_type - { - let engines_data = EnginesData { - has_target: navigation.status == NavigationStatus::Targeted, - fuel: engines.fuel, - }; - - let target = match &navigation.target_name { - Some(name) => masses.get(name), - None => None, - }; - - 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 deleted file mode 100644 index 0bf6956..0000000 --- a/src/server/mining.rs +++ /dev/null @@ -1,94 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::item::ItemType; -use crate::mass::{Mass, MassType}; -use crate::math::Vector; -use crate::modules::mining::{Mining, MiningStatus}; -use crate::server::connection::ServerConnection; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct MiningData { - pub has_astroid_target: bool, - pub astroid_has_minerals: bool, - pub is_within_range: bool, - pub status: MiningStatus, - pub range: f64, -} - -impl ServerConnection { - pub fn server_mining(&mut self, masses: &mut HashMap<String, Mass>) { - let mut ship = masses.remove(&self.name).unwrap(); - - if let MassType::Ship { - ref mut mining, - ref mut storage, - ref navigation, - .. - } = ship.mass_type - { - if let Some(target_name) = navigation.target_name.clone() { - let mut target = masses.remove(&target_name).unwrap(); - - let mining_data = get_mining_data(ship.position.clone(), mining, target.clone()); - - let send = serde_json::to_string(&mining_data).unwrap() + "\n"; - self.open = self.stream.write(send.as_bytes()).is_ok(); - - let recv = self.receive(); - mining.give_recv(recv, mining_data); - - if mining.status == MiningStatus::Mined { - if let MassType::Astroid { - ref mut resources, .. - } = target.mass_type - { - match resources.take_item(ItemType::CrudeMinerals) { - Some(item) => { - if !storage.give_item(item.clone()) { - let mass = Mass::new_item( - item.clone(), - ship.position.clone(), - ship.velocity.clone(), - ); - masses.insert(item.name.clone(), mass); - } - } - None => mining.off(), - } - } - mining.mined(); - } - masses.insert(target_name, target); - } - } - - masses.insert(self.name.clone(), ship); - } -} - -fn get_mining_data(position: Vector, mining: &Mining, target: Mass) -> MiningData { - let mut astroid_has_minerals = false; - let mut is_within_range = false; - let has_astroid_target = match target.mass_type { - MassType::Astroid { ref resources, .. } => { - astroid_has_minerals = resources - .items - .iter() - .any(|item| item.itemtype == ItemType::CrudeMinerals); - is_within_range = mining.range > position.distance_from(target.position.clone()); - true - } - _ => false, - }; - - MiningData { - has_astroid_target, - astroid_has_minerals, - is_within_range, - range: mining.range, - status: mining.status.clone(), - } -} diff --git a/src/server/mod.rs b/src/server/mod.rs deleted file mode 100644 index c195f3d..0000000 --- a/src/server/mod.rs +++ /dev/null @@ -1,8 +0,0 @@ -pub mod connection; -pub mod construction; -pub mod dashboard; -pub mod engines; -pub mod mining; -pub mod navigation; -pub mod refinery; -pub mod tractorbeam; diff --git a/src/server/navigation.rs b/src/server/navigation.rs deleted file mode 100644 index 67d6758..0000000 --- a/src/server/navigation.rs +++ /dev/null @@ -1,38 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::mass::{Mass, MassType}; -use crate::server::connection::ServerConnection; - -impl ServerConnection { - pub fn server_navigation(&mut self, masses: &mut HashMap<String, Mass>) { - let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); - - if let MassType::Ship { - ref mut navigation, .. - } = ship.mass_type - { - navigation.verify_target(ship.position.clone(), &masses); - let mut within_range: HashMap<&String, &Mass> = masses - .iter() - .filter(|&(_, mass)| { - ship_clone.position.distance_from(mass.position.clone()) < navigation.range - }) - .collect(); - within_range.insert(&self.name, &ship_clone); - - if self.open { - let send = serde_json::to_string(&within_range).unwrap() + "\n"; - self.open = self.stream.write(send.as_bytes()).is_ok(); - - let recv = self.receive(); - navigation.give_recv(recv); - } - } - - masses.insert(self.name.clone(), ship); - } -} diff --git a/src/server/refinery.rs b/src/server/refinery.rs deleted file mode 100644 index 1b6c944..0000000 --- a/src/server/refinery.rs +++ /dev/null @@ -1,51 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::item::{Item, ItemType}; -use crate::mass::{Mass, MassType}; -use crate::modules::refinery::RefineryStatus; -use crate::server::connection::ServerConnection; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct RefineryData { - pub has_crude_minerals: bool, - pub status: RefineryStatus, -} - -impl ServerConnection { - pub fn server_refinery(&mut self, masses: &mut HashMap<String, Mass>) { - let mut ship = masses.remove(&self.name).unwrap(); - - if let MassType::Ship { - ref mut refinery, - ref mut storage, - .. - } = ship.mass_type - { - let refinery_data = RefineryData { - has_crude_minerals: storage - .items - .iter() - .any(|item| item.itemtype == ItemType::CrudeMinerals), - status: refinery.status.clone(), - }; - - let send = serde_json::to_string(&refinery_data).unwrap() + "\n"; - self.open = self.stream.write(send.as_bytes()).is_ok(); - - let recv = self.receive(); - refinery.give_recv(recv, refinery_data); - - 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); - } -} diff --git a/src/server/tractorbeam.rs b/src/server/tractorbeam.rs deleted file mode 100644 index 9591c0b..0000000 --- a/src/server/tractorbeam.rs +++ /dev/null @@ -1,52 +0,0 @@ -extern crate serde_json; - -use std::collections::HashMap; -use std::io::Write; - -use crate::mass::{Mass, MassType}; -use crate::modules::navigation::NavigationStatus; -use crate::modules::tractorbeam::TractorbeamStatus; -use crate::server::connection::ServerConnection; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct TractorbeamData { - pub has_target: bool, - pub status: TractorbeamStatus, -} - -impl ServerConnection { - pub fn server_tractorbeam(&mut self, masses: &mut HashMap<String, Mass>) { - let mut ship = masses.remove(&self.name).unwrap(); - - if let MassType::Ship { - ref mut tractorbeam, - ref navigation, - .. - } = ship.mass_type - { - if self.open { - let tractorbeam_data = TractorbeamData { - has_target: navigation.status == NavigationStatus::Targeted, - status: tractorbeam.status.clone(), - }; - - let send = serde_json::to_string(&tractorbeam_data).unwrap() + "\n"; - self.open = self.stream.write(send.as_bytes()).is_ok(); - - let recv = self.receive(); - tractorbeam.give_recv(recv); - } - - if let Some(name) = navigation.target_name.clone() { - let target = masses.get_mut(&name).unwrap(); - let acceleration = - tractorbeam.get_acceleration(ship.position.clone(), target.clone()); - target.effects.give_acceleration(acceleration); - } else { - tractorbeam.off(); - } - } - - masses.insert(self.name.clone(), ship); - } -} |