summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-02-03 05:45:35 -0600
committertom barrett <spalf0@gmail.com>2019-02-03 05:45:35 -0600
commit46fa862e04bc43311ba79ef3db70abf9014b9104 (patch)
tree3507873ec788af1f1e0885bbb676dbb4a373d48e /src/server
parent828f0d83dcb258a8e5efd55a7775592c6e5f77bc (diff)
bringing to 2018
Diffstat (limited to 'src/server')
-rw-r--r--src/server/connection.rs44
-rw-r--r--src/server/construction.rs71
-rw-r--r--src/server/dashboard.rs8
-rw-r--r--src/server/engines.rs19
-rw-r--r--src/server/mining.rs102
-rw-r--r--src/server/mod.rs10
-rw-r--r--src/server/navigation.rs28
-rw-r--r--src/server/refinery.rs46
8 files changed, 180 insertions, 148 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs
index 973c5d1..4fed2b6 100644
--- a/src/server/connection.rs
+++ b/src/server/connection.rs
@@ -1,49 +1,51 @@
extern crate serde_json;
-use std::io::BufReader;
+use std::collections::HashMap;
use std::io::prelude::*;
+use std::io::BufReader;
use std::net::TcpStream;
-use std::collections::HashMap;
-use mass::Mass;
-use modules::types::ModuleType;
+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,
+ 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 {
+ 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 name = &recv[..recv.find(':').unwrap()];
- let ship = masses.entry(name.to_string()).or_insert(Mass::new_ship());
+ 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(send.as_bytes()).unwrap();
+ 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();
+ let module_type: ModuleType = serde_json::from_str(&recv.replace("\n", "")).unwrap();
stream.set_nonblocking(true).unwrap();
- ServerConnection {
- name : String::from(name),
- module_type : module_type,
- stream : stream,
- buff_r : buff_r,
- open : true,
+ ServerConnection {
+ name: String::from(name),
+ module_type,
+ stream,
+ buff_r,
+ open: true,
}
}
- pub fn process(&mut self, mut masses : &mut HashMap<String, Mass>) {
+ 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),
diff --git a/src/server/construction.rs b/src/server/construction.rs
index 94a6b0f..70038ae 100644
--- a/src/server/construction.rs
+++ b/src/server/construction.rs
@@ -1,47 +1,65 @@
extern crate serde_json;
+use std::collections::HashMap;
use std::io::BufRead;
use std::io::Write;
-use std::collections::HashMap;
-use mass::{Mass, MassType};
-use modules::construction::Construction;
-use server::connection::ServerConnection;
-use modules::construction::ConstructionStatus;
-use modules::types::ModuleType;
+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;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ConstructionData {
- pub status : ConstructionStatus,
- pub has_refined : bool,
+ pub status: ConstructionStatus,
+ pub has_refined: bool,
}
impl ServerConnection {
- pub fn server_construction(&mut self, masses : &mut HashMap<String, Mass>) {
+ pub fn server_construction(&mut self, masses: &mut HashMap<String, Mass>) {
let mut ship = masses.remove(&self.name).unwrap();
let ship_clone = ship.clone();
+ let mut construct = false;
- if let MassType::Ship{ref mut construction, ..} = ship.mass_type {
- let mut construction = construction.as_mut().unwrap();
+ if let MassType::Ship {
+ ref mut construction,
+ ..
+ } = ship.mass_type
+ {
+ let construction = construction.as_mut().unwrap();
let construction_data = get_construction_data(ship_clone.clone(), construction);
- if self.open {
- if self.txrx_construction(&construction_data) {
- construction.toggle();
- }
+ if self.open && self.txrx_construction(&construction_data) {
+ construction.toggle();
}
if construction_data.status == ConstructionStatus::Constructed {
- println!("inserted");
construction.take();
- masses.insert("Station".to_string(), Mass::new_station(ModuleType::Refinery, ship_clone.position, ship_clone.velocity));
+ masses.insert(
+ "Station".to_string(),
+ Mass::new_station(
+ ModuleType::Refinery,
+ ship_clone.position,
+ ship_clone.velocity,
+ ),
+ );
+ construct = true;
}
}
+ if construct {
+ ship.take("Refined Mineral");
+ ship.take("Refined Mineral");
+ ship.take("Refined Mineral");
+ ship.take("Refined Mineral");
+ ship.take("Refined Mineral");
+ }
+
masses.insert(self.name.clone(), ship);
}
- fn txrx_construction(&mut self, construction_data : &ConstructionData) -> bool {
+ 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;
@@ -52,14 +70,14 @@ impl ServerConnection {
match recv.as_bytes() {
b"c\n" => {
if construction_data.has_refined {
- return true
+ return true;
}
- },
+ }
_ => {
if result == 0 {
self.open = false;
}
- },
+ }
}
}
@@ -67,14 +85,11 @@ impl ServerConnection {
}
}
-fn get_construction_data(ship : Mass, construction : &Construction) -> ConstructionData {
- let mut has_refined = false;
- if ship.refined_count() >= 5 {
- has_refined = true;
- }
+fn get_construction_data(ship: Mass, construction: &Construction) -> ConstructionData {
+ let has_refined = ship.refined_count() >= 5;
ConstructionData {
- status : construction.status.clone(),
- has_refined : has_refined,
+ status: construction.status.clone(),
+ has_refined,
}
}
diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs
index d1aa8a4..981158b 100644
--- a/src/server/dashboard.rs
+++ b/src/server/dashboard.rs
@@ -1,13 +1,13 @@
extern crate serde_json;
-use std::io::Write;
use std::collections::HashMap;
+use std::io::Write;
-use mass::Mass;
-use server::connection::ServerConnection;
+use crate::mass::Mass;
+use crate::server::connection::ServerConnection;
impl ServerConnection {
- pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Mass>) {
+ 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";
diff --git a/src/server/engines.rs b/src/server/engines.rs
index c0d4ad5..559ecd9 100644
--- a/src/server/engines.rs
+++ b/src/server/engines.rs
@@ -1,20 +1,25 @@
extern crate serde_json;
-use std::io::Write;
-use std::io::BufRead;
use std::collections::HashMap;
+use std::io::BufRead;
+use std::io::Write;
-use mass::{Mass, MassType};
-use modules::navigation::NavigationStatus;
-use server::connection::ServerConnection;
+use crate::mass::{Mass, MassType};
+use crate::modules::navigation::NavigationStatus;
+use crate::server::connection::ServerConnection;
impl ServerConnection {
- pub fn server_engines(&mut self, masses : &mut HashMap<String, Mass>) {
+ pub fn server_engines(&mut self, masses: &mut HashMap<String, Mass>) {
if self.open {
let mut ship = masses.remove(&self.name).unwrap();
let ship_clone = ship.clone();
- if let MassType::Ship{ref mut engines, ref navigation, ..} = ship.mass_type {
+ 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;
diff --git a/src/server/mining.rs b/src/server/mining.rs
index bfe9223..6213e65 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -1,51 +1,51 @@
extern crate serde_json;
+use std::collections::HashMap;
use std::io::BufRead;
use std::io::Write;
-use std::collections::HashMap;
-use math::distance;
-use mass::{Mass, MassType};
-use modules::navigation::Navigation;
-use server::connection::ServerConnection;
-use modules::mining::{Mining, MiningStatus};
+use crate::mass::{Mass, MassType};
+use crate::math::distance;
+use crate::modules::mining::{Mining, MiningStatus};
+use crate::modules::navigation::Navigation;
+use crate::server::connection::ServerConnection;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MiningData {
- pub has_astroid_target : bool,
+ pub has_astroid_target: bool,
pub astroid_has_minerals: bool,
- pub is_within_range : bool,
- pub status : MiningStatus,
- pub range : f64,
+ pub is_within_range: bool,
+ pub status: MiningStatus,
+ pub range: f64,
}
impl ServerConnection {
- pub fn server_mining(&mut self, masses : &mut HashMap<String, Mass>) {
+ pub fn server_mining(&mut self, masses: &mut HashMap<String, Mass>) {
let mut ship = masses.remove(&self.name).unwrap();
let ship_clone = ship.clone();
let mut item = None;
- 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();
+ if let MassType::Ship {
+ ref mut mining,
+ ref navigation,
+ ..
+ } = ship.mass_type
+ {
+ let mining = mining.as_mut().unwrap();
+ let navigation = navigation.as_ref().unwrap();
let mining_data = get_mining_data(ship_clone, mining, navigation, masses);
- if self.open {
- if self.txrx_mining(&mining_data) {
- mining.toggle();
- }
+ if self.open && self.txrx_mining(&mining_data) {
+ mining.toggle();
}
if !mining_data.is_within_range {
mining.off();
- }
- else {
- if mining.status == MiningStatus::Mined {
- mining.take();
- if let Some(name) = navigation.target_name.clone() {
- let target = masses.get_mut(&name).unwrap();
- item = target.take("Mineral");
- }
+ } else if mining.status == MiningStatus::Mined {
+ mining.take();
+ if let Some(name) = navigation.target_name.clone() {
+ let target = masses.get_mut(&name).unwrap();
+ item = target.take("Mineral");
}
}
}
@@ -60,7 +60,7 @@ impl ServerConnection {
masses.insert(self.name.clone(), ship);
}
- fn txrx_mining(&mut self, mining_data : &MiningData) -> bool {
+ 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;
@@ -73,19 +73,24 @@ impl ServerConnection {
if mining_data.is_within_range {
return true;
}
- },
+ }
_ => {
if result == 0 {
self.open = false;
}
- },
+ }
}
}
false
}
}
-fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, masses : &mut HashMap<String, Mass>) -> MiningData {
+fn get_mining_data(
+ ship: Mass,
+ mining: &Mining,
+ navigation: &Navigation,
+ masses: &mut HashMap<String, Mass>,
+) -> MiningData {
match navigation.target_name.clone() {
Some(name) => {
let target = masses.get(&name);
@@ -95,37 +100,36 @@ fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, mass
Some(target) => {
astroid_has_minerals = target.has_minerals();
match target.mass_type {
- MassType::Astroid{..} => true,
+ MassType::Astroid { .. } => true,
_ => false,
}
- },
+ }
None => false,
};
- let is_within_range = match has_astroid_target {
- true => match target {
+ let is_within_range = if has_astroid_target {
+ match target {
Some(target) => mining.range > distance(ship.position, target.position),
_ => false,
}
- _ => false,
+ } else {
+ false
};
MiningData {
- has_astroid_target : has_astroid_target,
- astroid_has_minerals: astroid_has_minerals,
- is_within_range : is_within_range,
- range : mining.range,
- status : mining.status.clone(),
- }
- }
- _ => {
- MiningData {
- has_astroid_target : false,
- astroid_has_minerals: false,
- is_within_range : false,
- range : mining.range,
- status : mining.status.clone(),
+ has_astroid_target,
+ astroid_has_minerals,
+ is_within_range,
+ range: mining.range,
+ status: mining.status.clone(),
}
}
+ _ => MiningData {
+ has_astroid_target: false,
+ astroid_has_minerals: false,
+ is_within_range: false,
+ range: mining.range,
+ status: mining.status.clone(),
+ },
}
}
diff --git a/src/server/mod.rs b/src/server/mod.rs
index a5c359d..0fec4dc 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -1,7 +1,7 @@
-pub mod mining;
-pub mod engines;
-pub mod refinery;
-pub mod dashboard;
-pub mod navigation;
pub mod connection;
pub mod construction;
+pub mod dashboard;
+pub mod engines;
+pub mod mining;
+pub mod navigation;
+pub mod refinery;
diff --git a/src/server/navigation.rs b/src/server/navigation.rs
index 3eaaa8d..eb9095d 100644
--- a/src/server/navigation.rs
+++ b/src/server/navigation.rs
@@ -1,24 +1,30 @@
extern crate serde_json;
-use std::io::Write;
-use std::io::BufRead;
use std::collections::HashMap;
+use std::io::BufRead;
+use std::io::Write;
-use math::distance;
-use mass::{Mass, MassType};
-use server::connection::ServerConnection;
+use crate::mass::{Mass, MassType};
+use crate::math::distance;
+use crate::server::connection::ServerConnection;
impl ServerConnection {
- pub fn server_navigation(&mut self, masses : &mut HashMap<String, Mass>) {
+ 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 {
- let mut navigation = navigation.as_mut().unwrap();
+ if let MassType::Ship {
+ ref mut navigation, ..
+ } = ship.mass_type
+ {
+ let navigation = navigation.as_mut().unwrap();
navigation.verify_target(ship_clone.position, &masses);
- let mut within_range : HashMap<&String, &Mass> = masses.iter().filter(|&(_, mass)|
- distance(ship_clone.position, mass.position) < navigation.range)
- .collect();
+ let mut within_range: HashMap<&String, &Mass> = masses
+ .iter()
+ .filter(|&(_, mass)| {
+ distance(ship_clone.position, mass.position) < navigation.range
+ })
+ .collect();
within_range.insert(&self.name, &ship_clone);
if self.open {
diff --git a/src/server/refinery.rs b/src/server/refinery.rs
index eb82bb2..d4eb26b 100644
--- a/src/server/refinery.rs
+++ b/src/server/refinery.rs
@@ -1,38 +1,39 @@
extern crate serde_json;
-use std::io::Write;
-use std::io::BufRead;
use std::collections::HashMap;
+use std::io::BufRead;
+use std::io::Write;
-use item::Item;
-use mass::{Mass, MassType};
-use server::connection::ServerConnection;
-use modules::refinery::RefineryStatus;
+use crate::item::Item;
+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_minerals : bool,
- pub status : RefineryStatus,
+ pub has_minerals: bool,
+ pub status: RefineryStatus,
}
impl ServerConnection {
- pub fn server_refinery(&mut self, masses : &mut HashMap<String, Mass>) {
+ pub fn server_refinery(&mut self, masses: &mut HashMap<String, Mass>) {
let mut ship = masses.remove(&self.name).unwrap();
let ship_clone = ship.clone();
let mut refine = false;
- if let MassType::Ship{ref mut refinery, ..} = ship.mass_type {
- let mut refinery = refinery.as_mut().unwrap();
+ if let MassType::Ship {
+ ref mut refinery, ..
+ } = ship.mass_type
+ {
+ let refinery = refinery.as_mut().unwrap();
let refinery_data = RefineryData {
- has_minerals : ship_clone.has_minerals(),
- status : refinery.status.clone(),
+ has_minerals: ship_clone.has_minerals(),
+ status: refinery.status.clone(),
};
- if self.open {
- if self.txrx_refinery(&refinery_data) {
- refinery.toggle();
- }
+ if self.open && self.txrx_refinery(&refinery_data) {
+ refinery.toggle();
}
if !refinery_data.has_minerals {
@@ -44,7 +45,7 @@ impl ServerConnection {
refine = true;
}
}
-
+
if refine {
ship.take("Mineral");
ship.give(Item::new("Refined Mineral", 1));
@@ -53,7 +54,7 @@ impl ServerConnection {
masses.insert(self.name.clone(), ship);
}
- fn txrx_refinery(&mut self, refinery_data : &RefineryData) -> bool {
+ 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;
@@ -64,18 +65,17 @@ impl ServerConnection {
match recv.as_bytes() {
b"R\n" => {
if refinery_data.has_minerals {
- return true
+ return true;
}
- },
+ }
_ => {
if result == 0 {
self.open = false;
}
- },
+ }
}
}
false
}
}
-