-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathArmour.go
183 lines (138 loc) · 3.82 KB
/
Armour.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
173
174
175
176
177
178
179
180
181
182
183
// Armour
package main
import (
"encoding/xml"
"fmt"
"github.com/daviddengcn/go-colortext"
"strconv"
"strings"
)
type Armour struct {
Item
defense int
wearLocation string
}
//------------------- CONSTRUCTORS ------------------------//
func NewArmour(name1 string, descr string, def int, wearLoc string) Armour {
a := Armour{defense: def, wearLocation: strings.ToLower(wearLoc)}
a.name = name1
a.description = descr
return a
}
func NewArmourFromXML(armourData *ArmourXML) *Armour {
arm := new(Armour)
arm.Item = *NewItem(armourData.ItemInfo)
arm.defense = armourData.Defense
arm.wearLocation = strings.ToLower(armourData.WearLocation)
return arm
}
//------------------- GETTERS -----------------------------//
func (arm *Armour) GetType() int {
return ARMOUR
}
func (arm *Armour) GetCopy() Item_I {
armr := new(Armour)
*armr = *arm
return armr
}
func (arm *Armour) ToXML() ItemXML_I {
armXML := new(ArmourXML)
armXML.ItemInfo = arm.Item.ToXML().(*ItemXML)
armXML.Defense = arm.defense
armXML.WearLocation = arm.wearLocation
return armXML
}
func (a ArmourXML) ToItem() Item_I {
return NewArmourFromXML(&a)
}
//=================== ARMOURSET CLASS =====================//
var locations = [...]string{"head", "chest", "legs", "feet", "hands"}
type ArmourSet struct {
equipedArmour map[string]*Armour
}
//------------------- CONSTRUCTORS ------------------------//
func NewArmourSet() *ArmourSet {
as := new(ArmourSet)
as.equipedArmour = make(map[string]*Armour, 5)
return as
}
func NewArmourSetFromXML(armourSetData *ArmourSetXML) *ArmourSet {
as := NewArmourSet()
for _, arm := range armourSetData.ArmSet {
as.EquipArmour(NewArmourFromXML(&arm))
}
return as
}
//=================== CLASS FUNCTIONS =====================//
func (as *ArmourSet) GetDefense() int {
defense := 0
for _, armr := range as.equipedArmour {
defense += armr.defense
}
return defense
}
func (as *ArmourSet) GetAndRemoveArmour(nameOrLocation string) *Armour {
if IsLocation(nameOrLocation) {
loc := nameOrLocation
arm := as.equipedArmour[loc]
delete(as.equipedArmour, loc)
return arm
} else {
name := nameOrLocation
for loc, armr := range as.equipedArmour {
lcName := strings.ToLower(armr.name)
if strings.Contains(lcName, name) {
as.GetAndRemoveArmour(loc)
return armr
}
}
}
return nil
}
func (as *ArmourSet) EquipArmour(arm *Armour) {
as.equipedArmour[arm.wearLocation] = arm
}
func (as *ArmourSet) GetArmourWornPage() []FormattedString {
output := newFormattedStringCollection()
output.addMessage(ct.Green, "\t\t\tEquipped Armour\n")
output.addMessage2(fmt.Sprintf("\t%-20s %-20s %-20s\n", "Location", "Name", "Defense"))
output.addMessage(ct.Green, "--------------------------------------------------------------------\n")
for _, loc := range locations {
if arm, found := as.equipedArmour[loc]; found {
output.addMessage2(fmt.Sprintf("\t%-20s %-20s %-20s\n", loc, arm.name, strconv.Itoa(arm.defense)))
} else {
output.addMessage2(fmt.Sprintf("\t%-20s %-20s %-20s\n", loc, " ", "0"))
}
}
return output.fmtedStrings
}
func (as *ArmourSet) IsArmourAt(loc string) bool {
_, present := as.equipedArmour[loc]
return present
}
func (as *ArmourSet) ToXML() *ArmourSetXML {
asXML := new(ArmourSetXML)
for _, arm := range as.equipedArmour {
asXML.ArmSet = append(asXML.ArmSet, *arm.ToXML().(*ArmourXML))
}
return asXML
}
func IsLocation(loc string) bool {
for _, element := range locations {
if loc == element {
return true
}
}
return false
}
//=================== XML STUFF =====================//
type ArmourSetXML struct {
XMLName xml.Name `xml:"ArmourSet"`
ArmSet []ArmourXML `xml:"Armour"`
}
type ArmourXML struct {
XMLName xml.Name `xml:"Armour"`
ItemInfo *ItemXML `xml:"Item"`
Defense int `xml:"Defense"`
WearLocation string `xml:"Location"`
}