summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/client.rs2
-rw-r--r--src/client/construction.rs51
-rw-r--r--src/client/mod.rs1
-rw-r--r--src/item.rs2
-rw-r--r--src/mass.rs33
-rw-r--r--src/modules/construction.rs61
-rw-r--r--src/modules/mining.rs22
-rw-r--r--src/modules/mod.rs1
-rw-r--r--src/modules/refinery.rs21
-rw-r--r--src/modules/types.rs1
-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
-rw-r--r--src/storage.rs6
16 files changed, 261 insertions, 35 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs
index 24a87b8..3353e87 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -17,6 +17,7 @@ use space::client::engines::client_engines;
use space::client::refinery::client_refinery;
use space::client::dashboard::client_dashboard;
use space::client::navigation::client_navigation;
+use space::client::construction::client_construction;
#[derive(Debug, Deserialize)]
struct Config {
@@ -81,5 +82,6 @@ fn main() {
ModuleType::Engines => client_engines(stream, buff_r),
ModuleType::Refinery => client_refinery(stream, buff_r),
ModuleType::Navigation => client_navigation(name, stream, buff_r),
+ ModuleType::Construction => client_construction(stream, buff_r),
}
}
diff --git a/src/client/construction.rs b/src/client/construction.rs
new file mode 100644
index 0000000..7a316c6
--- /dev/null
+++ b/src/client/construction.rs
@@ -0,0 +1,51 @@
+extern crate termion;
+extern crate serde_json;
+
+use std::net::TcpStream;
+use self::termion::async_stdin;
+use std::io::{BufReader, BufRead};
+use std::io::{stdout, Read, Write};
+use self::termion::raw::IntoRawMode;
+
+use server::construction::ConstructionData;
+use modules::construction::ConstructionStatus;
+
+pub fn client_construction(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) {
+ let stdout = stdout();
+ let mut stdout = stdout.lock().into_raw_mode().unwrap();
+ let mut stdin = async_stdin().bytes();
+
+ loop {
+ let mut recv = String::new();
+ buff_r.read_line(&mut recv).unwrap();
+ let data : ConstructionData = serde_json::from_str(&recv.replace("\n", "")).unwrap();
+
+ write!(stdout, "{}", termion::clear::All).unwrap();
+
+ let clear = termion::cursor::Goto(1,1);
+
+ match data.has_refined {
+ true => match data.status {
+ ConstructionStatus::None => write!(stdout, "{}Press c to create a refinery.", clear).unwrap(),
+ _ => write!(stdout, "{}Press c to cancel..", clear).unwrap(),
+ },
+ false => write!(stdout, "{}You need 5 refined minerals to create a refinery.", clear).unwrap(),
+ }
+
+ match stdin.next() {
+ Some(c) => {
+ let c = c.unwrap();
+ let mut send = String::new();
+ send.push(c as char);
+ if send.as_bytes() == b"q" {
+ break;
+ }
+ send.push_str("\n");
+ stream.write(send.as_bytes()).unwrap();
+ }
+ None => ()
+ }
+
+ stdout.flush().unwrap();
+ }
+}
diff --git a/src/client/mod.rs b/src/client/mod.rs
index 8bb0eca..421a394 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -3,3 +3,4 @@ pub mod engines;
pub mod refinery;
pub mod dashboard;
pub mod navigation;
+pub mod construction;
diff --git a/src/item.rs b/src/item.rs
index f9c7490..af5c2f8 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -13,7 +13,7 @@ impl Item {
}
pub fn is_mineral(&self) -> bool {
- if self.name == "Iron" {
+ if self.name == "Mineral" {
true
}
else {
diff --git a/src/mass.rs b/src/mass.rs
index 252de49..f09685f 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -11,6 +11,7 @@ use modules::types::ModuleType;
use modules::refinery::Refinery;
use modules::dashboard::Dashboard;
use modules::navigation::Navigation;
+use modules::construction::Construction;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mass {
@@ -28,13 +29,17 @@ pub enum MassType {
refinery : Option<Refinery>,
dashboard : Option<Dashboard>,
navigation : Option<Navigation>,
+ construction: Option<Construction>,
},
Astroid {
resources : Storage,
},
Item {
item : Item,
- }
+ },
+ Station {
+ module_type : ModuleType,
+ },
}
impl Mass {
@@ -50,7 +55,7 @@ impl Mass {
let mut rr = Range::new(0, 20);
let mut resources = Vec::new();
for _ in 0..rr.sample(&mut rng) {
- resources.push(Item::new("Iron", 1));
+ resources.push(Item::new("Mineral", 1));
}
let astroid = MassType::Astroid {
@@ -71,6 +76,7 @@ impl Mass {
refinery : Some(Refinery::new()),
dashboard : Some(Dashboard::new()),
navigation : Some(Navigation::new()),
+ construction: Some(Construction::new()),
storage : Storage::new(Vec::new()),
};
@@ -89,6 +95,18 @@ impl Mass {
}
}
+ pub fn new_station(module_type : ModuleType, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass {
+ let mass_type = MassType::Station {
+ module_type : module_type
+ };
+
+ Mass {
+ mass_type : mass_type,
+ position : position,
+ velocity : velocity,
+ }
+ }
+
pub fn get_modules(&self) -> Vec<ModuleType> {
let mut modules = Vec::new();
modules.push(ModuleType::Mining);
@@ -96,16 +114,18 @@ impl Mass {
modules.push(ModuleType::Refinery);
modules.push(ModuleType::Dashboard);
modules.push(ModuleType::Navigation);
+ modules.push(ModuleType::Construction);
modules
}
pub fn process(&mut self) {
let mut acceleration = (0.0, 0.0, 0.0);
match self.mass_type {
- MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ..} => {
+ MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ref mut construction, ..} => {
mining.as_mut().unwrap().process();
refinery.as_mut().unwrap().process();
navigation.as_mut().unwrap().process();
+ construction.as_mut().unwrap().process();
acceleration = engines.as_mut().unwrap().recv_acceleration();
},
_ => (),
@@ -130,6 +150,13 @@ impl Mass {
}
}
+ pub fn refined_count(&self) -> usize {
+ match self.mass_type {
+ MassType::Ship{ref storage, ..} => storage.refined_count(),
+ _ => 0,
+ }
+ }
+
pub fn take(&mut self, name : &str) -> Option<Item> {
match self.mass_type {
MassType::Ship{ref mut storage, ..} => storage.take(name),
diff --git a/src/modules/construction.rs b/src/modules/construction.rs
new file mode 100644
index 0000000..22086a5
--- /dev/null
+++ b/src/modules/construction.rs
@@ -0,0 +1,61 @@
+use std::time::SystemTime;
+use modules::types::ModuleType;
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
+pub enum ConstructionStatus {
+ None,
+ Constructing,
+ Constructed,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Construction {
+ pub status : ConstructionStatus,
+ construction : Option<ModuleType>,
+ time : u64,
+ start : Option<SystemTime>,
+}
+
+impl Construction {
+ pub fn new() -> Construction {
+ Construction {
+ status : ConstructionStatus::None,
+ construction : None,
+ time : 5,
+ start : None,
+ }
+ }
+
+ pub fn process(&mut self) {
+ match self.start.clone() {
+ Some(timer) => {
+ if timer.elapsed().unwrap().as_secs() > self.time {
+ self.start = Some(SystemTime::now());
+ self.status = ConstructionStatus::Constructed;
+ }
+ }
+ _ => (),
+ }
+ }
+
+ pub fn toggle(&mut self) {
+ match self.status {
+ ConstructionStatus::None => self.on(),
+ _ => self.off(),
+ };
+ }
+
+ pub fn on(&mut self) {
+ self.start = Some(SystemTime::now());
+ self.status = ConstructionStatus::Constructing;
+ }
+
+ pub fn off(&mut self) {
+ self.start = None;
+ self.status = ConstructionStatus::None;
+ }
+
+ pub fn take(&mut self) {
+ self.status = ConstructionStatus::None;
+ }
+}
diff --git a/src/modules/mining.rs b/src/modules/mining.rs
index 4a1a37f..7a6461a 100644
--- a/src/modules/mining.rs
+++ b/src/modules/mining.rs
@@ -35,30 +35,26 @@ impl Mining {
}
_ => (),
}
- if self.status == MiningStatus::None {
- self.start = None;
- }
}
pub fn toggle(&mut self) {
- self.status = match self.status {
- MiningStatus::None => {
- self.start = Some(SystemTime::now());
- MiningStatus::Mining
- }
- _ => {
- self.start = None;
- MiningStatus::None
- }
+ match self.status {
+ MiningStatus::None => self.on(),
+ _ => self.off(),
};
}
+ pub fn on(&mut self) {
+ self.start = Some(SystemTime::now());
+ self.status = MiningStatus::Mining;
+ }
+
pub fn off(&mut self) {
+ self.start = None;
self.status = MiningStatus::None;
}
pub fn take(&mut self) {
self.status = MiningStatus::Mining;
}
-
}
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index 18cba81..d0ffd38 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -4,3 +4,4 @@ pub mod engines;
pub mod refinery;
pub mod dashboard;
pub mod navigation;
+pub mod construction;
diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs
index 177f2c5..cdb5de0 100644
--- a/src/modules/refinery.rs
+++ b/src/modules/refinery.rs
@@ -33,25 +33,22 @@ impl Refinery {
}
_ => (),
}
- if self.status == RefineryStatus::None {
- self.start = None;
- }
}
pub fn toggle(&mut self) {
- self.status = match self.status {
- RefineryStatus::None => {
- self.start = Some(SystemTime::now());
- RefineryStatus::Refining
- },
- _ => {
- self.start = None;
- RefineryStatus::None
- }
+ match self.status {
+ RefineryStatus::None => self.on(),
+ _ => self.off(),
};
}
+ pub fn on(&mut self) {
+ self.start = Some(SystemTime::now());
+ self.status = RefineryStatus::Refining;
+ }
+
pub fn off(&mut self) {
+ self.start = None;
self.status = RefineryStatus::None;
}
diff --git a/src/modules/types.rs b/src/modules/types.rs
index 48b3473..17e7c6c 100644
--- a/src/modules/types.rs
+++ b/src/modules/types.rs
@@ -5,4 +5,5 @@ pub enum ModuleType {
Refinery,
Dashboard,
Navigation,
+ Construction,
}
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);
diff --git a/src/storage.rs b/src/storage.rs
index 5cae0ff..72a3b27 100644
--- a/src/storage.rs
+++ b/src/storage.rs
@@ -27,6 +27,12 @@ impl Storage {
}
}
+ pub fn refined_count(&self) -> usize {
+ let mut items = self.items.clone();
+ items.retain(|item| item.name == "Refined Mineral");
+ items.len()
+ }
+
pub fn take(&mut self, name : &str) -> Option<Item> {
match self.items.iter().position(|item| item.name == name) {
Some(index) => {