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
use crate::resources::market::*;
use oxygengine_core::{
    prefab::{Prefab, PrefabComponent},
    Scalar,
};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

#[derive(Debug, Clone)]
pub enum InventoryError {
    InventoryFull,
    EmptyItem(MarketItemId),
    ItemDoesNotExists(MarketItemId),
    /// (item id, owned item count, removing item count)
    RemovingTooMuchItems(MarketItemId, usize, usize),
    /// (item id, item count)
    CannotGiveItem(MarketItemId, usize),
    /// (item id, item count)
    CannotReceiveItem(MarketItemId, usize),
}

#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct Inventory {
    capacity_weight: Option<Scalar>,
    items: HashMap<MarketItemId, usize>,
    items_resize: usize,
}

impl Inventory {
    pub fn new(capacity_weight: Option<Scalar>) -> Self {
        Self::with_resize(capacity_weight, 10)
    }

    pub fn with_resize(capacity_weight: Option<Scalar>, items_resize: usize) -> Self {
        Self {
            capacity_weight,
            items: HashMap::with_capacity(items_resize),
            items_resize,
        }
    }

    pub fn items(&self) -> impl Iterator<Item = (MarketItemId, usize)> + '_ {
        self.items.iter().map(|(id, count)| (*id, *count))
    }

    pub fn contains(&self, id: MarketItemId) -> Option<usize> {
        self.items.get(&id).copied()
    }

    pub fn can_add<T, V>(
        &self,
        id: MarketItemId,
        count: usize,
        database: &MarketDatabase<T, V>,
    ) -> bool
    where
        T: std::fmt::Debug + Clone + Send + Sync,
        V: Currency + std::fmt::Debug + Clone + Send + Sync,
    {
        count != 0
            && database
                .item(id)
                .map(|item| {
                    self.capacity_weight
                        .map(|capacity_weight| item.weight * count as Scalar <= capacity_weight)
                        .unwrap_or(true)
                })
                .unwrap_or_default()
    }

    pub fn add<T, V>(
        &mut self,
        id: MarketItemId,
        count: usize,
        database: &MarketDatabase<T, V>,
    ) -> Result<(), InventoryError>
    where
        T: std::fmt::Debug + Clone + Send + Sync,
        V: Currency + std::fmt::Debug + Clone + Send + Sync,
    {
        if count == 0 {
            return Err(InventoryError::EmptyItem(id));
        }
        let item = match database.item(id) {
            Some(item) => item,
            None => return Err(InventoryError::ItemDoesNotExists(id)),
        };
        if let Some(capacity_weight) = self.capacity_weight {
            if item.weight * count as Scalar > capacity_weight {
                return Err(InventoryError::InventoryFull);
            }
        }
        *self.items.entry(id).or_default() += count;
        Ok(())
    }

    pub fn can_remove(&self, id: MarketItemId, count: usize) -> bool {
        count != 0
            && self
                .items
                .get(&id)
                .copied()
                .and_then(|c| c.checked_sub(count))
                .is_some()
    }

    pub fn remove(&mut self, id: MarketItemId, count: usize) -> Result<(), InventoryError> {
        if count == 0 {
            return Err(InventoryError::EmptyItem(id));
        }
        if let Some(c) = self.items.get(&id).copied() {
            if let Some(c) = c.checked_sub(count) {
                if c == 0 {
                    self.items.remove(&id);
                    return Ok(());
                }
                self.items.insert(id, c);
                return Ok(());
            }
            return Err(InventoryError::RemovingTooMuchItems(id, c, count));
        }
        Err(InventoryError::ItemDoesNotExists(id))
    }

    pub fn transfer<T, V>(
        &mut self,
        receiver: &mut Self,
        id: MarketItemId,
        count: usize,
        database: &MarketDatabase<T, V>,
    ) -> Result<(), InventoryError>
    where
        T: std::fmt::Debug + Clone + Send + Sync,
        V: Currency + std::fmt::Debug + Clone + Send + Sync,
    {
        if !self.can_remove(id, count) {
            return Err(InventoryError::CannotGiveItem(id, count));
        }
        if !receiver.can_add(id, count, database) {
            return Err(InventoryError::CannotReceiveItem(id, count));
        }
        self.remove(id, count).unwrap();
        receiver.add(id, count, database).unwrap();
        Ok(())
    }
}

impl Prefab for Inventory {}
impl PrefabComponent for Inventory {}