Skip to content

Commit 029d33a

Browse files
authored
Create and use HighscoreCategory enum (#63)
1 parent 245eab7 commit 029d33a

File tree

4 files changed

+178
-60
lines changed

4 files changed

+178
-60
lines changed

src/HighscoreCategory.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"strings"
6+
)
7+
8+
type HighscoreCategory int
9+
10+
const (
11+
achievements HighscoreCategory = iota + 1
12+
axefighting
13+
charmpoints
14+
clubfighting
15+
distancefighting
16+
experience
17+
fishing
18+
fistfighting
19+
goshnarstaint
20+
loyaltypoints
21+
magiclevel
22+
shielding
23+
swordfighting
24+
dromescore
25+
)
26+
27+
func (hc HighscoreCategory) String() (string, error) {
28+
seasons := [...]string{"achievements", "axefighting", "charmpoints", "clubfighting", "distancefighting", "experience", "fishing", "fistfighting", "goshnarstaint", "loyaltypoints", "magiclevel", "shielding", "swordfighting", "dromescore"}
29+
if hc < achievements || hc > dromescore {
30+
return "", errors.New("invalid HighscoreCategory value")
31+
}
32+
return seasons[hc-1], nil
33+
}
34+
35+
func HighscoreCategoryFromString(input string) HighscoreCategory {
36+
// Sanatize of category value
37+
input = strings.ToLower(input)
38+
switch input {
39+
case "achievements", "achievement":
40+
return achievements
41+
case "axe", "axefighting":
42+
return axefighting
43+
case "charm", "charms", "charmpoints":
44+
return charmpoints
45+
case "club", "clubfighting":
46+
return clubfighting
47+
case "distance", "distancefighting":
48+
return distancefighting
49+
case "fishing":
50+
return fishing
51+
case "fist", "fistfighting":
52+
return fistfighting
53+
case "goshnar", "goshnars", "goshnarstaint":
54+
return goshnarstaint
55+
case "loyalty", "loyaltypoints":
56+
return loyaltypoints
57+
case "magic", "mlvl", "magiclevel":
58+
return magiclevel
59+
case "shielding", "shield":
60+
return shielding
61+
case "sword", "swordfighting":
62+
return swordfighting
63+
case "drome", "dromescore":
64+
return dromescore
65+
default:
66+
return experience
67+
}
68+
}

src/HighscoreCategory_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestHighscoreCategoryAchievementsString(t *testing.T) {
10+
assert := assert.New(t)
11+
12+
highscoreCategory := achievements
13+
stringValue, err := highscoreCategory.String()
14+
15+
assert.Nil(err)
16+
assert.Equal("achievements", stringValue)
17+
assert.Equal(HighscoreCategory(1), highscoreCategory)
18+
}
19+
20+
func TestHighscoreCategoryExperienceString(t *testing.T) {
21+
assert := assert.New(t)
22+
23+
highscoreCategory := experience
24+
stringValue, err := highscoreCategory.String()
25+
26+
assert.Nil(err)
27+
assert.Equal("experience", stringValue)
28+
assert.Equal(HighscoreCategory(6), highscoreCategory)
29+
}
30+
31+
func TestHighscoreCategoryDromescoreString(t *testing.T) {
32+
assert := assert.New(t)
33+
34+
highscoreCategory := dromescore
35+
stringValue, err := highscoreCategory.String()
36+
37+
assert.Nil(err)
38+
assert.Equal("dromescore", stringValue)
39+
assert.Equal(HighscoreCategory(14), highscoreCategory)
40+
}
41+
42+
func TestHighscoreCategoryInvalidValueString(t *testing.T) {
43+
assert := assert.New(t)
44+
45+
highscoreCategory := HighscoreCategory(99)
46+
_, err := highscoreCategory.String()
47+
48+
assert.NotNil(err)
49+
}
50+
51+
func TestHighscoreCategoryFromString(t *testing.T) {
52+
assert := assert.New(t)
53+
54+
assert.Equal(experience, HighscoreCategoryFromString("experience"))
55+
assert.Equal(experience, HighscoreCategoryFromString(""))
56+
57+
assert.Equal(achievements, HighscoreCategoryFromString("achievements"))
58+
assert.Equal(achievements, HighscoreCategoryFromString("achievement"))
59+
60+
assert.Equal(axefighting, HighscoreCategoryFromString("axe"))
61+
assert.Equal(axefighting, HighscoreCategoryFromString("axefighting"))
62+
63+
assert.Equal(charmpoints, HighscoreCategoryFromString("charm"))
64+
assert.Equal(charmpoints, HighscoreCategoryFromString("charms"))
65+
assert.Equal(charmpoints, HighscoreCategoryFromString("charmpoints"))
66+
67+
assert.Equal(clubfighting, HighscoreCategoryFromString("club"))
68+
assert.Equal(clubfighting, HighscoreCategoryFromString("clubfighting"))
69+
70+
assert.Equal(distancefighting, HighscoreCategoryFromString("distance"))
71+
assert.Equal(distancefighting, HighscoreCategoryFromString("distancefighting"))
72+
73+
assert.Equal(fishing, HighscoreCategoryFromString("fishing"))
74+
75+
assert.Equal(fistfighting, HighscoreCategoryFromString("fist"))
76+
assert.Equal(fistfighting, HighscoreCategoryFromString("fistfighting"))
77+
78+
assert.Equal(goshnarstaint, HighscoreCategoryFromString("goshnar"))
79+
assert.Equal(goshnarstaint, HighscoreCategoryFromString("goshnars"))
80+
assert.Equal(goshnarstaint, HighscoreCategoryFromString("goshnarstaint"))
81+
82+
assert.Equal(loyaltypoints, HighscoreCategoryFromString("loyalty"))
83+
assert.Equal(loyaltypoints, HighscoreCategoryFromString("loyaltypoints"))
84+
85+
assert.Equal(magiclevel, HighscoreCategoryFromString("magic"))
86+
assert.Equal(magiclevel, HighscoreCategoryFromString("mlvl"))
87+
assert.Equal(magiclevel, HighscoreCategoryFromString("magiclevel"))
88+
89+
assert.Equal(shielding, HighscoreCategoryFromString("shielding"))
90+
assert.Equal(shielding, HighscoreCategoryFromString("shield"))
91+
92+
assert.Equal(swordfighting, HighscoreCategoryFromString("sword"))
93+
assert.Equal(swordfighting, HighscoreCategoryFromString("swordfighting"))
94+
95+
assert.Equal(dromescore, HighscoreCategoryFromString("drome"))
96+
assert.Equal(dromescore, HighscoreCategoryFromString("dromescore"))
97+
}

src/TibiaHighscoresV3.go

Lines changed: 12 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"log"
55
"net/http"
66
"regexp"
7+
"strconv"
78
"strings"
89

910
"github.com/PuerkitoBio/goquery"
@@ -50,8 +51,7 @@ func TibiaHighscoresV3(c *gin.Context) {
5051
category := c.Param("category")
5152
vocation := c.Param("vocation")
5253

53-
// do some validation of category and vocation
54-
// maybe return error on faulty value?!
54+
// maybe return error on faulty vocation value?!
5555

5656
// Adding fix for First letter to be upper and rest lower
5757
if strings.EqualFold(world, "all") {
@@ -60,62 +60,13 @@ func TibiaHighscoresV3(c *gin.Context) {
6060
world = TibiadataStringWorldFormatToTitleV3(world)
6161
}
6262

63-
// Sanatize of category value
64-
category = strings.ToLower(category)
65-
var categoryid string = "6"
66-
if len(category) > 0 {
67-
switch category {
68-
case "achievements", "achievement":
69-
category = "achievements"
70-
categoryid = "1"
71-
case "axe", "axefighting":
72-
category = "axefighting"
73-
categoryid = "2"
74-
case "charm", "charms", "charmpoints":
75-
category = "charmpoints"
76-
categoryid = "3"
77-
case "club", "clubfighting":
78-
category = "clubfighting"
79-
categoryid = "4"
80-
case "distance", "distancefighting":
81-
category = "distancefighting"
82-
categoryid = "5"
83-
case "fishing":
84-
category = "fishing"
85-
categoryid = "7"
86-
case "fist", "fistfighting":
87-
category = "fistfighting"
88-
categoryid = "8"
89-
case "goshnar", "goshnars", "goshnarstaint":
90-
category = "goshnarstaint"
91-
categoryid = "9"
92-
case "loyalty", "loyaltypoints":
93-
category = "loyaltypoints"
94-
categoryid = "10"
95-
case "magic", "mlvl", "magiclevel":
96-
category = "magiclevel"
97-
categoryid = "11"
98-
case "shielding", "shield":
99-
category = "shielding"
100-
categoryid = "12"
101-
case "sword", "swordfighting":
102-
category = "swordfighting"
103-
categoryid = "13"
104-
case "drome", "dromescore":
105-
category = "dromescore"
106-
categoryid = "14"
107-
default:
108-
category = "experience"
109-
}
110-
} else {
111-
category = "experience"
112-
}
63+
highscoreCategory := HighscoreCategoryFromString(category)
11364

11465
// Sanitize of vocation input
11566
vocationName, vocationid := TibiaDataVocationValidator(vocation)
11667

11768
// Getting data with TibiadataHTMLDataCollectorV3
118-
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=highscores&world=" + TibiadataQueryEscapeStringV3(world) + "&category=" + TibiadataQueryEscapeStringV3(categoryid) + "&profession=" + TibiadataQueryEscapeStringV3(vocationid) + "&currentpage=400000000000000"
69+
TibiadataRequest.URL = "https://www.tibia.com/community/?subtopic=highscores&world=" + TibiadataQueryEscapeStringV3(world) + "&category=" + strconv.Itoa(int(highscoreCategory)) + "&profession=" + TibiadataQueryEscapeStringV3(vocationid) + "&currentpage=400000000000000"
11970
BoxContentHTML, err := TibiadataHTMLDataCollectorV3(TibiadataRequest)
12071

12172
// return error (e.g. for maintenance mode)
@@ -124,13 +75,13 @@ func TibiaHighscoresV3(c *gin.Context) {
12475
return
12576
}
12677

127-
jsonData := TibiaHighscoresV3Impl(world, category, vocationName, BoxContentHTML)
78+
jsonData := TibiaHighscoresV3Impl(world, highscoreCategory, vocationName, BoxContentHTML)
12879

12980
// return jsonData
13081
TibiaDataAPIHandleSuccessResponse(c, "TibiaHighscoresV3", jsonData)
13182
}
13283

133-
func TibiaHighscoresV3Impl(world string, category string, vocationName string, BoxContentHTML string) HighscoresResponse {
84+
func TibiaHighscoresV3Impl(world string, category HighscoreCategory, vocationName string, BoxContentHTML string) HighscoresResponse {
13485
// Loading HTML data into ReaderHTML for goquery with NewReader
13586
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
13687
if err != nil {
@@ -178,7 +129,7 @@ func TibiaHighscoresV3Impl(world string, category string, vocationName string, B
178129
Sword => Rank Name Vocation World Level Skill Level
179130
*/
180131

181-
if category == "loyaltypoints" {
132+
if category == loyaltypoints {
182133
subma1 = SevenColumnRegex.FindAllStringSubmatch(HighscoreDivHTML, -1)
183134
} else {
184135
subma1 = SixColumnRegex.FindAllStringSubmatch(HighscoreDivHTML, -1)
@@ -193,13 +144,13 @@ func TibiaHighscoresV3Impl(world string, category string, vocationName string, B
193144
log.Println("3 -> " + subma1[0][3])
194145
log.Println("4 -> " + subma1[0][4])
195146
log.Println("5 -> " + subma1[0][5])
196-
if category == "loyaltypoints" {
147+
if category == loyaltypoints {
197148
log.Println("6 -> " + subma1[0][6])
198149
}
199150
}
200151

201152
HighscoreDataRank++
202-
if category == "loyaltypoints" {
153+
if category == loyaltypoints {
203154
HighscoreDataTitle = subma1[0][2]
204155
HighscoreDataVocation = subma1[0][3]
205156
HighscoreDataWorld = subma1[0][4]
@@ -230,12 +181,14 @@ func TibiaHighscoresV3Impl(world string, category string, vocationName string, B
230181
log.Println(HighscoreData)
231182
}
232183

184+
categoryString, _ := category.String()
185+
233186
//
234187
// Build the data-blob
235188
return HighscoresResponse{
236189
Highscores{
237190
World: strings.Title(strings.ToLower(world)),
238-
Category: category,
191+
Category: categoryString,
239192
Vocation: vocationName,
240193
HighscoreAge: HighscoreAge,
241194
HighscoreList: HighscoreData,

src/TibiaHighscoresV3_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestHighscoresAll(t *testing.T) {
1414
return
1515
}
1616

17-
highscoresJson := TibiaHighscoresV3Impl("", "experience", "all", string(data))
17+
highscoresJson := TibiaHighscoresV3Impl("", experience, "all", string(data))
1818
assert := assert.New(t)
1919

2020
assert.Equal("", highscoresJson.Highscores.World)

0 commit comments

Comments
 (0)