-
Notifications
You must be signed in to change notification settings - Fork 1
/
Inventory.go
172 lines (140 loc) · 4.16 KB
/
Inventory.go
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
package main
import (
"encoding/xml"
"fmt"
"github.com/daviddengcn/go-colortext"
"io"
"os"
"strings"
)
type Inventory struct {
items map[string][]Item_I
}
//=================== CONSTRUCTORS =====================//
func NewInvXML() *InventoryXML {
invXML := new(InventoryXML)
invXML.Items = make([]interface{}, 10)
return invXML
}
func NewInventoryFromXML(invXml *InventoryXML) *Inventory {
inv := NewInventory()
for _, itm := range invXml.Items {
inv.AddItem(itm.(ItemXML_I).ToItem())
}
return inv
}
func NewInventory() *Inventory {
i := new(Inventory)
i.items = make(map[string][]Item_I)
return i
}
//================== CLASS FUNCTIONS =============//
//AddInventory will ad all items from the given inventory into this one.
func (inv *Inventory) AddInventory(otherInv *Inventory) {
for _, items := range otherInv.items {
inv.AddItems(items)
}
}
//AddItems will add all items in the provided splice to the inventory.
func (inv *Inventory) AddItems(items []Item_I) {
for _, item := range items {
inv.AddItem(item)
}
}
//AddItem will add the supplied item to the inventory.
func (inv *Inventory) AddItem(item Item_I) {
if val, ok := inv.items[item.GetName()]; ok { // the item is already there
inv.items[item.GetName()] = append(val, item)
} else {
inv.items[item.GetName()] = make([]Item_I, 0)
inv.AddItem(item)
}
}
//PossesItem returns true if the inventory contains an item with the given name, else false.
func (inv *Inventory) PossesItem(name string) bool {
_, found := inv.GetItem(name)
return found
}
//RemoveItem removes an item with the specified name from the inventory.
//If the item was not present then no change is made.
func (inv *Inventory) RemoveItem(name string) bool {
if _, found := inv.GetItem(name); found {
if len(inv.items[name]) == 1 {
delete(inv.items, name)
} else {
items := inv.items[name]
inv.items[name] = items[0 : len(items)-1]
}
return true
} else {
fmt.Fprintln(os.Stderr, "Failed to remove: "+name+", from inventory.")
return false
}
}
//GetAndRemoveItem returns a pointer to the item in the inventory with the specified
//name and removes it from the inventory.
//If the item is not found the pointer is nil and bool is false.
func (inv *Inventory) GetAndRemoveItem(name string) (Item_I, bool) {
if item, found := inv.GetItem(name); found {
return item, inv.RemoveItem(item.GetName())
} else {
return nil, false
}
}
//GetItem returns a pointer to the item in the inventory with the name in name.
//If the item is found it is not removed from the inventory.
//If the item is not found the pointer is nil and bool is false.
func (inv *Inventory) GetItem(name string) (Item_I, bool) {
for itmName, items := range inv.items {
itmName = strings.ToLower(itmName)
if strings.Contains(itmName, strings.ToLower(name)) && len(items) > 0 {
return items[len(items)-1], true
}
}
return nil, false
}
func (inv *Inventory) GetInventoryPage() []FormattedString {
desc := newFormattedStringCollection()
desc.addMessage2("\nItems\n")
desc.addMessage2(fmt.Sprintf("\t%-20s %3s\n", "Item Name", "Qty"))
desc.addMessage(ct.Green, "-----------------------------------------\n")
for name, itemEntry := range inv.items {
desc.addMessage(ct.Green, fmt.Sprintf("\t%-20s %3d", name, len(itemEntry))+"\n")
}
desc.addMessage2("\n")
return desc.fmtedStrings
}
func (inv *Inventory) toXML() *InventoryXML {
invXML := NewInvXML()
for _, items := range inv.items {
for _, item := range items {
invXML.Items = append(invXML.Items, item.ToXML())
}
}
return invXML
}
//================== XML STUFF =============//
type InventoryXML struct {
XMLName xml.Name `xml:"Inventory"`
Items []interface{} `xml:",any"`
}
func (c *InventoryXML) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var item ItemXML_I
for t, err := d.Token(); err != io.EOF; {
switch t1 := t.(type) {
case xml.StartElement:
if t1.Name.Local == "Armour" {
item = new(ArmourXML)
} else if t1.Name.Local == "Weapon" {
item = new(WeaponXML)
} else {
item = new(ItemXML)
}
err = d.DecodeElement(item, &t1)
checkError(err, true)
c.Items = append(c.Items, item)
}
t, err = d.Token()
}
return nil
}