-
Notifications
You must be signed in to change notification settings - Fork 0
/
hub.go
165 lines (139 loc) · 3.31 KB
/
hub.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
package main
import (
"math/rand"
"os"
"time"
)
// The amount of time to wait to erase a game after all of its players
// have left.
const KILL_TIME = time.Hour * time.Duration(24)
func newHub(prefix string) *Hub {
return &Hub{
coopGames: make(map[string]*CoopGameEntry),
prefix: prefix,
rng: rand.New(rand.NewSource(time.Now().UnixNano())),
registerCoop: make(chan *RegisterCoopGame),
getCoop: make(chan *GetCoopGame),
leaveCoop: make(chan string),
removeCoop: make(chan string),
getCSnapshots: make(chan chan<- []*CGameSnapshot),
phaseout: make(chan bool),
}
}
func (h *Hub) registerCoopGame(game *CoopGame) string {
nameChan := make(chan string)
r := &RegisterCoopGame{game: game, name: nameChan}
h.registerCoop <- r
return <-nameChan
}
func (h *Hub) getCoopGame(name string) *CoopGame {
gameChan := make(chan *CoopGame)
j := &GetCoopGame{game: gameChan, name: name}
h.getCoop <- j
return <-gameChan
}
func (h *Hub) leaveCoopGame(name string) {
h.leaveCoop <- name
}
func (h *Hub) getCoopSnapshots() []*CGameSnapshot {
s := make(chan []*CGameSnapshot)
h.getCSnapshots <- s
return <-s
}
func (h *Hub) phaseOutHub() {
h.phaseout <- true
}
// Generates a string with random lowercase alphanumeric chars
func generateGamename(rng *rand.Rand) string {
var gamename_b [5]byte
for i := 0; i < len(gamename_b); i++ {
r := rng.Intn(36)
if r < 10 {
gamename_b[i] = byte('0' + r)
} else {
gamename_b[i] = byte('a' + r - 10)
}
}
return string(gamename_b[:])
}
func (h *Hub) run() {
for {
select {
case rCoop := <-h.registerCoop:
var gamename = h.prefix + generateGamename(h.rng)
for {
if _, exists := h.coopGames[gamename]; !exists {
break
}
gamename = h.prefix + generateGamename(h.rng)
}
killTimer := time.AfterFunc(KILL_TIME, func() {
h.removeCoop <- gamename
})
killTimer.Stop()
h.coopGames[gamename] = &CoopGameEntry{
game: rCoop.game,
killTimer: killTimer,
connections: 1,
}
rCoop.name <- gamename
case gCoop := <-h.getCoop:
game, prs := h.coopGames[gCoop.name]
if !prs {
gCoop.game <- nil
continue
}
game.connections += 1
game.killTimer.Stop()
gCoop.game <- h.coopGames[gCoop.name].game
case lCoop := <-h.leaveCoop:
game := h.coopGames[lCoop]
game.connections -= 1
if game.connections == 0 {
game.killTimer.Reset(KILL_TIME)
}
case kCoop := <-h.removeCoop:
delete(h.coopGames, kCoop)
if h.phasedOut && len(h.coopGames) == 0 {
os.Exit(0)
}
case gs := <-h.getCSnapshots:
snapshots := make([]*CGameSnapshot, len(h.coopGames))
n := 0
for _, ge := range h.coopGames {
snapshots[n] = ge.game.getSnapshot()
}
gs <- snapshots
case <-h.phaseout:
h.phasedOut = true
if len(h.coopGames) == 0 {
os.Exit(0)
}
}
}
}
type CoopGameEntry struct {
game *CoopGame
killTimer *time.Timer
connections int
}
type Hub struct {
coopGames map[string]*CoopGameEntry
prefix string
phasedOut bool
rng *rand.Rand
registerCoop chan *RegisterCoopGame
getCoop chan *GetCoopGame
leaveCoop chan string
removeCoop chan string
phaseout chan bool
getCSnapshots chan chan<- []*CGameSnapshot
}
type RegisterCoopGame struct {
game *CoopGame
name chan string
}
type GetCoopGame struct {
name string
game chan *CoopGame
}