-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskill.go
265 lines (212 loc) · 5.48 KB
/
skill.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package runequest
import (
"fmt"
"sort"
"time"
)
// Skill is a learned ability of an RPG Character
type Skill struct {
Name string
Category string
UserChoice bool
CoreString string
UserString string
Custom bool
Base int
CategoryValue int
HomelandValue int
OccupationValue int
CultValue int
CreationBonusValue int
Updates []*Update
Value int
InPlayXPValue int
Total int
Min int
Max int
ExperienceCheck bool
IsBase bool
}
// SkillChoice is a choice between 2 or more skills
type SkillChoice struct {
Skills []Skill
}
// UpdateSkill totals skill values based on input
func (s *Skill) UpdateSkill() {
s.GenerateName()
updates := 0
for _, u := range s.Updates {
updates += u.Value
}
s.Total = s.Base + s.HomelandValue + s.OccupationValue + s.CultValue + s.CreationBonusValue + s.InPlayXPValue + s.Value + updates
if s.Total > 0 {
s.Total += s.CategoryValue
}
}
// AddSkillUpdate adds an update to a skill
func (s *Skill) AddSkillUpdate(st string, v int) {
if s != nil {
t := time.Now()
tString := t.Format("2006-01-02 15:04:05")
update := &Update{
Date: tString,
Event: st,
Value: v,
}
if s.Updates == nil {
s.Updates = []*Update{}
}
s.Updates = append(s.Updates, update)
s.UpdateSkill()
fmt.Printf("Updated Character Skill: %s: %s\n", st, s.Name)
} else {
fmt.Println("Error - skill doesn't exist")
}
}
// GenerateName sets the skill map name
func (s *Skill) GenerateName() {
var n string
if s.UserString != "" {
n = fmt.Sprintf("%s (%s)", s.CoreString, s.UserString)
} else {
n = s.CoreString
}
s.Name = n
}
func (s *Skill) String() string {
s.UpdateSkill()
text := ""
text += fmt.Sprintf("%s %d%%", s.Name, s.Total)
return text
}
// ModifySkill adds or modifies a Skill value
func (c *Character) ModifySkill(s Skill) {
/*
var response string
if s.UserChoice {
// Show slice of existing skills with identical CoreString
q := fmt.Sprintf("Enter a specialization for %s or hit Enter to use (%s): ",
s.CoreString, s.UserString)
response = UserQuery(q)
if response == "" {
response = s.UserString
}
s.UserString = response
}
*/
s.GenerateName()
if c.Skills[s.Name] == nil {
// Create new Skill in map
c.Skills[s.Name] = &Skill{
Name: s.Name,
Category: s.Category,
CoreString: s.CoreString,
UserChoice: false,
UserString: s.UserString,
Base: s.Base,
CategoryValue: s.CategoryValue,
HomelandValue: s.HomelandValue,
OccupationValue: s.OccupationValue,
CultValue: s.CultValue,
CreationBonusValue: s.CreationBonusValue,
InPlayXPValue: s.InPlayXPValue,
Value: s.Value,
Total: s.Total,
Min: s.Min,
Max: s.Max,
ExperienceCheck: s.ExperienceCheck,
}
// Remove base skill from map
delete(c.Skills, s.CoreString)
} else {
// Modify existing skill
sk := c.Skills[s.Name]
if sk.Base < s.Base {
// Change Skill.Base if advantageous
sk.Base = s.Base
}
// Add or subtract s.XXValue from skill
// This doesn't work
sk.Value = s.Value
sk.HomelandValue = s.HomelandValue
sk.OccupationValue = s.OccupationValue
sk.CultValue = s.CultValue
sk.CreationBonusValue = s.CreationBonusValue
sk.InPlayXPValue = s.InPlayXPValue
}
}
// ApplySkillChoice executes the skill choice on a character
func (c *Character) ApplySkillChoice(sc SkillChoice, r int) {
// Select index from choice.Skills
selected := sc.Skills[r]
// Modify or add skill
c.ModifySkill(selected)
}
// Skills Functions
func sortedSkills(skills map[string]*Skill) []*Skill {
skillArray := []*Skill{}
for _, v := range skills {
skillArray = append(skillArray, v)
}
total := func(s1, s2 *Skill) bool {
return s1.Total > s2.Total
}
By(total).Sort(skillArray)
if len(skillArray) < 9 {
return skillArray
}
return skillArray
}
// By is the type of a "less" function that defines the ordering of its Planet arguments.
type By func(s1, s2 *Skill) bool
// Sort is a method on the function type, By, that sorts the argument slice according to the function.
func (by By) Sort(skills []*Skill) {
ss := &skillSorter{
skills: skills,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Sort(ss)
}
// skillSorter joins a By function and a slice of Planets to be sorted.
type skillSorter struct {
skills []*Skill
by func(p1, p2 *Skill) bool // Closure used in the Less method.
}
// Len is part of sort.Interface.
func (s *skillSorter) Len() int {
return len(s.skills)
}
// Swap is part of sort.Interface.
func (s *skillSorter) Swap(i, j int) {
s.skills[i], s.skills[j] = s.skills[j], s.skills[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *skillSorter) Less(i, j int) bool {
return s.by(s.skills[i], s.skills[j])
}
func formatSkillMap(sa map[string]*Skill) string {
text := ""
end := len(sa)
counter := 0
for _, v := range sa {
if counter+1 == end {
text += v.String()
} else {
text += v.String() + ", "
counter++
}
}
return text
}
func formatSkillArray(sa []*Skill) string {
text := ""
end := len(sa)
for i, v := range sa {
if i+1 == end {
text += v.String()
} else {
text += v.String() + ", "
}
}
return text
}