-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
387 lines (348 loc) · 8.96 KB
/
main.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
"log"
"math"
"os"
"regexp"
"sort"
"strconv"
"strings"
"text/template"
"time"
"github.com/golang/protobuf/proto"
"github.com/menocar/xmageleaderboard/record"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/js"
)
var matchLog = flag.String("match_log", "", "")
type byEndTimeMsec []*record.Match
func (b byEndTimeMsec) Len() int { return len(b) }
func (b byEndTimeMsec) Less(i, j int) bool { return b[i].GetEndTimeMsec() < b[j].GetEndTimeMsec() }
func (b byEndTimeMsec) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
func read() *record.Matches {
data, err := ioutil.ReadFile(*matchLog)
if err != nil {
log.Fatal(err)
}
matches := &record.Matches{}
err = proto.Unmarshal(data, matches)
if err != nil {
log.Fatal(err)
}
sort.Sort(byEndTimeMsec(matches.GetMatches()))
return matches
}
func Elo(ratingA, ratingB, scoreA, scoreB float64) (newRatingA, newRatingB float64) {
expectedScoreA := 1.0 / (1.0 + math.Pow(10.0, (ratingB-ratingA)/400.0))
expectedScoreB := 1.0 / (1.0 + math.Pow(10.0, (ratingA-ratingB)/400.0))
const K = 16
newRatingA = ratingA + K*(scoreA-expectedScoreA)
newRatingB = ratingB + K*(scoreB-expectedScoreB)
return
}
type Player struct {
Name string
Draftbot int
Quit int
ComputerPlayer bool
Bye int
Win int
Lose int
Matches []*Match
Rating float64
NLCMember bool
}
type players map[string]*Player
func (ps players) getPlayer(name string) *Player {
p, ok := ps[name]
if !ok {
p = &Player{
Name: name,
Rating: 1500.0,
NLCMember: true,
}
ps[name] = p
}
return p
}
type byRating []*Player
func (b byRating) Len() int { return len(b) }
func (b byRating) Less(i, j int) bool { return b[i].Rating > b[j].Rating }
func (b byRating) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
type Match struct {
ID int
DeckTypeRaw string
DeckType string
DeckTypeComment string
Players []*Player
PlayersRaw string
GameType string
Result string
Start time.Time
End time.Time
Results []*MatchResult
}
type matches map[int]*Match
var deckTypeRegexp = regexp.MustCompile("([^\\[]+)(?:\\[(.+)\\])?")
var deckTypeReplaceRegexp = regexp.MustCompile("\\s*Limited\\s*")
func (ms matches) getMatch(mp *record.Match) *Match {
id := int(mp.GetId())
m, ok := ms[id]
if !ok {
dt := deckTypeRegexp.FindStringSubmatch(mp.GetDeckType())
const (
deckTypeIndex = 1
deckTypeCommentIndex = 2
)
deckType := dt[deckTypeIndex]
if deckType == "" {
log.Fatalf("deck type is empty (%s)", mp.GetDeckType())
}
m = &Match{
ID: id,
DeckTypeRaw: mp.GetDeckType(),
DeckType: deckTypeReplaceRegexp.ReplaceAllString(deckType, ""),
DeckTypeComment: dt[deckTypeCommentIndex],
PlayersRaw: mp.GetPlayers(),
GameType: mp.GetGameType(),
Result: mp.GetResult(),
Start: time.Unix(0, mp.GetStartTimeMsec()*1000000),
End: time.Unix(0, mp.GetEndTimeMsec()*1000000),
}
ms[id] = m
}
return m
}
type byEnd []*Match
func (b byEnd) Len() int { return len(b) }
func (b byEnd) Less(i, j int) bool { return b[i].End.After(b[j].End) }
func (b byEnd) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
type PlayerResult struct {
Player *Player
Bye bool
Wins int
Quit bool
Timeout bool
RatingChange float64
}
func (r *PlayerResult) String() string {
var m string
if r.Quit {
m = "Q"
} else if r.Timeout {
m = "T"
}
return fmt.Sprintf("%d%s", r.Wins, m)
}
type MatchResult struct {
Round int
Player *PlayerResult
Opponent *PlayerResult
}
func (r *MatchResult) String() string {
ret := fmt.Sprintf("R%d ", r.Round)
if r.Player.Bye {
ret += fmt.Sprintf(" %s Bye", r.Player.Player.Name)
} else {
ret += fmt.Sprintf(" %s vs %s [%s-%s] - ", r.Player.Player.Name,
r.Opponent.Player.Name, r.Player.String(), r.Opponent.String())
if r.Win() {
ret += r.Player.Player.Name
} else {
ret += r.Opponent.Player.Name
}
ret += " wins ("
sp, so, ok := r.Score()
if !ok {
ret += "no-op"
} else {
var score, rating float64
if r.Win() {
score, rating = sp, r.Player.RatingChange
} else {
score, rating = so, r.Opponent.RatingChange
}
ret += fmt.Sprintf("%.2f, +%.2f", score, rating)
}
ret += ")"
}
return ret
}
func (r *MatchResult) Win() bool {
if r.Player.Quit || r.Player.Timeout {
return false
}
if r.Opponent.Quit || r.Opponent.Timeout {
return true
}
return r.Player.Wins > r.Opponent.Wins
}
func (r *MatchResult) Score() (scoreP, scoreO float64, ok bool) {
if r.Player.Quit || r.Opponent.Quit {
// XMage disconnects a lot lately, safe to treat these as such.
return 0, 0, false
}
p, o := float64(r.Player.Wins), float64(r.Opponent.Wins)
return p / (p + o), o / (p + o), true
}
type byRound []*MatchResult
func (b byRound) Len() int { return len(b) }
func (b byRound) Less(i, j int) bool { return b[i].Round < b[j].Round }
func (b byRound) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
var resultRegexp = regexp.MustCompile("(?:([a-zA-Z0-9_]+)|Draftbot \\(([a-zA-Z0-9_]+)\\)|(Computer Player [0-9]+)):( R[0-9]+ (?:(Bye)|[a-zA-Z0-9_]+ \\[[0-9]+[QT]?\\-[0-9]+[QT]?\\]))*")
var subResultRegexp = regexp.MustCompile("R([0-9]+) (?:(Bye)|([a-zA-Z0-9_]+) \\[([0-9]+)([A-Z])?\\-([0-9]+)([A-Z])?\\])")
func writeData(ps []*Player, ms []*Match) {
t := template.Must(template.ParseFiles("./templates/data.js"))
var b bytes.Buffer
bio := bufio.NewWriter(&b)
data := struct {
Players []*Player
Matches []*Match
LastUpdated time.Time
}{
Players: ps,
Matches: ms,
LastUpdated: time.Now(),
}
err := t.Execute(bio, data)
if err != nil {
log.Fatal(err)
}
err = bio.Flush()
if err != nil {
log.Fatal(err)
}
f, err := os.OpenFile("./leaderboard/data.js", os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
if err != nil {
log.Fatal(err)
}
defer f.Close()
js.Minify(minify.New(), f, bytes.NewReader(b.Bytes()), nil)
}
func toInt(s string) int {
i, err := strconv.Atoi(s)
if err != nil {
log.Fatal(err)
}
return i
}
func init() {
flag.Parse()
}
func main() {
ps := make(players)
ms := make(matches)
for _, matchProto := range read().GetMatches() {
if !strings.HasPrefix(matchProto.GetGameType(), "Booster Draft") {
continue
}
m := ms.getMatch(matchProto)
rs := resultRegexp.FindAllStringSubmatch(matchProto.GetResult(), -1)
for _, r := range rs {
const (
allIndex = 0
nameIndex = 1
draftbotIndex = 2
computerPlayerIndex = 3
resultIndex = 4
)
var p *Player
if name := r[nameIndex]; name != "" {
p = ps.getPlayer(name)
p.NLCMember = true
} else if name := r[draftbotIndex]; name != "" {
p = ps.getPlayer(name)
p.Draftbot++
p.NLCMember = false
} else if name := r[computerPlayerIndex]; name != "" {
p = ps.getPlayer(name)
p.ComputerPlayer = true
}
p.Matches = append(p.Matches, m)
m.Players = append(m.Players, p)
for _, sr := range subResultRegexp.FindAllStringSubmatch(r[allIndex], -1) {
const (
roundIndex = 1
byeIndex = 2
opponentIndex = 3
winIndex = 4
matchDetailIndex = 5 // "T/Q"
oppWinIndex = 6
oppMatchDetailIndex = 7
)
mr := &MatchResult{
Round: toInt(sr[roundIndex]),
}
if sr[byeIndex] == "Bye" {
p.Bye++
mr.Player = &PlayerResult{
Player: p,
Bye: true,
}
} else {
pd := sr[matchDetailIndex]
mr.Player = &PlayerResult{
Player: p,
Wins: toInt(sr[winIndex]),
Quit: pd == "Q",
Timeout: pd == "T",
}
od := sr[oppMatchDetailIndex]
mr.Opponent = &PlayerResult{
Player: ps.getPlayer(sr[opponentIndex]),
Wins: toInt(sr[oppWinIndex]),
Quit: od == "Q",
Timeout: od == "T",
}
if mr.Win() {
p.Win++
} else {
p.Lose++
}
if mr.Player.Quit {
p.Quit++
}
}
// Add a same result only once.
if mr.Opponent == nil || mr.Player.Player.Name < mr.Opponent.Player.Name {
m.Results = append(m.Results, mr)
}
}
}
sort.Sort(byRound(m.Results))
for _, r := range m.Results {
p, o := r.Player, r.Opponent
if p.Bye {
continue
}
if p.Player.ComputerPlayer || o.Player.ComputerPlayer {
// Do not compute rating for computer players.
continue
}
sp, so, ok := r.Score()
if !ok {
continue
}
rp, ro := p.Player.Rating, o.Player.Rating
p.Player.Rating, o.Player.Rating = Elo(rp, ro, sp, so)
p.RatingChange = p.Player.Rating - rp
o.RatingChange = o.Player.Rating - ro
}
}
var pslice []*Player
for _, p := range ps {
pslice = append(pslice, p)
}
sort.Sort(byRating(pslice))
var mslice []*Match
for _, m := range ms {
mslice = append(mslice, m)
}
sort.Sort(byEnd(mslice))
writeData(pslice, mslice)
}