summaryrefslogtreecommitdiff
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
parenta6f21e236f86bc9a9bdcf687ba1086c464d079bc (diff)
-made modules their own structs instead of structs with enumeration
-rw-r--r--src/bin/client.rs17
-rw-r--r--src/client/navigation.rs87
-rw-r--r--src/mass.rs44
-rw-r--r--src/module.rs178
-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
8 files changed, 306 insertions, 287 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs
index 1766a59..a26f68a 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -5,9 +5,8 @@ use std::io;
use std::io::BufReader;
use std::io::prelude::*;
use std::net::TcpStream;
-use std::collections::BTreeMap;
-use space::module::{Module, ModuleType};
+use space::module::ModuleType;
use space::client::mining::client_mining;
use space::client::engines::client_engines;
use space::client::dashboard::client_dashboard;
@@ -32,24 +31,24 @@ fn main() {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let modules : BTreeMap<String, Module> = serde_json::from_str(&recv.replace("\n","")).unwrap();
+ let modules : Vec<ModuleType> = serde_json::from_str(&recv.replace("\n","")).unwrap();
println!("Choose your module:");
- for (i, module) in modules.keys().enumerate() {
+ for (i, module) in modules.iter().enumerate() {
println!("{}) {:?}", i, module);
}
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed");
- let module = modules.values().nth(choice.replace("\n", "").parse::<usize>().unwrap()).unwrap();
+ let module_type = modules.into_iter().nth(choice.replace("\n", "").parse::<usize>().unwrap()).unwrap();
- let send = serde_json::to_string(&module).unwrap() + "\n";
+ let send = serde_json::to_string(&module_type).unwrap() + "\n";
stream.write(send.as_bytes()).unwrap();
- match module.module_type {
+ match module_type {
ModuleType::Dashboard => client_dashboard(buff_r),
ModuleType::Engines => client_engines(stream, buff_r),
- ModuleType::Mining{..} => client_mining(stream, buff_r),
- ModuleType::Navigation{..} => client_navigation(name, stream, buff_r),
+ ModuleType::Mining => client_mining(stream, buff_r),
+ ModuleType::Navigation => client_navigation(name, stream, buff_r),
}
}
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
index eb9a2c4..2063de4 100644
--- a/src/client/navigation.rs
+++ b/src/client/navigation.rs
@@ -10,7 +10,7 @@ use self::termion::raw::IntoRawMode;
use math::distance;
use mass::{Mass, MassType};
-use module::ModuleType;
+use module::Navigation;
pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){
let stdout = stdout();
@@ -28,54 +28,53 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
let ship = within_range.remove(&name).unwrap();
- if let MassType::Ship{ref modules, ..} = ship.mass_type {
- if let ModuleType::Navigation{ref status, ref target_name, ..} = modules.get("Navigation").unwrap().module_type {
- for (i, (mass_name, mass)) in within_range.iter().enumerate() {
- let target_data = match target_name.clone() {
- Some(target_name) => {
- if &target_name == mass_name {
- serde_json::to_string(status).unwrap()
- }
- else {
- String::new()
- }
- }
- None => String::new(),
- };
-
- write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}",
- termion::cursor::Goto(1, 2 + i as u16),
- i,
- mass_name,
- mass.position.0,
- mass.position.1,
- mass.position.2,
- distance(mass.position, ship.position),
- target_data
- ).unwrap();
- }
+ if let MassType::Ship{ref navigation, ..} = ship.mass_type {
+ let navigation = navigation.clone().unwrap();
+ for (i, (mass_name, mass)) in within_range.iter().enumerate() {
+ let target_data = get_target_status(&navigation, &mass_name);
+ write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}",
+ termion::cursor::Goto(1, 2 + i as u16),
+ i,
+ mass_name,
+ mass.position.0,
+ mass.position.1,
+ mass.position.2,
+ distance(mass.position, ship.position),
+ target_data
+ ).unwrap();
+ }
- match stdin.next() {
- Some(c) => {
- let c = c.unwrap() as char;
- if c == 'q' {
- break;
- }
- else {
- let i = c.to_digit(10).unwrap() as usize;
- if i < within_range.len() {
- let mut send = String::new();
- send.push_str(within_range.iter().nth(i).unwrap().0);
- send.push_str("\n");
- stream.write(send.as_bytes()).unwrap();
- }
+ match stdin.next() {
+ Some(c) => {
+ let c = c.unwrap() as char;
+ if c == 'q' {
+ break;
+ }
+ else {
+ let i = c.to_digit(10).unwrap() as usize;
+ if i < within_range.len() {
+ let mut send = String::new();
+ send.push_str(within_range.iter().nth(i).unwrap().0);
+ send.push_str("\n");
+ stream.write(send.as_bytes()).unwrap();
}
}
- None => ()
- }
+ },
+ None => (),
}
}
-
stdout.flush().unwrap();
}
}
+
+fn get_target_status(navigation : &Navigation, mass_name : &String) -> String {
+ match navigation.target_name.clone() {
+ Some(name) => {
+ match &name == mass_name {
+ true => serde_json::to_string(&navigation.status).unwrap(),
+ false => String::new()
+ }
+ },
+ _ => String::new(),
+ }
+}
diff --git a/src/mass.rs b/src/mass.rs
index f1ac1b2..3a0e10e 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,12 +1,11 @@
extern crate rand;
-use std::collections::HashMap;
use self::rand::distributions::Range;
use self::rand::distributions::Sample;
use item::Item;
use storage::Storage;
-use module::Module;
+use module::{Mining, Navigation, Dashboard, Engines, ModuleType};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mass {
@@ -18,7 +17,10 @@ pub struct Mass {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
Ship {
- modules : HashMap<String, Module>,
+ mining : Option<Mining>,
+ navigation : Option<Navigation>,
+ engines : Option<Engines>,
+ dashboard : Option<Dashboard>,
storage : Storage,
},
Astroid{
@@ -55,15 +57,11 @@ impl Mass {
}
pub fn new_ship() -> Mass {
- let mut modules = HashMap::new();
-
- modules.insert("Mining".to_string(), Module::new_mining());
- modules.insert("Engines".to_string(), Module::new_engines());
- modules.insert("Dashboard".to_string(), Module::new_dashboard());
- modules.insert("Navigation".to_string(), Module::new_navigation());
-
let ship = MassType::Ship {
- modules : modules,
+ mining : Some(Mining::new()),
+ engines : Some(Engines::new()),
+ dashboard : Some(Dashboard::new()),
+ navigation : Some(Navigation::new()),
storage : Storage::new(Vec::new()),
};
@@ -74,18 +72,28 @@ impl Mass {
}
}
+ pub fn get_modules(&self) -> Vec<ModuleType> {
+ let mut modules = Vec::new();
+ modules.push(ModuleType::Mining);
+ modules.push(ModuleType::Engines);
+ modules.push(ModuleType::Dashboard);
+ modules.push(ModuleType::Navigation);
+ modules
+ }
+
pub fn process(&mut self) {
- self.position.0 += self.velocity.0;
- self.position.1 += self.velocity.1;
- self.position.2 += self.velocity.2;
+ let mut acceleration = (0.0, 0.0, 0.0);
match self.mass_type {
- MassType::Ship{ref mut modules, ..} => {
- for module in modules.values_mut() {
- module.process();
- }
+ MassType::Ship{ref mut navigation, ref mut engines, ..} => {
+ navigation.as_mut().unwrap().process();
+ acceleration = engines.as_mut().unwrap().recv_acceleration();
},
_ => (),
}
+ self.accelerate(acceleration);
+ self.position.0 += self.velocity.0;
+ self.position.1 += self.velocity.1;
+ self.position.2 += self.velocity.2;
}
pub fn accelerate(&mut self, acceleration : (f64, f64, f64)) {
diff --git a/src/module.rs b/src/module.rs
index 028eebe..333ba05 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -1,4 +1,16 @@
use std::time::SystemTime;
+use std::collections::HashMap;
+
+use mass::Mass;
+use math::distance;
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
+pub enum ModuleType {
+ Navigation,
+ Mining,
+ Engines,
+ Dashboard,
+}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum NavigationStatus {
@@ -8,85 +20,153 @@ pub enum NavigationStatus {
}
#[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct Module {
- pub module_type : ModuleType,
+pub struct Navigation {
+ pub range : f64,
+ pub status : NavigationStatus,
+ pub target_name : Option<String>,
+ time : u64,
+ start : Option<SystemTime>,
}
-#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
-pub enum ModuleType {
- Mining {
- range : f64,
- status : bool,
- time : u64,
- start : Option<SystemTime>,
- },
- Navigation {
- range : f64,
- status : NavigationStatus,
- time : u64,
- start : Option<SystemTime>,
- target_name : Option<String>,
- },
- Engines,
- Dashboard,
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Mining {
+ pub range : f64,
+ pub status : bool,
+ time : u64,
+ start : Option<SystemTime>,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Engines {
+ acceleration : (f64, f64, f64),
}
-impl Module {
- pub fn new_mining() -> Module {
- let mining = ModuleType::Mining {
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Dashboard {}
+
+impl Mining {
+ pub fn new() -> Mining {
+ Mining {
range : 10.0,
status : false,
time : 1,
start : None,
- };
-
- Module {
- module_type : mining,
}
}
- pub fn new_navigation() -> Module {
- let navigation = ModuleType::Navigation {
+ pub fn toggle(&self) {
+ }
+}
+
+impl Navigation {
+ pub fn new() -> Navigation {
+ Navigation {
target_name : None,
range : 100.0,
status : NavigationStatus::None,
time : 3,
start : None,
- };
+ }
+ }
- Module {
- module_type : navigation,
+ pub fn process(&mut self) {
+ match self.start.clone() {
+ Some(timer) => {
+ if timer.elapsed().unwrap().as_secs() > self.time {
+ self.status = NavigationStatus::Targeted;
+ self.start = None;
+ }
+ }
+ _ => (),
}
}
- pub fn new_dashboard() -> Module {
- Module {
- module_type : ModuleType::Dashboard,
+ pub fn give_target(&mut self, target_name : String) {
+ self.start = Some(SystemTime::now());
+ self.status = NavigationStatus::Targeting;
+ self.target_name = Some(target_name);
+ }
+
+ pub fn verify_target(&mut self, ship_position : (f64, f64, f64), masses : &HashMap<String, Mass>) {
+ match self.target_name.clone() {
+ Some(name) => {
+ let target = masses.get(&name).unwrap();
+ if distance(target.position, ship_position) > self.range {
+ self.target_name = None;
+ self.status = NavigationStatus::None;
+ }
+ }
+ _ => (),
}
}
+}
- pub fn new_engines() -> Module {
- Module {
- module_type : ModuleType::Engines,
+impl Dashboard {
+ pub fn new() -> Dashboard {
+ Dashboard {}
+ }
+}
+
+impl Engines {
+ pub fn new() -> Engines {
+ Engines {
+ acceleration : (0.0, 0.0, 0.0)
}
}
- pub fn process(&mut self) {
- match self.module_type {
- ModuleType::Navigation{ref mut status, ref mut start, ref time, ..} => {
- match start.clone() {
- Some(timer) => {
- if timer.elapsed().unwrap().as_secs() > *time {
- *status = NavigationStatus::Targeted;
- *start = None;
- }
- }
- _ => (),
+ pub fn recv_acceleration(&mut self) -> (f64, f64, f64) {
+ let acceleration = self.acceleration;
+ self.acceleration = (0.0, 0.0, 0.0);
+ acceleration
+ }
+
+ pub fn give_client_data(&mut self, ship : &Mass, target : Option<&Mass>, data : String) {
+ let mut acceleration = (0.0, 0.0, 0.0);
+ 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" => {
+ 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 {
+ Some(target) => {
+ 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 => (),
}
},
- ModuleType::Mining{..} => {
+ b"t\n" => {
+ match target {
+ Some(target) => {
+ 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 => (),
+ }
},
_ => (),
}
+ self.acceleration = acceleration;
}
}
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