summaryrefslogtreecommitdiff
path: root/src/modules/mining.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/modules/mining.rs')
-rw-r--r--src/modules/mining.rs120
1 files changed, 96 insertions, 24 deletions
diff --git a/src/modules/mining.rs b/src/modules/mining.rs
index a6cea92..8e5b001 100644
--- a/src/modules/mining.rs
+++ b/src/modules/mining.rs
@@ -1,20 +1,11 @@
use std::time::SystemTime;
use crate::constants;
-use crate::server::mining::MiningData;
-
-#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
-pub enum MiningStatus {
- None,
- Mining,
- Mined,
-}
-
-impl Default for MiningStatus {
- fn default() -> Self {
- MiningStatus::None
- }
-}
+use crate::item::ItemType;
+use crate::mass::{Mass, MassType};
+use crate::math::Vector;
+use crate::storage::Storage;
+use std::collections::HashMap;
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct Mining {
@@ -34,41 +25,122 @@ impl Mining {
}
}
- pub fn process(&mut self) {
+ pub fn process(
+ &mut self,
+ ship_position: Vector,
+ masses: &mut HashMap<String, Mass>,
+ target: &mut Mass,
+ storage: &mut Storage,
+ ) {
+ if self.range < ship_position.distance_from(target.position.clone()) {
+ self.off();
+ }
if let Some(timer) = self.start {
if timer.elapsed().unwrap().as_secs() > self.time {
self.status = MiningStatus::Mined;
- self.start = Some(SystemTime::now());
+ self.start = None;
+ }
+ }
+ if self.status == MiningStatus::Mined {
+ if let MassType::Astroid {
+ ref mut resources, ..
+ } = target.mass_type
+ {
+ match resources.take_item(ItemType::CrudeMinerals) {
+ Some(item) => {
+ if !storage.give_item(item.clone()) {
+ let mass = Mass::new_item(
+ item.clone(),
+ target.position.clone(),
+ target.velocity.clone(),
+ );
+ masses.insert(item.name.clone(), mass);
+ }
+ }
+ None => self.off(),
+ }
}
+ self.mined();
}
}
- pub fn give_recv(&mut self, recv: String, mining_data: MiningData) {
- if !mining_data.is_within_range {
- self.off();
- } else if let "F" = recv.as_str() {
+ pub fn give_received_data(&mut self, recv: String) {
+ if let "F" = recv.as_str() {
self.toggle()
}
}
- pub fn toggle(&mut self) {
+ pub fn get_client_data(&self, ship_position: Vector, target: Option<&Mass>) -> String {
+ let mut astroid_has_minerals = false;
+ let mut is_within_range = false;
+ let has_astroid_target = match target {
+ Some(target) => match target.mass_type {
+ MassType::Astroid { ref resources, .. } => {
+ astroid_has_minerals = resources
+ .items
+ .iter()
+ .any(|item| item.item_type == ItemType::CrudeMinerals);
+ is_within_range =
+ self.range > ship_position.distance_from(target.position.clone());
+ true
+ }
+ _ => false,
+ },
+ None => false,
+ };
+
+ let client_data = MiningClientData {
+ has_astroid_target,
+ astroid_has_minerals,
+ is_within_range,
+ range: self.range,
+ status: self.status.clone(),
+ };
+
+ serde_json::to_string(&client_data).unwrap() + "\n"
+ }
+
+ fn toggle(&mut self) {
match self.status {
MiningStatus::None => self.on(),
_ => self.off(),
};
}
- pub fn on(&mut self) {
+ fn on(&mut self) {
self.start = Some(SystemTime::now());
self.status = MiningStatus::Mining;
}
- pub fn off(&mut self) {
+ fn off(&mut self) {
self.start = None;
self.status = MiningStatus::None;
}
- pub fn mined(&mut self) {
+ fn mined(&mut self) {
self.status = MiningStatus::Mining;
+ self.start = Some(SystemTime::now());
+ }
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct MiningClientData {
+ pub has_astroid_target: bool,
+ pub astroid_has_minerals: bool,
+ pub is_within_range: bool,
+ pub status: MiningStatus,
+ pub range: f64,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
+pub enum MiningStatus {
+ None,
+ Mining,
+ Mined,
+}
+
+impl Default for MiningStatus {
+ fn default() -> Self {
+ MiningStatus::None
}
}