forked from fxaa/codenames.plus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathactionrouter.go
223 lines (184 loc) · 4.44 KB
/
actionrouter.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
package main
import (
"fmt"
"sync"
"time"
"github.com/sirupsen/logrus"
)
type ResponseEmitter interface {
Emit(msg string, success bool)
}
type resEmitFunc func(msg string, success bool)
func (r resEmitFunc) Emit(msg string, success bool) {
r(msg, success)
}
func ResEmitFunc(fn func(msg string, success bool)) resEmitFunc {
return resEmitFunc(fn)
}
type RoomAction func(r *Room)
type RoomActionReceiver chan<- RoomAction
type ActionRouter struct {
sync.RWMutex
playerRooms map[string]RoomActionReceiver
nameRooms map[string]RoomActionReceiver
}
func NewActionRouter() *ActionRouter {
return &ActionRouter{
playerRooms: map[string]RoomActionReceiver{},
nameRooms: map[string]RoomActionReceiver{},
}
}
func (a *ActionRouter) PlayerRoomReceiver(playerID string) RoomActionReceiver {
a.RLock()
defer a.RUnlock()
if rr, ok := a.playerRooms[playerID]; ok {
return rr
}
return nil
}
func (a *ActionRouter) RoomNameReceiver(roomName string) RoomActionReceiver {
a.RLock()
defer a.RUnlock()
if rr, ok := a.nameRooms[roomName]; ok {
return rr
}
return nil
}
func (a *ActionRouter) CreateRoom(playerID, nick, room, password string, res ResponseEmitter) {
if len(nick) == 0 {
res.Emit("invalid nickname", false)
return
}
if len(password) == 0 {
res.Emit("invalid password", false)
}
// this actions needs to be atomic
a.Lock()
// check if room exists
if _, ok := a.nameRooms[room]; ok {
a.Unlock()
res.Emit(fmt.Sprintf("room %s already exists.", room), false)
return
}
// check if player is an another room
if rr, ok := a.playerRooms[playerID]; ok {
rr <- func(r *Room) {
r.Leave(playerID)
}
}
r := NewRoom(room, password)
rr := startRoomRouter(r)
rr <- func(r *Room) {
r.Join(playerID, nick)
}
a.playerRooms[playerID] = rr
a.nameRooms[room] = rr
a.Unlock()
res.Emit("created the room", true)
}
func (a *ActionRouter) JoinRoom(playerID, nick, roomName, password string, action RoomAction) bool {
rr := a.RoomNameReceiver(roomName)
if rr == nil {
log.WithFields(logrus.Fields{
"PlayerID": playerID,
"RoomName": roomName,
"Nick": nick,
}).Info("player tried to join a nonexistant room")
return false
}
rr <- func(r *Room) {
if r.Password != password {
action(nil)
return
}
r.Join(playerID, nick)
a.Lock()
defer a.Unlock()
a.playerRooms[playerID] = rr
action(r)
}
return true
}
func (a *ActionRouter) RoomForPlayer(playerID string, action RoomAction) bool {
if rr := a.PlayerRoomReceiver(playerID); rr != nil {
rr <- action
return true
}
log.WithField("PlayerID", playerID).Warn("player not in any room, dropping action")
return false
}
func (a *ActionRouter) RoomByName(roomName string, action RoomAction) bool {
if rr := a.RoomNameReceiver(roomName); rr != nil {
rr <- action
return true
}
log.WithField("RoomName", roomName).Warn("room not found, dropping action")
return false
}
func startRoomRouter(r *Room) RoomActionReceiver {
actionChan := make(chan RoomAction)
go func() {
for action := range actionChan {
action(r)
}
}()
return actionChan
}
func (a *ActionRouter) CheckIfPlayerExists(playerID string, res func(players, rooms int, playerID string, isInRoom bool, gs gameState)) {
rr := a.PlayerRoomReceiver(playerID)
if rr == nil {
res(a.Players(), a.Rooms(), playerID, false, gameState{})
return
}
rr <- func(r *Room) {
res(a.Players(), a.Rooms(), playerID, true, r.GameState())
}
}
func (a *ActionRouter) TimedPlayerRoomAction(playerID string, action func(*Room) bool) {
go func() {
ticker := time.NewTicker(1 * time.Second)
for {
<-ticker.C
rr := a.PlayerRoomReceiver(playerID)
if rr == nil {
ticker.Stop()
log.WithField("PlayerID", playerID).Warn("could not setup timed action, player not in any room")
return
}
rr <- func(r *Room) {
if !action(r) {
ticker.Stop()
return
}
}
}
}()
}
func (a *ActionRouter) LeaveRoom(playerID string, action RoomAction) bool {
a.Lock()
defer a.Unlock()
if rr, ok := a.playerRooms[playerID]; ok {
rr <- action
rr <- func(r *Room) {
r.Leave(playerID)
delete(a.playerRooms, playerID)
// todo(voldy): delay this for later?
if len(r.Players) == 0 {
close(a.nameRooms[r.Name])
delete(a.nameRooms, r.Name)
}
}
return true
}
return false
}
func (a *ActionRouter) Players() int {
a.RLock()
defer a.RUnlock()
return len(a.playerRooms)
}
func (a *ActionRouter) Rooms() int {
a.RLock()
defer a.RUnlock()
return len(a.nameRooms)
}