Skip to content

Commit bdcee7a

Browse files
authored
fix: boosted creature parsing on overview page (#374)
1 parent dc3aaa0 commit bdcee7a

File tree

5 files changed

+29
-43
lines changed

5 files changed

+29
-43
lines changed

src/TibiaCreaturesOverview.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ type CreaturesOverviewResponse struct {
3030
}
3131

3232
var (
33-
BoostedCreatureImageRegex = regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`)
34-
CreatureInformationRegex = regexp.MustCompile(`.*race=(.*)"><img src="(.*)" border.*div>(.*)<\/div>`)
33+
BoostedCreatureNameAndRaceRegex = regexp.MustCompile(`<a.*race=(.*)".*?>(.*)</a>`)
34+
BoostedCreatureImageRegex = regexp.MustCompile(`<img[^>]+\bsrc=["']([^"']+)["']`)
35+
CreatureInformationRegex = regexp.MustCompile(`.*race=(.*)"><img src="(.*)" border.*div>(.*)<\/div>`)
3536
)
3637

3738
func TibiaCreaturesOverviewImpl(BoxContentHTML string) (CreaturesOverviewResponse, error) {
@@ -51,6 +52,15 @@ func TibiaCreaturesOverviewImpl(BoxContentHTML string) (CreaturesOverviewRespons
5152
return CreaturesOverviewResponse{}, fmt.Errorf("[error] TibiaCreaturesOverviewImpl failed at ReaderHTML.Find, err: %s", err)
5253
}
5354

55+
// Regex to get data for name and race param for boosted creature
56+
subma1b := BoostedCreatureNameAndRaceRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
57+
58+
if len(subma1b) > 0 {
59+
// Settings vars for usage in JSONData
60+
BoostedCreatureName = subma1b[0][2]
61+
BoostedCreatureRace = subma1b[0][1]
62+
}
63+
5464
// Regex to get image of boosted creature
5565
subma2b := BoostedCreatureImageRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)
5666

@@ -83,9 +93,7 @@ func TibiaCreaturesOverviewImpl(BoxContentHTML string) (CreaturesOverviewRespons
8393
if len(subma1) > 0 && len(subma1[0][3]) > 1 {
8494
// Adding bool to indicate features in creature_list
8595
FeaturedRace := false
86-
if TibiaDataVerifyBoostedCreatureImage(subma1[0][2], BoostedCreatureImage) {
87-
BoostedCreatureName = TibiaDataSanitizeEscapedString(subma1[0][3])
88-
BoostedCreatureRace = subma1[0][1]
96+
if subma1[0][1] == BoostedCreatureRace {
8997
FeaturedRace = true
9098
}
9199

src/TibiaCreaturesOverview_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,23 @@ func TestOverview(t *testing.T) {
2727

2828
assert := assert.New(t)
2929

30-
assert.Equal("Acid Blobs", creaturesJson.Creatures.Boosted.Name)
31-
assert.Equal("acidblob", creaturesJson.Creatures.Boosted.Race)
32-
assert.Equal("https://static.tibia.com/images/global/header/monsters/acidblob.gif", creaturesJson.Creatures.Boosted.ImageURL)
30+
assert.Equal("Minotaur Amazon", creaturesJson.Creatures.Boosted.Name)
31+
assert.Equal("minotauramazon", creaturesJson.Creatures.Boosted.Race)
32+
assert.Equal("https://static.tibia.com/images/global/header/monsters/minotauramazon.gif", creaturesJson.Creatures.Boosted.ImageURL)
3333

3434
assert.Equal(638, len(creaturesJson.Creatures.Creatures))
3535

3636
acidblob := creaturesJson.Creatures.Creatures[0]
3737
assert.Equal("Acid Blobs", acidblob.Name)
3838
assert.Equal("acidblob", acidblob.Race)
3939
assert.Equal("https://static.tibia.com/images/library/acidblob.gif", acidblob.ImageURL)
40-
assert.True(acidblob.Featured)
40+
assert.False(acidblob.Featured)
41+
42+
minotauramazon := creaturesJson.Creatures.Creatures[360]
43+
assert.Equal("Minotaur Amazons", minotauramazon.Name)
44+
assert.Equal("minotauramazon", minotauramazon.Race)
45+
assert.Equal("https://static.tibia.com/images/library/minotauramazon.gif", minotauramazon.ImageURL)
46+
assert.True(minotauramazon.Featured)
4147

4248
quarapredator := creaturesJson.Creatures.Creatures[465]
4349
assert.Equal("Quara Predators", quarapredator.Name)

src/TibiaDataUtils.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"log"
77
"net/url"
88
"os"
9-
"path"
109
"regexp"
1110
"strconv"
1211
"strings"
@@ -316,16 +315,3 @@ func TibiaDataGetNewsType(data string) string {
316315
return "unknown"
317316
}
318317
}
319-
320-
// TibiaDataVerifyBoostedCreatureImage compares two creature-image URLs and returns true if the filenames are the same, otherwise false.
321-
func TibiaDataVerifyBoostedCreatureImage(link1, link2 string) bool {
322-
getFilename := func(link string) string {
323-
parsedURL, err := url.Parse(link)
324-
if err != nil {
325-
return ""
326-
}
327-
return path.Base(parsedURL.Path)
328-
}
329-
330-
return getFilename(link1) == getFilename(link2)
331-
}

src/TibiaDataUtils_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,6 @@ func TestHTMLRemover(t *testing.T) {
204204
assert.Equal(t, sanitizedString, "abc")
205205
}
206206

207-
func TestTibiaDataVerifyBoostedCreatureImage(t *testing.T) {
208-
assert := assert.New(t)
209-
210-
// Test when the filenames are the same
211-
link1 := "https://static.tibia.com/images/global/header/monsters/acidblob.gif"
212-
link2 := "https://static.tibia.com/images/library/acidblob.gif"
213-
assert.True(TibiaDataVerifyBoostedCreatureImage(link1, link2))
214-
215-
// Test when the filenames are different
216-
link3 := "https://static.tibia.com/images/global/header/monsters/acidblob.gif"
217-
link4 := "https://static.tibia.com/images/library/cultacolyte.gif"
218-
assert.False(TibiaDataVerifyBoostedCreatureImage(link3, link4))
219-
}
220-
221207
func TestFake(t *testing.T) {
222208
assert := assert.New(t)
223209
const str = "&lt;"

src/static/testdata/creatures/creatures.html

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

0 commit comments

Comments
 (0)