-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinventory.go
More file actions
153 lines (122 loc) · 3.47 KB
/
Copy pathinventory.go
File metadata and controls
153 lines (122 loc) · 3.47 KB
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
package hoard
import (
"slices"
"sync"
)
// Inventory is a collection of items.
// It is used to store items that can be equipped.
// This interface is used internally by the [Hoard], [EquipDefault], and [EquipWithOption] functions.
// To create a custom inventory, use the [UseInventory] function.
type Inventory interface {
// Put adds an [Item] to the inventory.
// To get an [Item], refer to the [UseInventory] function.
//
// Example:
// Hoard(nil, UseInventory("test").Put(RememberAs("test", "test")))
Put(item Item) Inventory
// PutIfAbsent adds an [Item] to the inventory if it does not exist yet.
// The comparison is done by also comparing the alias of the item, hence if the alias is different, it will be considered a different item.
// To get an [Item], refer to the [UseInventory] function.
//
// Example:
// Hoard(nil, UseInventory("test").PutIfAbsent(RememberAs("test", "test")))
PutIfAbsent(item Item) Inventory
getName() string
// equip returns the itemImpl with the given name.
// Should only be used internally.
// Prefer using [EquipDefault] or [EquipWithOption] instead.
equip(name string) Item
merge(inventoryImpl Inventory) Inventory
loadout() func(func(string, Item) bool)
}
func newInventory(name string) Inventory {
return &inventoryImpl{
sortedKeys: make([]string, 0),
itemMap: make(map[string]Item),
name: name,
mu: sync.RWMutex{},
}
}
type inventoryImpl struct {
sortedKeys []string
itemMap map[string]Item
name string
mu sync.RWMutex
}
// Put adds an [Item] to the inventory.
// To get an [Item], refer to the [UseInventory] function.
//
// Example:
//
// Hoard(nil, UseInventory("test").Put(RememberAs("test", "test")))
func (b *inventoryImpl) Put(item Item) Inventory {
if item == nil {
return b
}
b.mu.Lock()
defer b.mu.Unlock()
b.itemMap[item.getName()] = item
b.sortedKeys = slices.DeleteFunc(b.sortedKeys, func(e string) bool {
return e == item.getName()
})
b.sortedKeys = append(b.sortedKeys, item.getName())
return b
}
// PutIfAbsent adds an [Item] to the inventory if it does not exist yet.
// The comparison is done by also comparing the alias of the item, hence if the alias is different, it will be considered adifferent item.
// To get an [Item], refer to the [UseInventory] function.
//
// Example:
//
// Hoard(nil, UseInventory("test").PutIfAbsent(RememberAs("test", "test")))
func (b *inventoryImpl) PutIfAbsent(item Item) Inventory {
if item == nil {
return b
}
b.mu.Lock()
defer b.mu.Unlock()
if _, ok := b.itemMap[item.getName()]; !ok {
b.itemMap[item.getName()] = item
b.sortedKeys = append(b.sortedKeys, item.getName())
}
return b
}
func (b *inventoryImpl) getName() string {
return b.name
}
func (b *inventoryImpl) equip(name string) Item {
b.mu.RLock()
defer b.mu.RUnlock()
v, ok := b.itemMap[name]
if !ok {
return nil
}
return v
}
func (b *inventoryImpl) merge(invent Inventory) Inventory {
if invent == nil {
return b
}
b.mu.Lock()
defer b.mu.Unlock()
for k, v := range invent.loadout() {
b.itemMap[k] = v
// re-insert the key to ensure the order is consistent
b.sortedKeys = slices.DeleteFunc(b.sortedKeys, func(e string) bool {
return e == k
})
b.sortedKeys = append(b.sortedKeys, k)
}
return b
}
func (b *inventoryImpl) loadout() func(func(string, Item) bool) {
return func(yield func(string, Item) bool) {
b.mu.RLock()
defer b.mu.RUnlock()
for _, k := range b.sortedKeys {
if !yield(k, b.itemMap[k]) {
break
}
}
}
}