-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaCreaturesCreatureV3.go
180 lines (162 loc) · 7.03 KB
/
TibiaCreaturesCreatureV3.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
package main
import (
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Child of JSONData
type Creature struct {
Name string `json:"name"`
Race string `json:"race"`
ImageURL string `json:"image_url"`
Description string `json:"description"`
Behaviour string `json:"behaviour"`
Hitpoints int `json:"hitpoints"`
ImmuneTo []string `json:"immune"`
StrongAgainst []string `json:"strong"`
WeaknessAgainst []string `json:"weakness"`
BeParalysed bool `json:"be_paralysed"`
BeSummoned bool `json:"be_summoned"`
SummonMana int `json:"summoned_mana"`
BeConvinced bool `json:"be_convinced"`
ConvincedMana int `json:"convinced_mana"`
SeeInvisible bool `json:"see_invisible"`
ExperiencePoints int `json:"experience_points"`
IsLootable bool `json:"is_lootable"`
LootList []string `json:"loot_list"`
Featured bool `json:"featured"`
}
// The base includes two levels: Creature and Information
type CreatureResponse struct {
Creature Creature `json:"creature"`
Information Information `json:"information"`
}
var (
CreatureDataRegex = regexp.MustCompile(`.*;">(.*)<\/h2> <img src="(.*)"\/>.*<p>(.*)<\/p> <p>(.*)<\/p> <p>(.*)<\/p>.*`)
CreatureHitpointsRegex = regexp.MustCompile(`.*have (.*) hitpoints. (.*)`)
CreatureImmuneRegex = regexp.MustCompile(`.*are immune to (.*)`)
CreatureStrongRegex = regexp.MustCompile(`.*are strong against (.*)`)
CreatureWeakRegex = regexp.MustCompile(`.*are weak against (.*)`)
CreatureManaRequiredRegex = regexp.MustCompile(`.*It takes (.*) mana to (.*)`)
CreatureLootRegex = regexp.MustCompile(`.*yield (.*) experience.*carry (.*)with them.`)
)
func TibiaCreaturesCreatureV3Impl(race string, BoxContentHTML string) CreatureResponse {
// local strings used in this function
var localDamageString = " damage"
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
log.Fatal(err)
}
// Getting data
InnerTableContainerTMP1, err := ReaderHTML.Find(".BoxContent div").First().NextAll().Html()
if err != nil {
log.Fatal(err)
}
// Regex to get data
subma1 := CreatureDataRegex.FindAllStringSubmatch(InnerTableContainerTMP1, -1)
// Preparing vars
var (
CreatureName, CreatureImageURL, CreatureDescription, CreatureBehaviour string
CreatureLootList, CreatureImmuneTo, CreatureStrongAgainst, CreatureWeaknessAgainst []string
CreatureHitpoints, CreatureSummonedMana, CreatureConvincedMana, CreatureExperiencePoints int
CreatureBeParalysed, CreatureBeSummoned, CreatureBeConvinced, CreatureSeeInvisible, CreatureIsLootable, CreatureIsBoosted bool
)
//Find boosted creature
boostedMonsterTitle, boostedCreatureFound := ReaderHTML.Find("#Monster").First().Attr("title")
if boostedCreatureFound {
boostedCreatureRace := boostedMonsterTitle[strings.Index(boostedMonsterTitle, ": ")+2:]
CreatureIsBoosted = boostedCreatureRace == race
}
// Preparing data for JSONData
if len(subma1) > 0 {
CreatureName = TibiaDataSanitizeEscapedString(subma1[0][1])
CreatureImageURL = subma1[0][2]
// Description (and unescape hmtl string)
CreatureDescription = strings.ReplaceAll(subma1[0][3], "<br/>", "\n")
CreatureDescription = TibiaDataSanitizeEscapedString(CreatureDescription)
// Behaviour
// Regex to get data..
subma2 := CreatureHitpointsRegex.FindAllStringSubmatch(subma1[0][4], -1)
// Add data to vars
CreatureHitpoints = TibiadataStringToIntegerV3(subma2[0][1])
CreatureBehaviour = subma2[0][2]
if !strings.Contains(subma1[0][4], "cannot be paralysed") {
CreatureBeParalysed = true
}
if strings.Contains(subma1[0][4], "sense invisible creatures") {
CreatureSeeInvisible = true
}
if strings.Contains(subma1[0][4], " are immune to ") {
subma21 := CreatureImmuneRegex.FindAllStringSubmatch(subma1[0][4], -1)
CreatureImmuneToTmp := strings.Split(subma21[0][1], localDamageString)
CreatureImmuneTo = strings.Split(strings.Replace(CreatureImmuneToTmp[0], " and ", ", ", 1), ", ")
}
if strings.Contains(subma1[0][4], " are strong against ") {
subma22 := CreatureStrongRegex.FindAllStringSubmatch(subma1[0][4], -1)
CreatureStrongAgainstTmp := strings.Split(subma22[0][1], localDamageString)
CreatureStrongAgainst = strings.Split(strings.Replace(CreatureStrongAgainstTmp[0], " and ", ", ", 1), ", ")
}
if strings.Contains(subma1[0][4], " are weak against ") {
subma23 := CreatureWeakRegex.FindAllStringSubmatch(subma1[0][4], -1)
CreatureWeaknessAgainstTmp := strings.Split(subma23[0][1], localDamageString)
CreatureWeaknessAgainst = strings.Split(strings.Replace(CreatureWeaknessAgainstTmp[0], " and ", ", ", 1), ", ")
}
if strings.Contains(subma1[0][4], "It takes ") && strings.Contains(subma1[0][4], " mana to ") {
subma24 := CreatureManaRequiredRegex.FindAllStringSubmatch(subma1[0][4], -1)
subma2402 := subma24[0][2]
if strings.Contains(subma2402, "convince these creatures but they cannot be") {
CreatureBeConvinced = true
CreatureConvincedMana = TibiadataStringToIntegerV3(subma24[0][1])
} else if strings.Contains(subma2402, "summon or convince these creatures") {
CreatureBeSummoned = true
CreatureSummonedMana = TibiadataStringToIntegerV3(subma24[0][1])
CreatureBeConvinced = true
CreatureConvincedMana = TibiadataStringToIntegerV3(subma24[0][1])
}
}
// Loot
// Regex to get loot information
subma3 := CreatureLootRegex.FindAllStringSubmatch(subma1[0][5], -1)
// Adding data to vars
CreatureExperiencePoints = TibiadataStringToIntegerV3(subma3[0][1])
if subma3[0][2] != "nothing" {
CreatureIsLootable = true
CreatureLootListTmp := strings.Split(strings.Replace(strings.Replace(subma3[0][2], "items ", "", 1), " and sometimes other ", "", 1), ", ")
for _, str := range CreatureLootListTmp {
if str != "" {
CreatureLootList = append(CreatureLootList, str)
}
}
}
}
// Build the data-blob
return CreatureResponse{
Creature{
Name: CreatureName,
Race: race,
ImageURL: CreatureImageURL,
Description: CreatureDescription,
Behaviour: CreatureBehaviour,
Hitpoints: CreatureHitpoints,
ImmuneTo: CreatureImmuneTo,
StrongAgainst: CreatureStrongAgainst,
WeaknessAgainst: CreatureWeaknessAgainst,
BeParalysed: CreatureBeParalysed,
BeSummoned: CreatureBeSummoned,
SummonMana: CreatureSummonedMana,
BeConvinced: CreatureBeConvinced,
ConvincedMana: CreatureConvincedMana,
SeeInvisible: CreatureSeeInvisible,
ExperiencePoints: CreatureExperiencePoints,
IsLootable: CreatureIsLootable,
LootList: CreatureLootList,
Featured: CreatureIsBoosted,
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}