-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaSpellsSpellV3.go
310 lines (275 loc) · 9.68 KB
/
TibiaSpellsSpellV3.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package main
import (
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Child of SpellInformation
type SpellInformation struct {
Formula string `json:"formula"`
Vocation []string `json:"vocation"`
GroupAttack bool `json:"group_attack"`
GroupHealing bool `json:"group_healing"`
GroupSupport bool `json:"group_support"`
TypeInstant bool `json:"type_instant"`
TypeRune bool `json:"type_rune"`
DamageType string `json:"damage_type"`
CooldownAlone int `json:"cooldown_alone"`
CooldownGroup int `json:"cooldown_group"`
SoulPoints int `json:"soul_points"`
Amount int `json:"amount"`
Level int `json:"level"`
Mana int `json:"mana"`
Price int `json:"price"`
City []string `json:"city"`
Premium bool `json:"premium_only"`
}
// Child of RuneInformation
type RuneInformation struct {
Vocation []string `json:"vocation"`
GroupAttack bool `json:"group_attack"`
GroupHealing bool `json:"group_healing"`
GroupSupport bool `json:"group_support"`
DamageType string `json:"damage_type"`
Level int `json:"level"`
MagicLevel int `json:"magic_level"`
}
// Child of Spells
type SpellData struct {
Name string `json:"name"`
Spell string `json:"spell_id"`
ImageURL string `json:"image_url"`
Description string `json:"description"`
HasSpellInformation bool `json:"has_spell_information"`
SpellInformation SpellInformation `json:"spell_information"`
HasRuneInformation bool `json:"has_rune_information"`
RuneInformation RuneInformation `json:"rune_information"`
}
// Child of JSONData
type SpellsContainer struct {
Spell SpellData `json:"spell"`
}
//
// The base includes two levels: Spell and Information
type SpellInformationResponse struct {
Spells SpellsContainer `json:"spells"`
Information Information `json:"information"`
}
var (
SpellDataRowRegex = regexp.MustCompile(`<td.*>(.*):<\/td><td.*>(.*)<\/td>`)
SpellNameAndImageRegex = regexp.MustCompile(`<td><img src="(.*)" width=.*<h2>(.*)<\/h2>.*`)
SpellCooldownRegex = regexp.MustCompile(`([0-9]+)s \(.*:.([0-9]+)s\)`)
SpellDescriptionRegex = regexp.MustCompile(`(.*)\.(Spell|Rune) InformationName:.*`)
)
// TibiaSpellsSpellV3 func
func TibiaSpellsSpellV3Impl(spell string, BoxContentHTML string) SpellInformationResponse {
//TODO: There is currently a bug with description, it always comes back empty
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
log.Fatal(err)
}
var (
// creating empty vars for later use
SpellsInfoVocation, SpellsInfoCity, RuneInfoVocation []string
// var SpellsInfoName, RuneInfoName string
SpellInformationSection, SpellName, SpellImageURL, SpellDescription, SpellsInfoFormula, SpellsInfoDamageType, RuneInfoDamageType string
SpellsInfoCooldownAlone, SpellsInfoCooldownGroup, SpellsInfoSoulPoints, SpellsInfoAmount, SpellsInfoLevel, SpellsInfoMana, SpellsInfoPrice, RuneInfoLevel, RuneInfoMagicLevel int
SpellsInfoGroupAttack, SpellsInfoGroupHealing, SpellsInfoGroupSupport, SpellsInfoTypeInstant, SpellsInfoTypeRune, RuneInfoGroupAttack, RuneInfoGroupHealing, RuneInfoGroupSupport, SpellsInfoPremium, SpellsHasSpellSection, SpellsHasRuneSection bool
)
ReaderHTML.Find(".BoxContent").Each(func(index int, s *goquery.Selection) {
NameAndImageSection, _ := s.Find("table tr").First().Html()
// Get the name and image
subma2 := SpellNameAndImageRegex.FindAllStringSubmatch(NameAndImageSection, -1)
if len(subma2) > 0 {
SpellName = subma2[0][2]
SpellImageURL = subma2[0][1]
}
s.Find(".TableContainer").Each(func(index int, s *goquery.Selection) {
SectionName := s.Find(".CaptionInnerContainer div.Text").Text()
// Determine if this is the spell or rune section
if SectionName == "Spell Information" {
SpellInformationSection = "spell"
SpellsHasSpellSection = true
} else if SectionName == "Rune Information" {
SpellInformationSection = "rune"
SpellsHasRuneSection = true
}
// Running query over each div
s.Find("table.Table2 tbody tr").Each(func(index int, s *goquery.Selection) {
// Storing HTML into SpellDivHTML
SpellDivHTML, err := s.Html()
if err != nil {
log.Fatal(err)
}
subma1 := SpellDataRowRegex.FindAllStringSubmatch(SpellDivHTML, -1)
// check if regex return length is over 0 and the match of name is over 1
if len(subma1) > 0 {
// Creating easy to use vars (and unescape hmtl right string)
LeftColumn := subma1[0][1]
RightColumn := TibiaDataSanitizeEscapedString(subma1[0][2])
// Formula
if LeftColumn == "Formula" {
SpellsInfoFormula = TibiaDataSanitizeDoubleQuoteString(RightColumn)
}
// Vocation
if LeftColumn == "Vocation" {
switch SpellInformationSection {
case "spell":
SpellsInfoVocation = strings.Split(RightColumn, ", ")
case "rune":
RuneInfoVocation = strings.Split(RightColumn, ", ")
}
}
// Group information
if LeftColumn == "Group" {
switch SpellInformationSection {
case "spell":
switch RightColumn {
case "Attack":
SpellsInfoGroupAttack = true
case "Healing":
SpellsInfoGroupHealing = true
case "Support":
SpellsInfoGroupSupport = true
}
case "rune":
switch RightColumn {
case "Attack":
RuneInfoGroupAttack = true
case "Healing":
RuneInfoGroupHealing = true
case "Support":
RuneInfoGroupSupport = true
}
}
}
// Spell type
if LeftColumn == "Type" {
switch RightColumn {
case "Instant":
SpellsInfoTypeInstant = true
case "Rune":
SpellsInfoTypeRune = true
}
}
// Damage
if LeftColumn == "Damage Type" || LeftColumn == "Magic Type" {
switch SpellInformationSection {
case "spell":
SpellsInfoDamageType = strings.ToLower(RightColumn)
case "rune":
RuneInfoDamageType = strings.ToLower(RightColumn)
}
}
// Cooldown
if LeftColumn == "Cooldown" {
subma3 := SpellCooldownRegex.FindAllStringSubmatch(SpellDivHTML, -1)
if len(subma3) > 0 {
SpellsInfoCooldownAlone = TibiadataStringToIntegerV3(subma3[0][1])
SpellsInfoCooldownGroup = TibiadataStringToIntegerV3(subma3[0][2])
}
}
// Soul Points
if LeftColumn == "Soul Points" {
SpellsInfoSoulPoints = TibiadataStringToIntegerV3(RightColumn)
}
// Amount
if LeftColumn == "Amount" {
SpellsInfoAmount = TibiadataStringToIntegerV3(RightColumn)
}
// Experience Level
if LeftColumn == "Exp Lvl" {
switch SpellInformationSection {
case "spell":
SpellsInfoLevel = TibiadataStringToIntegerV3(RightColumn)
case "rune":
RuneInfoLevel = TibiadataStringToIntegerV3(RightColumn)
}
}
// Mana
if LeftColumn == "Mana" {
SpellsInfoMana = TibiadataStringToIntegerV3(RightColumn)
}
// Price
if LeftColumn == "Price" {
if RightColumn == "free" {
SpellsInfoPrice = 0
} else {
SpellsInfoPrice = TibiadataStringToIntegerV3(RightColumn)
}
}
// City
if LeftColumn == "City" {
SpellsInfoCity = strings.Split(RightColumn, ", ")
}
// Premium
if LeftColumn == "Premium" {
if RightColumn == "yes" {
SpellsInfoPremium = true
} else {
SpellsInfoPremium = false
}
}
// Magic level
if LeftColumn == "Mag Lvl" {
RuneInfoMagicLevel = TibiadataStringToIntegerV3(RightColumn)
}
}
})
})
})
// Getting the description
InnerTableContainerTMPB := ReaderHTML.Find(".BoxContent").Text()
subma4 := SpellDescriptionRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
if len(subma4) > 0 {
SpellDescription = subma4[0][1] + "."
}
//
// Build the data-blob
return SpellInformationResponse{
SpellsContainer{
SpellData{
Name: SpellName,
Spell: strings.ToLower(SpellName),
ImageURL: SpellImageURL,
Description: SpellDescription,
HasSpellInformation: SpellsHasSpellSection,
SpellInformation: SpellInformation{
Formula: SpellsInfoFormula,
Vocation: SpellsInfoVocation,
GroupAttack: SpellsInfoGroupAttack,
GroupHealing: SpellsInfoGroupHealing,
GroupSupport: SpellsInfoGroupSupport,
TypeInstant: SpellsInfoTypeInstant,
TypeRune: SpellsInfoTypeRune,
DamageType: SpellsInfoDamageType,
CooldownAlone: SpellsInfoCooldownAlone,
CooldownGroup: SpellsInfoCooldownGroup,
SoulPoints: SpellsInfoSoulPoints,
Amount: SpellsInfoAmount,
Level: SpellsInfoLevel,
Mana: SpellsInfoMana,
Price: SpellsInfoPrice,
City: SpellsInfoCity,
Premium: SpellsInfoPremium,
},
HasRuneInformation: SpellsHasRuneSection,
RuneInformation: RuneInformation{
Vocation: RuneInfoVocation,
GroupAttack: RuneInfoGroupAttack,
GroupHealing: RuneInfoGroupHealing,
GroupSupport: RuneInfoGroupSupport,
DamageType: RuneInfoDamageType,
Level: RuneInfoLevel,
MagicLevel: RuneInfoMagicLevel,
},
},
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}