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 ) + "¤tpage=400000000000000"
69+ TibiadataRequest .URL = "https://www.tibia.com/community/?subtopic=highscores&world=" + TibiadataQueryEscapeStringV3 (world ) + "&category=" + strconv . Itoa ( int ( highscoreCategory ) ) + "&profession=" + TibiadataQueryEscapeStringV3 (vocationid ) + "¤tpage=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 ,
0 commit comments