-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlobby.go
289 lines (252 loc) · 6.89 KB
/
lobby.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
package main
import (
"encoding/json"
"fmt"
"log"
"sync/atomic"
)
var lastClientId uint64
var lastRoomId uint64
// Lobby is the first place for connected clients. It passes commands to games.
type Lobby struct {
// Registered clients.
clients map[*Client]bool
// Inbound messages from the clients.
broadcast chan []byte
// Register requests from the clients.
register chan *Client
// Unregister requests from clients.
unregister chan *Client
// Commands from clients
clientCommands chan *ClientCommand
// Started games
games []*Game
// Rooms created by clients
rooms map[*Client]*Room
gameLogger GameLogger
}
func newLobby(gameLogger GameLogger) *Lobby {
return &Lobby{
broadcast: make(chan []byte),
register: make(chan *Client),
unregister: make(chan *Client),
clients: make(map[*Client]bool),
clientCommands: make(chan *ClientCommand),
games: make([]*Game, 0),
rooms: make(map[*Client]*Room),
gameLogger: gameLogger,
}
}
func (l *Lobby) run() {
log.Println("Go lobby")
go func() {
for {
select {
case message, ok := <-l.broadcast:
if !ok {
continue
}
for client := range l.clients {
client.sendMessage(message)
}
}
}
}()
for {
select {
case client := <-l.register:
atomic.AddUint64(&lastClientId, 1)
lastClientIdSafe := atomic.LoadUint64(&lastClientId)
client.id = lastClientIdSafe
client.isValid = true
l.clients[client] = true
case client := <-l.unregister:
if _, ok := l.clients[client]; ok {
client.isValid = false
delete(l.clients, client)
l.onClientLeft(client)
close(client.send)
}
case clientCommand := <-l.clientCommands:
l.onClientCommand(clientCommand)
}
}
}
func (l *Lobby) broadcastEvent(event interface{}) {
jsonData, _ := eventToJSON(event)
l.broadcast <- jsonData
}
func (l *Lobby) onJoinCommand(c *Client, nickname string) {
c.nickname = nickname
broadcastEvent := &ClientBroadCastJoinedEvent{
Id: c.id,
Nickname: c.nickname,
}
l.broadcastEvent(broadcastEvent)
clientsInList := make([]*ClientInList, 0)
for client := range l.clients {
clientInList := &ClientInList{
Id: client.id,
Nickname: client.Nickname(),
}
clientsInList = append(clientsInList, clientInList)
}
roomsInList := make([]*RoomInList, 0)
for _, room := range l.rooms {
roomInList := room.toRoomInList()
roomsInList = append(roomsInList, roomInList)
}
event := &ClientJoinedEvent{
YourId: c.id,
YourNickname: nickname,
Clients: clientsInList,
Rooms: roomsInList,
}
c.sendEvent(event)
}
func (l *Lobby) onClientLeft(client *Client) {
room := client.room
if room != nil {
l.onLeftRoom(client, room)
}
leftEvent := &ClientLeftEvent{
Id: client.id,
}
l.broadcastEvent(leftEvent)
}
func (l *Lobby) onCreateNewRoomCommand(c *Client) {
_, roomExists := l.rooms[c]
if roomExists {
errEvent := &ClientCommandError{errorYouCanCreateOneRoomOnly}
c.sendEvent(errEvent)
return
}
oldRoomJoined := c.room
if oldRoomJoined != nil {
l.onLeftRoom(c, oldRoomJoined)
}
atomic.AddUint64(&lastRoomId, 1)
lastRoomIdSafe := atomic.LoadUint64(&lastRoomId)
room := newRoom(lastRoomIdSafe, c, l)
l.rooms[c] = room
event := &ClientCreatedRoomEvent{room.toRoomInList()}
l.broadcastEvent(event)
roomJoinedEvent := RoomJoinedEvent{room.toRoomInfo()}
c.sendEvent(roomJoinedEvent)
}
func (l *Lobby) getRoomById(roomId uint64) (room *Room, err error) {
for _, r := range l.rooms {
if r.Id() == roomId {
return r, nil
}
}
return nil, fmt.Errorf("room not found by id = %d", roomId)
}
func (l *Lobby) onLeftRoom(c *Client, room *Room) {
changedOwner, roomBecameEmpty := room.removeClient(c)
c.room = nil
if roomBecameEmpty {
roomInListRemovedEvent := &RoomInListRemovedEvent{room.Id()}
l.broadcastEvent(roomInListRemovedEvent)
l.rooms[c] = nil
delete(l.rooms, c)
return
}
if changedOwner {
roomOwnerClient, ok := room.owner.client.(*Client)
if !ok {
return
}
l.rooms[roomOwnerClient] = room
delete(l.rooms, c)
}
roomInListUpdatedEvent := &RoomInListUpdatedEvent{room.toRoomInList()}
l.broadcastEvent(roomInListUpdatedEvent)
}
func (l *Lobby) onJoinRoomCommand(c *Client, roomId uint64) {
log.Printf("OnJoinRoomCommand: %s, %d", c.Nickname(), roomId)
oldRoomJoined := c.room
if oldRoomJoined != nil && oldRoomJoined.Id() == roomId {
return
}
if oldRoomJoined != nil {
l.onLeftRoom(c, oldRoomJoined)
}
room, err := l.getRoomById(roomId)
if err == nil {
room.addClient(c)
roomInListUpdatedEvent := &RoomInListUpdatedEvent{room.toRoomInList()}
l.broadcastEvent(roomInListUpdatedEvent)
log.Printf("Client %s joined room %d", c.Nickname(), roomId)
} else {
errEvent := &ClientCommandError{errorRoomDoesNotExist}
c.sendEvent(errEvent)
}
}
func (l *Lobby) onClientCommand(cc *ClientCommand) {
if cc.Type == ClientCommandTypeLobby {
if cc.SubType == ClientCommandLobbySubTypeJoin {
var nickname string
if err := json.Unmarshal(cc.Data, &nickname); err != nil {
return
}
l.onJoinCommand(cc.client, nickname)
} else if cc.SubType == ClientCommandLobbySubTypeCreateRoom {
l.onCreateNewRoomCommand(cc.client)
} else if cc.SubType == ClientCommandLobbySubTypeJoinRoom {
var roomId uint64
if err := json.Unmarshal(cc.Data, &roomId); err != nil {
return
}
l.onJoinRoomCommand(cc.client, roomId)
}
} else if cc.Type == ClientCommandTypeGame {
l.dispatchGameEvent(cc)
} else if cc.Type == ClientCommandTypeRoom {
if cc.client.room == nil {
return
}
cc.client.room.onClientCommand(cc)
}
}
func (l *Lobby) dispatchGameEvent(cc *ClientCommand) {
if cc.client.room == nil {
return
}
if cc.client.room.game == nil {
return
}
game := cc.client.room.game
if game.status != GameStatusPlaying {
return
}
player := game.findPlayerOfClient(cc.client)
if player == nil {
return
}
var playerAction *PlayerAction
if cc.SubType == ClientCommandGameSubTypeAttack {
var attackActionData AttackActionData
if err := json.Unmarshal(cc.Data, &attackActionData); err != nil {
return
}
playerAction = &PlayerAction{Name: PlayerActionNameAttack, Data: attackActionData, player: player}
} else if cc.SubType == ClientCommandGameSubTypeDefend {
var defendActionData DefendActionData
if err := json.Unmarshal(cc.Data, &defendActionData); err != nil {
return
}
playerAction = &PlayerAction{Name: PlayerActionNameDefend, Data: defendActionData, player: player}
} else if cc.SubType == ClientCommandGameSubTypePickUp {
playerAction = &PlayerAction{Name: PlayerActionNamePickUp, player: player}
} else if cc.SubType == ClientCommandGameSubTypeComplete {
playerAction = &PlayerAction{Name: PlayerActionNameComplete, player: player}
}
if playerAction != nil {
game.playerActions <- playerAction
}
}
func (l *Lobby) sendRoomUpdate(room *Room) {
roomInListUpdatedEvent := &RoomInListUpdatedEvent{room.toRoomInList()}
l.broadcastEvent(roomInListUpdatedEvent)
}