-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathvalidation.go
More file actions
266 lines (212 loc) · 7.49 KB
/
Copy pathvalidation.go
File metadata and controls
266 lines (212 loc) · 7.49 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
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
266
package validation
import (
"encoding/json"
"errors"
"strings"
"sync"
"unicode/utf8"
"github.com/tibiadata/tibiadata-api-go/src/tibiamapping"
)
// validator is a local struct representing some validation data
type validator struct {
Worlds []string `json:"worlds"`
Towns []string `json:"towns"`
Houses []House `json:"houses"`
Creatures []Creature `json:"creatures"`
Spells []Spell `json:"spells"`
}
type Creature struct {
Endpoint string `json:"endpoint"`
PluralName string `json:"plural_name"`
Name string `json:"name"`
}
type Spell struct {
Name string `json:"name"`
Formula string `json:"formula"`
Endpoint string `json:"endpoint"`
}
// Houses represents a house
type House struct {
ID int `json:"house_id"`
Town string `json:"town"`
Type string `json:"type"`
}
var (
initiated bool // initiated reports whether the validator has already been initiated
val = validator{} // val is the local validator that will be read from to get the necessary data
locker = sync.RWMutex{} // locker is a locker to prevent InitiateValidator to be accessed concurrently
sha256sum string // sha256sum stores the sha256sum of the data.min.json file
sha512sum string // sha512sum stores the sha512sum of the data.min.json file
smallestCreatureName, biggestCreatureName, smallestCreatureWord, biggestCreatureWord string // smallest and biggest creature names and words
smallestCreatureNameRuneCount, biggestCreatureNameRuneCount, smallestCreatureWordRuneCount, biggestCreatureWordRuneCount int // smallest and biggest creature names and words rune count
smallestSpellNameOrFormula, biggestSpellNameOrFormula, smallestSpellWord, biggestSpellWord string // smalles and biggest spell names or formulas and words
smallestSpellNameOrFormulaRuneCount, biggestSpellNameOrFormulaRuneCount, smallestSpellWordRuneCount, biggestSpellWordRuneCount int // smallest and biggest creature names or formulas and words rune count
)
// Initiate initiates the validator, this should be called on the init() func
func Initiate(TibiaDataUserAgent string) error {
// Make sure InitiateValidator can not be called concurrently
locker.Lock()
defer locker.Unlock()
// Check if the validator has already been initiated
// as there is no need to initiate it twice
if initiated {
return ErrorAlreadyRunning
}
// Get the assets
tibiaMapping, err := tibiamapping.Run(TibiaDataUserAgent)
if err != nil {
panic(err)
}
// Check if we got a nil struct
if len(tibiaMapping.RawData) == 0 &&
tibiaMapping.Sha256Sum == "" &&
tibiaMapping.Sha512Sum == "" {
return errors.New("tibia mapping struct is nil")
}
bytes := tibiaMapping.RawData
sha256Fields := strings.Fields(tibiaMapping.Sha256Sum)
sha256sum = sha256Fields[2]
sha512Fields := strings.Fields(tibiaMapping.Sha512Sum)
sha512sum = sha512Fields[2]
// Check if the file is empty
if len(bytes) == 0 {
return errors.New("data.json file is empty")
}
// Unmarshal the json bytes into a go struct
err = json.Unmarshal(bytes, &val)
if err != nil {
return err
}
// Set non changing vars
setVars()
// The validator is properly initiated
initiated = true
return nil
}
func setVars() {
setCreaturesVars()
setSpellsVars()
}
// setCreaturesVars sets creatures vars
// this only needs to be called once as it will never change during runtime
func setCreaturesVars() {
if smallestCreatureName == "" {
smallestName := val.Creatures[0].Name
var smallestWord string
for _, creature := range val.Creatures {
if utf8.RuneCountInString(creature.Name) < utf8.RuneCountInString(smallestName) {
smallestName = creature.Name
}
fields := strings.Fields(creature.Name)
if utf8.RuneCountInString(smallestWord) == 0 {
smallestWord = fields[0]
}
for _, str := range fields {
if utf8.RuneCountInString(str) < utf8.RuneCountInString(smallestWord) {
smallestWord = str
}
}
if utf8.RuneCountInString(creature.Endpoint) < utf8.RuneCountInString(smallestWord) {
smallestWord = creature.Endpoint
}
}
smallestCreatureName = smallestName
smallestCreatureNameRuneCount = utf8.RuneCountInString(smallestName)
smallestCreatureWord = smallestWord
smallestCreatureWordRuneCount = utf8.RuneCountInString(smallestWord)
}
if biggestCreatureName == "" {
biggestName := val.Creatures[0].PluralName
var biggestWord string
for _, creature := range val.Creatures {
if utf8.RuneCountInString(creature.Name) > utf8.RuneCountInString(biggestName) {
biggestName = creature.PluralName
}
fields := strings.Fields(creature.Name)
if utf8.RuneCountInString(biggestWord) == 0 {
biggestWord = fields[0]
}
for _, str := range fields {
if utf8.RuneCountInString(str) > utf8.RuneCountInString(biggestWord) {
biggestWord = str
}
}
if utf8.RuneCountInString(creature.Endpoint) > utf8.RuneCountInString(biggestWord) {
biggestWord = creature.Endpoint
}
}
biggestCreatureName = biggestName
biggestCreatureNameRuneCount = utf8.RuneCountInString(biggestName)
biggestCreatureWord = biggestWord
biggestCreatureWordRuneCount = utf8.RuneCountInString(biggestWord)
}
}
// setSpellsVarss sets spells vars
// this only needs to be called once as it will never change during runtime
func setSpellsVars() {
if smallestSpellNameOrFormula == "" {
smallestName := val.Spells[0].Name
var smallestWord string
for _, spell := range val.Spells {
if len(spell.Name) < utf8.RuneCountInString(smallestName) {
smallestName = spell.Name
}
if len(spell.Formula) < utf8.RuneCountInString(smallestName) {
smallestName = spell.Formula
}
fieldsName := strings.Fields(spell.Name)
if utf8.RuneCountInString(smallestWord) == 0 {
smallestWord = fieldsName[0]
}
for _, str := range fieldsName {
if utf8.RuneCountInString(str) < utf8.RuneCountInString(smallestWord) {
smallestWord = str
}
}
fieldsFormula := strings.Fields(spell.Formula)
for _, str := range fieldsFormula {
if utf8.RuneCountInString(str) < utf8.RuneCountInString(smallestWord) {
smallestWord = str
}
}
}
smallestSpellNameOrFormula = smallestName
smallestSpellNameOrFormulaRuneCount = utf8.RuneCountInString(smallestName)
smallestSpellWord = smallestWord
smallestSpellWordRuneCount = utf8.RuneCountInString(smallestWord)
}
if biggestSpellNameOrFormula == "" {
biggestName := val.Spells[0].Name
var biggestWord string
for _, spell := range val.Spells {
if len(spell.Name) > utf8.RuneCountInString(biggestName) {
biggestName = spell.Name
}
if len(spell.Formula) > utf8.RuneCountInString(biggestName) {
biggestName = spell.Formula
}
fieldsName := strings.Fields(spell.Name)
if utf8.RuneCountInString(biggestWord) == 0 {
biggestWord = fieldsName[0]
}
for _, str := range fieldsName {
if utf8.RuneCountInString(str) > utf8.RuneCountInString(biggestWord) {
biggestWord = str
}
}
fieldsFormula := strings.Fields(spell.Formula)
for _, str := range fieldsFormula {
if utf8.RuneCountInString(str) > utf8.RuneCountInString(biggestWord) {
biggestWord = str
}
}
if utf8.RuneCountInString(spell.Endpoint) > utf8.RuneCountInString(biggestWord) {
biggestWord = spell.Endpoint
}
}
biggestSpellNameOrFormula = biggestName
biggestSpellNameOrFormulaRuneCount = utf8.RuneCountInString(biggestName)
biggestSpellWord = biggestWord
biggestSpellWordRuneCount = utf8.RuneCountInString(biggestWord)
}
}