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
|
package game
import (
"context"
"errors"
"log"
"time"
"github.com/jackc/pgx/v5/pgtype"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/api"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/db"
"github.com/nsfisis/iosdc-japan-2024-albatross/backend/taskqueue"
)
type playerClientState int
const (
playerClientStateWaitingEntries playerClientState = iota
playerClientStateEntried
playerClientStateReady
)
type gameHub struct {
ctx context.Context
game *game
q *db.Queries
taskQueue *taskqueue.Queue
players map[*playerClient]playerClientState
registerPlayer chan *playerClient
unregisterPlayer chan *playerClient
playerC2SMessages chan *playerMessageC2SWithClient
watchers map[*watcherClient]bool
registerWatcher chan *watcherClient
unregisterWatcher chan *watcherClient
testcaseExecution chan taskqueue.TaskExecResult
}
func newGameHub(ctx context.Context, game *game, q *db.Queries, taskQueue *taskqueue.Queue) *gameHub {
return &gameHub{
ctx: ctx,
game: game,
q: q,
taskQueue: taskQueue,
players: make(map[*playerClient]playerClientState),
registerPlayer: make(chan *playerClient),
unregisterPlayer: make(chan *playerClient),
playerC2SMessages: make(chan *playerMessageC2SWithClient),
watchers: make(map[*watcherClient]bool),
registerWatcher: make(chan *watcherClient),
unregisterWatcher: make(chan *watcherClient),
testcaseExecution: make(chan taskqueue.TaskExecResult),
}
}
func (hub *gameHub) run() {
ticker := time.NewTicker(10 * time.Second)
defer func() {
ticker.Stop()
}()
for {
select {
case player := <-hub.registerPlayer:
hub.players[player] = playerClientStateWaitingEntries
case player := <-hub.unregisterPlayer:
if _, ok := hub.players[player]; ok {
hub.closePlayerClient(player)
}
case watcher := <-hub.registerWatcher:
hub.watchers[watcher] = true
case watcher := <-hub.unregisterWatcher:
if _, ok := hub.watchers[watcher]; ok {
hub.closeWatcherClient(watcher)
}
case message := <-hub.playerC2SMessages:
switch msg := message.message.(type) {
case *playerMessageC2SEntry:
log.Printf("entry: %v", message.message)
// TODO: assert state is waiting_entries
hub.players[message.client] = playerClientStateEntried
entriedPlayerCount := 0
for _, state := range hub.players {
if playerClientStateEntried <= state {
entriedPlayerCount++
}
}
if entriedPlayerCount == hub.game.playerCount {
err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
State: string(gameStateWaitingStart),
})
if err != nil {
log.Fatalf("failed to set game state: %v", err)
}
hub.game.state = gameStateWaitingStart
}
case *playerMessageC2SReady:
log.Printf("ready: %v", message.message)
// TODO: assert state is prepare
hub.players[message.client] = playerClientStateReady
readyPlayerCount := 0
for _, state := range hub.players {
if playerClientStateReady <= state {
readyPlayerCount++
}
}
if readyPlayerCount == hub.game.playerCount {
startAt := time.Now().Add(11 * time.Second).UTC()
for player := range hub.players {
player.s2cMessages <- &playerMessageS2CStart{
Type: playerMessageTypeS2CStart,
Data: playerMessageS2CStartPayload{
StartAt: int(startAt.Unix()),
},
}
}
for watcher := range hub.watchers {
watcher.s2cMessages <- &watcherMessageS2CStart{
Type: watcherMessageTypeS2CStart,
Data: watcherMessageS2CStartPayload{
StartAt: int(startAt.Unix()),
},
}
}
err := hub.q.UpdateGameStartedAt(hub.ctx, db.UpdateGameStartedAtParams{
GameID: int32(hub.game.gameID),
StartedAt: pgtype.Timestamp{
Time: startAt,
InfinityModifier: pgtype.Finite,
Valid: true,
},
})
if err != nil {
log.Fatalf("failed to set game state: %v", err)
}
hub.game.startedAt = &startAt
err = hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
State: string(gameStateStarting),
})
if err != nil {
log.Fatalf("failed to set game state: %v", err)
}
hub.game.state = gameStateStarting
}
case *playerMessageC2SCode:
// TODO: assert game state is gaming
log.Printf("code: %v", message.message)
code := msg.Data.Code
for watcher := range hub.watchers {
watcher.s2cMessages <- &watcherMessageS2CCode{
Type: watcherMessageTypeS2CCode,
Data: watcherMessageS2CCodePayload{
PlayerID: message.client.playerID,
Code: code,
},
}
}
case *playerMessageC2SSubmit:
// TODO: assert game state is gaming
log.Printf("submit: %v", message.message)
code := msg.Data.Code
task, err := taskqueue.NewExecTask(hub.game.gameID, message.client.playerID, code)
if err != nil {
log.Fatalf("failed to create task: %v", err)
}
hub.taskQueue.Enqueue(task)
default:
log.Printf("unexpected message type: %T", message.message)
}
case executionResult := <-hub.testcaseExecution:
for player := range hub.players {
player.s2cMessages <- &playerMessageS2CExecResult{
Type: playerMessageTypeS2CExecResult,
Data: playerMessageS2CExecResultPayload{
Score: nil,
Status: api.GamePlayerMessageS2CExecResultPayloadStatus(executionResult.Result),
},
}
}
// broadcast to watchers
case <-ticker.C:
if hub.game.state == gameStateStarting {
if time.Now().After(*hub.game.startedAt) {
err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
State: string(gameStateGaming),
})
if err != nil {
log.Fatalf("failed to set game state: %v", err)
}
hub.game.state = gameStateGaming
}
} else if hub.game.state == gameStateGaming {
if time.Now().After(hub.game.startedAt.Add(time.Duration(hub.game.durationSeconds) * time.Second)) {
err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
State: string(gameStateFinished),
})
if err != nil {
log.Fatalf("failed to set game state: %v", err)
}
hub.game.state = gameStateFinished
hub.close()
return
}
}
}
}
}
func (hub *gameHub) startGame() error {
for player := range hub.players {
player.s2cMessages <- &playerMessageS2CPrepare{
Type: playerMessageTypeS2CPrepare,
Data: playerMessageS2CPreparePayload{
Problem: api.Problem{
ProblemID: 1,
Title: "the answer",
Description: "print 42",
},
},
}
}
err := hub.q.UpdateGameState(hub.ctx, db.UpdateGameStateParams{
GameID: int32(hub.game.gameID),
State: string(gameStatePrepare),
})
if err != nil {
return err
}
hub.game.state = gameStatePrepare
return nil
}
func (hub *gameHub) close() {
for client := range hub.players {
hub.closePlayerClient(client)
}
close(hub.registerPlayer)
close(hub.unregisterPlayer)
close(hub.playerC2SMessages)
for watcher := range hub.watchers {
hub.closeWatcherClient(watcher)
}
close(hub.registerWatcher)
close(hub.unregisterWatcher)
}
func (hub *gameHub) closePlayerClient(player *playerClient) {
delete(hub.players, player)
close(player.s2cMessages)
}
func (hub *gameHub) closeWatcherClient(watcher *watcherClient) {
delete(hub.watchers, watcher)
close(watcher.s2cMessages)
}
type GameHubs struct {
hubs map[int]*gameHub
q *db.Queries
taskQueue *taskqueue.Queue
taskResults chan taskqueue.TaskExecResult
}
func NewGameHubs(q *db.Queries, taskQueue *taskqueue.Queue, taskResults chan taskqueue.TaskExecResult) *GameHubs {
return &GameHubs{
hubs: make(map[int]*gameHub),
q: q,
taskQueue: taskQueue,
taskResults: taskResults,
}
}
func (hubs *GameHubs) Close() {
log.Println("closing all game hubs")
for _, hub := range hubs.hubs {
hub.close()
}
}
func (hubs *GameHubs) getHub(gameID int) *gameHub {
return hubs.hubs[gameID]
}
func (hubs *GameHubs) RestoreFromDB(ctx context.Context) error {
games, err := hubs.q.ListGames(ctx)
if err != nil {
return err
}
for _, row := range games {
var startedAt *time.Time
if row.StartedAt.Valid {
startedAt = &row.StartedAt.Time
}
var problem_ *problem
if row.ProblemID != nil {
if row.Title == nil || row.Description == nil {
panic("inconsistent data")
}
problem_ = &problem{
problemID: int(*row.ProblemID),
title: *row.Title,
description: *row.Description,
}
}
// TODO: N+1
playerRows, err := hubs.q.ListGamePlayers(ctx, int32(row.GameID))
if err != nil {
return err
}
hubs.hubs[int(row.GameID)] = newGameHub(ctx, &game{
gameID: int(row.GameID),
durationSeconds: int(row.DurationSeconds),
state: gameState(row.State),
displayName: row.DisplayName,
startedAt: startedAt,
problem: problem_,
playerCount: len(playerRows),
}, hubs.q, hubs.taskQueue)
}
return nil
}
func (hubs *GameHubs) Run() {
for _, hub := range hubs.hubs {
go hub.run()
}
for taskResult := range hubs.taskResults {
hub := hubs.getHub(taskResult.Task.GameID)
if hub == nil {
log.Printf("no such game: %d", taskResult.Task.GameID)
continue
}
hub.testcaseExecution <- taskResult
}
}
func (hubs *GameHubs) SockHandler() *sockHandler {
return newSockHandler(hubs)
}
func (hubs *GameHubs) StartGame(gameID int) error {
hub := hubs.getHub(gameID)
if hub == nil {
return errors.New("no such game")
}
return hub.startGame()
}
|