Skip to content

Commit

Permalink
fix: boosted creature parsing on overview page (#372)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasehlert authored Jul 22, 2024
1 parent 2246c8b commit 9505c7b
Show file tree
Hide file tree
Showing 5 changed files with 508 additions and 504 deletions.
18 changes: 5 additions & 13 deletions src/TibiaCreaturesOverview.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ type CreaturesOverviewResponse struct {
}

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

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

// Regex to get data for name and race param for boosted creature
subma1b := BoostedCreatureNameAndRaceRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)

if len(subma1b) > 0 {
// Settings vars for usage in JSONData
BoostedCreatureName = subma1b[0][2]
BoostedCreatureRace = subma1b[0][1]
}

// Regex to get image of boosted creature
subma2b := BoostedCreatureImageRegex.FindAllStringSubmatch(InnerTableContainerTMPB, -1)

Expand Down Expand Up @@ -93,7 +83,9 @@ func TibiaCreaturesOverviewImpl(BoxContentHTML string) (CreaturesOverviewRespons
if len(subma1) > 0 && len(subma1[0][3]) > 1 {
// Adding bool to indicate features in creature_list
FeaturedRace := false
if subma1[0][1] == BoostedCreatureRace {
if TibiaDataVerifyBoostedCreatureImage(subma1[0][2], BoostedCreatureImage) {
BoostedCreatureName = TibiaDataSanitizeEscapedString(subma1[0][3])
BoostedCreatureRace = subma1[0][1]
FeaturedRace = true
}

Expand Down
28 changes: 17 additions & 11 deletions src/TibiaCreaturesOverview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ func TestOverview(t *testing.T) {

assert := assert.New(t)

assert.Equal("Quara Predator", creaturesJson.Creatures.Boosted.Name)
assert.Equal("quarapredator", creaturesJson.Creatures.Boosted.Race)
assert.Equal("https://static.tibia.com/images/global/header/monsters/quarapredator.gif", creaturesJson.Creatures.Boosted.ImageURL)
assert.Equal("Acid Blobs", creaturesJson.Creatures.Boosted.Name)
assert.Equal("acidblob", creaturesJson.Creatures.Boosted.Race)
assert.Equal("https://static.tibia.com/images/global/header/monsters/acidblob.gif", creaturesJson.Creatures.Boosted.ImageURL)

assert.Equal(553, len(creaturesJson.Creatures.Creatures))
assert.Equal(638, len(creaturesJson.Creatures.Creatures))

slimes := creaturesJson.Creatures.Creatures[444]
assert.Equal("Slimes", slimes.Name)
assert.Equal("slime", slimes.Race)
assert.Equal("https://static.tibia.com/images/library/slime.gif", slimes.ImageURL)
assert.False(slimes.Featured)
acidblob := creaturesJson.Creatures.Creatures[0]
assert.Equal("Acid Blobs", acidblob.Name)
assert.Equal("acidblob", acidblob.Race)
assert.Equal("https://static.tibia.com/images/library/acidblob.gif", acidblob.ImageURL)
assert.True(acidblob.Featured)

quarapredator := creaturesJson.Creatures.Creatures[407]
quarapredator := creaturesJson.Creatures.Creatures[465]
assert.Equal("Quara Predators", quarapredator.Name)
assert.Equal("quarapredator", quarapredator.Race)
assert.Equal("https://static.tibia.com/images/library/quarapredator.gif", quarapredator.ImageURL)
assert.True(quarapredator.Featured)
assert.False(quarapredator.Featured)

slimes := creaturesJson.Creatures.Creatures[510]
assert.Equal("Slimes", slimes.Name)
assert.Equal("slime", slimes.Race)
assert.Equal("https://static.tibia.com/images/library/slime.gif", slimes.ImageURL)
assert.False(slimes.Featured)
}
14 changes: 14 additions & 0 deletions src/TibiaDataUtils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/url"
"os"
"path"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -315,3 +316,16 @@ func TibiaDataGetNewsType(data string) string {
return "unknown"
}
}

// TibiaDataVerifyBoostedCreatureImage compares two creature-image URLs and returns true if the filenames are the same, otherwise false.
func TibiaDataVerifyBoostedCreatureImage(link1, link2 string) bool {
getFilename := func(link string) string {
parsedURL, err := url.Parse(link)
if err != nil {
return ""
}
return path.Base(parsedURL.Path)
}

return getFilename(link1) == getFilename(link2)
}
14 changes: 14 additions & 0 deletions src/TibiaDataUtils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ func TestHTMLRemover(t *testing.T) {
assert.Equal(t, sanitizedString, "abc")
}

func TestTibiaDataVerifyBoostedCreatureImage(t *testing.T) {
assert := assert.New(t)

// Test when the filenames are the same
link1 := "https://static.tibia.com/images/global/header/monsters/acidblob.gif"
link2 := "https://static.tibia.com/images/library/acidblob.gif"
assert.True(TibiaDataVerifyBoostedCreatureImage(link1, link2))

// Test when the filenames are different
link3 := "https://static.tibia.com/images/global/header/monsters/acidblob.gif"
link4 := "https://static.tibia.com/images/library/cultacolyte.gif"
assert.False(TibiaDataVerifyBoostedCreatureImage(link3, link4))
}

func TestFake(t *testing.T) {
assert := assert.New(t)
const str = "&lt;"
Expand Down
Loading

0 comments on commit 9505c7b

Please sign in to comment.