/
tictactoe.go
413 lines (369 loc) · 10.8 KB
/
tictactoe.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
package main
import (
"encoding/json"
"log"
"github.com/coopernurse/gorp"
"github.com/gorilla/websocket"
)
// tictactoe domain objects
type TicTacToe_Turn struct {
Id int
Player int // foreign key to player
Game string // foreign key to game (do we need this?)
Move int // the last move the player entered
}
type TicTacToe_Board struct {
Id int
Game string // foreign key to game
Board string // the board represented as a string
}
func (g TicTacToe_Board) getBoard() ([]int, error) {
d := []int{}
err := json.Unmarshal([]byte(g.Board), &d)
if err != nil {
return nil, err
}
return d, nil
}
func (g *TicTacToe_Board) setBoard(v []int) error {
b, err := json.Marshal(v)
if err != nil {
return err
}
g.Board = string(b)
return nil
}
type Action func(msg Message, gameId string, playerId int, gs GameService, ws *websocket.Conn, db *gorp.DbMap, log *log.Logger) error
// To define a game, all you need is to insert key-value pairs of message types to actions (handlers), and
// provide PlayerInit and HostInit functions.
var HostFromWeb map[string]Action
var HostFromPlayer map[string]Action
var PlayerFromWeb map[string]Action
var PlayerFromHost map[string]Action
func init() {
PlayerFromWeb = map[string]Action{
"move": playerMove,
}
PlayerFromHost = map[string]Action{
"update": playerForward,
}
HostFromWeb = map[string]Action{
"state": hostState,
}
HostFromPlayer = map[string]Action{
"join": hostJoinLeave,
"leave": hostJoinLeave,
"move": hostMove,
}
}
func PlayerInit(playerId int, gameId string, gs GameService, ws *websocket.Conn, db *gorp.DbMap) error {
log.Printf("Player is connected: %#v", playerId)
game, _, err := gs.GetGame(db, gameId, playerId)
if err != nil {
log.Printf("Couldn't get player and/or game")
return err
}
if game.State == "start" {
log.Printf("Player %#v rejoining game in play", playerId)
board, err := getBoard(gameId, db)
if err != nil {
log.Printf("Can't get TTT board: %#v", err)
return err
}
log.Printf("Got board, getting nicer one: %#v", board)
niceBoard, err := board.getBoard()
if err != nil {
log.Printf("Unable to get nice board: %#v", err)
return err
}
log.Printf("Got board for player %#v: %#v", playerId, board)
// There may not be a board yet so just try and send it
ws.WriteJSON(Message{
"type": "update",
"state": game.State,
"board": niceBoard,
})
} else {
ws.WriteJSON(Message{
"type": "update",
"state": game.State,
"board": nil,
})
}
// check to make sure this player has a turn row
turn := TicTacToe_Turn{}
err = db.SelectOne(&turn, "select * from tictactoe_turn where game=? and player=?", gameId, playerId)
if err != nil {
turn.Game = gameId
turn.Player = playerId
turn.Move = -1
err = db.Insert(&turn)
if err != nil {
log.Printf("Unable to insert initial turn row: %#v", err)
return err
}
}
gs.SendHost(gameId, Message{"type": "join"})
return nil
}
func PlayerLeave(playerId int, gameId string, gs GameService, ws *websocket.Conn, db *gorp.DbMap) {
gs.SendHost(gameId, Message{"type": "leave"})
}
// Called first when a host connects.
// NOTE that this may be called multiple times as a host may drop and reconnect.
func HostInit(playerId int, gameId string, gs GameService, ws *websocket.Conn, wsReadChan chan Message, db *gorp.DbMap) error {
log.Printf("Host initing")
// since host is always the first to connect, setup tables if they don't already exist
db.AddTableWithName(TicTacToe_Board{}, "tictactoe_board").SetKeys(true, "Id")
db.AddTableWithName(TicTacToe_Turn{}, "tictactoe_turn").SetKeys(true, "Id")
err := db.CreateTablesIfNotExists()
if err != nil {
log.Printf("Unable to create TTT tables: %#v", err)
return err
}
log.Printf("Tables created")
// get the game so we know what state we should be in
game, _, err := gs.GetGame(db, gameId, playerId)
if err != nil {
log.Printf("Host failed to get game: %#v", err)
return err
}
log.Printf("got game")
if game.State == "lobby" {
log.Printf("Game is still in lobby")
// update the lobby based on players that are currently connected
pids := gs.GetConnectedPlayers(gameId)
// angular wants an array of objects, it can't handle an array of ints
players := []Message{}
for _, pid := range pids {
players = append(players, Message{"id": pid})
}
ws.WriteJSON(Message{
"type": "players",
"players": players,
})
ws.WriteJSON(Message{
"type": "state",
"state": game.State,
})
} else {
log.Printf("Host rejoining game in progress")
// get the game board so we can send an update
board, err := getBoard(gameId, db)
if err != nil {
log.Printf("Could not get board, this might not be an error: %#v", err)
return err
}
log.Printf("Getting nicer board: %#v", board)
niceBoard, err := board.getBoard()
if err != nil {
log.Printf("Can't init with board: %#v", err)
return err
}
ws.WriteJSON(Message{
"type": "update",
"board": niceBoard,
"state": "start",
})
}
return nil
}
func playerMove(msg Message, gameId string, playerId int, gs GameService, ws *websocket.Conn, db *gorp.DbMap, log *log.Logger) error {
log.Printf("Sending move to host")
turn := TicTacToe_Turn{}
err := db.SelectOne(&turn, "select * from tictactoe_turn where game=? and player=?", gameId, playerId)
if err != nil {
log.Printf("Unable to get turn in move: %#v", err)
return err
}
// the player move comes as an integer from [0-8] representing the location of the move
// TODO: assert this is the case before saving
turn.Move = int(msg["move"].(float64))
_, err = db.Update(&turn)
if err != nil {
log.Printf("Failed to update moving player: %#v", err)
return err
}
// send notice to the host that we've moved so it can attempt to resolve the current round
gs.SendHost(gameId, msg)
return nil
}
func playerForward(msg Message, gameId string, playerId int, gs GameService, ws *websocket.Conn, db *gorp.DbMap, log *log.Logger) error {
log.Printf("Sending %v to player %v", msg["type"], playerId)
ws.WriteJSON(msg)
return nil
}
func hostState(msg Message, gameId string, playerId int, gs GameService, ws *websocket.Conn, db *gorp.DbMap, log *log.Logger) error {
log.Printf("Got state change request from host: %v", msg["state"])
game, _, err := gs.GetGame(db, gameId, playerId)
if err != nil {
log.Printf("%#v", err)
return err
}
// TODO: check to make sure this is a valid state
game.State = msg["state"].(string)
var board *TicTacToe_Board
if game.State == "start" {
board = &TicTacToe_Board{Game: gameId}
log.Printf("Setting up starting objects")
// we are starting a game, so insert a new board
err = board.setBoard([]int{0, 0, 0, 0, 0, 0, 0, 0, 0})
if err != nil {
log.Printf("Unable to set game board: %#v", err)
return err
}
log.Printf("Inserting board: %#v", board)
err = db.Insert(board)
if err != nil {
log.Printf("Couldn't insert board: %#v", err)
return err
}
} else {
board, err = getBoard(gameId, db)
if err != nil {
log.Printf("Unable to get board: %#v", err)
return err
}
}
log.Printf("Updating game state to %#v", game.State)
count, err := db.Update(game)
if err != nil || count == 0 {
log.Printf("Unable to change game state: %v", err)
return err
}
niceBoard, err := board.getBoard()
if err != nil {
log.Printf("Error getting board: %#v", board)
return err
}
log.Printf("Sending state %v to all players", msg["state"])
gs.Broadcast(gameId, Message{
"type": "update",
"board": niceBoard,
"state": "start",
})
log.Printf("Updating UI")
ws.WriteJSON(Message{
"type": "update",
"board": niceBoard,
"state": "start",
})
return nil
}
func hostJoinLeave(msg Message, gameId string, playerId int, gs GameService, ws *websocket.Conn, db *gorp.DbMap, log *log.Logger) error {
log.Printf("player %v", msg["type"])
// send a fresh list of players to the UI
pids := gs.GetConnectedPlayers(gameId)
// angular wants an array of objects, it can't handle an array of ints
players := []Message{}
for _, pid := range pids {
players = append(players, Message{"id": pid})
}
ws.WriteJSON(Message{
"type": "players",
"players": players,
})
return nil
}
func hostMove(msg Message, gameId string, playerId int, gs GameService, ws *websocket.Conn, db *gorp.DbMap, log *log.Logger) error {
log.Printf("Checking player move")
var players []*Player
_, err := db.Select(&players, "select * from players where game=?", gameId)
if err != nil {
log.Printf("Failed to select players during move for game %v", gameId)
return err
}
resolveRound := true
for _, p := range players {
if p.Role == Host {
continue
}
turn := TicTacToe_Turn{}
err = db.SelectOne(&turn, "select * from tictactoe_turn where game=? and player=?", gameId, p.Id)
if err != nil {
log.Printf("Couldn't get turn %#v", err)
return err
}
if turn.Move == -1 {
resolveRound = false
}
}
if !resolveRound {
log.Printf("Round cannot be resolved")
return nil // not an error, just nothing to do
}
board, err := getBoard(gameId, db)
if err != nil {
log.Printf("Couldn't get board: %#v", err)
return err
}
// all players have set their moves, update the board
thisRound := []int{0, 0, 0, 0, 0, 0, 0, 0, 0}
for _, p := range players {
if p.Role == Host {
continue
}
turn := TicTacToe_Turn{}
err = db.SelectOne(&turn, "select * from tictactoe_turn where game=? and player=?", gameId, p.Id)
if err != nil {
log.Printf("Couldn't get turn %#v", err)
return err
}
if thisRound[turn.Move] == 0 {
thisRound[turn.Move] = p.Id
} else {
thisRound[turn.Move] = 0 // two players went in the same spot
}
}
niceBoard, err := board.getBoard()
if err != nil {
log.Printf("Error getting board: %#v", err)
return err
}
for i, v := range niceBoard {
if v == 0 {
niceBoard[i] = thisRound[i]
}
}
board.setBoard(niceBoard)
count, err := db.Update(board)
if err != nil || count == 0 {
log.Printf("Unable to save board after move: %v", err)
return err
}
for _, p := range players {
if p.Role == Host {
continue
}
turn := TicTacToe_Turn{}
err = db.SelectOne(&turn, "select * from tictactoe_turn where game=? and player=?", gameId, p.Id)
if err != nil {
log.Printf("Couldn't get turn %#v", err)
return err
}
turn.Move = -1
log.Printf("Resetting player %v turn", p.Id)
count, err := db.Update(&turn)
if count == 0 || err != nil {
log.Printf("Failed to update player turn: %#v -- %#v", err, p.Id)
return err
}
}
gs.Broadcast(gameId, Message{
"type": "update",
"board": niceBoard,
"state": "start",
})
ws.WriteJSON(Message{
"type": "update",
"board": niceBoard,
"state": "start",
})
return nil
}
// helpers
func getBoard(gameId string, db *gorp.DbMap) (*TicTacToe_Board, error) {
board := &TicTacToe_Board{}
err := db.SelectOne(board, "select * from tictactoe_board where game=?", gameId)
return board, err
}