-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtournament.go
352 lines (315 loc) · 8.46 KB
/
tournament.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package pawntown_chess_results_module
import (
"errors"
"fmt"
"html"
"net/url"
"regexp"
"strconv"
"strings"
)
type Tournament struct {
api *Api
id string
data TournamentData
secInputData url.Values
}
type TournamentData struct {
RawMeta map[string]string
Name string
Organizer string
Federation string
Director string
ChiefArbiter string
DeputyChiefArbiter string
TimeControl string
Location string
NumberOfRounds string
TournamentMode string
EloCalculation string
AverageElo string
FromTo string
Rounds []Round
Players []Player
}
const regex = `<.*?>`
func stripTags(s string) string {
r := regexp.MustCompile(regex)
return r.ReplaceAllString(s, "")
}
func convertChessNum(s string) (float32, error) {
if strings.HasSuffix(s, "½") {
points, err := strconv.Atoi(s[0 : len(s)-2])
if err != nil {
return 0, err
}
return float32(points) + 0.5, nil
}
points, err := strconv.Atoi(s)
if err != nil {
return 0, err
}
return float32(points), nil
}
func parsePlayerRow(rowData string) (Player, error) {
player := Player{}
playerCellRegex := regexp.MustCompile(`<td[^>]*>(.*?)<\/td>`)
playerCells := playerCellRegex.FindAllStringSubmatch(rowData, -1)
if len(playerCells) != 7 {
return Player{}, errors.New("Not the correct amount of cells")
}
for index, match := range playerCells {
switch index {
case 0:
num, err := strconv.Atoi(stripTags(match[1]))
if err != nil {
return Player{}, err
}
player.Num = num
case 1:
title, err := parseFideTitle(stripTags(match[1]))
if err != nil {
return Player{}, err
}
player.FideTitle = title
case 2:
name := stripTags(match[1])
if len(name) < 1 {
return Player{}, errors.New("no name is present")
}
player.Name = html.UnescapeString(name)
case 3:
fideId, err := strconv.Atoi(stripTags(match[1]))
if err != nil {
player.FideID = 0
} else {
player.FideID = fideId
}
case 4:
player.Country = stripTags(match[1])
case 5:
elo, err := strconv.Atoi(stripTags(match[1]))
if err != nil {
return Player{}, err
}
player.Elo = elo
case 6:
player.Club = html.UnescapeString(stripTags(match[1]))
}
}
return player, nil
}
func parseRoundPairing(rowData string) (Pairing, error) {
pairing := Pairing{
White: PairingPlayerDetails{},
Black: PairingPlayerDetails{},
}
pairingCellRegex := regexp.MustCompile(`<td[^>]*>(.*?)<\/td>`)
pairingCells := pairingCellRegex.FindAllStringSubmatch(rowData, -1)
if len(pairingCells) != 12 {
return Pairing{}, errors.New("Not the correct amount of cells")
}
for index, match := range pairingCells {
switch index {
case 0:
num, err := strconv.Atoi(stripTags(match[1]))
if err != nil {
return Pairing{}, err
}
pairing.Board = num
case 1:
num, err := strconv.Atoi(stripTags(match[1]))
if err != nil {
return Pairing{}, err
}
pairing.White.Num = num
case 2:
title, err := parseFideTitle(stripTags(match[1]))
if err != nil {
return Pairing{}, err
}
pairing.White.FideTitle = title
case 3:
name := stripTags(match[1])
if len(name) < 1 {
return Pairing{}, errors.New("no name is present")
}
pairing.White.Name = name
case 4:
elo, err := strconv.Atoi(stripTags(match[1]))
if err != nil {
return Pairing{}, err
}
pairing.White.Elo = elo
case 5:
if points, err := convertChessNum(stripTags(match[1])); err == nil {
pairing.Black.Points = points
}
case 6:
pairing.Result = parseGameResult(stripTags(match[1]))
case 7:
if points, err := convertChessNum(stripTags(match[1])); err == nil {
pairing.Black.Points = points
}
case 8:
if title, err := parseFideTitle(stripTags(match[1])); err == nil {
pairing.Black.FideTitle = title
}
case 9:
name := stripTags(match[1])
if len(name) > 0 {
pairing.Black.Name = name
}
case 10:
if elo, err := strconv.Atoi(stripTags(match[1])); err == nil {
pairing.Black.Elo = elo
}
case 11:
if num, err := strconv.Atoi(stripTags(match[1])); err == nil {
pairing.Black.Num = num
}
}
}
return pairing, nil
}
func parseRound(title string, rawTable string) (Round, error) {
round := Round{
Title: title,
}
reRoundPairings := regexp.MustCompile(`<tr[^>]*>(.*?)<\/tr>`)
roundPairings := reRoundPairings.FindAllStringSubmatch(rawTable, -1)
var pairings []Pairing
for _, pairing := range roundPairings {
parsed, err := parseRoundPairing(pairing[1])
if err == nil {
pairings = append(pairings, parsed)
}
}
round.Pairings = pairings
return round, nil
}
func newTournament(api *Api, id string) (*Tournament, error) {
tournament := &Tournament{
api: api,
id: id,
}
if err := tournament.Refresh(); err != nil {
return nil, err
}
return tournament, nil
}
func (t *Tournament) UpdateSecurityData() error {
rawData, err := t.api.get(fmt.Sprintf("/%s.aspx", t.id))
if err != nil {
return err
}
secValues := url.Values{}
secValues.Set("cb_alleDetails", "Turnierdetails anzeigen")
secValues.Set("txt_name", "")
reSecData := regexp.MustCompile(`<input type="hidden".*?name="([^"]*?)".*?value="([^"]*?)"`)
inputsData := reSecData.FindAllStringSubmatch(rawData, -1)
for _, match := range inputsData {
secValues.Set(match[1], match[2])
}
t.secInputData = secValues
return nil
}
func (t *Tournament) Refresh() error {
t.data = TournamentData{}
if err := t.UpdateSecurityData(); err != nil {
return err
}
rawData, err := t.api.post(fmt.Sprintf("/%s.aspx?turdet=YES", t.id), t.secInputData)
if err != nil {
return err
}
// Tournament Information
reName := regexp.MustCompile(`<h2>(.*?)</h2>`)
reMetaReader := regexp.MustCompile(`<tr[^>]*?><td[^>]*?>([^>]*?)</td><td[^>]*>(.*?)</td></tr>`)
t.data.Name = html.UnescapeString(reName.FindStringSubmatch(rawData)[1])
t.data.RawMeta = map[string]string{}
metaEntries := reMetaReader.FindAllStringSubmatch(rawData, -1)
for _, match := range metaEntries {
key := match[1]
value := html.UnescapeString(match[2])
if key != "" {
t.data.RawMeta[key] = value
switch key {
case "Organizer(s)":
t.data.Organizer = value
case "Federation":
t.data.Federation = value
case "Tournament director":
t.data.Director = value
case "Chief Arbiter":
t.data.ChiefArbiter = value
case "Deputy Chief Arbiter":
t.data.DeputyChiefArbiter = value
case "Time control (Standard)":
t.data.TimeControl = value
case "Location":
t.data.Location = value
case "Number of rounds":
t.data.NumberOfRounds = value
case "Tournament type":
t.data.TournamentMode = value
case "Rating calculation":
t.data.EloCalculation = value
case "Rating-Ø":
t.data.AverageElo = value
case "Date":
t.data.FromTo = value
}
}
}
// Player Information
rePlayerRows := regexp.MustCompile(`<tr[^>]*>(.*?)<\/tr>`)
var players []Player
playerRows := rePlayerRows.FindAllStringSubmatch(rawData, -1)
for _, match := range playerRows {
player, err := parsePlayerRow(match[1])
if err == nil {
players = append(players, player)
}
}
t.data.Players = players
// Round Information
roundRawData, err := t.api.post(fmt.Sprintf("/%s.aspx?art=2&zeilen=99999&turdet=YES", t.id), t.secInputData)
if err != nil {
return err
}
reRounds := regexp.MustCompile(`<h3>(.*?)<\/h3>.*?<table.*?>(.*?)<\/table>`)
var rounds []Round
roundTables := reRounds.FindAllStringSubmatch(strings.ReplaceAll(roundRawData, "\n", ""), -1)
if len(roundTables) > 0 {
for _, match := range roundTables {
title := html.UnescapeString(match[1])
table := html.UnescapeString(match[2])
round, err := parseRound(title, table)
if err == nil {
rounds = append(rounds, round)
}
}
t.data.Rounds = rounds
} else {
reRoundHeadlines := regexp.MustCompile(`<tr[^>]*><td[^>]*>([^<]*)</td></tr>`)
reRoundRows := regexp.MustCompile(`(<tr[^>]*><td[^>]*>[^<]*</td></tr>|</table>)`)
var rounds []Round
rawData := strings.ReplaceAll(roundRawData, "\n", "")
roundHeadlines := reRoundHeadlines.FindAllStringSubmatch(rawData, -1)
for _, match := range roundHeadlines {
title := html.UnescapeString(match[1])
rawWithoutPrefix := strings.Split(rawData, match[0])[1]
split := reRoundRows.Split(rawWithoutPrefix, 2)
if len(split) > 0 {
table := html.UnescapeString(split[0])
round, err := parseRound(title, table)
if err == nil {
rounds = append(rounds, round)
}
}
}
t.data.Rounds = rounds
}
return nil
}