forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaderboard_cache.go
593 lines (517 loc) · 16.5 KB
/
leaderboard_cache.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// Copyright 2018 The Nakama Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package server
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"strconv"
"sync"
"time"
"github.com/gorhill/cronexpr"
"github.com/lib/pq"
"go.uber.org/zap"
)
const (
LeaderboardSortOrderAscending = iota
LeaderboardSortOrderDescending
)
const (
LeaderboardOperatorBest = iota
LeaderboardOperatorSet
LeaderboardOperatorIncrement
)
type Leaderboard struct {
Id string
Authoritative bool
SortOrder int
Operator int
ResetScheduleStr string
ResetSchedule *cronexpr.Expression
Metadata string
CreateTime int64
Category int
Description string
Duration int
EndTime int64
JoinRequired bool
MaxSize int
MaxNumScore int
Title string
StartTime int64
}
func (l *Leaderboard) IsTournament() bool {
return l.Duration != 0
}
func (l *Leaderboard) GetId() string {
return l.Id
}
func (l *Leaderboard) GetAuthoritative() bool {
return l.Authoritative
}
func (l *Leaderboard) GetSortOrder() string {
switch l.SortOrder {
case LeaderboardSortOrderAscending:
return "asc"
case LeaderboardSortOrderDescending:
fallthrough
default:
return "desc"
}
}
func (l *Leaderboard) GetOperator() string {
switch l.Operator {
case LeaderboardOperatorSet:
return "set"
case LeaderboardOperatorIncrement:
return "incr"
case LeaderboardOperatorBest:
fallthrough
default:
return "best"
}
}
func (l *Leaderboard) GetReset() string {
return l.ResetScheduleStr
}
func (l *Leaderboard) GetMetadata() map[string]interface{} {
metadata := make(map[string]interface{})
if l.Metadata == "" || l.Metadata == "{}" {
return metadata
}
if err := json.Unmarshal([]byte(l.Metadata), &metadata); err != nil {
log.Printf("Could not unmarshal leaderboard metadata into map[string]interface{}: %v \r\n", err)
}
return metadata
}
func (l *Leaderboard) GetCreateTime() int64 {
return l.CreateTime
}
type LeaderboardCache interface {
Get(id string) *Leaderboard
GetAllLeaderboards() []*Leaderboard
RefreshAllLeaderboards(ctx context.Context) error
Create(ctx context.Context, id string, authoritative bool, sortOrder, operator int, resetSchedule, metadata string) (*Leaderboard, error)
Insert(id string, authoritative bool, sortOrder, operator int, resetSchedule, metadata string, createTime int64)
CreateTournament(ctx context.Context, id string, sortOrder, operator int, resetSchedule, metadata, title, description string, category, startTime, endTime, duration, maxSize, maxNumScore int, joinRequired bool) (*Leaderboard, error)
InsertTournament(id string, sortOrder, operator int, resetSchedule, metadata, title, description string, category, duration, maxSize, maxNumScore int, joinRequired bool, createTime, startTime, endTime int64)
Delete(ctx context.Context, id string) error
Remove(id string)
}
type LocalLeaderboardCache struct {
sync.RWMutex
logger *zap.Logger
db *sql.DB
leaderboards map[string]*Leaderboard
}
func NewLocalLeaderboardCache(logger, startupLogger *zap.Logger, db *sql.DB) LeaderboardCache {
l := &LocalLeaderboardCache{
logger: logger,
db: db,
leaderboards: make(map[string]*Leaderboard),
}
err := l.RefreshAllLeaderboards(context.Background())
if err != nil {
startupLogger.Fatal("Error loading leaderboard cache from database", zap.Error(err))
}
return l
}
func (l *LocalLeaderboardCache) RefreshAllLeaderboards(ctx context.Context) error {
query := `
SELECT
id, authoritative, sort_order, operator, reset_schedule, metadata, create_time,
category, description, duration, end_time, join_required, max_size, max_num_score, title, start_time
FROM leaderboard`
rows, err := l.db.QueryContext(ctx, query)
if err != nil {
l.logger.Error("Error loading leaderboard cache from database", zap.Error(err))
return err
}
defer rows.Close()
leaderboards := make(map[string]*Leaderboard)
for rows.Next() {
var id string
var authoritative bool
var sortOrder int
var operator int
var resetSchedule sql.NullString
var metadata string
var createTime pq.NullTime
var category int
var description string
var duration int
var endTime pq.NullTime
var joinRequired bool
var maxSize int
var maxNumScore int
var title string
var startTime pq.NullTime
err = rows.Scan(&id, &authoritative, &sortOrder, &operator, &resetSchedule, &metadata, &createTime,
&category, &description, &duration, &endTime, &joinRequired, &maxSize, &maxNumScore, &title, &startTime)
if err != nil {
l.logger.Error("Error parsing leaderboard cache from database", zap.Error(err))
return err
}
leaderboard := &Leaderboard{
Id: id,
Authoritative: authoritative,
SortOrder: sortOrder,
Operator: operator,
Metadata: metadata,
CreateTime: createTime.Time.Unix(),
Category: category,
Description: description,
Duration: duration,
EndTime: 0,
JoinRequired: joinRequired,
MaxSize: maxSize,
MaxNumScore: maxNumScore,
Title: title,
StartTime: startTime.Time.Unix(),
}
if resetSchedule.Valid {
expr, err := cronexpr.Parse(resetSchedule.String)
if err != nil {
l.logger.Error("Error parsing leaderboard reset schedule from database", zap.Error(err))
return err
}
leaderboard.ResetScheduleStr = resetSchedule.String
leaderboard.ResetSchedule = expr
}
if endTime.Valid {
leaderboard.EndTime = endTime.Time.Unix()
}
leaderboards[id] = leaderboard
}
l.Lock()
l.leaderboards = leaderboards
l.Unlock()
return nil
}
func (l *LocalLeaderboardCache) Get(id string) *Leaderboard {
var lb *Leaderboard
l.RLock()
lb = l.leaderboards[id]
l.RUnlock()
return lb
}
func (l *LocalLeaderboardCache) GetAllLeaderboards() []*Leaderboard {
l.RLock()
leaderboards := make([]*Leaderboard, 0, len(l.leaderboards))
for _, v := range l.leaderboards {
leaderboards = append(leaderboards, v)
}
l.RUnlock()
return leaderboards
}
func (l *LocalLeaderboardCache) Create(ctx context.Context, id string, authoritative bool, sortOrder, operator int, resetSchedule, metadata string) (*Leaderboard, error) {
l.Lock()
if leaderboard, ok := l.leaderboards[id]; ok {
// Creation is an idempotent operation.
l.Unlock()
return leaderboard, nil
}
var expr *cronexpr.Expression
var err error
if resetSchedule != "" {
expr, err = cronexpr.Parse(resetSchedule)
if err != nil {
l.logger.Error("Error parsing leaderboard reset schedule", zap.Error(err))
return nil, err
}
}
// Insert into database first.
query := "INSERT INTO leaderboard (id, authoritative, sort_order, operator, metadata"
if resetSchedule != "" {
query += ", reset_schedule"
}
query += ") VALUES ($1, $2, $3, $4, $5"
if resetSchedule != "" {
query += ", $6"
}
query += ") RETURNING create_time"
params := []interface{}{id, authoritative, sortOrder, operator, metadata}
if resetSchedule != "" {
params = append(params, resetSchedule)
}
var createTime pq.NullTime
err = l.db.QueryRowContext(ctx, query, params...).Scan(&createTime)
if err != nil {
l.Unlock()
l.logger.Error("Error creating leaderboard", zap.Error(err))
return nil, err
}
// Then add to cache.
leaderboard := &Leaderboard{
Id: id,
Authoritative: authoritative,
SortOrder: sortOrder,
Operator: operator,
ResetScheduleStr: resetSchedule,
ResetSchedule: expr,
Metadata: metadata,
CreateTime: createTime.Time.Unix(),
}
l.leaderboards[id] = leaderboard
l.Unlock()
return leaderboard, nil
}
func (l *LocalLeaderboardCache) Insert(id string, authoritative bool, sortOrder, operator int, resetSchedule, metadata string, createTime int64) {
var expr *cronexpr.Expression
var err error
if resetSchedule != "" {
expr, err = cronexpr.Parse(resetSchedule)
if err != nil {
// Not expected, this insert is as a result of a previous create that has succeeded.
l.logger.Error("Error parsing leaderboard reset schedule for insert", zap.Error(err))
return
}
}
l.Lock()
l.leaderboards[id] = &Leaderboard{
Id: id,
Authoritative: authoritative,
SortOrder: sortOrder,
Operator: operator,
ResetScheduleStr: resetSchedule,
ResetSchedule: expr,
Metadata: metadata,
CreateTime: createTime,
}
l.Unlock()
}
func (l *LocalLeaderboardCache) CreateTournament(ctx context.Context, id string, sortOrder, operator int, resetSchedule, metadata, title, description string, category, startTime, endTime, duration, maxSize, maxNumScore int, joinRequired bool) (*Leaderboard, error) {
if err := checkTournamentConfig(resetSchedule, startTime, endTime, duration, maxSize, maxNumScore); err != nil {
l.logger.Error("Error while creating tournament", zap.Error(err))
return nil, err
}
l.RLock()
leaderboard := l.leaderboards[id]
l.RUnlock()
if leaderboard != nil {
if leaderboard.Duration > 0 {
// Creation is an idempotent operation.
return nil, nil // return nil for leaderboard to indicate no new creation
} else {
l.logger.Error("Cannot create tournament as leaderboard is already in use.", zap.String("leaderboard_id", id))
return nil, fmt.Errorf("cannot create tournament as leaderboard is already in use")
}
}
params := make([]interface{}, 0)
paramsIndex := make(map[string]string)
params = append(params, id)
paramsIndex["id"] = strconv.Itoa(len(params))
params = append(params, true)
paramsIndex["authoritative"] = strconv.Itoa(len(params))
params = append(params, sortOrder)
paramsIndex["sort_order"] = strconv.Itoa(len(params))
params = append(params, operator)
paramsIndex["operator"] = strconv.Itoa(len(params))
params = append(params, duration)
paramsIndex["duration"] = strconv.Itoa(len(params))
if resetSchedule != "" {
params = append(params, resetSchedule)
paramsIndex["reset_schedule"] = strconv.Itoa(len(params))
}
if metadata != "" {
params = append(params, metadata)
paramsIndex["metadata"] = strconv.Itoa(len(params))
}
if category >= 0 {
params = append(params, category)
paramsIndex["category"] = strconv.Itoa(len(params))
}
if description != "" {
params = append(params, description)
paramsIndex["description"] = strconv.Itoa(len(params))
}
if endTime > 0 {
params = append(params, time.Unix(int64(endTime), 0).UTC())
paramsIndex["end_time"] = strconv.Itoa(len(params))
}
if joinRequired {
params = append(params, joinRequired)
paramsIndex["join_required"] = strconv.Itoa(len(params))
}
if maxSize > 0 {
params = append(params, maxSize)
paramsIndex["max_size"] = strconv.Itoa(len(params))
}
if maxNumScore > 0 {
params = append(params, maxNumScore)
paramsIndex["max_num_score"] = strconv.Itoa(len(params))
}
if title != "" {
params = append(params, title)
paramsIndex["title"] = strconv.Itoa(len(params))
}
if startTime > 0 {
params = append(params, time.Unix(int64(startTime), 0).UTC())
paramsIndex["start_time"] = strconv.Itoa(len(params))
}
columns := ""
values := ""
for k, v := range paramsIndex {
if columns != "" {
columns += ", "
values += ", "
}
columns += k
values += "$" + v
}
query := "INSERT INTO leaderboard (" + columns + ") VALUES (" + values + ") RETURNING create_time, start_time, end_time"
l.logger.Debug("Create tournament query", zap.String("query", query))
var createTime pq.NullTime
var dbStartTime pq.NullTime
var dbEndTime pq.NullTime
err := l.db.QueryRowContext(ctx, query, params...).Scan(&createTime, &dbStartTime, &dbEndTime)
if err != nil {
l.logger.Error("Error creating tournament", zap.Error(err))
return nil, err
}
cron, _ := cronexpr.Parse(resetSchedule)
leaderboard = &Leaderboard{
Id: id,
Authoritative: true,
SortOrder: sortOrder,
Operator: operator,
ResetScheduleStr: resetSchedule,
ResetSchedule: cron,
Metadata: metadata,
CreateTime: createTime.Time.Unix(),
Category: category,
Description: description,
Duration: duration,
EndTime: 0,
JoinRequired: joinRequired,
MaxSize: maxSize,
MaxNumScore: maxNumScore,
Title: title,
StartTime: dbStartTime.Time.Unix(),
}
if dbEndTime.Valid {
leaderboard.EndTime = dbEndTime.Time.Unix()
}
l.Lock()
l.leaderboards[id] = leaderboard
l.Unlock()
return leaderboard, nil
}
func (l *LocalLeaderboardCache) InsertTournament(id string, sortOrder, operator int, resetSchedule, metadata, title, description string, category, duration, maxSize, maxNumScore int, joinRequired bool, createTime, startTime, endTime int64) {
var expr *cronexpr.Expression
var err error
if resetSchedule != "" {
expr, err = cronexpr.Parse(resetSchedule)
if err != nil {
// Not expected, this insert is as a result of a previous create that has succeeded.
l.logger.Error("Error parsing tournament reset schedule for insert", zap.Error(err))
return
}
}
l.Lock()
l.leaderboards[id] = &Leaderboard{
Id: id,
Authoritative: true,
SortOrder: sortOrder,
Operator: operator,
ResetScheduleStr: resetSchedule,
ResetSchedule: expr,
Metadata: metadata,
CreateTime: createTime,
Category: category,
Description: description,
Duration: duration,
JoinRequired: joinRequired,
MaxSize: maxSize,
MaxNumScore: maxNumScore,
Title: title,
StartTime: startTime,
EndTime: endTime,
}
l.Unlock()
}
func (l *LocalLeaderboardCache) Delete(ctx context.Context, id string) error {
l.Lock()
_, leaderboardFound := l.leaderboards[id]
l.Unlock()
if !leaderboardFound {
// Deletion is an idempotent operation.
return nil
}
// Delete from database first.
query := "DELETE FROM leaderboard WHERE id = $1"
_, err := l.db.ExecContext(ctx, query, id)
if err != nil {
l.logger.Error("Error deleting leaderboard", zap.Error(err))
return err
}
l.Lock()
// Then delete from cache.
delete(l.leaderboards, id)
l.Unlock()
return nil
}
func (l *LocalLeaderboardCache) Remove(id string) {
l.Lock()
delete(l.leaderboards, id)
l.Unlock()
}
func checkTournamentConfig(resetSchedule string, startTime, endTime, duration, maxSize, maxNumScore int) error {
if startTime < 0 {
return fmt.Errorf("tournament start time must be a unix UTC time in the future")
} else if startTime == 0 {
startTime = int(time.Now().UTC().Unix())
} else if time.Now().UTC().After(time.Unix(int64(startTime), 0).UTC()) {
return fmt.Errorf("tournament start time must be a unix UTC time in the future")
}
if duration <= 0 {
return fmt.Errorf("tournament duration must be greater than zero")
}
if maxSize < 0 {
return fmt.Errorf("tournament max must be greater than zero")
}
if maxNumScore < 0 {
return fmt.Errorf("tournament m num score must be greater than zero")
}
if (endTime > 0) && (endTime < startTime) {
return fmt.Errorf("tournament end time cannot be before start time")
}
if (endTime > 0) && (endTime < (startTime + duration)) {
return fmt.Errorf("tournament end time cannot be before end of first session")
}
var cron *cronexpr.Expression
if resetSchedule != "" {
expr, err := cronexpr.Parse(resetSchedule)
if err != nil {
return fmt.Errorf("could not parse reset schedule: %s", err.Error())
}
cron = expr
}
if cron != nil {
schedules := cron.NextN(time.Unix(int64(startTime), 0).UTC(), 2)
firstResetUnix := schedules[0].UTC().Unix()
secondResetUnix := schedules[1].UTC().Unix()
// Check that the end time (if specified) is at least strictly after the first active period start time.
if (endTime > 0) && (int64(endTime) <= firstResetUnix) {
return fmt.Errorf("tournament end time cannot be before first reset schedule - either increase end time or change/disable reset schedule")
}
// Check that the gap between resets is >= the duration of each tournament round.
if secondResetUnix-firstResetUnix < int64(duration) {
return fmt.Errorf("tournament cannot be scheduled to be reset while it is ongoing - either decrease duration or change/disable reset schedule")
}
}
return nil
}