poke_data/models/
berry.rs

1use crate::data::link_context::LinkContext;
2use crate::data::linkable::Linkable;
3use crate::models::berry_firmness::{BerryFirmness, BerryFirmnessId};
4use crate::models::berry_flavor::{BerryFlavor, BerryFlavorId};
5use crate::models::item::{Item, ItemId};
6use crate::models::localized_names::LocalizedStrings;
7use crate::traits::has_identifier::HasIdentifier;
8use crate::traits::has_localized_names::HasLocalizedNames;
9use serde::{Deserialize, Serialize};
10use std::sync::Arc;
11
12pub type BerryId = u16;
13
14#[derive(Debug)]
15pub struct Berry {
16    pub id: BerryId,
17    pub item: Arc<Item>,
18    pub firmness: Arc<BerryFirmness>,
19    pub size: u16,
20    pub max_harvest: u8,
21    pub growth_time: u8,
22    pub soil_dryness: u8,
23    pub smoothness: u8,
24    pub flavors: Vec<(Arc<BerryFlavor>, u8)>,
25    // ToDo: Natural Gift Power
26    // ToDo: Natural Gift Type
27}
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct UnlinkedBerry {
31    pub id: BerryId,
32    pub item_id: ItemId,
33    pub firmness_id: BerryFirmnessId,
34    pub size: u16,
35    pub max_harvest: u8,
36    pub growth_time: u8,
37    pub soil_dryness: u8,
38    pub smoothness: u8,
39    pub flavors: Vec<(BerryFlavorId, u8)>,
40}
41
42impl Linkable for UnlinkedBerry {
43    type Linked = Arc<Berry>;
44
45    fn link(&self, context: &LinkContext) -> Self::Linked {
46        let item = context
47            .items
48            .get(&self.item_id)
49            .unwrap_or_else(|| panic!("No item '{}' found for berry '{}'", self.item_id, self.id))
50            .clone();
51
52        let firmness = context
53            .berry_firmnesses
54            .get(&self.firmness_id)
55            .unwrap_or_else(|| {
56                panic!(
57                    "No firmness '{}' found for berry '{}'",
58                    self.firmness_id, self.id
59                )
60            })
61            .clone();
62
63        let flavors = self
64            .flavors
65            .iter()
66            .map(|(flavor_id, amount)| {
67                let flavor = context
68                    .berry_flavors
69                    .get(flavor_id)
70                    .unwrap_or_else(|| {
71                        panic!(
72                            "No berry flavor '{}' found for berry '{}'",
73                            flavor_id, self.id
74                        )
75                    })
76                    .clone();
77                (flavor, *amount)
78            })
79            .collect();
80
81        let berry = Berry {
82            id: self.id,
83            item,
84            firmness,
85            size: self.size,
86            max_harvest: self.max_harvest,
87            growth_time: self.growth_time,
88            soil_dryness: self.soil_dryness,
89            smoothness: self.smoothness,
90            flavors,
91        };
92
93        Arc::new(berry)
94    }
95}
96
97impl HasIdentifier for Berry {
98    fn identifier(&self) -> &str {
99        &self.item.identifier
100    }
101}
102
103impl HasLocalizedNames for Berry {
104    fn localized_names(&self) -> &LocalizedStrings {
105        &self.item.names
106    }
107}