-
Notifications
You must be signed in to change notification settings - Fork 58
/
player.go
357 lines (318 loc) · 7.42 KB
/
player.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
package main
import (
"encoding/json"
"errors"
"fmt"
"github.com/gorilla/websocket"
)
type PlayerMessageType int
const (
PlayerSendTurn PlayerMessageType = 1
PlayerQuitRoom PlayerMessageType = 2
)
type RoomChans struct {
// player reads, room writes
read chan RoomMessage
// room reads, player writes
send chan PlayerMessage
}
type PlayerMessage struct {
msgType PlayerMessageType
roomCode string
tankActions []TankAction
chans RoomChans
}
type Player struct {
notInRoom PlayerStateNotInRoom
waitingForRoom PlayerStateWaitingForRoom
inRoom PlayerStateInRoom
waitForClose PlayerStateWaitForClose
state PlayerState
conn *websocket.Conn
wsClosed bool
roomRequests chan RoomRequest
clientRead chan ClientMessage
room RoomChans
done chan struct{}
}
func NewPlayer(conn *websocket.Conn, roomRequests chan RoomRequest) *Player {
p := &Player{
conn: conn,
roomRequests: roomRequests,
clientRead: make(chan ClientMessage),
}
p.notInRoom = PlayerStateNotInRoom{p}
p.waitingForRoom = PlayerStateWaitingForRoom{p}
p.inRoom = PlayerStateInRoom{p}
p.waitForClose = PlayerStateWaitForClose{p}
p.setState(p.notInRoom)
return p
}
func (p *Player) Run() {
go p.readPump()
defer fmt.Println("player goroutine exited")
for {
select {
case cm, ok := <-p.clientRead:
if p.state.handleClientMessage(cm, ok) {
return
}
case rm, ok := <-p.room.read:
if p.state.handleRoomMessage(rm, ok) {
return
}
}
}
}
func (p *Player) setState(state PlayerState) {
p.state = state
}
func (p *Player) Write(msg ServerMessage) error {
if p.wsClosed {
return errors.New("conn closed")
}
err := p.conn.WriteJSON(msg)
if err != nil {
p.wsClosed = true
p.conn.Close()
return err
}
return nil
}
func (p *Player) readPump() {
defer func() {
p.conn.Close()
close(p.clientRead)
}()
for {
msgType, msg, err := p.conn.ReadMessage()
if err != nil {
return
}
if msgType != websocket.TextMessage {
return
}
clientMsg := ClientMessage{}
err = json.Unmarshal(msg, &clientMsg)
if err != nil {
return
}
p.clientRead <- clientMsg
}
}
type PlayerState interface {
handleClientMessage(cm ClientMessage, ok bool) bool
handleRoomMessage(rm RoomMessage, ok bool) bool
}
type PlayerStateNotInRoom struct {
player *Player
}
func (ps PlayerStateNotInRoom) handleClientMessage(cm ClientMessage, ok bool) bool {
if !ok {
ps.player.clientRead = nil
return true
}
switch cm.Type {
case ClientJoinRoom:
chans := RoomChans{
read: make(chan RoomMessage),
send: make(chan PlayerMessage),
}
ps.player.room = chans
ps.player.done = make(chan struct{})
go func(chans RoomChans) {
ps.player.roomRequests <- RoomRequest{
code: cm.RoomCode,
chans: chans,
}
}(chans)
ps.player.setState(ps.player.waitingForRoom)
case ClientQuitRoom:
fmt.Println("illegal")
case ClientSendTurn:
fmt.Println("illegal")
}
return false
}
func (ps PlayerStateNotInRoom) handleRoomMessage(rm RoomMessage, ok bool) bool {
panic("shouldnt receive from room while in PlayerStateNotInRoom")
}
type PlayerStateWaitingForRoom struct {
player *Player
}
func (ps PlayerStateWaitingForRoom) handleClientMessage(cm ClientMessage, ok bool) bool {
if !ok {
ps.player.clientRead = nil
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
select {
case ch <- PlayerMessage{msgType: PlayerQuitRoom}:
case <-done:
}
}(ch, done)
ps.player.setState(ps.player.waitForClose)
return false
}
switch cm.Type {
case ClientJoinRoom:
fmt.Println("illegal")
case ClientQuitRoom:
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
select {
case ch <- PlayerMessage{msgType: PlayerQuitRoom}:
case <-done:
}
}(ch, done)
case ClientSendTurn:
fmt.Println("illegal")
}
return false
}
func (ps PlayerStateWaitingForRoom) handleRoomMessage(rm RoomMessage, ok bool) bool {
if !ok {
close(ps.player.done)
ps.player.done = nil
ps.player.room.read = nil
ps.player.room.send = nil
ps.player.setState(ps.player.notInRoom)
err := ps.player.Write(newRoomDisconnectedMessage())
if err != nil {
return true
}
return false
}
switch rm.msgType {
case RoomJoined:
ps.player.setState(ps.player.inRoom)
err := ps.player.Write(newRoomJoinedMessage())
if err != nil {
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
select {
case ch <- PlayerMessage{msgType: PlayerQuitRoom}:
case <-done:
}
}(ch, done)
ps.player.setState(ps.player.waitForClose)
return false
}
case RoomGameStarted:
panic("game shouldnt start before joining room")
case RoomTurnResult:
panic("shouldnt rececive turn result before joining room")
case RoomGameFinished:
panic("shouldnt rececive game finished before joining room")
}
return false
}
type PlayerStateInRoom struct {
player *Player
}
func (ps PlayerStateInRoom) handleClientMessage(cm ClientMessage, ok bool) bool {
if !ok {
ps.player.clientRead = nil
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
select {
case ch <- PlayerMessage{msgType: PlayerQuitRoom}:
case <-done:
}
}(ch, done)
ps.player.setState(ps.player.waitForClose)
return false
}
switch cm.Type {
case ClientJoinRoom:
fmt.Println("illegal")
case ClientQuitRoom:
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
select {
case ch <- PlayerMessage{msgType: PlayerQuitRoom}:
case <-done:
}
}(ch, done)
case ClientSendTurn:
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
msg := PlayerMessage{
msgType: PlayerSendTurn,
tankActions: cm.Actions,
}
select {
case ch <- msg:
case <-done:
}
}(ch, done)
}
return false
}
func (ps PlayerStateInRoom) handleRoomMessage(rm RoomMessage, ok bool) bool {
if !ok {
close(ps.player.done)
ps.player.done = nil
ps.player.room.read = nil
ps.player.room.send = nil
err := ps.player.Write(newRoomDisconnectedMessage())
if err != nil {
ps.player.conn.Close()
return true
}
ps.player.setState(ps.player.notInRoom)
return false
}
var err error
switch rm.msgType {
case RoomJoined:
panic("shouldnt receive room joined message while in room")
case RoomGameStarted:
err = ps.player.Write(newStartGameMessage(rm.config))
case RoomTurnResult:
err = ps.player.Write(newTurnResultsMessage(rm.turnResults))
case RoomGameFinished:
err = ps.player.Write(newGameFinishedMessage(rm.gameResult))
}
if err != nil {
ch := ps.player.room.send
done := ps.player.done
go func(ch chan PlayerMessage, done chan struct{}) {
select {
case ch <- PlayerMessage{msgType: PlayerQuitRoom}:
case <-done:
}
}(ch, done)
ps.player.setState(ps.player.waitForClose)
}
return false
}
type PlayerStateWaitForClose struct {
player *Player
}
func (ps PlayerStateWaitForClose) handleClientMessage(cm ClientMessage, ok bool) bool {
if ok {
return false
}
ps.player.clientRead = nil
if ps.player.room.read == nil {
return true
}
return false
}
func (ps PlayerStateWaitForClose) handleRoomMessage(rm RoomMessage, ok bool) bool {
if ok {
return false
}
ps.player.room.read = nil
close(ps.player.done)
if ps.player.clientRead == nil {
return true
}
return false
}