@@ -10,46 +10,44 @@ import (
1010 "github.com/gin-gonic/gin"
1111)
1212
13+ // Child of Worlds
14+ type World struct {
15+ Name string `json:"name"`
16+ Status string `json:"status"` // Online:
17+ PlayersOnline int `json:"players_online"` // Online:
18+ Location string `json:"location"` // Location:
19+ PvpType string `json:"pvp_type"` // PvP Type:
20+ PremiumOnly bool `json:"premium_only"` // Additional Information: premium = true / else: false
21+ TransferType string `json:"transfer_type"` // Additional Information: regular (if not present) / locked / blocked
22+ BattleyeProtected bool `json:"battleye_protected"` // BattlEye Status: true if protected / false if "Not protected by BattlEye."
23+ BattleyeDate string `json:"battleye_date"` // BattlEye Status: null if since release / else show date?
24+ GameWorldType string `json:"game_world_type"` // BattlEye Status: regular / experimental / tournament (if Tournament World Type exists)
25+ TournamentWorldType string `json:"tournament_world_type"` // BattlEye Status: null (default?) / regular / restricted
26+ }
27+
28+ // Child of JSONData
29+ type Worlds struct {
30+ PlayersOnline int `json:"players_online"` // Calculated value
31+ RecordPlayers int `json:"record_players"` // Overall Maximum:
32+ RecordDate string `json:"record_date"` // Overall Maximum:
33+ RegularWorlds []World `json:"regular_worlds"`
34+ TournamentWorlds []World `json:"tournament_worlds"`
35+ }
36+
37+ //
38+ // The base includes two levels: Worlds and Information
39+ type WorldsOverviewResponse struct {
40+ Worlds Worlds `json:"worlds"`
41+ Information Information `json:"information"`
42+ }
43+
1344var (
1445 worldPlayerRecordRegex = regexp .MustCompile (`.*<\/b>...(.*) players \(on (.*)\)` )
1546 worldInformationRegex = regexp .MustCompile (`.*world=.*">(.*)<\/a><\/td>.*right;">(.*)<\/td><td>(.*)<\/td><td>(.*)<\/td><td align="center" valign="middle">(.*)<\/td><td>(.*)<\/td>` )
1647 worldBattlEyeProtectedSinceRegex = regexp .MustCompile (`.*game world has been protected by BattlEye since (.*).<\/p.*` )
1748)
1849
19- // TibiaWorldsOverviewV3 func
2050func TibiaWorldsOverviewV3 (c * gin.Context ) {
21-
22- // Child of Worlds
23- type World struct {
24- Name string `json:"name"`
25- Status string `json:"status"` // Online:
26- PlayersOnline int `json:"players_online"` // Online:
27- Location string `json:"location"` // Location:
28- PvpType string `json:"pvp_type"` // PvP Type:
29- PremiumOnly bool `json:"premium_only"` // Additional Information: premium = true / else: false
30- TransferType string `json:"transfer_type"` // Additional Information: regular (if not present) / locked / blocked
31- BattleyeProtected bool `json:"battleye_protected"` // BattlEye Status: true if protected / false if "Not protected by BattlEye."
32- BattleyeDate string `json:"battleye_date"` // BattlEye Status: null if since release / else show date?
33- GameWorldType string `json:"game_world_type"` // BattlEye Status: regular / experimental / tournament (if Tournament World Type exists)
34- TournamentWorldType string `json:"tournament_world_type"` // BattlEye Status: null (default?) / regular / restricted
35- }
36-
37- // Child of JSONData
38- type Worlds struct {
39- PlayersOnline int `json:"players_online"` // Calculated value
40- RecordPlayers int `json:"record_players"` // Overall Maximum:
41- RecordDate string `json:"record_date"` // Overall Maximum:
42- RegularWorlds []World `json:"regular_worlds"`
43- TournamentWorlds []World `json:"tournament_worlds"`
44- }
45-
46- //
47- // The base includes two levels: Worlds and Information
48- type JSONData struct {
49- Worlds Worlds `json:"worlds"`
50- Information Information `json:"information"`
51- }
52-
5351 // Getting data with TibiadataHTMLDataCollectorV3
5452 TibiadataRequest .URL = "https://www.tibia.com/community/?subtopic=worlds"
5553 BoxContentHTML , err := TibiadataHTMLDataCollectorV3 (TibiadataRequest )
@@ -60,6 +58,13 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
6058 return
6159 }
6260
61+ jsonData := TibiaWorldsOverviewV3Impl (BoxContentHTML )
62+
63+ TibiaDataAPIHandleSuccessResponse (c , "TibiaWorldsOverviewV3" , jsonData )
64+ }
65+
66+ // TibiaWorldsOverviewV3 func
67+ func TibiaWorldsOverviewV3Impl (BoxContentHTML string ) WorldsOverviewResponse {
6368 // Loading HTML data into ReaderHTML for goquery with NewReader
6469 ReaderHTML , err := goquery .NewDocumentFromReader (strings .NewReader (BoxContentHTML ))
6570 if err != nil {
@@ -102,23 +107,27 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
102107
103108 // check if regex return length is over 0
104109 if len (subma2 ) > 0 {
110+ WorldsPlayersOnline := 0
111+
112+ if subma2 [0 ][2 ] == "-" {
113+ WorldsStatus = "unknown"
114+ } else {
115+ WorldsPlayersOnline = TibiadataStringToIntegerV3 (subma2 [0 ][2 ])
116+
117+ // Setting the players_online & overall players_online
118+ WorldsAllOnlinePlayers += WorldsPlayersOnline
119+
120+ if WorldsPlayersOnline > 0 {
121+ WorldsStatus = "online"
122+ } else {
123+ WorldsStatus = "offline"
124+ }
125+ }
105126
106127 // Creating better to use vars
107- WorldsPlayersOnline := TibiadataStringToIntegerV3 (subma2 [0 ][2 ])
108128 WorldsBattlEye := subma2 [0 ][5 ]
109129 WorldsAdditionalInfo := subma2 [0 ][6 ]
110130
111- // Setting the players_online & overall players_online
112- WorldsAllOnlinePlayers += WorldsPlayersOnline
113- switch {
114- case WorldsPlayersOnline > 0 :
115- WorldsStatus = "online"
116- case subma2 [0 ][2 ] == "-" :
117- WorldsStatus = "unknown"
118- default :
119- WorldsStatus = "offline"
120- }
121-
122131 // Setting the premium_only
123132 if strings .Contains (WorldsAdditionalInfo , "premium" ) {
124133 WorldsPremiumOnly = true
@@ -149,7 +158,7 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
149158 WorldsBattleyeDate = "release"
150159 } else {
151160 subma21 := worldBattlEyeProtectedSinceRegex .FindAllStringSubmatch (WorldsBattlEye , - 1 )
152- WorldsBattleyeDate = subma21 [0 ][1 ]
161+ WorldsBattleyeDate = TibiadataDateV3 ( subma21 [0 ][1 ])
153162 }
154163 } else {
155164 // This world is without protection..
@@ -196,7 +205,7 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
196205
197206 //
198207 // Build the data-blob
199- jsonData := JSONData {
208+ return WorldsOverviewResponse {
200209 Worlds {
201210 PlayersOnline : WorldsAllOnlinePlayers ,
202211 RecordPlayers : WorldsRecordPlayers ,
@@ -209,7 +218,4 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
209218 Timestamp : TibiadataDatetimeV3 ("" ),
210219 },
211220 }
212-
213- // return jsonData
214- TibiaDataAPIHandleSuccessResponse (c , "TibiaWorldsOverviewV3" , jsonData )
215221}
0 commit comments