@@ -10,46 +10,44 @@ import (
10
10
"github.com/gin-gonic/gin"
11
11
)
12
12
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
+
13
44
var (
14
45
worldPlayerRecordRegex = regexp .MustCompile (`.*<\/b>...(.*) players \(on (.*)\)` )
15
46
worldInformationRegex = regexp .MustCompile (`.*world=.*">(.*)<\/a><\/td>.*right;">(.*)<\/td><td>(.*)<\/td><td>(.*)<\/td><td align="center" valign="middle">(.*)<\/td><td>(.*)<\/td>` )
16
47
worldBattlEyeProtectedSinceRegex = regexp .MustCompile (`.*game world has been protected by BattlEye since (.*).<\/p.*` )
17
48
)
18
49
19
- // TibiaWorldsOverviewV3 func
20
50
func 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
-
53
51
// Getting data with TibiadataHTMLDataCollectorV3
54
52
TibiadataRequest .URL = "https://www.tibia.com/community/?subtopic=worlds"
55
53
BoxContentHTML , err := TibiadataHTMLDataCollectorV3 (TibiadataRequest )
@@ -60,6 +58,13 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
60
58
return
61
59
}
62
60
61
+ jsonData := TibiaWorldsOverviewV3Impl (BoxContentHTML )
62
+
63
+ TibiaDataAPIHandleSuccessResponse (c , "TibiaWorldsOverviewV3" , jsonData )
64
+ }
65
+
66
+ // TibiaWorldsOverviewV3 func
67
+ func TibiaWorldsOverviewV3Impl (BoxContentHTML string ) WorldsOverviewResponse {
63
68
// Loading HTML data into ReaderHTML for goquery with NewReader
64
69
ReaderHTML , err := goquery .NewDocumentFromReader (strings .NewReader (BoxContentHTML ))
65
70
if err != nil {
@@ -102,23 +107,27 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
102
107
103
108
// check if regex return length is over 0
104
109
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
+ }
105
126
106
127
// Creating better to use vars
107
- WorldsPlayersOnline := TibiadataStringToIntegerV3 (subma2 [0 ][2 ])
108
128
WorldsBattlEye := subma2 [0 ][5 ]
109
129
WorldsAdditionalInfo := subma2 [0 ][6 ]
110
130
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
-
122
131
// Setting the premium_only
123
132
if strings .Contains (WorldsAdditionalInfo , "premium" ) {
124
133
WorldsPremiumOnly = true
@@ -149,7 +158,7 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
149
158
WorldsBattleyeDate = "release"
150
159
} else {
151
160
subma21 := worldBattlEyeProtectedSinceRegex .FindAllStringSubmatch (WorldsBattlEye , - 1 )
152
- WorldsBattleyeDate = subma21 [0 ][1 ]
161
+ WorldsBattleyeDate = TibiadataDateV3 ( subma21 [0 ][1 ])
153
162
}
154
163
} else {
155
164
// This world is without protection..
@@ -196,7 +205,7 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
196
205
197
206
//
198
207
// Build the data-blob
199
- jsonData := JSONData {
208
+ return WorldsOverviewResponse {
200
209
Worlds {
201
210
PlayersOnline : WorldsAllOnlinePlayers ,
202
211
RecordPlayers : WorldsRecordPlayers ,
@@ -209,7 +218,4 @@ func TibiaWorldsOverviewV3(c *gin.Context) {
209
218
Timestamp : TibiadataDatetimeV3 ("" ),
210
219
},
211
220
}
212
-
213
- // return jsonData
214
- TibiaDataAPIHandleSuccessResponse (c , "TibiaWorldsOverviewV3" , jsonData )
215
221
}
0 commit comments