summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-06-27 07:51:47 -0500
committertom barrett <spalf0@gmail.com>2018-06-27 07:51:47 -0500
commitae79327d4f0ee6de0ef6b8e3c51299aebfe3bc25 (patch)
treedb6d4b049d94e41989178566a3dca52ab24b5b1e /src/server
parent25d556ad995a83c0c783f0bf9ca555b17cb0bfb0 (diff)
-added elementry construction module, doesnt do anything other than exist as a mass
Diffstat (limited to 'src/server')
-rw-r--r--src/server/connection.rs1
-rw-r--r--src/server/construction.rs81
-rw-r--r--src/server/mining.rs8
-rw-r--r--src/server/mod.rs1
-rw-r--r--src/server/refinery.rs4
5 files changed, 89 insertions, 6 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs
index 80ecde8..973c5d1 100644
--- a/src/server/connection.rs
+++ b/src/server/connection.rs
@@ -50,6 +50,7 @@ impl ServerConnection {
ModuleType::Refinery => self.server_refinery(&mut masses),
ModuleType::Dashboard => self.server_dashboard(&mut masses),
ModuleType::Navigation => self.server_navigation(&mut masses),
+ ModuleType::Construction => self.server_construction(&mut masses),
}
}
}
diff --git a/src/server/construction.rs b/src/server/construction.rs
new file mode 100644
index 0000000..0eb2179
--- /dev/null
+++ b/src/server/construction.rs
@@ -0,0 +1,81 @@
+extern crate serde_json;
+
+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;
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct ConstructionData {
+ pub status : ConstructionStatus,
+ pub has_refined : bool,
+}
+
+impl ServerConnection {
+ pub fn server_construction(&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 construction, ..} = ship.mass_type {
+ let mut 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 construction_data.status == ConstructionStatus::Constructed {
+ construction.take();
+ masses.insert("Station".to_string(), Mass::new_station(ModuleType::Refinery, ship_clone.position, ship_clone.velocity));
+ }
+ }
+
+ masses.insert(self.name.clone(), ship);
+ }
+
+ fn txrx_construction(&mut self, construction_data : &ConstructionData) -> bool {
+ let send = serde_json::to_string(construction_data).unwrap() + "\n";
+ match self.stream.write(send.as_bytes()) {
+ Err(_error) => self.open = false,
+ _ => (),
+ }
+
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(result) => match recv.as_bytes() {
+ b"c\n" => {
+ if construction_data.has_refined {
+ return true
+ }
+ },
+ _ => {
+ if result == 0 {
+ self.open = false;
+ }
+ },
+ }
+ _ => (),
+ }
+
+ false
+ }
+}
+
+fn get_construction_data(ship : Mass, construction : &Construction) -> ConstructionData {
+ let mut has_refined = false;
+ if ship.refined_count() >= 5 {
+ has_refined = true;
+ }
+
+ ConstructionData {
+ status : construction.status.clone(),
+ has_refined : has_refined,
+ }
+}
diff --git a/src/server/mining.rs b/src/server/mining.rs
index 190ca24..115c034 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -32,9 +32,7 @@ impl ServerConnection {
if self.open {
if self.txrx_mining(&mining_data) {
- if mining_data.is_within_range {
- mining.toggle();
- }
+ mining.toggle();
}
}
@@ -47,7 +45,7 @@ impl ServerConnection {
match navigation.target_name.clone() {
Some(name) => {
let target = masses.get_mut(&name).unwrap();
- item = target.take("Iron");
+ item = target.take("Mineral");
}
_ => (),
}
@@ -80,7 +78,9 @@ impl ServerConnection {
match self.buff_r.read_line(&mut recv) {
Ok(result) => match recv.as_bytes() {
b"F\n" => {
+ if mining_data.is_within_range {
return true;
+ }
},
_ => {
if result == 0 {
diff --git a/src/server/mod.rs b/src/server/mod.rs
index 11dfdce..a5c359d 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -4,3 +4,4 @@ pub mod refinery;
pub mod dashboard;
pub mod navigation;
pub mod connection;
+pub mod construction;
diff --git a/src/server/refinery.rs b/src/server/refinery.rs
index 9df078c..1f7628f 100644
--- a/src/server/refinery.rs
+++ b/src/server/refinery.rs
@@ -46,8 +46,8 @@ impl ServerConnection {
}
if refine {
- ship.take("Iron");
- ship.give(Item::new("Refined Iron", 1));
+ ship.take("Mineral");
+ ship.give(Item::new("Refined Mineral", 1));
}
masses.insert(self.name.clone(), ship);