summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bin/client.rs39
-rw-r--r--src/bin/server.rs28
-rw-r--r--src/client/construction.rs57
-rw-r--r--src/client/dashboard.rs28
-rw-r--r--src/client/engines.rs38
-rw-r--r--src/client/mining.rs52
-rw-r--r--src/client/mod.rs8
-rw-r--r--src/client/navigation.rs71
-rw-r--r--src/client/refinery.rs39
-rw-r--r--src/item.rs17
-rw-r--r--src/lib.rs6
-rw-r--r--src/mass.rs168
-rw-r--r--src/math.rs11
-rw-r--r--src/modules/construction.rs28
-rw-r--r--src/modules/dashboard.rs2
-rw-r--r--src/modules/engines.rs66
-rw-r--r--src/modules/mining.rs26
-rw-r--r--src/modules/mod.rs10
-rw-r--r--src/modules/navigation.rs44
-rw-r--r--src/modules/refinery.rs22
-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
-rw-r--r--src/storage.rs38
29 files changed, 622 insertions, 504 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs
index 3353e87..e452492 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -1,29 +1,29 @@
+extern crate serde_json;
extern crate space;
extern crate toml;
-extern crate serde_json;
#[macro_use]
extern crate serde_derive;
-use std::io;
use std::fs::File;
-use std::io::BufReader;
+use std::io;
use std::io::prelude::*;
+use std::io::BufReader;
use std::net::TcpStream;
-use space::modules::types::ModuleType;
-use space::client::mining::client_mining;
-use space::client::engines::client_engines;
-use space::client::refinery::client_refinery;
+use space::client::construction::client_construction;
use space::client::dashboard::client_dashboard;
+use space::client::engines::client_engines;
+use space::client::mining::client_mining;
use space::client::navigation::client_navigation;
-use space::client::construction::client_construction;
+use space::client::refinery::client_refinery;
+use space::modules::types::ModuleType;
#[derive(Debug, Deserialize)]
struct Config {
- username : Option<String>,
- password : Option<String>,
- server : Option<String>,
+ username: Option<String>,
+ password: Option<String>,
+ server: Option<String>,
}
fn main() {
@@ -35,12 +35,12 @@ fn main() {
Ok(mut config_file) => {
let mut config_string = String::new();
config_file.read_to_string(&mut config_string).unwrap();
- let config : Config = toml::from_str(&config_string).unwrap();
+ let config: Config = toml::from_str(&config_string).unwrap();
server = config.server.unwrap();
name = config.username.unwrap();
send = name.clone() + ":" + &config.password.unwrap() + "\n";
- },
+ }
Err(_err) => {
println!("Ship Name:");
io::stdin().read_line(&mut name).expect("Failed");
@@ -52,17 +52,17 @@ fn main() {
server = "localhost:6000".to_string();
send = name.clone() + ":" + &password;
- },
+ }
}
let mut stream = TcpStream::connect(&server).unwrap();
let mut buff_r = BufReader::new(stream.try_clone().unwrap());
- 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 modules : Vec<ModuleType> = 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.iter().enumerate() {
@@ -71,10 +71,13 @@ fn main() {
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed");
- let module_type = modules.into_iter().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_type).unwrap() + "\n";
- stream.write(send.as_bytes()).unwrap();
+ stream.write_all(send.as_bytes()).unwrap();
match module_type {
ModuleType::Dashboard => client_dashboard(buff_r),
diff --git a/src/bin/server.rs b/src/bin/server.rs
index e20acf6..ea961b7 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -1,16 +1,16 @@
extern crate space;
+use std::collections::HashMap;
+use std::net::TcpListener;
use std::thread::sleep;
use std::time::Duration;
-use std::net::TcpListener;
-use std::collections::HashMap;
use space::mass::Mass;
use space::math::rand_name;
use space::server::connection::ServerConnection;
fn populate() -> HashMap<String, Mass> {
- let mut masses : HashMap<String, Mass> = HashMap::new();
+ let mut masses: HashMap<String, Mass> = HashMap::new();
for _ in 0..10 {
masses.insert(rand_name(), Mass::new_astroid());
@@ -25,23 +25,23 @@ fn main() {
let mut masses = populate();
- let mut connections : Vec<ServerConnection> = Vec::new();
+ let mut connections: Vec<ServerConnection> = Vec::new();
for stream in listener.incoming() {
match stream {
Ok(stream) => {
let new_connection = ServerConnection::new(stream, &mut masses);
- let exists = connections.iter().position(|connection|
- connection.name == new_connection.name &&
- connection.module_type == new_connection.module_type);
- match exists {
- Some(index) => { connections.remove(index); },
- _ => (),
+ let exists = connections.iter().position(|connection| {
+ connection.name == new_connection.name
+ && connection.module_type == new_connection.module_type
+ });
+ if let Some(index) = exists {
+ connections.remove(index);
}
connections.push(new_connection);
- },
+ }
_ => {
- for i in 0..connections.len() {
- connections[i].process(&mut masses);
+ for connection in &mut connections {
+ connection.process(&mut masses);
}
for mass in masses.values_mut() {
@@ -49,7 +49,7 @@ fn main() {
}
sleep(Duration::from_millis(100));
- },
+ }
}
}
}
diff --git a/src/client/construction.rs b/src/client/construction.rs
index 7a316c6..b034b77 100644
--- a/src/client/construction.rs
+++ b/src/client/construction.rs
@@ -1,16 +1,16 @@
-extern crate termion;
extern crate serde_json;
+extern crate termion;
-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 std::io::{stdout, Read, Write};
+use std::io::{BufRead, BufReader};
+use std::net::TcpStream;
-use server::construction::ConstructionData;
-use modules::construction::ConstructionStatus;
+use crate::modules::construction::ConstructionStatus;
+use crate::server::construction::ConstructionData;
-pub fn client_construction(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) {
+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();
@@ -18,32 +18,37 @@ pub fn client_construction(mut stream : TcpStream, mut buff_r : BufReader<TcpStr
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let data : ConstructionData = serde_json::from_str(&recv.replace("\n", "")).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);
+ 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(),
+ if data.has_refined {
+ match data.status {
+ ConstructionStatus::None => {
+ write!(stdout, "{}Press c to create a refinery.", clear).unwrap()
+ }
+ _ => write!(stdout, "{}Press c to cancel..", clear).unwrap(),
+ }
+ } else {
+ 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();
+ if let Some(c) = stdin.next() {
+ let c = c.unwrap();
+ let mut send = String::new();
+ send.push(c as char);
+ if send.as_bytes() == b"q" {
+ break;
}
- None => ()
+ send.push_str("\n");
+ stream.write_all(send.as_bytes()).unwrap();
}
stdout.flush().unwrap();
diff --git a/src/client/dashboard.rs b/src/client/dashboard.rs
index 5815ee2..ec1fd4b 100644
--- a/src/client/dashboard.rs
+++ b/src/client/dashboard.rs
@@ -1,14 +1,14 @@
-extern crate termion;
extern crate serde_json;
+extern crate termion;
-use std::net::TcpStream;
-use std::io::{BufRead, BufReader, stdout, Write, Read};
-use self::termion::raw::IntoRawMode;
use self::termion::async_stdin;
+use self::termion::raw::IntoRawMode;
+use std::io::{stdout, BufRead, BufReader, Read, Write};
+use std::net::TcpStream;
-use mass::Mass;
+use crate::mass::Mass;
-pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) {
+pub fn client_dashboard(mut buff_r: BufReader<TcpStream>) {
let stdout = stdout();
let mut stdout = stdout.lock().into_raw_mode().unwrap();
let mut stdin = async_stdin().bytes();
@@ -16,15 +16,19 @@ pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) {
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let ship : Mass = serde_json::from_str(&recv).unwrap();
+ let ship: Mass = serde_json::from_str(&recv).unwrap();
- write!(stdout, "{}{}{:?}",
- termion::clear::All,
- termion::cursor::Goto(1,1),
- ship).unwrap();
+ write!(
+ stdout,
+ "{}{}{:?}",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1),
+ ship
+ )
+ .unwrap();
if let Some(c) = stdin.next() {
- let c = c.unwrap() as char;
+ let c = c.unwrap() as char;
if c == 'q' {
break;
}
diff --git a/src/client/engines.rs b/src/client/engines.rs
index 9b2d314..8f83998 100644
--- a/src/client/engines.rs
+++ b/src/client/engines.rs
@@ -1,15 +1,15 @@
-extern crate termion;
extern crate serde_json;
+extern crate termion;
-use std::thread::sleep;
-use std::time::Duration;
-use std::net::TcpStream;
use self::termion::async_stdin;
-use std::io::{BufRead, BufReader};
-use std::io::{Read, Write, stdout};
use self::termion::raw::IntoRawMode;
+use std::io::{stdout, Read, Write};
+use std::io::{BufRead, BufReader};
+use std::net::TcpStream;
+use std::thread::sleep;
+use std::time::Duration;
-pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) {
+pub fn client_engines(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();
@@ -19,15 +19,31 @@ pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>)
buff_r.read_line(&mut recv).unwrap();
let has_target = serde_json::from_str(&recv.replace("\n", "")).unwrap();
- write!(stdout, "{}{}use numpad to freely move\n", termion::clear::All, termion::cursor::Goto(1, 1)).unwrap();
+ writeln!(
+ stdout,
+ "{}{}use numpad to freely move",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1)
+ )
+ .unwrap();
write!(stdout, "{}+ : speedup", termion::cursor::Goto(1, 2)).unwrap();
write!(stdout, "{}- : slowdown", termion::cursor::Goto(1, 3)).unwrap();
write!(stdout, "{}s : stop", termion::cursor::Goto(1, 4)).unwrap();
write!(stdout, "{}q : quit", termion::cursor::Goto(1, 5)).unwrap();
if has_target {
- write!(stdout, "{}c : mimic targets velocity vector", termion::cursor::Goto(1, 6)).unwrap();
- write!(stdout, "{}t : accelerate torwards target", termion::cursor::Goto(1, 7)).unwrap();
+ write!(
+ stdout,
+ "{}c : mimic targets velocity vector",
+ termion::cursor::Goto(1, 6)
+ )
+ .unwrap();
+ write!(
+ stdout,
+ "{}t : accelerate torwards target",
+ termion::cursor::Goto(1, 7)
+ )
+ .unwrap();
}
if let Some(c) = stdin.next() {
@@ -38,7 +54,7 @@ pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>)
break;
}
send.push_str("\n");
- stream.write(send.as_bytes()).unwrap();
+ stream.write_all(send.as_bytes()).unwrap();
}
stdout.flush().unwrap();
diff --git a/src/client/mining.rs b/src/client/mining.rs
index 221f7ce..5515f3a 100644
--- a/src/client/mining.rs
+++ b/src/client/mining.rs
@@ -1,16 +1,16 @@
-extern crate termion;
extern crate serde_json;
+extern crate termion;
-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 std::io::{stdout, Read, Write};
+use std::io::{BufRead, BufReader};
+use std::net::TcpStream;
-use server::mining::MiningData;
-use modules::mining::MiningStatus;
+use crate::modules::mining::MiningStatus;
+use crate::server::mining::MiningData;
-pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) {
+pub fn client_mining(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();
@@ -18,24 +18,34 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>)
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let data : MiningData = serde_json::from_str(&recv.replace("\n", "")).unwrap();
+ let data: MiningData = serde_json::from_str(&recv.replace("\n", "")).unwrap();
write!(stdout, "{}", termion::clear::All).unwrap();
- let clear = termion::cursor::Goto(1,1);
+ let clear = termion::cursor::Goto(1, 1);
- match data.has_astroid_target {
- true => match data.is_within_range {
- true => match data.astroid_has_minerals {
- true => match data.status {
- MiningStatus::None => write!(stdout, "{}Press F to begin mining.", clear).unwrap(),
- _ => write!(stdout, "{}Press F to stop mining.", clear).unwrap(),
- },
- false => write!(stdout, "{}Astroid has ran out of minerals.", clear).unwrap(),
+ if data.has_astroid_target {
+ if data.is_within_range {
+ if data.astroid_has_minerals {
+ match data.status {
+ MiningStatus::None => {
+ write!(stdout, "{}Press F to begin mining.", clear).unwrap()
+ }
+ _ => write!(stdout, "{}Press F to stop mining.", clear).unwrap(),
+ };
+ } else {
+ write!(stdout, "{}Astroid has ran out of minerals.", clear).unwrap();
}
- false => write!(stdout, "{}Astroid must be within range of {}.", clear, data.range).unwrap(),
- },
- false => write!(stdout, "{}Ship has no astroid targeted.", clear).unwrap(),
+ } else {
+ write!(
+ stdout,
+ "{}Astroid must be within range of {}.",
+ clear, data.range
+ )
+ .unwrap();
+ }
+ } else {
+ write!(stdout, "{}Ship has no astroid targeted.", clear).unwrap();
}
if let Some(c) = stdin.next() {
@@ -46,7 +56,7 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>)
break;
}
send.push_str("\n");
- stream.write(send.as_bytes()).unwrap();
+ stream.write_all(send.as_bytes()).unwrap();
}
stdout.flush().unwrap();
diff --git a/src/client/mod.rs b/src/client/mod.rs
index 421a394..530e2de 100644
--- a/src/client/mod.rs
+++ b/src/client/mod.rs
@@ -1,6 +1,6 @@
-pub mod mining;
-pub mod engines;
-pub mod refinery;
+pub mod construction;
pub mod dashboard;
+pub mod engines;
+pub mod mining;
pub mod navigation;
-pub mod construction;
+pub mod refinery;
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
index 34e1b7a..0ab25c6 100644
--- a/src/client/navigation.rs
+++ b/src/client/navigation.rs
@@ -1,18 +1,18 @@
-extern crate termion;
extern crate serde_json;
+extern crate termion;
-use std::net::TcpStream;
-use std::collections::BTreeMap;
use self::termion::async_stdin;
-use std::io::{BufRead, BufReader};
-use std::io::{stdout, Read, Write};
use self::termion::raw::IntoRawMode;
+use std::collections::BTreeMap;
+use std::io::{stdout, Read, Write};
+use std::io::{BufRead, BufReader};
+use std::net::TcpStream;
-use math::distance;
-use mass::{Mass, MassType};
-use modules::navigation::Navigation;
+use crate::mass::{Mass, MassType};
+use crate::math::distance;
+use crate::modules::navigation::Navigation;
-pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){
+pub fn client_navigation(name: String, 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();
@@ -20,42 +20,48 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let mut within_range : BTreeMap<String, Mass> = serde_json::from_str(&recv).unwrap();
+ let mut within_range: BTreeMap<String, Mass> = serde_json::from_str(&recv).unwrap();
- write!(stdout, "{}{}Targets:",
- termion::clear::All,
- termion::cursor::Goto(1,1)).unwrap();
+ write!(
+ stdout,
+ "{}{}Targets:",
+ termion::clear::All,
+ termion::cursor::Goto(1, 1)
+ )
+ .unwrap();
let ship = within_range.remove(&name).unwrap();
- if let MassType::Ship{ref navigation, ..} = ship.mass_type {
+ 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();
+ 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 Some(c) = stdin.next() {
let c = c.unwrap() as char;
if c == 'q' {
break;
- }
- else {
+ } 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();
+ stream.write_all(send.as_bytes()).unwrap();
}
}
}
@@ -64,14 +70,15 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
}
}
-fn get_target_status(navigation : &Navigation, mass_name : &String) -> String {
+fn get_target_status(navigation: &Navigation, mass_name: &str) -> String {
match navigation.target_name.clone() {
Some(name) => {
- match &name == mass_name {
- true => serde_json::to_string(&navigation.status).unwrap(),
- false => String::new()
+ if name == mass_name {
+ serde_json::to_string(&navigation.status).unwrap()
+ } else {
+ String::new()
}
- },
+ }
_ => String::new(),
}
}
diff --git a/src/client/refinery.rs b/src/client/refinery.rs
index 013172d..caceda7 100644
--- a/src/client/refinery.rs
+++ b/src/client/refinery.rs
@@ -1,16 +1,16 @@
-extern crate termion;
extern crate serde_json;
+extern crate termion;
-use std::net::TcpStream;
use self::termion::async_stdin;
-use std::io::{BufRead, BufReader};
-use std::io::{stdout, Read, Write};
use self::termion::raw::IntoRawMode;
+use std::io::{stdout, Read, Write};
+use std::io::{BufRead, BufReader};
+use std::net::TcpStream;
-use server::refinery::RefineryData;
-use modules::refinery::RefineryStatus;
+use crate::modules::refinery::RefineryStatus;
+use crate::server::refinery::RefineryData;
-pub fn client_refinery(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>) {
+pub fn client_refinery(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();
@@ -18,21 +18,24 @@ pub fn client_refinery(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let data : RefineryData = serde_json::from_str(&recv.replace("\n", "")).unwrap();
+ let data: RefineryData = serde_json::from_str(&recv.replace("\n", "")).unwrap();
write!(stdout, "{}", termion::clear::All).unwrap();
- let clear = termion::cursor::Goto(1,1);
-
- match data.has_minerals {
- true => match data.status {
- RefineryStatus::None => write!(stdout, "{}Press R to begin refining.", clear).unwrap(),
- _ => write!(stdout, "{}Press R to stop refining.", clear).unwrap(),
- },
- false => write!(stdout, "{}You have no refinable minerals.", clear).unwrap(),
+ let clear = termion::cursor::Goto(1, 1);
+
+ if data.has_minerals {
+ match data.status {
+ RefineryStatus::None => {
+ write!(stdout, "{}Press R to begin refining.", clear).unwrap()
+ }
+ _ => write!(stdout, "{}Press R to stop refining.", clear).unwrap(),
+ };
+ } else {
+ write!(stdout, "{}You have no refinable minerals.", clear).unwrap();
}
- if let Some(c) = stdin.next() {
+ if let Some(c) = stdin.next() {
let c = c.unwrap();
let mut send = String::new();
send.push(c as char);
@@ -40,7 +43,7 @@ pub fn client_refinery(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>
break;
}
send.push_str("\n");
- stream.write(send.as_bytes()).unwrap();
+ stream.write_all(send.as_bytes()).unwrap();
}
stdout.flush().unwrap();
diff --git a/src/item.rs b/src/item.rs
index af5c2f8..c51b1a2 100644
--- a/src/item.rs
+++ b/src/item.rs
@@ -1,23 +1,18 @@
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Item {
- pub name : String,
- pub size : usize,
+ pub name: String,
+ pub size: usize,
}
impl Item {
- pub fn new(name : &str, size : usize) -> Item {
+ pub fn new(name: &str, size: usize) -> Item {
Item {
- name : String::from(name),
- size : size,
+ name: String::from(name),
+ size,
}
}
pub fn is_mineral(&self) -> bool {
- if self.name == "Mineral" {
- true
- }
- else {
- false
- }
+ self.name == "Mineral"
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 0c917f7..406bab9 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,10 +1,10 @@
#[macro_use]
extern crate serde_derive;
-pub mod mass;
+pub mod client;
pub mod item;
+pub mod mass;
pub mod math;
-pub mod client;
-pub mod server;
pub mod modules;
+pub mod server;
pub mod storage;
diff --git a/src/mass.rs b/src/mass.rs
index f09685f..db97d49 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,44 +1,44 @@
extern crate rand;
-use self::rand::distributions::Range;
-use self::rand::distributions::Sample;
-
-use item::Item;
-use storage::Storage;
-use modules::mining::Mining;
-use modules::engines::Engines;
-use modules::types::ModuleType;
-use modules::refinery::Refinery;
-use modules::dashboard::Dashboard;
-use modules::navigation::Navigation;
-use modules::construction::Construction;
+use self::rand::distributions::Uniform;
+use self::rand::Rng;
+
+use crate::item::Item;
+use crate::modules::construction::Construction;
+use crate::modules::dashboard::Dashboard;
+use crate::modules::engines::Engines;
+use crate::modules::mining::Mining;
+use crate::modules::navigation::Navigation;
+use crate::modules::refinery::Refinery;
+use crate::modules::types::ModuleType;
+use crate::storage::Storage;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mass {
- pub mass_type : MassType,
- pub position : (f64, f64, f64),
- pub velocity : (f64, f64, f64),
+ pub mass_type: MassType,
+ pub position: (f64, f64, f64),
+ pub velocity: (f64, f64, f64),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
Ship {
- storage : Storage,
- mining : Option<Mining>,
- engines : Option<Engines>,
- refinery : Option<Refinery>,
- dashboard : Option<Dashboard>,
- navigation : Option<Navigation>,
+ storage: Storage,
+ mining: Option<Mining>,
+ engines: Option<Engines>,
+ refinery: Option<Refinery>,
+ dashboard: Option<Dashboard>,
+ navigation: Option<Navigation>,
construction: Option<Construction>,
},
Astroid {
- resources : Storage,
+ resources: Storage,
},
Item {
- item : Item,
+ item: Item,
},
Station {
- module_type : ModuleType,
+ module_type: ModuleType,
},
}
@@ -46,64 +46,73 @@ impl Mass {
pub fn new_astroid() -> Mass {
let mut rng = rand::thread_rng();
- let mut pr = Range::new(-50.0, 50.0);
- let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng));
+ let p_range = Uniform::new(-50.0, 50.0);
+ let position = (
+ rng.sample(p_range),
+ rng.sample(p_range),
+ rng.sample(p_range),
+ );
- let mut vr = Range::new(-0.5, 0.5);
- let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng));
+ let v_range = Uniform::new(-0.5, 0.5);
+ let velocity = (
+ rng.sample(v_range),
+ rng.sample(v_range),
+ rng.sample(v_range),
+ );
- let mut rr = Range::new(0, 20);
let mut resources = Vec::new();
- for _ in 0..rr.sample(&mut rng) {
+ for _ in 0..rng.gen_range(0, 20) {
resources.push(Item::new("Mineral", 1));
}
let astroid = MassType::Astroid {
- resources : Storage::new(resources),
+ resources: Storage::new(resources),
};
Mass {
- mass_type : astroid,
- position : position,
- velocity : velocity,
+ mass_type: astroid,
+ position,
+ velocity,
}
}
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()),
+ mining: Some(Mining::new()),
+ engines: Some(Engines::new()),
+ refinery: Some(Refinery::new()),
+ dashboard: Some(Dashboard::new()),
+ navigation: Some(Navigation::new()),
construction: Some(Construction::new()),
- storage : Storage::new(Vec::new()),
+ storage: Storage::new(Vec::new()),
};
Mass {
- mass_type : ship,
- position : (0.0, 0.0, 0.0),
- velocity : (0.0, 0.0, 0.0),
+ mass_type: ship,
+ position: (0.0, 0.0, 0.0),
+ velocity: (0.0, 0.0, 0.0),
}
}
- pub fn new_item(item : Item, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass {
+ pub fn new_item(item: Item, position: (f64, f64, f64), velocity: (f64, f64, f64)) -> Mass {
Mass {
- mass_type : MassType::Item{item : item},
- position : position,
- velocity : velocity,
+ mass_type: MassType::Item { item },
+ position,
+ velocity,
}
}
- 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
- };
+ pub fn new_station(
+ module_type: ModuleType,
+ position: (f64, f64, f64),
+ velocity: (f64, f64, f64),
+ ) -> Mass {
+ let mass_type = MassType::Station { module_type };
Mass {
- mass_type : mass_type,
- position : position,
- velocity : velocity,
+ mass_type,
+ position,
+ velocity,
}
}
@@ -120,15 +129,20 @@ impl Mass {
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, 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();
- },
- _ => (),
+ if let MassType::Ship {
+ ref mut navigation,
+ ref mut engines,
+ ref mut mining,
+ ref mut refinery,
+ ref mut construction,
+ ..
+ } = self.mass_type
+ {
+ 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();
}
self.accelerate(acceleration);
self.position.0 += self.velocity.0;
@@ -136,7 +150,7 @@ impl Mass {
self.position.2 += self.velocity.2;
}
- pub fn accelerate(&mut self, acceleration : (f64, f64, f64)) {
+ pub fn accelerate(&mut self, acceleration: (f64, f64, f64)) {
self.velocity.0 += acceleration.0;
self.velocity.1 += acceleration.1;
self.velocity.2 += acceleration.2;
@@ -144,31 +158,39 @@ impl Mass {
pub fn has_minerals(&self) -> bool {
match self.mass_type {
- MassType::Ship{ref storage, ..} => storage.has_minerals(),
- MassType::Astroid{ref resources, ..} => resources.has_minerals(),
+ MassType::Ship { ref storage, .. } => storage.has_minerals(),
+ MassType::Astroid { ref resources, .. } => resources.has_minerals(),
_ => false,
}
}
pub fn refined_count(&self) -> usize {
match self.mass_type {
- MassType::Ship{ref storage, ..} => storage.refined_count(),
+ MassType::Ship { ref storage, .. } => storage.refined_count(),
_ => 0,
}
}
- pub fn take(&mut self, name : &str) -> Option<Item> {
+ pub fn take(&mut self, name: &str) -> Option<Item> {
match self.mass_type {
- MassType::Ship{ref mut storage, ..} => storage.take(name),
- MassType::Astroid{ref mut resources, ..} => resources.take(name),
+ MassType::Ship {
+ ref mut storage, ..
+ } => storage.take(name),
+ MassType::Astroid {
+ ref mut resources, ..
+ } => resources.take(name),
_ => None,
}
}
- pub fn give(&mut self, item : Item) -> bool {
+ pub fn give(&mut self, item: Item) -> bool {
match self.mass_type {
- MassType::Ship{ref mut storage, ..} => storage.give(item),
- MassType::Astroid{ref mut resources, ..} => resources.give(item),
+ MassType::Ship {
+ ref mut storage, ..
+ } => storage.give(item),
+ MassType::Astroid {
+ ref mut resources, ..
+ } => resources.give(item),
_ => false,
}
}
diff --git a/src/math.rs b/src/math.rs
index 127d4b4..689ee65 100644
--- a/src/math.rs
+++ b/src/math.rs
@@ -1,11 +1,16 @@
extern crate rand;
+use self::rand::distributions::Alphanumeric;
use self::rand::Rng;
+use std::iter::repeat;
-pub fn distance(l0 : (f64, f64, f64), l1 : (f64, f64, f64)) -> f64 {
- (((l1.0-l0.0).powf(2.0) + (l1.1-l0.1).powf(2.0) + (l1.2-l0.2).powf(2.0))).sqrt()
+pub fn distance(l0: (f64, f64, f64), l1: (f64, f64, f64)) -> f64 {
+ ((l1.0 - l0.0).powf(2.0) + (l1.1 - l0.1).powf(2.0) + (l1.2 - l0.2).powf(2.0)).sqrt()
}
pub fn rand_name() -> String {
- rand::thread_rng().gen_ascii_chars().take(8).collect()
+ repeat(())
+ .map(|()| rand::thread_rng().sample(Alphanumeric))
+ .take(8)
+ .collect()
}
diff --git a/src/modules/construction.rs b/src/modules/construction.rs
index 46ddbb1..f55a2fb 100644
--- a/src/modules/construction.rs
+++ b/src/modules/construction.rs
@@ -1,5 +1,5 @@
+use crate::modules::types::ModuleType;
use std::time::SystemTime;
-use modules::types::ModuleType;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ConstructionStatus {
@@ -8,26 +8,32 @@ pub enum ConstructionStatus {
Constructed,
}
-#[derive(Serialize, Deserialize, Debug, Clone)]
+impl Default for ConstructionStatus {
+ fn default() -> Self {
+ ConstructionStatus::None
+ }
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Construction {
- pub status : ConstructionStatus,
- construction : Option<ModuleType>,
- time : u64,
- start : Option<SystemTime>,
+ 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,
+ status: ConstructionStatus::None,
+ construction: None,
+ time: 5,
+ start: None,
}
}
pub fn process(&mut self) {
- if let Some(timer) = self.start.clone() {
+ if let Some(timer) = self.start {
if timer.elapsed().unwrap().as_secs() > self.time {
self.start = Some(SystemTime::now());
self.status = ConstructionStatus::Constructed;
diff --git a/src/modules/dashboard.rs b/src/modules/dashboard.rs
index e7f739b..acc1fb8 100644
--- a/src/modules/dashboard.rs
+++ b/src/modules/dashboard.rs
@@ -1,4 +1,4 @@
-#[derive(Serialize, Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Dashboard {}
impl Dashboard {
diff --git a/src/modules/engines.rs b/src/modules/engines.rs
index 4741a03..a930847 100644
--- a/src/modules/engines.rs
+++ b/src/modules/engines.rs
@@ -1,14 +1,14 @@
-use mass::Mass;
+use crate::mass::Mass;
-#[derive(Serialize, Deserialize, Debug, Clone)]
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Engines {
- acceleration : (f64, f64, f64),
+ acceleration: (f64, f64, f64),
}
impl Engines {
pub fn new() -> Engines {
Engines {
- acceleration : (0.0, 0.0, 0.0)
+ acceleration: (0.0, 0.0, 0.0),
}
}
@@ -18,7 +18,7 @@ impl Engines {
acceleration
}
- pub fn give_client_data(&mut self, ship : &Mass, target : Option<&Mass>, data : String) {
+ 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,
@@ -29,46 +29,38 @@ impl Engines {
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);
- },
+ 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);
- },
+ acceleration = (
+ -1.0 * m_v.0 * 0.05,
+ -1.0 * m_v.1 * 0.05,
+ -1.0 * m_v.2 * 0.05,
+ );
+ }
b"s\n" => {
let m_v = ship.velocity;
- acceleration = (-1.0 * m_v.0,
- -1.0 * m_v.1,
- -1.0 * m_v.2);
- },
+ acceleration = (-1.0 * m_v.0, -1.0 * m_v.1, -1.0 * m_v.2);
+ }
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 => (),
+ if let Some(target) = 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);
}
- },
+ }
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 => (),
+ if let Some(target) = 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,
+ );
}
- },
+ }
_ => (),
}
self.acceleration = acceleration;
diff --git a/src/modules/mining.rs b/src/modules/mining.rs
index 3c091b9..b923480 100644
--- a/src/modules/mining.rs
+++ b/src/modules/mining.rs
@@ -7,26 +7,32 @@ pub enum MiningStatus {
Mined,
}
-#[derive(Serialize, Deserialize, Debug, Clone)]
+impl Default for MiningStatus {
+ fn default() -> Self {
+ MiningStatus::None
+ }
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Mining {
- pub range : f64,
- pub status : MiningStatus,
- time : u64,
- start : Option<SystemTime>,
+ pub range: f64,
+ pub status: MiningStatus,
+ time: u64,
+ start: Option<SystemTime>,
}
impl Mining {
pub fn new() -> Mining {
Mining {
- range : 10.0,
- status : MiningStatus::None,
- time : 5,
- start : None,
+ range: 10.0,
+ status: MiningStatus::None,
+ time: 5,
+ start: None,
}
}
pub fn process(&mut self) {
- if let Some(timer) = self.start.clone() {
+ if let Some(timer) = self.start {
if timer.elapsed().unwrap().as_secs() > self.time {
self.status = MiningStatus::Mined;
self.start = Some(SystemTime::now());
diff --git a/src/modules/mod.rs b/src/modules/mod.rs
index d0ffd38..3929e24 100644
--- a/src/modules/mod.rs
+++ b/src/modules/mod.rs
@@ -1,7 +1,7 @@
-pub mod types;
-pub mod mining;
-pub mod engines;
-pub mod refinery;
+pub mod construction;
pub mod dashboard;
+pub mod engines;
+pub mod mining;
pub mod navigation;
-pub mod construction;
+pub mod refinery;
+pub mod types;
diff --git a/src/modules/navigation.rs b/src/modules/navigation.rs
index 84f315c..11f3e94 100644
--- a/src/modules/navigation.rs
+++ b/src/modules/navigation.rs
@@ -1,8 +1,8 @@
-use std::time::SystemTime;
use std::collections::HashMap;
+use std::time::SystemTime;
-use mass::Mass;
-use math::distance;
+use crate::mass::Mass;
+use crate::math::distance;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum NavigationStatus {
@@ -11,28 +11,34 @@ pub enum NavigationStatus {
Targeted,
}
-#[derive(Serialize, Deserialize, Debug, Clone)]
+impl Default for NavigationStatus {
+ fn default() -> Self {
+ NavigationStatus::None
+ }
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Navigation {
- pub range : f64,
- pub status : NavigationStatus,
- pub target_name : Option<String>,
- time : u64,
- start : Option<SystemTime>,
+ pub range: f64,
+ pub status: NavigationStatus,
+ pub target_name: Option<String>,
+ time: u64,
+ start: Option<SystemTime>,
}
impl Navigation {
pub fn new() -> Navigation {
Navigation {
- target_name : None,
- range : 100.0,
- status : NavigationStatus::None,
- time : 3,
- start : None,
+ target_name: None,
+ range: 100.0,
+ status: NavigationStatus::None,
+ time: 3,
+ start: None,
}
}
pub fn process(&mut self) {
- if let Some(timer) = self.start.clone() {
+ if let Some(timer) = self.start {
if timer.elapsed().unwrap().as_secs() > self.time {
self.status = NavigationStatus::Targeted;
self.start = None;
@@ -40,13 +46,17 @@ impl Navigation {
}
}
- pub fn give_target(&mut self, target_name : String) {
+ 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>) {
+ pub fn verify_target(
+ &mut self,
+ ship_position: (f64, f64, f64),
+ masses: &HashMap<String, Mass>,
+ ) {
if let Some(name) = self.target_name.clone() {
let target = masses.get(&name).unwrap();
if distance(target.position, ship_position) > self.range {
diff --git a/src/modules/refinery.rs b/src/modules/refinery.rs
index 5100f3d..5fdc10c 100644
--- a/src/modules/refinery.rs
+++ b/src/modules/refinery.rs
@@ -7,24 +7,30 @@ pub enum RefineryStatus {
Refined,
}
-#[derive(Serialize, Deserialize, Debug, Clone)]
+impl Default for RefineryStatus {
+ fn default() -> Self {
+ RefineryStatus::None
+ }
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Refinery {
- time : u64,
- start : Option<SystemTime>,
- pub status : RefineryStatus,
+ time: u64,
+ start: Option<SystemTime>,
+ pub status: RefineryStatus,
}
impl Refinery {
pub fn new() -> Refinery {
Refinery {
- time : 5,
- start : None,
- status : RefineryStatus::None,
+ time: 5,
+ start: None,
+ status: RefineryStatus::None,
}
}
pub fn process(&mut self) {
- if let Some(timer) = self.start.clone() {
+ if let Some(timer) = self.start {
if timer.elapsed().unwrap().as_secs() > self.time {
self.status = RefineryStatus::Refined;
self.start = Some(SystemTime::now());
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
}
}
-
diff --git a/src/storage.rs b/src/storage.rs
index 72a3b27..f43bec4 100644
--- a/src/storage.rs
+++ b/src/storage.rs
@@ -1,30 +1,27 @@
-use item::Item;
+use crate::item::Item;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Storage {
- items : Vec<Item>,
- carrying : usize,
- capacity : usize,
+ items: Vec<Item>,
+ carrying: usize,
+ capacity: usize,
}
impl Storage {
- pub fn new(items : Vec<Item>) -> Storage {
+ pub fn new(items: Vec<Item>) -> Storage {
let mut carrying = 0;
for item in items.iter() {
carrying += item.size;
}
Storage {
- items : items,
- capacity : 10,
- carrying : carrying,
+ items,
+ capacity: 10,
+ carrying,
}
}
pub fn has_minerals(&self) -> bool {
- match self.items.iter().position(|item| item.is_mineral()) {
- Some(_) => true,
- None => false,
- }
+ self.items.iter().any(|item| item.is_mineral())
}
pub fn refined_count(&self) -> usize {
@@ -33,7 +30,7 @@ impl Storage {
items.len()
}
- pub fn take(&mut self, name : &str) -> Option<Item> {
+ pub fn take(&mut self, name: &str) -> Option<Item> {
match self.items.iter().position(|item| item.name == name) {
Some(index) => {
let item = self.items.remove(index);
@@ -44,14 +41,13 @@ impl Storage {
}
}
- pub fn give(&mut self, item : Item) -> bool {
- match self.capacity >= self.carrying + item.size {
- true => {
- self.carrying += item.size;
- self.items.push(item);
- true
- },
- false => false,
+ pub fn give(&mut self, item: Item) -> bool {
+ if self.capacity >= self.carrying + item.size {
+ self.carrying += item.size;
+ self.items.push(item);
+ true
+ } else {
+ false
}
}
}