summaryrefslogtreecommitdiff
path: root/src/bin
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/bin
parent828f0d83dcb258a8e5efd55a7775592c6e5f77bc (diff)
bringing to 2018
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/client.rs39
-rw-r--r--src/bin/server.rs28
2 files changed, 35 insertions, 32 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));
- },
+ }
}
}
}