From 430b4ee12c72364e6901436951d66e4e6c43990d Mon Sep 17 00:00:00 2001 From: tom barrett Date: Thu, 26 Apr 2018 03:33:49 -0500 Subject: -removed cloning of mass list --- src/server/engines.rs | 14 +++++++------- src/server/mining.rs | 19 ++++++++++--------- src/server/navigation.rs | 21 ++++++++++++--------- 3 files changed, 29 insertions(+), 25 deletions(-) diff --git a/src/server/engines.rs b/src/server/engines.rs index 17391e6..710ed3a 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -10,10 +10,9 @@ use server::connection::ServerConnection; impl ServerConnection { pub fn server_engines(&mut self, masses : &mut HashMap) -> bool { - let masses_clone = masses.clone(); - - let ship = masses.get_mut(&self.name).unwrap(); + let mut ship = masses.remove(&self.name).unwrap(); let ship_clone = ship.clone(); + let mut connection_good = true; if let MassType::Ship{ref mut engines, ref navigation, ..} = ship.mass_type { let navigation = navigation.clone().unwrap(); @@ -23,11 +22,11 @@ impl ServerConnection { let send = serde_json::to_string(&targeted).unwrap() + "\n"; match self.stream.write(send.as_bytes()) { Ok(_result) => (), - Err(_error) => return false, + Err(_error) => connection_good = false, } let target = match navigation.target_name { - Some(name) => masses_clone.get(&name), + Some(name) => masses.get(&name), None => None, }; let mut recv = String::new(); @@ -35,13 +34,14 @@ impl ServerConnection { Ok(result) => { engines.give_client_data(&ship_clone, target, recv); if result == 0 { - return false; + connection_good = false; } }, Err(_error) => (), } } - true + masses.insert(self.name.clone(), ship); + connection_good } } diff --git a/src/server/mining.rs b/src/server/mining.rs index c5c0ce7..c64f03e 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -20,20 +20,19 @@ pub struct MiningData { impl ServerConnection { pub fn server_mining(&mut self, masses : &mut HashMap) -> bool { - let masses_clone = masses.clone(); - let ship = masses.get_mut(&self.name).unwrap(); + let mut ship = masses.remove(&self.name).unwrap(); let ship_clone = ship.clone(); - + let mut connection_good = true; 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(); - let mining_data = get_mining_data(ship_clone, mining, navigation, masses_clone); + let mining_data = get_mining_data(ship_clone, mining, navigation, masses); let send = serde_json::to_string(&mining_data).unwrap() + "\n"; match self.stream.write(send.as_bytes()) { Ok(_result) => (), - Err(_error) => return false, + Err(_error) => connection_good = false, } let mut recv = String::new(); @@ -46,21 +45,23 @@ impl ServerConnection { }, _ => { if result == 0 { - return false + connection_good = false; } }, } Err(_error) => (), } } - true + + masses.insert(self.name.clone(), ship); + connection_good } } -fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, masses_clone : HashMap) -> MiningData { +fn get_mining_data(ship : Mass, mining : &Mining, navigation : &Navigation, masses : &mut HashMap) -> MiningData { match navigation.target_name.clone() { Some(name) => { - let target = masses_clone.get(&name); + let target = masses.get(&name); let has_astroid_target = match target { Some(target) => match target.mass_type { MassType::Astroid{..} => true, diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 8168fac..2a2f105 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -10,20 +10,21 @@ use server::connection::ServerConnection; impl ServerConnection { pub fn server_navigation(&mut self, masses : &mut HashMap) -> bool { - let masses_clone = masses.clone(); - let ship = masses.get_mut(&self.name).unwrap(); - let ship_position = ship.position; + let mut ship = masses.remove(&self.name).unwrap(); + let ship_clone = ship.clone(); + let mut connection_good = true; if let MassType::Ship{ref mut navigation, ..} = ship.mass_type { let mut navigation = navigation.as_mut().unwrap(); - navigation.verify_target(ship_position, &masses_clone); - let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)| - distance(ship_position, mass.position) < navigation.range) - .collect(); + 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(); + within_range.insert(&self.name, &ship_clone); let send = serde_json::to_string(&within_range).unwrap() + "\n"; match self.stream.write(send.as_bytes()) { Ok(_result) => (), - Err(_error) => return false, + Err(_error) => connection_good = false, } let mut recv = String::new(); @@ -35,6 +36,8 @@ impl ServerConnection { navigation.give_target(recv.replace("\n", "")); } } - true + + masses.insert(self.name.clone(), ship); + connection_good } } -- cgit v1.2.3