forked from heroiclabs/nakama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
api_tournament.go
468 lines (398 loc) · 19.1 KB
/
api_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
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
// 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 (
"bytes"
"context"
"encoding/base64"
"encoding/gob"
"encoding/json"
"fmt"
"time"
"go.opencensus.io/stats"
"go.opencensus.io/tag"
"go.opencensus.io/trace"
"go.uber.org/zap"
"github.com/gofrs/uuid"
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/wrappers"
"github.com/heroiclabs/nakama/api"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func (s *ApiServer) JoinTournament(ctx context.Context, in *api.JoinTournamentRequest) (*empty.Empty, error) {
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
username := ctx.Value(ctxUsernameKey{}).(string)
// Before hook.
if fn := s.runtime.BeforeJoinTournament(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
result, err, code := fn(ctx, s.logger, userID.String(), username, ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return nil, status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", fullMethod), zap.String("uid", userID.String()))
return nil, status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
tournamentId := in.GetTournamentId()
if err := TournamentJoin(ctx, s.logger, s.db, s.leaderboardCache, userID.String(), username, tournamentId); err != nil {
if err == ErrTournamentNotFound {
return nil, status.Error(codes.NotFound, "Tournament not found.")
} else if err == ErrTournamentMaxSizeReached {
return nil, status.Error(codes.InvalidArgument, "Tournament cannot be joined as it has reached its max size.")
} else if err == ErrTournamentOutsideDuration {
return nil, status.Error(codes.InvalidArgument, "Tournament is not active and cannot accept new joins.")
}
return nil, status.Error(codes.Internal, "Error while trying to join tournament.")
}
// After hook.
if fn := s.runtime.AfterJoinTournament(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, userID.String(), username, ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
return &empty.Empty{}, nil
}
func (s *ApiServer) ListTournamentRecords(ctx context.Context, in *api.ListTournamentRecordsRequest) (*api.TournamentRecordList, error) {
// Before hook.
if fn := s.runtime.BeforeListTournamentRecords(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
result, err, code := fn(ctx, s.logger, ctx.Value(ctxUserIDKey{}).(uuid.UUID).String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return nil, status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", fullMethod), zap.String("uid", ctx.Value(ctxUserIDKey{}).(uuid.UUID).String()))
return nil, status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
if in.GetTournamentId() == "" {
return nil, status.Error(codes.InvalidArgument, "Tournament ID must be provided")
}
tournament := s.leaderboardCache.Get(in.GetTournamentId())
if tournament == nil {
return nil, status.Error(codes.NotFound, "Tournament not found.")
}
if tournament.EndTime <= time.Now().UTC().Unix() {
return nil, status.Error(codes.NotFound, "Tournament not found or has ended.")
}
var limit *wrappers.Int32Value
if in.GetLimit() != nil {
if in.GetLimit().Value < 1 || in.GetLimit().Value > 100 {
return nil, status.Error(codes.InvalidArgument, "Invalid limit - limit must be between 1 and 100.")
}
limit = in.GetLimit()
} else if len(in.GetOwnerIds()) == 0 || in.GetCursor() != "" {
limit = &wrappers.Int32Value{Value: 1}
}
if len(in.GetOwnerIds()) != 0 {
for _, ownerId := range in.OwnerIds {
if _, err := uuid.FromString(ownerId); err != nil {
return nil, status.Error(codes.InvalidArgument, "One or more owner IDs are invalid.")
}
}
}
records, err := LeaderboardRecordsList(ctx, s.logger, s.db, s.leaderboardCache, s.leaderboardRankCache, in.GetTournamentId(), limit, in.GetCursor(), in.GetOwnerIds(), 0)
if err == ErrLeaderboardNotFound {
return nil, status.Error(codes.NotFound, "Tournament not found.")
} else if err == ErrLeaderboardInvalidCursor {
return nil, status.Error(codes.InvalidArgument, "Cursor is invalid or expired.")
} else if err != nil {
return nil, status.Error(codes.Internal, "Error listing records from tournament.")
}
recordList := &api.TournamentRecordList{
Records: records.Records,
OwnerRecords: records.OwnerRecords,
NextCursor: records.NextCursor,
PrevCursor: records.PrevCursor,
}
// After hook.
if fn := s.runtime.AfterListTournamentRecords(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, ctx.Value(ctxUserIDKey{}).(uuid.UUID).String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, recordList, in)
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
return recordList, nil
}
func (s *ApiServer) ListTournaments(ctx context.Context, in *api.ListTournamentsRequest) (*api.TournamentList, error) {
// Before hook.
if fn := s.runtime.BeforeListTournaments(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
result, err, code := fn(ctx, s.logger, ctx.Value(ctxUserIDKey{}).(uuid.UUID).String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return nil, status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", fullMethod), zap.String("uid", ctx.Value(ctxUserIDKey{}).(uuid.UUID).String()))
return nil, status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
var incomingCursor *tournamentListCursor
if in.GetCursor() != "" {
if cb, err := base64.StdEncoding.DecodeString(in.GetCursor()); err != nil {
return nil, ErrLeaderboardInvalidCursor
} else {
incomingCursor = &tournamentListCursor{}
if err := gob.NewDecoder(bytes.NewReader(cb)).Decode(incomingCursor); err != nil {
return nil, ErrLeaderboardInvalidCursor
}
}
}
categoryStart := 0
if in.GetCategoryStart() != nil {
categoryStart = int(in.GetCategoryStart().GetValue())
}
categoryEnd := 127
if in.GetCategoryEnd() != nil {
categoryEnd = int(in.GetCategoryEnd().GetValue())
if categoryEnd >= 128 {
return nil, status.Error(codes.InvalidArgument, "Tournament category end must be >=0 and <128.")
}
if categoryEnd < categoryStart {
return nil, status.Error(codes.InvalidArgument, "Tournament category end must be greater than category start.")
}
}
startTime := -1 // don't include start time in query
if in.GetStartTime() != nil {
startTime = int(in.GetStartTime().GetValue())
}
endTime := int(time.Now().UTC().AddDate(1, 0, 0).Unix()) // one year from now
if in.GetEndTime() != nil {
endTime = int(in.GetEndTime().GetValue())
if endTime < startTime {
return nil, status.Error(codes.InvalidArgument, "Tournament end time must be greater than start time.")
}
}
limit := 1
if in.GetLimit() != nil {
limit = int(in.GetLimit().GetValue())
if limit < 1 || limit > 100 {
return nil, status.Error(codes.InvalidArgument, "Limit must be between 1 and 100.")
}
}
records, err := TournamentList(ctx, s.logger, s.db, categoryStart, categoryEnd, startTime, endTime, limit, incomingCursor)
if err != nil {
return nil, status.Error(codes.Internal, "Error listing tournaments.")
}
// After hook.
if fn := s.runtime.AfterListTournaments(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, ctx.Value(ctxUserIDKey{}).(uuid.UUID).String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, records, in)
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
return records, nil
}
func (s *ApiServer) WriteTournamentRecord(ctx context.Context, in *api.WriteTournamentRecordRequest) (*api.LeaderboardRecord, error) {
userID := ctx.Value(ctxUserIDKey{}).(uuid.UUID)
username := ctx.Value(ctxUsernameKey{}).(string)
// Before hook.
if fn := s.runtime.BeforeWriteTournamentRecord(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
result, err, code := fn(ctx, s.logger, userID.String(), username, ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return nil, status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", fullMethod), zap.String("uid", userID.String()))
return nil, status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
if in.GetTournamentId() == "" {
return nil, status.Error(codes.InvalidArgument, "Tournament ID must be provided")
}
if in.GetTournamentId() == "" {
return nil, status.Error(codes.InvalidArgument, "Invalid tournament ID.")
} else if in.GetRecord() == nil {
return nil, status.Error(codes.InvalidArgument, "Invalid input, record score value is required.")
} else if in.GetRecord().GetMetadata() != "" {
var maybeJSON map[string]interface{}
if json.Unmarshal([]byte(in.GetRecord().GetMetadata()), &maybeJSON) != nil {
return nil, status.Error(codes.InvalidArgument, "Metadata value must be JSON, if provided.")
}
}
tournament := s.leaderboardCache.Get(in.GetTournamentId())
if tournament == nil {
return nil, status.Error(codes.NotFound, "Tournament not found.")
}
if tournament.EndTime > 0 && tournament.EndTime <= time.Now().UTC().Unix() {
return nil, status.Error(codes.NotFound, "Tournament not found or has ended.")
}
record, err := TournamentRecordWrite(ctx, s.logger, s.db, s.leaderboardCache, s.leaderboardRankCache, in.GetTournamentId(), userID, username, in.GetRecord().GetScore(), in.GetRecord().GetSubscore(), in.GetRecord().GetMetadata())
if err != nil {
if err == ErrTournamentMaxSizeReached {
return nil, status.Error(codes.InvalidArgument, "Tournament has reached max size.")
} else if err == ErrTournamentWriteMaxNumScoreReached {
return nil, status.Error(codes.InvalidArgument, "Reached allowed max number of score attempts.")
} else if err == ErrTournamentWriteJoinRequired {
return nil, status.Error(codes.InvalidArgument, "Must join tournament before attempting to write value.")
} else if err == ErrTournamentOutsideDuration {
return nil, status.Error(codes.InvalidArgument, "Tournament is not active and cannot accept new scores.")
} else {
return nil, status.Error(codes.Internal, "Error writing score to tournament.")
}
}
// After hook.
if fn := s.runtime.AfterWriteTournamentRecord(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, userID.String(), username, ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, record, in)
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
return record, nil
}
func (s *ApiServer) ListTournamentRecordsAroundOwner(ctx context.Context, in *api.ListTournamentRecordsAroundOwnerRequest) (*api.TournamentRecordList, error) {
// Before hook.
if fn := s.runtime.BeforeListTournamentRecordsAroundOwner(); fn != nil {
// Stats measurement start boundary.
fullMethod := ctx.Value(ctxFullMethodKey{}).(string)
name := fmt.Sprintf("%v-before", fullMethod)
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
result, err, code := fn(ctx, s.logger, ctx.Value(ctxUserIDKey{}).(uuid.UUID).String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, in)
if err != nil {
return nil, status.Error(code, err.Error())
}
if result == nil {
// If result is nil, requested resource is disabled.
s.logger.Warn("Intercepted a disabled resource.", zap.Any("resource", fullMethod), zap.String("uid", ctx.Value(ctxUserIDKey{}).(uuid.UUID).String()))
return nil, status.Error(codes.NotFound, "Requested resource was not found.")
}
in = result
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
if in.GetTournamentId() == "" {
return nil, status.Error(codes.InvalidArgument, "Invalid tournament ID.")
}
limit := 1
if in.GetLimit() != nil {
if in.GetLimit().Value < 1 || in.GetLimit().Value > 100 {
return nil, status.Error(codes.InvalidArgument, "Invalid limit - limit must be between 1 and 100.")
}
limit = int(in.GetLimit().Value)
}
if in.GetOwnerId() == "" {
return nil, status.Error(codes.InvalidArgument, "Owner ID must be provided for a haystack query.")
}
ownerId, err := uuid.FromString(in.GetOwnerId())
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid owner ID provided.")
}
records, err := TournamentRecordsHaystack(ctx, s.logger, s.db, s.leaderboardCache, s.leaderboardRankCache, in.GetTournamentId(), ownerId, limit)
if err == ErrLeaderboardNotFound {
return nil, status.Error(codes.NotFound, "Tournament not found.")
} else if err != nil {
return nil, status.Error(codes.Internal, "Error querying records from leaderboard.")
}
list := &api.TournamentRecordList{Records: records}
// After hook.
if fn := s.runtime.AfterListTournamentRecordsAroundOwner(); fn != nil {
// Stats measurement start boundary.
name := fmt.Sprintf("%v-after", ctx.Value(ctxFullMethodKey{}).(string))
statsCtx, _ := tag.New(context.Background(), tag.Upsert(MetricsFunction, name))
startNanos := time.Now().UTC().UnixNano()
span := trace.NewSpan(name, nil, trace.StartOptions{})
// Extract request information and execute the hook.
clientIP, clientPort := extractClientAddress(s.logger, ctx)
fn(ctx, s.logger, ctx.Value(ctxUserIDKey{}).(uuid.UUID).String(), ctx.Value(ctxUsernameKey{}).(string), ctx.Value(ctxExpiryKey{}).(int64), clientIP, clientPort, list, in)
// Stats measurement end boundary.
span.End()
stats.Record(statsCtx, MetricsApiTimeSpentMsec.M(float64(time.Now().UTC().UnixNano()-startNanos)/1000), MetricsApiCount.M(1))
}
return list, nil
}