-
Notifications
You must be signed in to change notification settings - Fork 26
/
TibiaWorldsOverviewV3.go
203 lines (176 loc) · 7.17 KB
/
TibiaWorldsOverviewV3.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package main
import (
"log"
"regexp"
"strings"
"github.com/PuerkitoBio/goquery"
)
// Child of Worlds
type OverviewWorld struct {
Name string `json:"name"`
Status string `json:"status"` // Online:
PlayersOnline int `json:"players_online"` // Online:
Location string `json:"location"` // Location:
PvpType string `json:"pvp_type"` // PvP Type:
PremiumOnly bool `json:"premium_only"` // Additional Information: premium = true / else: false
TransferType string `json:"transfer_type"` // Additional Information: regular (if not present) / locked / blocked
BattleyeProtected bool `json:"battleye_protected"` // BattlEye Status: true if protected / false if "Not protected by BattlEye."
BattleyeDate string `json:"battleye_date"` // BattlEye Status: null if since release / else show date?
GameWorldType string `json:"game_world_type"` // BattlEye Status: regular / experimental / tournament (if Tournament World Type exists)
TournamentWorldType string `json:"tournament_world_type"` // BattlEye Status: null (default?) / regular / restricted
}
// Child of JSONData
type OverviewWorlds struct {
PlayersOnline int `json:"players_online"` // Calculated value
RecordPlayers int `json:"record_players"` // Overall Maximum:
RecordDate string `json:"record_date"` // Overall Maximum:
RegularWorlds []OverviewWorld `json:"regular_worlds"`
TournamentWorlds []OverviewWorld `json:"tournament_worlds"`
}
//
// The base includes two levels: Worlds and Information
type WorldsOverviewResponse struct {
Worlds OverviewWorlds `json:"worlds"`
Information Information `json:"information"`
}
var (
worldPlayerRecordRegex = regexp.MustCompile(`.*<\/b>...(.*) players \(on (.*)\)`)
worldInformationRegex = regexp.MustCompile(`.*world=.*">(.*)<\/a><\/td>.*right;">(.*)<\/td><td>(.*)<\/td><td>(.*)<\/td><td align="center" valign="middle">(.*)<\/td><td>(.*)<\/td>`)
worldBattlEyeProtectedSinceRegex = regexp.MustCompile(`.*game world has been protected by BattlEye since (.*).<\/p.*`)
)
// TibiaWorldsOverviewV3 func
func TibiaWorldsOverviewV3Impl(BoxContentHTML string) WorldsOverviewResponse {
// Loading HTML data into ReaderHTML for goquery with NewReader
ReaderHTML, err := goquery.NewDocumentFromReader(strings.NewReader(BoxContentHTML))
if err != nil {
log.Fatal(err)
}
// Creating empty vars
var (
RegularWorldsData, TournamentWorldsData []OverviewWorld
WorldsRecordDate, WorldsWorldCategory, WorldsBattleyeDate, WorldsTransferType, WorldsTournamentWorldType, WorldsGameWorldType, WorldsStatus string
WorldsRecordPlayers, WorldsAllOnlinePlayers int
WorldsPremiumOnly, WorldsBattleyeProtected bool
)
// Running query over each div
ReaderHTML.Find(".TableContentContainer .TableContent tbody tr").Each(func(index int, s *goquery.Selection) {
// Storing HTML into CreatureDivHTML
WorldsDivHTML, err := s.Html()
if err != nil {
log.Fatal(err)
}
// Regex to get data for record values
subma1 := worldPlayerRecordRegex.FindAllStringSubmatch(WorldsDivHTML, -1)
if len(subma1) > 0 {
// setting record values
WorldsRecordPlayers = TibiadataStringToIntegerV3(subma1[0][1])
WorldsRecordDate = TibiadataDatetimeV3(subma1[0][2])
}
if strings.Contains(WorldsDivHTML, ">Regular Worlds<") {
WorldsWorldCategory = "regular"
} else if strings.Contains(WorldsDivHTML, ">Tournament Worlds<") {
WorldsWorldCategory = "tournament"
}
subma2 := worldInformationRegex.FindAllStringSubmatch(WorldsDivHTML, -1)
// check if regex return length is over 0
if len(subma2) > 0 {
WorldsPlayersOnline := 0
if subma2[0][2] == "-" {
WorldsStatus = "unknown"
} else {
WorldsPlayersOnline = TibiadataStringToIntegerV3(subma2[0][2])
// Setting the players_online & overall players_online
WorldsAllOnlinePlayers += WorldsPlayersOnline
if WorldsPlayersOnline > 0 {
WorldsStatus = "online"
} else {
WorldsStatus = "offline"
}
}
// Creating better to use vars
WorldsBattlEye := subma2[0][5]
WorldsAdditionalInfo := subma2[0][6]
// Setting the premium_only
if strings.Contains(WorldsAdditionalInfo, "premium") {
WorldsPremiumOnly = true
} else {
WorldsPremiumOnly = false
}
// Setting the transfer_type
WorldsTransferType = "regular"
if strings.Contains(WorldsAdditionalInfo, "blocked") {
WorldsTransferType = "blocked"
} else if strings.Contains(WorldsAdditionalInfo, "locked") {
WorldsTransferType = "locked"
}
// Setting the game_world_type
WorldsGameWorldType = "regular"
if strings.Contains(WorldsAdditionalInfo, "experimental") {
WorldsGameWorldType = "experimental"
} else if WorldsWorldCategory == "tournament" {
WorldsGameWorldType = "tournament"
}
// Determine Battleye Protection
if len(WorldsBattlEye) > 0 {
WorldsBattleyeProtected = true
if strings.Contains(WorldsBattlEye, "BattlEye since its release") {
WorldsBattleyeDate = "release"
} else {
subma21 := worldBattlEyeProtectedSinceRegex.FindAllStringSubmatch(WorldsBattlEye, -1)
WorldsBattleyeDate = TibiadataDateV3(subma21[0][1])
}
} else {
// This world is without protection..
WorldsBattleyeProtected = false
WorldsBattleyeDate = ""
}
// Setting the tournament_world_type param
switch WorldsWorldCategory {
case "regular":
WorldsTournamentWorldType = ""
case "tournament":
WorldsGameWorldType = "tournament"
WorldsTournamentWorldType = "regular"
if strings.Contains(WorldsAdditionalInfo, "restricted") {
WorldsTournamentWorldType = "restricted"
}
}
// Creating data block to return
OneWorld := OverviewWorld{
Name: subma2[0][1],
Status: WorldsStatus,
PlayersOnline: WorldsPlayersOnline,
Location: subma2[0][3],
PvpType: subma2[0][4],
PremiumOnly: WorldsPremiumOnly,
TransferType: WorldsTransferType,
BattleyeProtected: WorldsBattleyeProtected,
BattleyeDate: WorldsBattleyeDate,
GameWorldType: WorldsGameWorldType,
TournamentWorldType: WorldsTournamentWorldType,
}
// Adding OneWorld to correct category
switch WorldsWorldCategory {
case "regular":
RegularWorldsData = append(RegularWorldsData, OneWorld)
case "tournament":
TournamentWorldsData = append(TournamentWorldsData, OneWorld)
}
}
})
//
// Build the data-blob
return WorldsOverviewResponse{
OverviewWorlds{
PlayersOnline: WorldsAllOnlinePlayers,
RecordPlayers: WorldsRecordPlayers,
RecordDate: WorldsRecordDate,
RegularWorlds: RegularWorldsData,
TournamentWorlds: TournamentWorldsData,
},
Information{
APIVersion: TibiadataAPIversion,
Timestamp: TibiadataDatetimeV3(""),
},
}
}