-
Notifications
You must be signed in to change notification settings - Fork 4
/
room.go
121 lines (106 loc) · 2.19 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
package main
import (
"sync"
"github.com/abdelq/lode-runner/game"
msg "github.com/abdelq/lode-runner/message"
)
var rooms = sync.Map{}
type room struct {
join chan *join
leave chan *leave
broadcast chan *msg.Message
clients map[*client]game.Player
game *game.Game
}
type leave = client
type join struct {
client *client
player game.Player
}
func newRoom(name string) *room {
room := &room{
join: make(chan *join, 7),
leave: make(chan *leave, 7),
broadcast: make(chan *msg.Message, 10), // XXX
clients: make(map[*client]game.Player),
}
room.game = game.NewGame(name, room.broadcast)
go room.listen()
return room
}
func findRoom(client *client) string {
var name string
rooms.Range(func(n, r interface{}) bool {
if _, ok := r.(*room).clients[client]; ok {
name = n.(string)
return false
}
return true
})
return name
}
func (r *room) delete() {
rooms.Range(func(n, r2 interface{}) bool {
if r == r2.(*room) {
rooms.Delete(n)
return false
}
return true
})
}
func (r *room) listen() {
for {
select {
case join := <-r.join:
client, player := join.client, join.player
if _, ok := r.clients[client]; ok {
client.out <- msg.NewMessage("error", "already in room")
continue
}
r.clients[client] = nil
if player == nil || r.game.Started() {
continue
}
if err := player.Join(r.game); err != nil {
client.out <- msg.NewMessage("error", err.Error())
continue
}
r.clients[client] = player
case client := <-r.leave:
player := r.clients[client]
if _, ok := r.clients[client]; !ok {
client.out <- msg.NewMessage("error", "not in room")
continue
}
delete(r.clients, client)
if player == nil {
if len(r.clients) == 0 {
r.delete()
return
}
continue
}
player.Leave(r.game)
case msg := <-r.broadcast:
switch msg.Event {
case "next":
for client, player := range r.clients {
if player == nil {
client.out <- msg
}
}
case "quit":
for client := range r.clients {
client.out <- msg
//client.close()
}
r.delete()
return
default:
for client := range r.clients {
client.out <- msg
}
}
}
}
}