fish-lib 0.2.3

A work-in-progress fishing game library containing the game/storage logic for a discord fishing game I'm working on.
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
use crate::config::validation_error::ConfigValidationError;
use crate::config::validation_report::ConfigValidationReport;
use crate::data::encounter_data::EncounterData;
use crate::data::item_data::ItemData;
use crate::data::location_data::LocationData;
use crate::data::settings::Settings;
use crate::data::species_data::SpeciesData;
use crate::enums::item_category::ItemCategory;
use crate::models::item::attributes::ItemAttributesType;
use crate::models::item::attributes_container::ItemAttributesContainerInterface;
use crate::models::item::properties::ItemPropertiesType;
use crate::models::item::properties_container::ItemPropertiesContainerInterface;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fs::File;
use std::path::Path;
use std::sync::Arc;

mod validation_error;
mod validation_report;

pub trait ConfigInterface: Debug + Send + Sync {
    fn species(&self) -> Arc<HashMap<i32, Arc<SpeciesData>>>;
    fn locations(&self) -> Arc<HashMap<i32, Arc<LocationData>>>;
    fn items(&self) -> Arc<HashMap<i32, Arc<ItemData>>>;
    fn settings(&self) -> Arc<Settings>;
    fn species_names(&self) -> Arc<HashMap<i32, String>>;
    fn location_names(&self) -> Arc<HashMap<i32, String>>;
    fn item_names(&self) -> Arc<HashMap<i32, String>>;
    fn items_by_attributes(&self) -> Arc<HashMap<ItemAttributesType, Arc<Vec<Arc<ItemData>>>>>;
    fn items_by_properties(&self) -> Arc<HashMap<ItemPropertiesType, Arc<Vec<Arc<ItemData>>>>>;
    fn items_by_category(&self) -> Arc<HashMap<ItemCategory, Arc<Vec<Arc<ItemData>>>>>;
    fn item_ids_by_category(&self) -> Arc<HashMap<ItemCategory, Arc<Vec<i32>>>>;

    fn get_species_data(&self, species_id: i32) -> Option<Arc<SpeciesData>> {
        self.species().get(&species_id).cloned()
    }

    fn get_location_data(&self, location_id: i32) -> Option<Arc<LocationData>> {
        self.locations().get(&location_id).cloned()
    }

    fn get_item_data(&self, item_id: i32) -> Option<Arc<ItemData>> {
        self.items().get(&item_id).cloned()
    }

    fn get_items_by_attributes_type(
        &self,
        attributes_type: ItemAttributesType,
    ) -> Option<Arc<Vec<Arc<ItemData>>>> {
        self.items_by_attributes().get(&attributes_type).cloned()
    }

    fn get_items_by_properties_type(
        &self,
        properties_type: ItemPropertiesType,
    ) -> Option<Arc<Vec<Arc<ItemData>>>> {
        self.items_by_properties().get(&properties_type).cloned()
    }

    fn get_items_by_category(
        &self,
        item_category: ItemCategory,
    ) -> Option<Arc<Vec<Arc<ItemData>>>> {
        self.items_by_category().get(&item_category).cloned()
    }

    fn get_item_ids_by_category(&self, item_category: ItemCategory) -> Option<Arc<Vec<i32>>> {
        self.item_ids_by_category().get(&item_category).cloned()
    }

    fn is_item_in_category(&self, item_data: Arc<ItemData>, category: ItemCategory) -> bool {
        self.is_item_id_in_category(item_data.id, category)
    }

    fn is_item_id_in_category(&self, item_id: i32, item_category: ItemCategory) -> bool {
        self.get_item_ids_by_category(item_category)
            .unwrap_or_default()
            .contains(&item_id)
    }
}

#[derive(Debug, Default, PartialEq)]
pub struct Config {
    /// Mapping specimen to their species ID
    species: Arc<HashMap<i32, Arc<SpeciesData>>>,
    locations: Arc<HashMap<i32, Arc<LocationData>>>,
    items: Arc<HashMap<i32, Arc<ItemData>>>,
    settings: Arc<Settings>,
    species_names: Arc<HashMap<i32, String>>,
    location_names: Arc<HashMap<i32, String>>,
    item_names: Arc<HashMap<i32, String>>,
    items_by_attributes: Arc<HashMap<ItemAttributesType, Arc<Vec<Arc<ItemData>>>>>,
    items_by_properties: Arc<HashMap<ItemPropertiesType, Arc<Vec<Arc<ItemData>>>>>,
    items_by_category: Arc<HashMap<ItemCategory, Arc<Vec<Arc<ItemData>>>>>,
    item_ids_by_category: Arc<HashMap<ItemCategory, Arc<Vec<i32>>>>,
}

impl ConfigInterface for Config {
    fn species(&self) -> Arc<HashMap<i32, Arc<SpeciesData>>> {
        self.species.clone()
    }

    fn locations(&self) -> Arc<HashMap<i32, Arc<LocationData>>> {
        self.locations.clone()
    }

    fn items(&self) -> Arc<HashMap<i32, Arc<ItemData>>> {
        self.items.clone()
    }

    fn settings(&self) -> Arc<Settings> {
        self.settings.clone()
    }

    fn species_names(&self) -> Arc<HashMap<i32, String>> {
        self.species_names.clone()
    }

    fn location_names(&self) -> Arc<HashMap<i32, String>> {
        self.location_names.clone()
    }

    fn item_names(&self) -> Arc<HashMap<i32, String>> {
        self.item_names.clone()
    }

    fn items_by_attributes(&self) -> Arc<HashMap<ItemAttributesType, Arc<Vec<Arc<ItemData>>>>> {
        self.items_by_attributes.clone()
    }

    fn items_by_properties(&self) -> Arc<HashMap<ItemPropertiesType, Arc<Vec<Arc<ItemData>>>>> {
        self.items_by_properties.clone()
    }

    fn items_by_category(&self) -> Arc<HashMap<ItemCategory, Arc<Vec<Arc<ItemData>>>>> {
        self.items_by_category.clone()
    }

    fn item_ids_by_category(&self) -> Arc<HashMap<ItemCategory, Arc<Vec<i32>>>> {
        self.item_ids_by_category.clone()
    }
}

impl Config {
    pub fn builder() -> ConfigBuilder {
        ConfigBuilder::default()
    }
}

pub trait ConfigBuilderInterface: Send + Sync {
    fn build(self) -> Result<Arc<dyn ConfigInterface>, ConfigValidationReport>;
}

#[derive(Debug, Default)]
pub struct ConfigBuilder {
    config: Config,
}

impl ConfigBuilderInterface for ConfigBuilder {
    fn build(self) -> Result<Arc<dyn ConfigInterface>, ConfigValidationReport> {
        let validation_report = self.validate();
        if validation_report.has_errors() {
            Err(validation_report)
        } else {
            Ok(Arc::new(self.config))
        }
    }
}

impl ConfigBuilder {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn species(mut self, specimens: HashMap<i32, SpeciesData>) -> Self {
        let species = specimens
            .into_iter()
            .map(|(id, mut data)| {
                data.id = id;
                (id, Arc::new(data))
            })
            .collect();
        self.config.species = Arc::new(species);

        self.map_species_names();

        self
    }

    pub fn species_json(self, json_string: &str) -> Result<Self, serde_json::Error> {
        let specimens: HashMap<i32, SpeciesData> = serde_json::from_str(json_string)?;
        Ok(self.species(specimens))
    }

    pub fn species_json_file(
        self,
        json_file_path: &Path,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let file = File::open(json_file_path)?;
        let specimens: HashMap<i32, SpeciesData> = serde_json::from_reader(file)?;
        Ok(self.species(specimens))
    }

    pub fn locations(mut self, locations: HashMap<i32, LocationData>) -> Self {
        let locations = locations
            .into_iter()
            .map(|(id, mut data)| {
                data.id = id;
                (id, Arc::new(data))
            })
            .collect();
        self.config.locations = Arc::new(locations);

        self.map_location_names();

        self
    }

    pub fn locations_json(self, json_string: &str) -> Result<Self, serde_json::Error> {
        let locations: HashMap<i32, LocationData> = serde_json::from_str(json_string)?;
        Ok(self.locations(locations))
    }

    pub fn locations_json_file(
        self,
        json_file_path: &Path,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let file = File::open(json_file_path)?;
        let locations: HashMap<i32, LocationData> = serde_json::from_reader(file)?;
        Ok(self.locations(locations))
    }

    pub fn items(mut self, items: HashMap<i32, ItemData>) -> Self {
        let items = items
            .into_iter()
            .map(|(id, mut data)| {
                data.id = id;
                (id, Arc::new(data))
            })
            .collect();
        self.config.items = Arc::new(items);

        self.map_item_names();
        self.map_items_by_attributes_and_properties_types();
        self.map_items_by_category();

        self
    }

    pub fn items_json(self, json_string: &str) -> Result<Self, serde_json::Error> {
        let items: HashMap<i32, ItemData> = serde_json::from_str(json_string)?;
        Ok(self.items(items))
    }

    pub fn items_json_file(
        self,
        json_file_path: &Path,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let file = File::open(json_file_path)?;
        let items: HashMap<i32, ItemData> = serde_json::from_reader(file)?;
        Ok(self.items(items))
    }

    pub fn settings(mut self, settings: Settings) -> Self {
        self.config.settings = Arc::new(settings);
        self
    }

    pub fn settings_json(self, json_string: &str) -> Result<Self, serde_json::Error> {
        let settings: Settings = serde_json::from_str(json_string)?;
        Ok(self.settings(settings))
    }

    pub fn settings_json_file(
        self,
        json_file_path: &Path,
    ) -> Result<Self, Box<dyn std::error::Error>> {
        let file = File::open(json_file_path)?;
        let settings: Settings = serde_json::from_reader(file)?;
        Ok(self.settings(settings))
    }

    // Helper function for building config indices
    fn map_species_names(&mut self) {
        let species_names: HashMap<i32, String> = self
            .config
            .species
            .iter()
            .map(|(id, data)| (*id, data.name.clone()))
            .collect();
        self.config.species_names = Arc::new(species_names);
    }

    fn map_location_names(&mut self) {
        let location_names: HashMap<i32, String> = self
            .config
            .locations
            .iter()
            .map(|(id, data)| (*id, data.name.clone()))
            .collect();
        self.config.location_names = Arc::new(location_names);
    }

    fn map_item_names(&mut self) {
        let item_names: HashMap<i32, String> = self
            .config
            .items
            .iter()
            .map(|(id, data)| (*id, data.name.clone()))
            .collect();
        self.config.item_names = Arc::new(item_names);
    }

    fn map_items_by_attributes_and_properties_types(&mut self) {
        let mut by_attributes: HashMap<ItemAttributesType, Vec<Arc<ItemData>>> = HashMap::new();
        let mut by_properties: HashMap<ItemPropertiesType, Vec<Arc<ItemData>>> = HashMap::new();

        self.config.items.values().for_each(|item| {
            item.attributes
                .get_attributes_types()
                .iter()
                .for_each(|attr_type| {
                    by_attributes
                        .entry(*attr_type)
                        .or_default()
                        .push(item.clone());
                });

            item.default_properties
                .get_properties_types()
                .iter()
                .for_each(|prop_type| {
                    by_properties
                        .entry(*prop_type)
                        .or_default()
                        .push(item.clone());
                });
        });

        let by_attributes = by_attributes
            .into_iter()
            .map(|(k, v)| (k, Arc::new(v)))
            .collect();
        let by_properties = by_properties
            .into_iter()
            .map(|(k, v)| (k, Arc::new(v)))
            .collect();

        self.config.items_by_attributes = Arc::new(by_attributes);
        self.config.items_by_properties = Arc::new(by_properties);
    }

    pub fn map_items_by_category(&mut self) {
        let mut by_category: HashMap<ItemCategory, Vec<Arc<ItemData>>> = HashMap::new();
        let mut ids_by_category: HashMap<ItemCategory, Vec<i32>> = HashMap::new();

        self.config.items.values().for_each(|item| {
            if item.is_purchasable() {
                by_category
                    .entry(ItemCategory::Shop)
                    .or_default()
                    .push(item.clone());
                ids_by_category
                    .entry(ItemCategory::Shop)
                    .or_default()
                    .push(item.id);
            }

            if item.is_bait() {
                by_category
                    .entry(ItemCategory::Bait)
                    .or_default()
                    .push(item.clone());
                ids_by_category
                    .entry(ItemCategory::Bait)
                    .or_default()
                    .push(item.id);
            }

            if item.is_rod() {
                by_category
                    .entry(ItemCategory::Rod)
                    .or_default()
                    .push(item.clone());
                ids_by_category
                    .entry(ItemCategory::Rod)
                    .or_default()
                    .push(item.id);
            }
        });

        let by_category = by_category
            .into_iter()
            .map(|(k, v)| (k, Arc::new(v)))
            .collect();
        let ids_by_category = ids_by_category
            .into_iter()
            .map(|(k, v)| (k, Arc::new(v)))
            .collect();
        self.config.items_by_category = Arc::new(by_category);
        self.config.item_ids_by_category = Arc::new(ids_by_category);
    }

    // Validation
    pub fn validate(&self) -> ConfigValidationReport {
        let mut report = ConfigValidationReport::new();
        self.validate_species(&mut report);
        self.validate_locations(&mut report);
        self.validate_items(&mut report);
        report
    }

    fn validate_locations(&self, report: &mut ConfigValidationReport) {
        for location_data in self.config.locations.values() {
            for required_location_id in &location_data.required_locations_unlocked {
                self.validate_locations_required_locations(
                    report,
                    location_data,
                    *required_location_id,
                );
            }

            for required_species_id in &location_data.required_species_caught {
                self.validate_locations_required_species(
                    report,
                    location_data,
                    *required_species_id,
                )
            }
        }
    }

    fn validate_locations_required_locations(
        &self,
        report: &mut ConfigValidationReport,
        location_data: &Arc<LocationData>,
        required_location_id: i32,
    ) {
        if self
            .config
            .get_location_data(required_location_id)
            .is_none()
        {
            report.add_error(ConfigValidationError::location_required_location(
                location_data.id,
                required_location_id,
            ));
        }
    }

    fn validate_locations_required_species(
        &self,
        report: &mut ConfigValidationReport,
        location_data: &Arc<LocationData>,
        required_species_id: i32,
    ) {
        if self.config.get_species_data(required_species_id).is_none() {
            report.add_error(ConfigValidationError::location_required_species(
                location_data.id,
                required_species_id,
            ));
        }
    }

    fn validate_species(&self, report: &mut ConfigValidationReport) {
        for species_data in self.config.species.values() {
            for encounter in &species_data.encounters {
                self.validate_species_encounters(report, species_data, encounter);
            }
        }
    }

    fn validate_species_encounters(
        &self,
        report: &mut ConfigValidationReport,
        species_data: &Arc<SpeciesData>,
        encounter_data: &EncounterData,
    ) {
        let location_id = encounter_data.location_id;
        if self.config.get_location_data(location_id).is_none() {
            report.add_error(ConfigValidationError::species_encounter_location(
                species_data.id,
                location_id,
            ));
        }
    }

    fn validate_items(&self, report: &mut ConfigValidationReport) {
        for item_data in self.config.items.values() {
            if item_data.max_count < 1 {
                report.add_error(ConfigValidationError::item_invalid_max_count(item_data.id));
            }

            if item_data.is_stackable() && item_data.max_count != 1 {
                report.add_error(ConfigValidationError::item_non_unique_not_stackable(
                    item_data.id,
                ));
            }
        }
    }
}