forked from vanadium-archive/go.ref
/
judge.go
366 lines (330 loc) · 9.03 KB
/
judge.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
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"errors"
"fmt"
"strconv"
"sync"
"time"
"v.io/v23/context"
"v.io/v23/naming"
"v.io/x/ref/examples/rps"
"v.io/x/ref/examples/rps/internal"
"v.io/x/ref/internal/logger"
"v.io/x/ref/lib/stats"
"v.io/x/ref/lib/stats/counter"
)
var (
errBadGameID = errors.New("requested gameID doesn't exist")
errTooManyPlayers = errors.New("all players are already seated")
)
type Judge struct {
lock sync.Mutex
games map[rps.GameId]*gameInfo
gamesRun *counter.Counter
}
type sendStream interface {
Send(item rps.JudgeAction) error
}
type gameInfo struct {
id rps.GameId
startTime time.Time
score rps.ScoreCard
streams []sendStream
playerIn chan playerInput
playerOut []chan rps.JudgeAction
scoreChan chan scoreData
}
func (g *gameInfo) moveOptions() []string {
switch g.score.Opts.GameType {
case rps.LizardSpock:
return []string{"rock", "paper", "scissors", "lizard", "spock"}
default:
return []string{"rock", "paper", "scissors"}
}
}
func (g *gameInfo) validMove(m string) bool {
for _, x := range g.moveOptions() {
if x == m {
return true
}
}
return false
}
type playerInput struct {
player int
action rps.PlayerAction
}
type scoreData struct {
err error
score rps.ScoreCard
}
func NewJudge() *Judge {
return &Judge{
games: make(map[rps.GameId]*gameInfo),
gamesRun: stats.NewCounter("judge/games-run"),
}
}
func (j *Judge) Stats() int64 {
return j.gamesRun.Value()
}
// createGame handles a request to create a new game.
func (j *Judge) createGame(ownName string, opts rps.GameOptions) (rps.GameId, error) {
logger.Global().VI(1).Infof("createGame called")
score := rps.ScoreCard{Opts: opts, Judge: ownName}
now := time.Now()
id := rps.GameId{Id: strconv.FormatInt(now.UnixNano(), 16)}
j.lock.Lock()
defer j.lock.Unlock()
for k, v := range j.games {
if now.Sub(v.startTime) > 1*time.Hour {
logger.Global().Infof("Removing stale game ID %v", k)
delete(j.games, k)
}
}
j.games[id] = &gameInfo{
id: id,
startTime: now,
score: score,
playerIn: make(chan playerInput),
playerOut: []chan rps.JudgeAction{
make(chan rps.JudgeAction),
make(chan rps.JudgeAction),
},
scoreChan: make(chan scoreData),
}
return id, nil
}
// play interacts with a player for the duration of a game.
func (j *Judge) play(ctx *context.T, call rps.JudgePlayServerCall, name string, id rps.GameId) (rps.PlayResult, error) {
ctx.VI(1).Infof("play from %q for %v", name, id)
nilResult := rps.PlayResult{}
pIn, pOut, s, err := j.gameChannels(id)
if err != nil {
return nilResult, err
}
playerNum, err := j.addPlayer(name, id, call)
if err != nil {
return nilResult, err
}
ctx.VI(1).Infof("This is player %d", playerNum)
// Send all user input to the player input channel.
stopRead := make(chan struct{})
defer close(stopRead)
go func() {
rStream := call.RecvStream()
for rStream.Advance() {
action := rStream.Value()
select {
case pIn <- playerInput{player: playerNum, action: action}:
case <-stopRead:
return
}
}
select {
case pIn <- playerInput{player: playerNum, action: rps.PlayerActionQuit{}}:
case <-stopRead:
}
}()
// Send all the output to the user.
writeDone := make(chan struct{})
go func() {
defer close(writeDone)
for packet := range pOut[playerNum-1] {
if err := call.SendStream().Send(packet); err != nil {
ctx.Infof("error sending to player stream: %v", err)
}
}
}()
defer func() {
close(pOut[playerNum-1])
<-writeDone
}()
pOut[playerNum-1] <- rps.JudgeActionPlayerNum{int32(playerNum)}
// When the second player connects, we start the game.
if playerNum == 2 {
go j.manageGame(ctx, id)
}
// Wait for the ScoreCard.
scoreData := <-s
if scoreData.err != nil {
return rps.PlayResult{}, scoreData.err
}
return rps.PlayResult{YouWon: scoreData.score.Winner == rps.WinnerTag(playerNum)}, nil
}
func (j *Judge) manageGame(ctx *context.T, id rps.GameId) {
j.gamesRun.Incr(1)
j.lock.Lock()
info, exists := j.games[id]
if !exists {
e := scoreData{err: errBadGameID}
info.scoreChan <- e
info.scoreChan <- e
return
}
delete(j.games, id)
j.lock.Unlock()
// Inform each player of their opponent's name.
for p := 0; p < 2; p++ {
opp := 1 - p
info.playerOut[p] <- rps.JudgeActionOpponentName{info.score.Players[opp]}
}
win1, win2 := 0, 0
goal := int(info.score.Opts.NumRounds)
// Play until one player has won 'goal' times.
for win1 < goal && win2 < goal {
round, err := j.playOneRound(info)
if err != nil {
err := scoreData{err: err}
info.scoreChan <- err
info.scoreChan <- err
return
}
if round.Winner == rps.Player1 {
win1++
} else if round.Winner == rps.Player2 {
win2++
}
info.score.Rounds = append(info.score.Rounds, round)
}
if win1 > win2 {
info.score.Winner = rps.Player1
} else {
info.score.Winner = rps.Player2
}
info.score.StartTime = info.startTime
info.score.EndTime = time.Now()
// Send the score card to the players.
action := rps.JudgeActionScore{info.score}
for _, p := range info.playerOut {
p <- action
}
// Send the score card to the score keepers.
scoreCtx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
keepers, err := internal.FindScoreKeepers(scoreCtx, mountPrefix)
if err != nil || len(keepers) == 0 {
ctx.Infof("No score keepers: %v", err)
return
}
var wg sync.WaitGroup
wg.Add(len(keepers))
for _, k := range keepers {
go j.sendScore(scoreCtx, k, info.score, &wg)
}
wg.Wait()
info.scoreChan <- scoreData{score: info.score}
info.scoreChan <- scoreData{score: info.score}
}
func (j *Judge) playOneRound(info *gameInfo) (rps.Round, error) {
round := rps.Round{StartTime: time.Now()}
var action rps.JudgeAction
action = rps.JudgeActionMoveOptions{info.moveOptions()}
for _, p := range info.playerOut {
p <- action
}
for x := 0; x < 2; x++ {
in := <-info.playerIn
switch v := in.action.(type) {
case rps.PlayerActionQuit:
return round, fmt.Errorf("player %d quit the game", in.player)
case rps.PlayerActionMove:
move := v.Value
if !info.validMove(move) {
return round, fmt.Errorf("player %d made an invalid move: %s", in.player, move)
}
if len(round.Moves[in.player-1]) > 0 {
return round, fmt.Errorf("player %d played twice in the same round!", in.player)
}
round.Moves[in.player-1] = move
default:
logger.Global().Infof("unexpected message type: %T", in.action)
}
}
round.Winner, round.Comment = j.compareMoves(round.Moves[0], round.Moves[1])
logger.Global().VI(1).Infof("Player 1 played %q. Player 2 played %q. Winner: %d %s", round.Moves[0], round.Moves[1], round.Winner, round.Comment)
action = rps.JudgeActionRoundResult{round}
for _, p := range info.playerOut {
p <- action
}
round.EndTime = time.Now()
return round, nil
}
func (j *Judge) addPlayer(name string, id rps.GameId, stream rps.JudgePlayServerStream) (int, error) {
j.lock.Lock()
defer j.lock.Unlock()
info, exists := j.games[id]
if !exists {
return 0, errBadGameID
}
if len(info.streams) == 2 {
return 0, errTooManyPlayers
}
info.score.Players = append(info.score.Players, name)
info.streams = append(info.streams, stream.SendStream())
return len(info.streams), nil
}
func (j *Judge) gameChannels(id rps.GameId) (chan playerInput, []chan rps.JudgeAction, chan scoreData, error) {
j.lock.Lock()
defer j.lock.Unlock()
info, exists := j.games[id]
if !exists {
return nil, nil, nil, errBadGameID
}
return info.playerIn, info.playerOut, info.scoreChan, nil
}
func (j *Judge) sendScore(ctx *context.T, address string, score rps.ScoreCard, wg *sync.WaitGroup) error {
defer wg.Done()
if err := rps.ScoreKeeperClient(naming.Join(mountPrefix, address)).Record(ctx, score); err != nil {
logger.Global().Infof("Record: %v", err)
return err
}
return nil
}
var moveComments = map[string]string{
"lizard-paper": "lizard eats paper",
"lizard-rock": "rock crushes lizard",
"lizard-scissors": "scissors decapitates lizard",
"lizard-spock": "lizard poisons spock",
"paper-rock": "paper covers rock",
"paper-scissors": "scissors cuts paper",
"paper-spock": "paper disproves spock",
"rock-scissors": "rock crushes scissors",
"rock-spock": "spock vaporizes rock",
"scissors-spock": "spock smashes scissors",
}
func (j *Judge) compareMoves(m1, m2 string) (winner rps.WinnerTag, comment string) {
if m1 < m2 {
comment = moveComments[m1+"-"+m2]
} else {
comment = moveComments[m2+"-"+m1]
}
if m1 == m2 {
winner = rps.Draw
return
}
if m1 == "rock" && (m2 == "scissors" || m2 == "lizard") {
winner = rps.Player1
return
}
if m1 == "paper" && (m2 == "rock" || m2 == "spock") {
winner = rps.Player1
return
}
if m1 == "scissors" && (m2 == "paper" || m2 == "lizard") {
winner = rps.Player1
return
}
if m1 == "lizard" && (m2 == "paper" || m2 == "spock") {
winner = rps.Player1
return
}
if m1 == "spock" && (m2 == "scissors" || m2 == "rock") {
winner = rps.Player1
return
}
winner = rps.Player2
return
}