Skip to content

Commit 30f328f

Browse files
committed
Migration of codebase to GitHub
1 parent 2d08d38 commit 30f328f

11 files changed

+2986
-0
lines changed

src/TibiaCharactersCharacterV3.go

Lines changed: 556 additions & 0 deletions
Large diffs are not rendered by default.

src/TibiaCreaturesCreatureV3.go

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"regexp"
8+
"strings"
9+
10+
"github.com/PuerkitoBio/goquery"
11+
)
12+
13+
// TibiaCreaturesCreatureV3 func
14+
func TibiaCreaturesCreatureV3(race string) string {
15+
16+
// Child of JSONData
17+
type Creature struct {
18+
Name string `json:"name"`
19+
Race string `json:"race"`
20+
ImageURL string `json:"image_url"`
21+
Description string `json:"description"`
22+
Behaviour string `json:"behaviour"`
23+
Hitpoints int `json:"hitpoints"`
24+
ImmuneTo []string `json:"immune"`
25+
StrongAgainst []string `json:"strong"`
26+
WeaknessAgainst []string `json:"weakness"`
27+
BeParalysed bool `json:"be_paralysed"`
28+
BeSummoned bool `json:"be_summoned"`
29+
SummonMana int `json:"summoned_mana"`
30+
BeConvinced bool `json:"be_convinced"`
31+
ConvincedMana int `json:"convinced_mana"`
32+
SeeInvisible bool `json:"see_invisible"`
33+
ExperiencePoints int `json:"experience_points"`
34+
IsLootable bool `json:"is_lootable"`
35+
LootList []string `json:"loot_list"`
36+
Featured bool `json:"featured"`
37+
}
38+
39+
//
40+
// The base includes two levels: Creature and Information
41+
type JSONData struct {
42+
Creature Creature `json:"creature"`
43+
Information Information `json:"information"`
44+
}
45+
46+
// Getting data with TibiadataHTMLDataCollectorV3
47+
BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/library/?subtopic=creatures&race=" + TibiadataQueryEscapeStringV3(race))
48+
49+
// Loading HTML data into ReaderHTML for goquery with NewReader
50+
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
51+
if err != nil {
52+
log.Fatal(err)
53+
}
54+
55+
// Getting data
56+
InnerTableContainerTMP1, err := ReaderHTML.Find(".BoxContent div").First().NextAll().Html()
57+
if err != nil {
58+
log.Fatal(err)
59+
}
60+
61+
// Regex to get data
62+
regex1 := regexp.MustCompile(`.*;">(.*)<\/h2> <img src="(.*)"\/>.*<p>(.*)<\/p> <p>(.*)<\/p> <p>(.*)<\/p>.*`)
63+
subma1 := regex1.FindAllStringSubmatch(InnerTableContainerTMP1, -1)
64+
65+
// Preparing vars
66+
var CreatureDescription, CreatureBehaviour string
67+
var CreatureLootList, CreatureImmuneTo, CreatureStrongAgainst, CreatureWeaknessAgainst []string
68+
var CreatureHitpoints, CreatureSummonedMana, CreatureConvincedMana, CreatureExperiencePoints int
69+
var CreatureBeParalysed, CreatureBeSummoned, CreatureBeConvinced, CreatureSeeInvisible, CreatureIsLootable bool
70+
71+
// Preparing data for JSONData
72+
if len(subma1) > 0 {
73+
74+
// Description
75+
CreatureDescription = strings.ReplaceAll(subma1[0][3], "<br/>", "\n")
76+
77+
// Behaviour
78+
// Regex to get data..
79+
regex2 := regexp.MustCompile(`.*have (.*) hitpoints. (.*)`)
80+
subma2 := regex2.FindAllStringSubmatch(subma1[0][4], -1)
81+
// Add data to vars
82+
CreatureHitpoints = TibiadataStringToIntegerV3(subma2[0][1])
83+
CreatureBehaviour = subma2[0][2]
84+
if !strings.Contains(subma1[0][4], "cannot be paralysed") {
85+
CreatureBeParalysed = true
86+
}
87+
if strings.Contains(subma1[0][4], "sense invisible creatures") {
88+
CreatureSeeInvisible = true
89+
}
90+
if strings.Contains(subma1[0][4], " are immune to ") {
91+
regex21 := regexp.MustCompile(`.*are immune to (.*)`)
92+
subma21 := regex21.FindAllStringSubmatch(subma1[0][4], -1)
93+
CreatureImmuneToTmp := strings.Split(subma21[0][1], " damage")
94+
CreatureImmuneTo = strings.Split(strings.Replace(CreatureImmuneToTmp[0], " and ", ", ", 1), ", ")
95+
}
96+
if strings.Contains(subma1[0][4], " are strong against ") {
97+
regex22 := regexp.MustCompile(`.*are strong against (.*)`)
98+
subma22 := regex22.FindAllStringSubmatch(subma1[0][4], -1)
99+
CreatureStrongAgainstTmp := strings.Split(subma22[0][1], " damage")
100+
CreatureStrongAgainst = strings.Split(strings.Replace(CreatureStrongAgainstTmp[0], " and ", ", ", 1), ", ")
101+
}
102+
if strings.Contains(subma1[0][4], " are weak against ") {
103+
regex23 := regexp.MustCompile(`.*are weak against (.*)`)
104+
subma23 := regex23.FindAllStringSubmatch(subma1[0][4], -1)
105+
CreatureWeaknessAgainstTmp := strings.Split(subma23[0][1], " damage")
106+
CreatureWeaknessAgainst = strings.Split(strings.Replace(CreatureWeaknessAgainstTmp[0], " and ", ", ", 1), ", ")
107+
}
108+
if strings.Contains(subma1[0][4], "It takes ") && strings.Contains(subma1[0][4], " mana to ") {
109+
regex24 := regexp.MustCompile(`.*It takes (.*) mana to (.*)`)
110+
subma24 := regex24.FindAllStringSubmatch(subma1[0][4], -1)
111+
if strings.Contains(subma24[0][2], "convince these creatures but they cannot be") {
112+
CreatureBeConvinced = true
113+
CreatureConvincedMana = TibiadataStringToIntegerV3(subma24[0][1])
114+
} else if strings.Contains(subma24[0][2], "summon or convince these creatures") {
115+
CreatureBeSummoned = true
116+
CreatureSummonedMana = TibiadataStringToIntegerV3(subma24[0][1])
117+
CreatureBeConvinced = true
118+
CreatureConvincedMana = TibiadataStringToIntegerV3(subma24[0][1])
119+
}
120+
}
121+
122+
// Loot
123+
// Regex to get loot information
124+
regex3 := regexp.MustCompile(`.*yield (.*) experience.*carry (.*)with them.`)
125+
subma3 := regex3.FindAllStringSubmatch(subma1[0][5], -1)
126+
// Adding data to vars
127+
CreatureExperiencePoints = TibiadataStringToIntegerV3(subma3[0][1])
128+
if subma3[0][2] != "nothing" {
129+
CreatureIsLootable = true
130+
CreatureLootListTmp := strings.Split(strings.Replace(strings.Replace(subma3[0][2], "items ", "", 1), " and sometimes other ", "", 1), ", ")
131+
for _, str := range CreatureLootListTmp {
132+
if str != "" {
133+
CreatureLootList = append(CreatureLootList, str)
134+
}
135+
}
136+
}
137+
}
138+
139+
//
140+
// Build the data-blob
141+
jsonData := JSONData{
142+
Creature{
143+
Name: subma1[0][1],
144+
Race: race,
145+
ImageURL: subma1[0][2],
146+
Description: CreatureDescription,
147+
Behaviour: CreatureBehaviour,
148+
Hitpoints: CreatureHitpoints,
149+
ImmuneTo: CreatureImmuneTo,
150+
StrongAgainst: CreatureStrongAgainst,
151+
WeaknessAgainst: CreatureWeaknessAgainst,
152+
BeParalysed: CreatureBeParalysed,
153+
BeSummoned: CreatureBeSummoned,
154+
SummonMana: CreatureSummonedMana,
155+
BeConvinced: CreatureBeConvinced,
156+
ConvincedMana: CreatureConvincedMana,
157+
SeeInvisible: CreatureSeeInvisible,
158+
ExperiencePoints: CreatureExperiencePoints,
159+
IsLootable: CreatureIsLootable,
160+
LootList: CreatureLootList,
161+
},
162+
Information{
163+
APIVersion: TibiadataAPIversion,
164+
Timestamp: TibiadataDatetimeV3(""),
165+
},
166+
}
167+
168+
js, _ := json.Marshal(jsonData)
169+
if TibiadataDebug {
170+
fmt.Printf("%s\n", js)
171+
}
172+
return string(js)
173+
}

src/TibiaCreaturesOverviewV3.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
"regexp"
8+
"strings"
9+
10+
"github.com/PuerkitoBio/goquery"
11+
)
12+
13+
// TibiaCreaturesOverviewV3 func
14+
func TibiaCreaturesOverviewV3() string {
15+
16+
// Child of Creatures (used for list of creatures and boosted section)
17+
type Creature struct {
18+
Name string `json:"name"`
19+
Race string `json:"race"`
20+
ImageURL string `json:"image_url"`
21+
Featured bool `json:"featured"`
22+
}
23+
24+
// Child of JSONData
25+
type Creatures struct {
26+
Boosted Creature `json:"boosted"`
27+
Creatures []Creature `json:"creature_list"`
28+
}
29+
30+
//
31+
// The base includes two levels: Creatures and Information
32+
type JSONData struct {
33+
Creatures Creatures `json:"creatures"`
34+
Information Information `json:"information"`
35+
}
36+
37+
// Getting data with TibiadataHTMLDataCollectorV3
38+
BoxContentHTML := TibiadataHTMLDataCollectorV3("https://www.tibia.com/library/?subtopic=creatures")
39+
40+
// Loading HTML data into ReaderHTML for goquery with NewReader
41+
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
42+
if err != nil {
43+
log.Fatal(err)
44+
}
45+
46+
// Getting data from div.InnerTableContainer and then first p
47+
InnerTableContainerTMPB, err := ReaderHTML.Find(".InnerTableContainer p").First().Html()
48+
if err != nil {
49+
log.Fatal(err)
50+
}
51+
52+
// Regex to get data for name and race param for boosted creature
53+
regex1b := regexp.MustCompile(`<a.*race=(.*)".*?>(.*)</a>`)
54+
subma1b := regex1b.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
55+
// Settings vars for usage in JSONData
56+
BoostedCreatureName := subma1b[0][2]
57+
BoostedCreatureRace := subma1b[0][1]
58+
59+
// Regex to get image of boosted creature
60+
regex2b := regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`)
61+
subma2b := regex2b.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
62+
// Settings vars for usage in JSONData
63+
BoostedCreatureImage := subma2b[0][1]
64+
65+
// Creating empty CreaturesData var
66+
var CreaturesData []Creature
67+
68+
// Running query over each div
69+
ReaderHTML.Find(".BoxContent div div").Each(func(index int, s *goquery.Selection) {
70+
71+
// Storing HTML into CreatureDivHTML
72+
CreatureDivHTML, err := s.Html()
73+
if err != nil {
74+
log.Fatal(err)
75+
}
76+
77+
// Regex to get data for name, race and img src param for creature
78+
regex1 := regexp.MustCompile(`.*race=(.*)"><img src="(.*)" border.*div>(.*)<\/div>`)
79+
subma1 := regex1.FindAllStringSubmatch(CreatureDivHTML, -1)
80+
81+
// check if regex return length is over 0 and the match of name is over 1
82+
if len(subma1) > 0 && len(subma1[0][3]) > 1 {
83+
84+
// printing the name of the creature
85+
//log.Println(subma1[0][1])
86+
87+
// Adding bool to indicate features in creature_list
88+
FeaturedRace := false
89+
if subma1[0][1] == BoostedCreatureRace {
90+
FeaturedRace = true
91+
}
92+
93+
// Creating data block to return
94+
CreaturesData = append(CreaturesData, Creature{
95+
Name: subma1[0][3],
96+
Race: subma1[0][1],
97+
ImageURL: subma1[0][2],
98+
Featured: FeaturedRace,
99+
})
100+
101+
}
102+
})
103+
104+
// Printing the CreaturesData data to log
105+
// log.Println(CreaturesData)
106+
107+
//
108+
// Build the data-blob
109+
jsonData := JSONData{
110+
Creatures{
111+
Boosted: Creature{
112+
Name: BoostedCreatureName,
113+
Race: BoostedCreatureRace,
114+
ImageURL: BoostedCreatureImage,
115+
Featured: true,
116+
},
117+
Creatures: CreaturesData,
118+
},
119+
Information{
120+
APIVersion: TibiadataAPIversion,
121+
Timestamp: TibiadataDatetimeV3(""),
122+
},
123+
}
124+
125+
js, _ := json.Marshal(jsonData)
126+
if TibiadataDebug {
127+
fmt.Printf("%s\n", js)
128+
}
129+
return string(js)
130+
}

0 commit comments

Comments
 (0)