1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
use std::time::SystemTime;
use crate::constants;
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 {
pub range: f64,
pub status: Status,
time: u64,
start: Option<SystemTime>,
}
impl Mining {
pub fn new() -> Mining {
Mining {
range: constants::SHIP_MINING_RANGE,
status: Status::None,
time: constants::SHIP_MINING_TIME,
start: None,
}
}
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 = Status::Mined;
self.start = None;
}
}
if self.status == Status::Mined {
match target.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_received_data(&mut self, recv: String) {
if let "F" = recv.as_str() {
self.toggle()
}
}
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 = ClientData {
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 {
Status::None => self.on(),
_ => self.off(),
};
}
fn on(&mut self) {
self.start = Some(SystemTime::now());
self.status = Status::Mining;
}
fn off(&mut self) {
self.start = None;
self.status = Status::None;
}
fn mined(&mut self) {
self.status = Status::Mining;
self.start = Some(SystemTime::now());
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientData {
pub has_astroid_target: bool,
pub astroid_has_minerals: bool,
pub is_within_range: bool,
pub status: Status,
pub range: f64,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum Status {
None,
Mining,
Mined,
}
impl Default for Status {
fn default() -> Self {
Status::None
}
}
|