summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-13 03:39:24 -0500
committertom barrett <spalf0@gmail.com>2018-04-13 03:39:24 -0500
commit4d7a7e368a3f0a6b1261e1b9180a2647eb158047 (patch)
tree50ead62fdc5e22e839920dae44a02dc25b247678 /src/server
parent74c7e70096ebcb0cab10f24ce1d348198d583161 (diff)
moved over to mass struct with enum for types
Diffstat (limited to 'src/server')
-rw-r--r--src/server/dashboard.rs4
-rw-r--r--src/server/engines.rs42
-rw-r--r--src/server/mining.rs40
-rw-r--r--src/server/navigation.rs72
4 files changed, 83 insertions, 75 deletions
diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs
index d12ea35..82f73f9 100644
--- a/src/server/dashboard.rs
+++ b/src/server/dashboard.rs
@@ -7,9 +7,9 @@ use mass::Mass;
use connection::Connection;
impl Connection {
- pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let ship = masses.get(&self.name).unwrap();
- let send = ship.serialize() + "\n";
+ let send = serde_json::to_string(&ship).unwrap();
match self.stream.write(send.as_bytes()) {
Ok(_result) => true,
Err(_error) => false,
diff --git a/src/server/engines.rs b/src/server/engines.rs
index ec55ee0..aa0e703 100644
--- a/src/server/engines.rs
+++ b/src/server/engines.rs
@@ -4,19 +4,21 @@ use std::io::Write;
use std::io::BufRead;
use std::collections::HashMap;
-use ship::Ship;
-use mass::Mass;
+use mass::{Mass, MassType};
use connection::Connection;
use targeting::TargetingStatus;
impl Connection {
- pub fn server_engines(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_engines(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let masses_clone = masses.clone();
- let mass = masses.get_mut(&self.name).unwrap();
- let ship = mass.downcast_mut::<Ship>().unwrap();
+ let ship = masses.get_mut(&self.name).unwrap();
+ let targeting = match ship.mass_type {
+ MassType::Ship{ref targeting, ..} => Some(targeting.clone()),
+ _ => None,
+ }.unwrap();
- let targeted = ship.recv_targeting_status() == TargetingStatus::Targeted;
+ let targeted = targeting.status == TargetingStatus::Targeted;
let send = serde_json::to_string(&targeted).unwrap() + "\n";
match self.stream.write(send.as_bytes()) {
@@ -34,14 +36,24 @@ impl Connection {
b"2\n" => acceleration.1 -= 0.1,
b"4\n" => acceleration.2 += 0.1,
b"6\n" => acceleration.2 -= 0.1,
- b"+\n" => ship.speedup(),
- b"-\n" => ship.slow(),
+ b"+\n" => {
+ let m_v = ship.velocity;
+ 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);
+ },
b"c\n" => {
- match ship.recv_target() {
+ match targeting.target {
Some(name) => {
let target = masses_clone.get(&name).unwrap();
- let d_v = target.recv_velocity();
- let m_v = ship.recv_velocity();
+ 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);
@@ -50,11 +62,11 @@ impl Connection {
}
},
b"t\n" => {
- match ship.recv_target() {
+ match targeting.target {
Some(name) => {
let target = masses_clone.get(&name).unwrap();
- let d_p = target.position();
- let m_p = ship.position();
+ 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);
@@ -71,7 +83,7 @@ impl Connection {
Err(_error) => (),
}
- ship.give_acceleration(acceleration);
+ ship.accelerate(acceleration);
true
}
diff --git a/src/server/mining.rs b/src/server/mining.rs
index 9aa054f..ff26840 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -4,7 +4,6 @@ use std::io::BufRead;
use std::io::Write;
use std::collections::HashMap;
-use ship::Ship;
use math::distance;
use mass::{Mass, MassType};
use connection::Connection;
@@ -18,40 +17,38 @@ struct ServerData {
}
impl Connection {
- pub fn server_mining(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_mining(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let masses_clone = masses.clone();
- let mass = masses.get_mut(&self.name).unwrap();
- let ship = mass.downcast_mut::<Ship>().unwrap();
+ let ship = masses.get_mut(&self.name).unwrap();
- let target = match ship.recv_target() {
- Some(name) => masses_clone.get(&name),
- None => None,
+ let (mining, targeting) = match ship.mass_type {
+ MassType::Ship{ref targeting, ref mut mining, ..} => (Some(mining), Some(targeting.clone())),
+ _ => (None, None),
};
+ let mining = mining.unwrap();
+ let target = masses_clone.get(&targeting.unwrap().target.unwrap());
let has_astroid_target = match target {
- Some(target) => match target.recv_mass_type() {
- MassType::Ship => false,
- MassType::Astroid => true,
+ Some(target) => match target.mass_type {
+ MassType::Astroid{..} => true,
+ _ => false,
},
None => false,
};
let is_within_range = match has_astroid_target {
true => match target {
- Some(target) => match ship.recv_mining_range() > distance(ship.position(), target.position()) {
- true => true,
- false => false,
- },
- None => false,
+ Some(target) => mining.range > distance(ship.position, target.position),
+ _ => false,
}
- false => false,
+ _ => false,
};
let send = serde_json::to_string(&ServerData {
has_astroid_target : has_astroid_target,
is_within_range : is_within_range,
- mining_range : ship.recv_mining_range(),
- mining_status : ship.recv_mining_status(),
+ mining_range : mining.range,
+ mining_status : mining.status,
}).unwrap() + "\n";
match self.stream.write(send.as_bytes()) {
@@ -64,9 +61,9 @@ impl Connection {
Ok(result) => match recv.as_bytes() {
b"F\n" => {
if is_within_range {
- match ship.recv_mining_status() {
- true => ship.stop_mining(),
- false => ship.start_mining(),
+ match mining.status {
+ true => mining.stop(),
+ false => mining.start(),
}
}
},
@@ -78,7 +75,6 @@ impl Connection {
}
Err(_error) => (),
}
-
true
}
}
diff --git a/src/server/navigation.rs b/src/server/navigation.rs
index f75c2b7..3813732 100644
--- a/src/server/navigation.rs
+++ b/src/server/navigation.rs
@@ -4,51 +4,51 @@ use std::io::Write;
use std::io::BufRead;
use std::collections::HashMap;
-use mass::Mass;
-use ship::Ship;
+use mass::{Mass, MassType};
use math::distance;
use connection::Connection;
impl Connection {
- pub fn server_navigation(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_navigation(&mut self, masses : &mut HashMap<String, 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 = masses_clone.get(&name).unwrap();
- if distance(target.position(), ship.position()) > ship.recv_range() {
- ship.give_target(None);
+ let ship = masses.get_mut(&self.name).unwrap();
+ let ship_position = ship.position;
+
+ match ship.mass_type {
+ MassType::Ship{ref mut targeting, ..} => {
+ let target_name = targeting.clone().target;
+ match target_name {
+ Some(target_name) => {
+ let target = masses_clone.get(&target_name).unwrap();
+ if distance(target.position, ship.position) > targeting.range {
+ targeting.target = None;
+ }
+ },
+ _ => (),
}
- }
- None => (),
- }
- let within_range : HashMap<&String, &Box<Mass>> = masses_clone.iter().filter(|&(_, mass)|
- distance(ship.position(), mass.position()) < ship.recv_range())
- .collect();
- let mut send = String::new();
- for (name, mass) in within_range {
- send.push_str(name);
- send.push_str("@");
- send.push_str(&mass.serialize());
- send.push_str(";");
- }
- send.push_str("\n");
- match self.stream.write(send.as_bytes()) {
- Ok(_result) => (),
- Err(_error) => return false,
- }
+ let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)|
+ distance(ship_position, mass.position) < targeting.range)
+ .collect();
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(_result) => (),
- Err(_error) => (),
- }
- if recv.len() > 0 {
- ship.give_target(Some(recv.replace("\n", "")));
+ let send = serde_json::to_string(&within_range).unwrap() + "\n";
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
+ }
+
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(_result) => (),
+ Err(_error) => (),
+ }
+ if !recv.is_empty() {
+ targeting.give_target(Some(recv.replace("\n", "")));
+ }
+ },
+ _ => (),
}
+
true
}
}