summaryrefslogtreecommitdiff
path: root/src/server
diff options
context:
space:
mode:
Diffstat (limited to 'src/server')
-rw-r--r--src/server/dashboard.rs18
-rw-r--r--src/server/engines.rs78
-rw-r--r--src/server/mining.rs84
-rw-r--r--src/server/mod.rs4
-rw-r--r--src/server/navigation.rs54
5 files changed, 238 insertions, 0 deletions
diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs
new file mode 100644
index 0000000..d12ea35
--- /dev/null
+++ b/src/server/dashboard.rs
@@ -0,0 +1,18 @@
+extern crate serde_json;
+
+use std::io::Write;
+use std::collections::HashMap;
+
+use mass::Mass;
+use connection::Connection;
+
+impl Connection {
+ 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,
+ Err(_error) => false,
+ }
+ }
+}
diff --git a/src/server/engines.rs b/src/server/engines.rs
new file mode 100644
index 0000000..ec55ee0
--- /dev/null
+++ b/src/server/engines.rs
@@ -0,0 +1,78 @@
+extern crate serde_json;
+
+use std::io::Write;
+use std::io::BufRead;
+use std::collections::HashMap;
+
+use ship::Ship;
+use mass::Mass;
+use connection::Connection;
+use targeting::TargetingStatus;
+
+impl Connection {
+ 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;
+ let send = serde_json::to_string(&targeted).unwrap() + "\n";
+
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
+ }
+
+ let mut acceleration = (0.0, 0.0, 0.0);
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(result) => match recv.as_bytes() {
+ b"5\n" => acceleration.0 += 0.1,
+ b"0\n" => acceleration.0 -= 0.1,
+ b"8\n" => acceleration.1 += 0.1,
+ 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"c\n" => {
+ match ship.recv_target() {
+ Some(name) => {
+ 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,
+ d_v.1 - m_v.1,
+ d_v.2 - m_v.2);
+ },
+ None => (),
+ }
+ },
+ b"t\n" => {
+ match ship.recv_target() {
+ Some(name) => {
+ 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,
+ (d_p.1 - m_p.1) * 0.01,
+ (d_p.2 - m_p.2) * 0.01);
+ },
+ None => (),
+ }
+ },
+ _ => {
+ if result == 0 {
+ return false
+ }
+ },
+ },
+ Err(_error) => (),
+ }
+
+ ship.give_acceleration(acceleration);
+
+ true
+ }
+}
diff --git a/src/server/mining.rs b/src/server/mining.rs
new file mode 100644
index 0000000..9aa054f
--- /dev/null
+++ b/src/server/mining.rs
@@ -0,0 +1,84 @@
+extern crate serde_json;
+
+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;
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+struct ServerData {
+ has_astroid_target : bool,
+ is_within_range : bool,
+ mining_range : f64,
+ mining_status : bool,
+}
+
+impl Connection {
+ 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) => masses_clone.get(&name),
+ None => None,
+ };
+
+ let has_astroid_target = match target {
+ Some(target) => match target.recv_mass_type() {
+ MassType::Ship => false,
+ MassType::Astroid => true,
+ },
+ 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,
+ }
+ 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(),
+ }).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) => match recv.as_bytes() {
+ b"F\n" => {
+ if is_within_range {
+ match ship.recv_mining_status() {
+ true => ship.stop_mining(),
+ false => ship.start_mining(),
+ }
+ }
+ },
+ _ => {
+ if result == 0 {
+ return false
+ }
+ },
+ }
+ Err(_error) => (),
+ }
+
+ true
+ }
+}
diff --git a/src/server/mod.rs b/src/server/mod.rs
new file mode 100644
index 0000000..baabf96
--- /dev/null
+++ b/src/server/mod.rs
@@ -0,0 +1,4 @@
+pub mod mining;
+pub mod engines;
+pub mod dashboard;
+pub mod navigation;
diff --git a/src/server/navigation.rs b/src/server/navigation.rs
new file mode 100644
index 0000000..f75c2b7
--- /dev/null
+++ b/src/server/navigation.rs
@@ -0,0 +1,54 @@
+extern crate serde_json;
+
+use std::io::Write;
+use std::io::BufRead;
+use std::collections::HashMap;
+
+use mass::Mass;
+use ship::Ship;
+use math::distance;
+use connection::Connection;
+
+impl Connection {
+ 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 = masses_clone.get(&name).unwrap();
+ if distance(target.position(), ship.position()) > ship.recv_range() {
+ ship.give_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 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", "")));
+ }
+ true
+ }
+}