summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/client/engines.rs2
-rw-r--r--src/client/navigation.rs1
-rw-r--r--src/mass.rs39
-rw-r--r--src/modules/construction.rs9
-rw-r--r--src/modules/engines.rs30
-rw-r--r--src/modules/mining.rs9
-rw-r--r--src/modules/navigation.rs6
-rw-r--r--src/modules/refinery.rs9
-rw-r--r--src/modules/tractorbeam.rs9
-rw-r--r--src/server/connection.rs21
-rw-r--r--src/server/construction.rs15
-rw-r--r--src/server/engines.rs25
-rw-r--r--src/server/mining.rs20
-rw-r--r--src/server/navigation.rs11
-rw-r--r--src/server/refinery.rs20
-rw-r--r--src/server/tractorbeam.rs16
16 files changed, 109 insertions, 133 deletions
diff --git a/src/client/engines.rs b/src/client/engines.rs
index 60ffcfd..32d869a 100644
--- a/src/client/engines.rs
+++ b/src/client/engines.rs
@@ -23,7 +23,7 @@ pub fn client_engines(mut stream: TcpStream, mut buff_r: BufReader<TcpStream>) {
writeln!(
stdout,
- "{}{}{}Fuel\nuse numpad to freely move",
+ "{}{}Fuel: {:.2}\nuse numpad to freely move",
termion::clear::All,
termion::cursor::Goto(1, 1),
engines_data.fuel
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
index 1f2ed06..43daafb 100644
--- a/src/client/navigation.rs
+++ b/src/client/navigation.rs
@@ -32,7 +32,6 @@ pub fn client_navigation(name: String, mut stream: TcpStream, mut buff_r: BufRea
let ship = within_range.remove(&name).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!(
diff --git a/src/mass.rs b/src/mass.rs
index 0c19bad..33144ab 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -51,13 +51,13 @@ impl Effects {
pub enum MassType {
Ship {
storage: Storage,
- mining: Option<Mining>,
- engines: Option<Engines>,
- refinery: Option<Refinery>,
- dashboard: Option<Dashboard>,
- navigation: Option<Navigation>,
- tractorbeam: Option<Tractorbeam>,
- construction: Option<Construction>,
+ mining: Mining,
+ engines: Engines,
+ refinery: Refinery,
+ dashboard: Dashboard,
+ navigation: Navigation,
+ tractorbeam: Tractorbeam,
+ construction: Construction,
},
Astroid {
resources: Storage,
@@ -110,13 +110,13 @@ impl Mass {
pub fn new_ship() -> Mass {
let ship = MassType::Ship {
- mining: Some(Mining::new()),
- engines: Some(Engines::new()),
- refinery: Some(Refinery::new()),
- dashboard: Some(Dashboard::new()),
- navigation: Some(Navigation::new()),
- tractorbeam: Some(Tractorbeam::new()),
- construction: Some(Construction::new()),
+ mining: Mining::new(),
+ engines: Engines::new(),
+ refinery: Refinery::new(),
+ dashboard: Dashboard::new(),
+ navigation: Navigation::new(),
+ tractorbeam: Tractorbeam::new(),
+ construction: Construction::new(),
storage: Storage::new(Vec::new(), constants::SHIP_STORAGE_CAPACITY),
};
@@ -170,12 +170,11 @@ impl Mass {
..
} = self.mass_type
{
- mining.as_mut().unwrap().process();
- refinery.as_mut().unwrap().process();
- navigation.as_mut().unwrap().process();
- construction.as_mut().unwrap().process();
- self.effects
- .give_acceleration(engines.as_mut().unwrap().recv_acceleration())
+ mining.process();
+ refinery.process();
+ navigation.process();
+ construction.process();
+ self.effects.give_acceleration(engines.take_acceleration())
}
self.velocity += self.effects.take_acceleration();
diff --git a/src/modules/construction.rs b/src/modules/construction.rs
index 20f1688..6c27db5 100644
--- a/src/modules/construction.rs
+++ b/src/modules/construction.rs
@@ -2,6 +2,7 @@ use std::time::SystemTime;
use crate::constants;
use crate::modules::types::ModuleType;
+use crate::server::construction::ConstructionData;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ConstructionStatus {
@@ -43,6 +44,14 @@ impl Construction {
}
}
+ pub fn give_recv(&mut self, recv: String, construction_data: &ConstructionData) {
+ if let "c" = recv.as_str() {
+ if construction_data.has_enough {
+ self.toggle()
+ }
+ }
+ }
+
pub fn toggle(&mut self) {
match self.status {
ConstructionStatus::None => self.on(),
diff --git a/src/modules/engines.rs b/src/modules/engines.rs
index fa16bf9..ab6335a 100644
--- a/src/modules/engines.rs
+++ b/src/modules/engines.rs
@@ -30,27 +30,15 @@ impl Engines {
}
}
- pub fn recv_acceleration(&mut self) -> Vector {
- let acceleration = self.acceleration.clone();
- self.acceleration = Vector::default();
-
- if self.fuel - acceleration.magnitude() >= 0.0 {
- self.fuel -= acceleration.magnitude();
- acceleration
- } else {
- Vector::default()
- }
- }
-
- pub fn give_client_data(
+ pub fn give_recv(
&mut self,
+ recv: String,
position: Vector,
velocity: Vector,
target: Option<&Mass>,
- data: String,
) {
let mut acceleration = Vector::default();
- match data.as_str() {
+ match recv.as_str() {
"5" => acceleration.x += 0.1,
"0" => acceleration.x -= 0.1,
"8" => acceleration.y += 0.1,
@@ -78,4 +66,16 @@ impl Engines {
}
self.acceleration = acceleration;
}
+
+ pub fn take_acceleration(&mut self) -> Vector {
+ let acceleration = self.acceleration.clone();
+ self.acceleration = Vector::default();
+
+ if self.fuel - acceleration.magnitude() >= 0.0 {
+ self.fuel -= acceleration.magnitude();
+ acceleration
+ } else {
+ Vector::default()
+ }
+ }
}
diff --git a/src/modules/mining.rs b/src/modules/mining.rs
index 1295593..120ba8c 100644
--- a/src/modules/mining.rs
+++ b/src/modules/mining.rs
@@ -1,6 +1,7 @@
use std::time::SystemTime;
use crate::constants;
+use crate::server::mining::MiningData;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum MiningStatus {
@@ -42,6 +43,14 @@ impl Mining {
}
}
+ pub fn give_recv(&mut self, recv: String, mining_data: MiningData) {
+ if !mining_data.is_within_range {
+ self.off();
+ } else if let "F" = recv.as_str() {
+ self.toggle()
+ }
+ }
+
pub fn toggle(&mut self) {
match self.status {
MiningStatus::None => self.on(),
diff --git a/src/modules/navigation.rs b/src/modules/navigation.rs
index 0e010cb..5fcf89f 100644
--- a/src/modules/navigation.rs
+++ b/src/modules/navigation.rs
@@ -47,11 +47,11 @@ impl Navigation {
}
}
- pub fn give_target(&mut self, target_name: String) {
- if !target_name.is_empty() {
+ pub fn give_recv(&mut self, recv: String) {
+ if !recv.is_empty() {
self.start = Some(SystemTime::now());
self.status = NavigationStatus::Targeting;
- self.target_name = Some(target_name);
+ self.target_name = Some(recv);
}
}
diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs
index 5760306..4a4ca85 100644
--- a/src/modules/refinery.rs
+++ b/src/modules/refinery.rs
@@ -1,6 +1,7 @@
use std::time::SystemTime;
use crate::constants;
+use crate::server::refinery::RefineryData;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum RefineryStatus {
@@ -40,6 +41,14 @@ impl Refinery {
}
}
+ pub fn give_recv(&mut self, recv: String, refinery_data: RefineryData) {
+ if !refinery_data.has_crude_minerals {
+ self.off();
+ } else if let "R" = recv.as_str() {
+ self.toggle();
+ }
+ }
+
pub fn toggle(&mut self) {
match self.status {
RefineryStatus::None => self.on(),
diff --git a/src/modules/tractorbeam.rs b/src/modules/tractorbeam.rs
index 445b066..c77ea1f 100644
--- a/src/modules/tractorbeam.rs
+++ b/src/modules/tractorbeam.rs
@@ -32,6 +32,15 @@ impl Tractorbeam {
}
}
+ pub fn give_recv(&mut self, recv: String) {
+ match recv.as_str() {
+ "o" => self.toggle_pull(),
+ "p" => self.toggle_push(),
+ "b" => self.toggle_bring(5.0),
+ _ => (),
+ }
+ }
+
pub fn toggle_pull(&mut self) {
self.status = match self.status {
TractorbeamStatus::None => TractorbeamStatus::Pull,
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() {