summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-11 09:00:27 -0500
committertom barrett <spalf0@gmail.com>2018-04-11 09:00:27 -0500
commitab797e7f30e5e8913faf73516346129b9a620550 (patch)
tree17ac1c1ee27d2d82c32772c24a5a246b61a73756 /src
parentf3955f07478ace406422ebfe71abab359fe4c229 (diff)
-moved over to hashmap
Diffstat (limited to 'src')
-rw-r--r--src/bin/server.rs9
-rw-r--r--src/connection.rs12
-rw-r--r--src/dashboard.rs5
-rw-r--r--src/engines.rs13
-rw-r--r--src/mining.rs10
-rw-r--r--src/navigation.rs15
6 files changed, 34 insertions, 30 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs
index 16186d7..f7aaffe 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -1,6 +1,7 @@
use std::thread::sleep;
use std::time::Duration;
use std::net::TcpListener;
+use std::collections::HashMap;
extern crate space;
use space::mass::Mass;
@@ -8,11 +9,11 @@ use space::astroid::Astroid;
use space::connection::Connection;
-fn populate() -> Vec<Box<Mass>> {
- let mut masses : Vec<Box<Mass>> = Vec::new();
+fn populate() -> HashMap<String, Box<Mass>> {
+ let mut masses : HashMap<String, Box<Mass>> = HashMap::new();
for _ in 0..10 {
- masses.push(Box::new(Astroid::new()));
+ masses.insert("zz".to_string(), Box::new(Astroid::new()));
}
masses
@@ -34,7 +35,7 @@ fn main() {
}
connections.retain(|connection| connection.open);
- for mass in masses.iter_mut() {
+ for mass in masses.values_mut() {
mass.process();
}
diff --git a/src/connection.rs b/src/connection.rs
index 081805b..6ec2d16 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -1,6 +1,7 @@
use std::io::prelude::*;
use std::io::BufReader;
use std::net::TcpStream;
+use std::collections::HashMap;
extern crate serde_json;
@@ -17,19 +18,14 @@ pub struct Connection {
}
impl Connection {
- pub fn new(mut stream : TcpStream, masses : &mut Vec<Box<Mass>>) -> Connection {
+ pub fn new(mut stream : TcpStream, masses : &mut HashMap<String, Box<Mass>>) -> Connection {
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()];
- match masses.iter().find(|ship| ship.name() == name).is_some() {
- false => masses.push(Box::new(Ship::new(name, (0.0, 0.0, 0.0)))),
- _ => (),
- }
-
- let mass = masses.iter().find(|ship| ship.name() == name).unwrap();
+ let mass = masses.entry(name.to_string()).or_insert(Box::new(Ship::new(name, (0.0, 0.0, 0.0))));
let ship = mass.downcast_ref::<Ship>().unwrap();
let modules = ship.recv_modules();
@@ -50,7 +46,7 @@ impl Connection {
}
}
- pub fn process(&mut self, mut masses : &mut Vec<Box<Mass>>) {
+ pub fn process(&mut self, mut masses : &mut HashMap<String, Box<Mass>>) {
self.open = match self.module {
Module::Engines => self.server_engines(&mut masses),
Module::Dashboard => self.server_dashboard(&mut masses),
diff --git a/src/dashboard.rs b/src/dashboard.rs
index 94b4519..663818a 100644
--- a/src/dashboard.rs
+++ b/src/dashboard.rs
@@ -2,6 +2,7 @@ use std::io::BufReader;
use std::io::BufRead;
use std::net::TcpStream;
use std::io::Write;
+use std::collections::HashMap;
extern crate serde_json;
@@ -19,8 +20,8 @@ pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) {
}
impl Connection {
- pub fn server_dashboard(&mut self, masses : &mut Vec<Box<Mass>>) -> bool {
- let ship = masses.iter().find(|ship| ship.name() == &self.name).unwrap();
+ pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ let ship = masses.get(&self.name).unwrap();
let send = ship.serialize() + "\n";
match self.stream.write(send.as_bytes()) {
Ok(_result) => true,
diff --git a/src/engines.rs b/src/engines.rs
index cba189e..ce21e35 100644
--- a/src/engines.rs
+++ b/src/engines.rs
@@ -5,6 +5,7 @@ use std::thread::sleep;
use std::io::{Read, Write, stdout};
use std::time::Duration;
use std::io::{BufRead, BufReader};
+use std::collections::HashMap;
extern crate termion;
extern crate serde_json;
@@ -54,9 +55,10 @@ pub fn client_engines(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>)
}
impl Connection {
- pub fn server_engines(&mut self, masses : &mut Vec<Box<Mass>>) -> bool {
- let m = masses.to_vec();
- let mass = masses.into_iter().find(|ship| ship.name() == &self.name).unwrap();
+ pub fn server_engines(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ let masses_clone = masses.clone();
+
+ let mass = masses.get_mut(&self.name).unwrap();
let ship = mass.downcast_mut::<Ship>().unwrap();
let targeted = ship.recv_targeting_status() == TargetingStatus::Targeted;
@@ -82,7 +84,7 @@ impl Connection {
b"c\n" => {
match ship.recv_target() {
Some(name) => {
- let target = m.into_iter().find(|target| target.name() == &name).unwrap();
+ let target = masses_clone.get(&name).unwrap();
let d_v = target.recv_velocity();
let m_v = ship.recv_velocity();
acceleration = (d_v.0 - m_v.0,
@@ -95,7 +97,7 @@ impl Connection {
b"t\n" => {
match ship.recv_target() {
Some(name) => {
- let target = m.into_iter().find(|target| target.name() == &name).unwrap();
+ let target = masses_clone.get(&name).unwrap();
let d_p = target.position();
let m_p = ship.position();
acceleration = ((d_p.0 - m_p.0) * 0.01,
@@ -113,6 +115,7 @@ impl Connection {
},
Err(_error) => (),
}
+
ship.give_acceleration(acceleration);
true
diff --git a/src/mining.rs b/src/mining.rs
index ecc83c5..df425e0 100644
--- a/src/mining.rs
+++ b/src/mining.rs
@@ -1,4 +1,5 @@
use std::io::{BufReader, BufRead};
+use std::collections::HashMap;
use std::net::TcpStream;
use std::io::{stdout, Read, Write};
use termion::raw::IntoRawMode;
@@ -59,12 +60,13 @@ pub fn client_mining(mut stream : TcpStream, mut buff_r : BufReader<TcpStream>)
}
impl Connection {
- pub fn server_mining(&mut self, masses : &mut Vec<Box<Mass>>) -> bool {
- let m = masses.to_vec();
- let mass = masses.into_iter().find(|ship| ship.name() == &self.name).unwrap();
+ pub fn server_mining(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ let masses_clone = masses.clone();
+ let mass = masses.get_mut(&self.name).unwrap();
let ship = mass.downcast_mut::<Ship>().unwrap();
+
let target = match ship.recv_target() {
- Some(name) => m.iter().find(|target| target.name() == &name),
+ Some(name) => masses_clone.get(&name),
None => None,
};
diff --git a/src/navigation.rs b/src/navigation.rs
index b15103e..1a4d0dd 100644
--- a/src/navigation.rs
+++ b/src/navigation.rs
@@ -1,3 +1,4 @@
+use std::collections::HashMap;
use std::net::TcpStream;
use std::io::{BufRead, BufReader};
use std::io::{stdout, Read, Write};
@@ -92,14 +93,14 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
}
impl Connection {
- pub fn server_navigation(&mut self, masses : &mut Vec<Box<Mass>>) -> bool {
- let m = masses.to_vec();
- let mass = masses.into_iter().find(|ship| ship.name() == &self.name).unwrap();
+ pub fn server_navigation(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ let masses_clone = masses.clone();
+ let mass = masses.get_mut(&self.name).unwrap();
let ship = mass.downcast_mut::<Ship>().unwrap();
match ship.recv_target() {
Some(name) => {
- let target = m.iter().find(|target| target.name() == &name).unwrap();
+ let target = masses_clone.get(&name).unwrap();
if distance(target.position(), ship.position()) > ship.recv_range() {
ship.give_target(None);
}
@@ -107,9 +108,9 @@ impl Connection {
None => (),
}
- let within_range : Vec<&Box<Mass>> = m.iter().filter(|mass|
- distance(ship.position(), mass.position()) < ship.recv_range())
- .collect();
+ let within_range : Vec<&Box<Mass>> = masses_clone.values().filter(|mass|
+ distance(ship.position(), mass.position()) < ship.recv_range())
+ .collect();
let mut send = String::new();
for mass in within_range {
send.push_str(&mass.serialize());