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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// use std::collections::HashMap;
use rustc_hash::HashMap;
/**
* The StoreProps struct represents the properties of a store.
*/
struct StoreProps {
actors: Option<Vec<String>>,
entities: Option<Vec<String>>,
components: Components,
inputs: Inputs,
}
/**
* The Components struct represents a mapping from keys to any value.
*/
type Components = HashMap<String, HashMap<String, serde_json::Value>>;
/**
* The Inputs struct represents a mapping from keys to any array.
*/
type Inputs = HashMap<String, Vec<serde_json::Value>>;
/**
* The Store struct represents a store with actors, entities, components, and inputs.
*/
struct Store {
actors: Vec<String>,
entities: Vec<String>,
components: Components,
inputs: Inputs,
}
impl Store {
/**
* Constructs a new Store object.
*
* @param store - The properties of the store.
*/
fn new(store: StoreProps) -> Self {
let StoreProps {
actors,
entities,
components,
inputs,
} = store;
Store {
actors: actors.unwrap_or_default(),
entities: entities.unwrap_or_default(),
components: components,
inputs: inputs,
}
}
/**
* Removes an actor ID.
*
* @param id - The ID of the actor to remove.
* @returns True if the actor ID was removed, false otherwise.
*/
fn destroy_actor(&mut self, id: &str) -> bool {
let actors = self.get_actors();
self.destroy_id(&mut actors, id)
}
/**
* Removes a component.
*
* @param id - The ID of the component to remove.
* @param key - The key of the component to remove.
*/
fn destroy_component(&mut self, id: &str, key: &str) {
if let Some(component) = self.components.get_mut(id) {
component.remove(key);
}
}
/**
* Removes an entity ID.
*
* @param id - The ID of the entity to remove.
* @returns True if the entity ID was removed, false otherwise.
*/
fn destroy_entity(&mut self, id: &str) -> bool {
let entities = self.get_entities();
self.destroy_id(&mut entities, id)
}
/**
* Removes an ID from a list if it exists.
*
* @param list - The list to remove the ID from.
* @param id - The ID to remove.
* @returns True if the ID was removed, false otherwise.
*/
fn destroy_id(&mut self, list: &mut Vec<String>, id: &str) -> bool {
if let Some(index) = list.iter().position(|x| x == id) {
list.remove(index);
true
} else {
false
}
}
/**
* Fetches a component.
*
* @param id - The ID of the component to fetch.
* @param key - The key of the component to fetch.
* @returns The fetched component.
*/
fn fetch_component(&mut self, id: &str, key: &str) -> Option<&serde_json::Value> {
self.components
.entry(id.to_string())
.or_insert_with(HashMap::new)
.get(key)
}
/**
* Gets the actors.
*
* @returns The actors.
*/
fn get_actors(&self) -> &Vec<String> {
&self.actors
}
/**
* Gets the components.
*
* @returns The components.
*/
fn get_components(&self) -> &Components {
&self.components
}
/**
* Gets the entities.
*
* @returns The entities.
*/
fn get_entities(&self) -> &Vec<String> {
&self.entities
}
/**
* Gets the inputs.
*
* @returns The inputs.
*/
fn get_inputs(&self) -> &Inputs {
&self.inputs
}
/**
* Sets the actors.
*
* @param actors - The actors to set.
* @returns The actors.
*/
fn set_actors(&mut self, actors: Vec<String>) -> &Vec<String> {
self.actors = actors;
&self.actors
}
/**
* Sets the components.
*
* @param components - The components to set.
* @returns The components.
*/
fn set_components(&mut self, components: Components) -> &Components {
self.components = components;
&self.components
}
/**
* Sets the entities.
*
* @param entities - The entities to set.
* @returns The entities.
*/
fn set_entities(&mut self, entities: Vec<String>) -> &Vec<String> {
self.entities = entities;
&self.entities
}
/**
* Sets the inputs.
*
* @param inputs - The inputs to set.
* @returns The inputs.
*/
fn set_inputs(&mut self, inputs: Inputs) -> &Inputs {
self.inputs = inputs;
&self.inputs
}
/**
* Stores an actor ID.
*
* @param id - The ID of the actor to store.
* @returns True if the actor ID was stored, false otherwise.
*/
fn store_actor(&mut self, id: &str) -> bool {
let actors = self.get_actors();
self.store_id(&mut actors, id)
}
/**
* Stores a component.
*
* @param id - The ID of the component to store.
* @param key - The key of the component to store.
* @param value - The value of the component to store.
*/
fn store_component(&mut self, id: &str, key: &str, value: serde_json::Value) {
self.components
.entry(id.to_string())
.or_insert_with(HashMap::new)
.insert(key.to_string(), value);
}
/**
* Stores an entity ID.
*
* @param id - The ID of the entity to store.
* @returns True if the entity ID was stored, false otherwise.
*/
fn store_entity(&mut self, id: &str) -> bool {
let entities = self.get_entities();
self.store_id(&mut entities, id)
}
/**
* Stores an ID in a list if it doesn't exist already.
*
* @param list - The list to store the ID in.
* @param id - The ID to store.
* @returns True if the ID was stored, false otherwise.
*/
fn store_id(&mut self, list: &mut Vec<String>, id: &str) -> bool {
if !list.contains(&id.to_string()) {
list.push(id.to_string());
true
} else {
false
}
}
/**
* Stores an input.
*
* @param id - The ID of the input to store.
* @param payload - The payload of the input to store.
* @returns The new index of the stored input.
*/
fn store_input(&mut self, id: &str, payload: serde_json::Value) -> usize {
let inputs = self.get_inputs();
let input_list = inputs.entry(id.to_string()).or_insert_with(Vec::new);
let new_index = input_list.len();
input_list.push(payload);
new_index
}
}