-
Notifications
You must be signed in to change notification settings - Fork 0
/
coop.go
218 lines (183 loc) · 4.55 KB
/
coop.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
package main
//import "fmt"
func newCPlayer(name string, send FnSendText) *CPlayer {
return &CPlayer{
name: name,
gaveup: false,
send: send,
}
}
func newCoopGame(firstPlayer *CPlayer, wordsmap map[string]string) *CoopGame {
return &CoopGame{
wordsmap: wordsmap,
players: []*CPlayer{firstPlayer},
playerjoin: make(chan *CPlayerJoinEvent),
playerquit: make(chan *CPlayerQuitEvent),
attempt: make(chan *CAttemptEvent),
giveup: make(chan *CGiveupEvent),
snapshot: make(chan chan<- *CGameSnapshot),
}
}
// Adds a player to the game, and returns the position that this player will
// be in, or -1 if this name was already taken.
func (g *CoopGame) playerJoin(name string, send FnSendText) int {
result := make(chan int)
g.playerjoin <- &CPlayerJoinEvent{
name: name,
send: send,
pnum: result,
}
return <-result
}
// Called on a player disconnect. Returns true if all players have quit.
func (g *CoopGame) playerQuit(pnum int) {
g.playerquit <- &CPlayerQuitEvent{pnum: pnum}
}
func (g *CoopGame) wordAttempt(pnum int, word string) {
g.attempt <- &CAttemptEvent{pnum: pnum, word: word}
}
func (g *CoopGame) playerGiveup(pnum int, didGiveUp bool) {
g.giveup <- &CGiveupEvent{pnum: pnum, didGiveUp: didGiveUp}
}
func (g *CoopGame) getSnapshot() *CGameSnapshot {
s := make(chan *CGameSnapshot)
g.snapshot <- s
return <-s
}
func (g *CoopGame) run() {
for {
select {
case pJoin := <-g.playerjoin:
pnum := g.attemptJoinM(pJoin.name, pJoin.send)
if pnum >= 0 {
gameSendState(g, pnum)
}
pJoin.pnum <- pnum
case pQuit := <-g.playerquit:
g.players[pQuit.pnum].send = nil
gameSendQuit(g, pQuit.pnum)
g.checkGiveupM()
case wordAttempt := <-g.attempt:
word := wordAttempt.word
if guesser, ok := g.wordsmap[word]; ok && guesser == "" {
pname := g.players[wordAttempt.pnum].name
g.wordsmap[word] = pname
gameSendAttempt(g, wordAttempt.pnum, word)
}
case giveup := <-g.giveup:
g.players[giveup.pnum].gaveup = giveup.didGiveUp
gameSendGiveup(g, giveup.pnum, giveup.didGiveUp)
g.checkGiveupM()
case ss := <-g.snapshot:
players := make([]PlayerEntry, len(g.players))
for n, p := range g.players {
players[n] = PlayerEntry{name: p.name, present: p.send != nil}
}
var theWord string
guessed := 0
for w, g := range g.wordsmap {
if g != "" && g != "_" {
guessed++
}
if len(w) == 6 {
theWord = w
}
}
ss <- &CGameSnapshot{
players: players,
word: theWord,
wordsGuessed: guessed,
}
}
}
}
func (g *CoopGame) announceMsg(msg []byte, exclude int) {
for n, p := range g.players {
if n != exclude && p.send != nil {
p.send(msg)
}
}
}
// Will immediately mutate the coop game and attempt to join this player.
// Should only be called from the game's main loop. Will return the player
// number if successful, or -1 if the name was already taken.
func (g *CoopGame) attemptJoinM(name string, send FnSendText) int {
for n, player := range g.players {
if player.name == name {
if player.send == nil {
player.send = send
return n
} else {
return -1
}
}
}
new_p := newCPlayer(name, send)
g.players = append(g.players, new_p)
return len(g.players) - 1
}
// Checks if all players have given up, and if they have, mutates the game
// state to fill in unguessed words with _, and sends the :allgiveup message to
// all clients. If all players have disconnected, this will not trigger the
// allgiveup event-- at least one player must be present and giving up for this
// to happen.
func (g *CoopGame) checkGiveupM() {
allGiveup := true
allQuit := true
for _, p := range g.players {
if p.send != nil {
allQuit = false
if !p.gaveup {
allGiveup = false
break
}
}
}
if !allQuit && allGiveup {
gameAllGiveup(g)
for k, v := range g.wordsmap {
if v == "" {
g.wordsmap[k] = "_"
}
}
}
}
type CoopGame struct {
wordsmap map[string]string
players []*CPlayer
playerjoin chan *CPlayerJoinEvent
playerquit chan *CPlayerQuitEvent
attempt chan *CAttemptEvent
giveup chan *CGiveupEvent
snapshot chan chan<- *CGameSnapshot
}
type CPlayer struct {
name string
gaveup bool
send FnSendText
}
type CPlayerJoinEvent struct {
name string
send FnSendText
pnum chan int
}
type CPlayerQuitEvent struct {
pnum int
}
type CAttemptEvent struct {
pnum int
word string
}
type CGiveupEvent struct {
pnum int
didGiveUp bool
}
type PlayerEntry struct {
name string
present bool
}
type CGameSnapshot struct {
players []PlayerEntry
word string
wordsGuessed int
}