summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-20 09:15:29 -0500
committertom barrett <spalf0@gmail.com>2018-04-20 09:15:29 -0500
commit7211ed31e5412eb84f8fbf0e3a465305068f1d7c (patch)
tree43c9f407ff3ba4ffdb846315c42b42e66b6fc0de /src/server
parenta6f21e236f86bc9a9bdcf687ba1086c464d079bc (diff)
-made modules their own structs instead of structs with enumeration
Diffstat (limited to 'src/server')
-rw-r--r--src/server/connection.rs21
-rw-r--r--src/server/engines.rs90
-rw-r--r--src/server/mining.rs104
-rw-r--r--src/server/navigation.rs52
4 files changed, 100 insertions, 167 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs
index 8354d70..a1b808d 100644
--- a/src/server/connection.rs
+++ b/src/server/connection.rs
@@ -5,12 +5,12 @@ use std::io::prelude::*;
use std::net::TcpStream;
use std::collections::HashMap;
-use module::{Module, ModuleType};
-use mass::{Mass, MassType};
+use mass::Mass;
+use module::ModuleType;
pub struct ServerConnection {
pub name : String,
- pub module : Module,
+ pub module_type : ModuleType,
pub stream : TcpStream,
pub buff_r : BufReader<TcpStream>,
pub open : bool,
@@ -26,20 +26,17 @@ impl ServerConnection {
let ship = masses.entry(name.to_string()).or_insert(Mass::new_ship());
- let send = match ship.mass_type {
- MassType::Ship{ref modules, ..} => serde_json::to_string(modules).unwrap() + "\n",
- _ => String::new(),
- };
+ let send = serde_json::to_string(&ship.get_modules()).unwrap() + "\n";
stream.write(send.as_bytes()).unwrap();
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let module : Module = serde_json::from_str(&recv.replace("\n","")).unwrap();
+ let module_type : ModuleType = serde_json::from_str(&recv.replace("\n","")).unwrap();
stream.set_nonblocking(true).unwrap();
ServerConnection {
name : String::from(name),
- module : module,
+ module_type : module_type,
stream : stream,
buff_r : buff_r,
open : true,
@@ -47,11 +44,11 @@ impl ServerConnection {
}
pub fn process(&mut self, mut masses : &mut HashMap<String, Mass>) {
- self.open = match self.module.module_type {
+ self.open = match self.module_type {
ModuleType::Dashboard => self.server_dashboard(&mut masses),
ModuleType::Engines => self.server_engines(&mut masses),
- ModuleType::Mining{..} => self.server_mining(&mut masses),
- ModuleType::Navigation{..} => self.server_navigation(&mut masses),
+ ModuleType::Mining => self.server_mining(&mut masses),
+ ModuleType::Navigation => self.server_navigation(&mut masses),
};
}
}
diff --git a/src/server/engines.rs b/src/server/engines.rs
index b837a90..fcce76a 100644
--- a/src/server/engines.rs
+++ b/src/server/engines.rs
@@ -5,7 +5,6 @@ use std::io::BufRead;
use std::collections::HashMap;
use mass::{Mass, MassType};
-use module::{ModuleType};
use module::NavigationStatus;
use server::connection::ServerConnection;
@@ -14,78 +13,35 @@ impl ServerConnection {
let masses_clone = masses.clone();
let ship = masses.get_mut(&self.name).unwrap();
- let mut acceleration = (0.0, 0.0, 0.0);
+ let ship_clone = ship.clone();
- if let MassType::Ship{ref modules, ..} = ship.mass_type {
- if let ModuleType::Navigation{ref status, ref target_name, ..} = modules.get("Navigation").unwrap().module_type {
- let targeted = status == &NavigationStatus::Targeted;
+ if let MassType::Ship{ref mut engines, ref navigation, ..} = ship.mass_type {
+ 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";
- match self.stream.write(send.as_bytes()) {
- Ok(_result) => (),
- Err(_error) => return false,
- }
+ let send = serde_json::to_string(&targeted).unwrap() + "\n";
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
+ }
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(result) => match recv.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" => {
- let m_v = ship.velocity;
- acceleration = (m_v.0 * 0.05,
- m_v.1 * 0.05,
- m_v.2 * 0.05);
- },
- b"-\n" => {
- let m_v = ship.velocity;
- acceleration = (-1.0 * m_v.0 * 0.05,
- -1.0 * m_v.1 * 0.05,
- -1.0 * m_v.2 * 0.05);
- },
- b"c\n" => {
- match target_name {
- &Some(ref name) => {
- let target = masses_clone.get(name).unwrap();
- let d_v = target.velocity;
- let m_v = ship.velocity;
- acceleration = (d_v.0 - m_v.0,
- d_v.1 - m_v.1,
- d_v.2 - m_v.2);
- },
- &None => (),
- }
- },
- b"t\n" => {
- match target_name {
- &Some(ref name) => {
- let target = masses_clone.get(name).unwrap();
- let d_p = target.position;
- let m_p = ship.position;
- acceleration = ((d_p.0 - m_p.0) * 0.01,
- (d_p.1 - m_p.1) * 0.01,
- (d_p.2 - m_p.2) * 0.01);
- },
- &None => (),
- }
- },
- _ => {
- if result == 0 {
- return false
- }
- },
- },
- Err(_error) => (),
- }
+ let target = match navigation.target_name {
+ Some(name) => masses_clone.get(&name),
+ None => None,
+ };
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(result) => {
+ engines.give_client_data(&ship_clone, target, recv);
+ if result == 0 {
+ return false;
+ }
+ },
+ Err(_error) => (),
}
}
- ship.accelerate(acceleration);
-
true
}
}
diff --git a/src/server/mining.rs b/src/server/mining.rs
index 06f2418..c02937a 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -6,8 +6,8 @@ use std::collections::HashMap;
use math::distance;
use mass::{Mass, MassType};
-use module::{Module, ModuleType};
use server::connection::ServerConnection;
+use module::{Navigation, Mining};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MiningData {
@@ -24,8 +24,10 @@ impl ServerConnection {
let ship_clone = ship.clone();
- if let MassType::Ship{ref mut modules, ..} = ship.mass_type {
- let mining_data = get_mining_data(ship_clone, modules, masses_clone);
+ if let MassType::Ship{ref mut mining, ref navigation, ..} = ship.mass_type {
+ let mut mining = mining.as_mut().unwrap();
+ let mut navigation = navigation.as_ref().unwrap();
+ let mining_data = get_mining_data(ship_clone, mining, navigation, masses_clone);
let send = serde_json::to_string(&mining_data).unwrap() + "\n";
match self.stream.write(send.as_bytes()) {
@@ -33,66 +35,60 @@ impl ServerConnection {
Err(_error) => return false,
}
- if let ModuleType::Mining{ref mut status, ..} = modules.get_mut("Mining").unwrap().module_type {
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(result) => match recv.as_bytes() {
- b"F\n" => {
- if mining_data.is_within_range {
- *status = !*status;
- }
- },
- _ => {
- if result == 0 {
- return false
- }
- },
- }
- Err(_error) => (),
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(result) => match recv.as_bytes() {
+ b"F\n" => {
+ if mining_data.is_within_range {
+ mining.toggle();
+ }
+ },
+ _ => {
+ if result == 0 {
+ return false
+ }
+ },
}
+ Err(_error) => (),
}
}
true
}
}
-fn get_mining_data(ship : Mass, modules : &mut HashMap<String, Module>, masses_clone : HashMap<String, Mass>) -> MiningData {
- let mut mining_range = 0.0;
- let mut mining_status = false;
- if let ModuleType::Mining{ref range, ref status, ..} = modules.get("Mining").unwrap().module_type {
- mining_range = *range;
- mining_status = *status;
- }
-
- let mut has_astroid_target = false;
- let mut is_within_range = false;
- if let ModuleType::Navigation{ref target_name, ..} = modules.get("Navigation").unwrap().module_type {
- match target_name.clone() {
- Some(name) => {
- let target = masses_clone.get(&name);
- has_astroid_target = match target {
- Some(target) => match target.mass_type {
- MassType::Astroid{..} => true,
- _ => false,
- },
- None => false,
- };
- is_within_range = match has_astroid_target {
- true => match target {
- Some(target) => mining_range > distance(ship.position, target.position),
- _ => false,
- }
+fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, masses_clone : HashMap<String, Mass>) -> MiningData {
+ match navigation.target_name.clone() {
+ Some(name) => {
+ let target = masses_clone.get(&name);
+ let has_astroid_target = match target {
+ Some(target) => match target.mass_type {
+ MassType::Astroid{..} => true,
+ _ => false,
+ },
+ None => false,
+ };
+ let is_within_range = match has_astroid_target {
+ true => match target {
+ Some(target) => mining.range > distance(ship.position, target.position),
_ => false,
- };
+ }
+ _ => false,
+ };
+
+ MiningData {
+ has_astroid_target : has_astroid_target,
+ is_within_range : is_within_range,
+ range : mining.range,
+ status : mining.status,
+ }
+ }
+ _ => {
+ MiningData {
+ has_astroid_target : false,
+ is_within_range : false,
+ range : mining.range,
+ status : mining.status,
}
- _ => (),
}
- }
-
- MiningData {
- has_astroid_target : has_astroid_target,
- is_within_range : is_within_range,
- range : mining_range,
- status : mining_status,
}
}
diff --git a/src/server/navigation.rs b/src/server/navigation.rs
index 7f4a1bc..8168fac 100644
--- a/src/server/navigation.rs
+++ b/src/server/navigation.rs
@@ -2,11 +2,9 @@ extern crate serde_json;
use std::io::Write;
use std::io::BufRead;
-use std::time::SystemTime;
use std::collections::HashMap;
use math::distance;
-use module::{ModuleType, NavigationStatus};
use mass::{Mass, MassType};
use server::connection::ServerConnection;
@@ -16,39 +14,25 @@ impl ServerConnection {
let ship = masses.get_mut(&self.name).unwrap();
let ship_position = ship.position;
- if let MassType::Ship{ref mut modules, ..} = ship.mass_type {
- let mut navigation = modules.get_mut("Navigation").unwrap();
- if let ModuleType::Navigation{ref mut target_name, ref mut start, ref mut status, ref range, ..} = navigation.module_type {
-
- match target_name.clone() {
- Some(name) => {
- let target = masses_clone.get(&name).unwrap();
- if distance(target.position, ship.position) > *range {
- *target_name = None;
- }
- }
- _ => (),
- }
-
- let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)|
- distance(ship_position, mass.position) < *range)
- .collect();
- let send = serde_json::to_string(&within_range).unwrap() + "\n";
- match self.stream.write(send.as_bytes()) {
- Ok(_result) => (),
- Err(_error) => return false,
- }
+ if let MassType::Ship{ref mut navigation, ..} = ship.mass_type {
+ let mut navigation = navigation.as_mut().unwrap();
+ navigation.verify_target(ship_position, &masses_clone);
+ let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)|
+ distance(ship_position, mass.position) < navigation.range)
+ .collect();
+ let send = serde_json::to_string(&within_range).unwrap() + "\n";
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
+ }
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(_result) => (),
- Err(_error) => (),
- }
- if !recv.is_empty() {
- *target_name = Some(recv.replace("\n", ""));
- *start = Some(SystemTime::now());
- *status = NavigationStatus::Targeting;
- }
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(_result) => (),
+ Err(_error) => (),
+ }
+ if !recv.is_empty() {
+ navigation.give_target(recv.replace("\n", ""));
}
}
true