-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathroom.go
265 lines (218 loc) · 5.64 KB
/
room.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
package main
import "fmt"
type RoomMessageType int
const (
RoomJoined RoomMessageType = 1
RoomGameStarted RoomMessageType = 2
RoomGameFinished RoomMessageType = 3
RoomTurnResult RoomMessageType = 4
)
type RoomMessage struct {
msgType RoomMessageType
config ClientConfig
turnResults []TurnResult
gameResult GameResult
}
type QueuedActions struct {
actions []TankAction
queued bool
}
type Room struct {
waitingForP1 RoomStateWaitingForP1
waitingForP2 RoomStateWaitingForP2
running RoomStateRunning
closing RoomStateClosing
state RoomState
code string
requests chan RoomRequest
close chan string
player1chans RoomChans
player2chans RoomChans
gamestate *GameState
queuedP1 QueuedActions
queuedP2 QueuedActions
}
func NewRoom(code string, close chan string) *Room {
requests := make(chan RoomRequest)
room := &Room{
code: code,
requests: requests,
close: close,
}
room.waitingForP1 = RoomStateWaitingForP1{room}
room.waitingForP2 = RoomStateWaitingForP2{room}
room.running = RoomStateRunning{room}
room.state = room.waitingForP1
return room
}
func (r *Room) Run() {
for {
select {
case joinRequest, ok := <-r.requests:
if r.state.handleJoinRequest(joinRequest, ok) {
return
}
case msg := <-r.player1chans.send:
r.state.handlePlayerMessage(msg, true)
case msg := <-r.player2chans.send:
r.state.handlePlayerMessage(msg, false)
}
}
}
func (r *Room) setState(state RoomState) {
r.state = state
}
func (r *Room) QueueActions(actions []TankAction, p1 bool) bool {
if p1 {
r.queuedP1.queued = true
r.queuedP1.actions = actions
} else {
r.queuedP2.queued = true
r.queuedP2.actions = actions
}
if r.queuedP1.queued && r.queuedP2.queued {
return true
}
return false
}
func (r *Room) ClearActions() {
r.queuedP1.queued = false
r.queuedP1.actions = []TankAction{}
r.queuedP2.queued = false
r.queuedP2.actions = []TankAction{}
}
func (r *Room) Actions() ([]TankAction, []TankAction) {
return r.queuedP1.actions, r.queuedP2.actions
}
type RoomState interface {
handleJoinRequest(req RoomRequest, ok bool) bool
handlePlayerMessage(msg PlayerMessage, isP1 bool)
}
type RoomStateWaitingForP1 struct {
r *Room
}
func (s RoomStateWaitingForP1) handleJoinRequest(req RoomRequest, ok bool) bool {
if !ok {
panic("shouldnt require closing in waiting state")
}
s.r.player1chans = req.chans
s.r.player1chans.read <- RoomMessage{msgType: RoomJoined}
s.r.setState(s.r.waitingForP2)
return false
}
func (s RoomStateWaitingForP1) handlePlayerMessage(msg PlayerMessage, isP1 bool) {
panic("shouldnt receive player message while in waiting state")
}
type RoomStateWaitingForP2 struct {
r *Room
}
func (s RoomStateWaitingForP2) handleJoinRequest(req RoomRequest, ok bool) bool {
if !ok {
panic("shouldnt require closing in waiting state")
}
s.r.player2chans = req.chans
s.r.player2chans.read <- RoomMessage{msgType: RoomJoined}
s.r.gamestate = NewGameState(NewBasicConfig(false, true))
cfg1, cfg2 := s.r.gamestate.ClientConfigs()
s.r.player1chans.read <- RoomMessage{msgType: RoomGameStarted, config: cfg1}
s.r.player2chans.read <- RoomMessage{msgType: RoomGameStarted, config: cfg2}
s.r.setState(s.r.running)
return false
}
func (s RoomStateWaitingForP2) handlePlayerMessage(msg PlayerMessage, isP1 bool) {
if !isP1 {
panic("shouldnt receive message from p2 before him joining")
}
switch msg.msgType {
case PlayerQuitRoom:
close(s.r.player1chans.read)
s.r.player1chans.send = nil
ch := s.r.close
go func(ch chan string) {
ch <- s.r.code
}(ch)
s.r.setState(s.r.closing)
case PlayerSendTurn:
fmt.Println("illegal")
}
}
type RoomStateRunning struct {
r *Room
}
func (s RoomStateRunning) handleJoinRequest(req RoomRequest, ok bool) bool {
if !ok {
panic("shouldnt require closing in waiting state")
}
close(req.chans.read)
return false
}
func (s RoomStateRunning) handlePlayerMessage(msg PlayerMessage, isP1 bool) {
switch msg.msgType {
case PlayerQuitRoom:
res1, res2 := Win, Win
if isP1 {
res1 = Lose
} else {
res2 = Lose
}
s.r.player1chans.read <- RoomMessage{msgType: RoomGameFinished, gameResult: res1}
s.r.player2chans.read <- RoomMessage{msgType: RoomGameFinished, gameResult: res2}
close(s.r.player1chans.read)
close(s.r.player2chans.read)
s.r.player1chans.send = nil
s.r.player2chans.send = nil
ch := s.r.close
go func(ch chan string) {
ch <- s.r.code
}(ch)
s.r.setState(s.r.closing)
case PlayerSendTurn:
if s.r.QueueActions(msg.tankActions, isP1) {
p1, p2 := s.r.Actions()
s.r.ClearActions()
fmt.Println("queued", p1, p2)
results1, results2 := s.r.gamestate.ResolveActions(p1, p2)
s.r.player1chans.read <- RoomMessage{
msgType: RoomTurnResult,
turnResults: results1,
}
s.r.player2chans.read <- RoomMessage{
msgType: RoomTurnResult,
turnResults: results2,
}
gameResultP1, gameResultP2, ok := s.r.gamestate.Result()
if !ok {
return
}
s.r.player1chans.read <- RoomMessage{
msgType: RoomGameFinished,
gameResult: gameResultP1,
}
s.r.player2chans.read <- RoomMessage{
msgType: RoomGameFinished,
gameResult: gameResultP2,
}
close(s.r.player1chans.read)
close(s.r.player2chans.read)
s.r.player1chans.send = nil
s.r.player2chans.send = nil
ch := s.r.close
go func(ch chan string) {
ch <- s.r.code
}(ch)
s.r.setState(s.r.closing)
}
}
}
type RoomStateClosing struct {
r *Room
}
func (s RoomStateClosing) handleJoinRequest(req RoomRequest, ok bool) bool {
if !ok {
return true
}
close(req.chans.read)
return false
}
func (s RoomStateClosing) handlePlayerMessage(msg PlayerMessage, isP1 bool) {
}