-
Notifications
You must be signed in to change notification settings - Fork 0
/
match.go
210 lines (163 loc) · 4.08 KB
/
match.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
package battleword
import (
"context"
"encoding/json"
"fmt"
"sync"
"time"
"github.com/google/uuid"
"github.com/sirupsen/logrus"
)
type Match struct {
uuid string
players []*Player
games []Game
start time.Time
end time.Time
numRounds int
numLetters int
allWords []string
commonWords []string
// this is used to stop writing to allow us to take a snapshot for upload
mu *sync.Mutex
log logrus.FieldLogger
}
// MatchSnapshot is what clients can use to get the current state of the game,
// and what gets sent to the contestants at the end of matches
type MatchSnapshot struct {
UUID string `json:"match_id,omitempty"`
Start time.Time `json:"start,omitempty"`
End time.Time `json:"end,omitempty"`
Players []Player `json:"players,omitempty"`
Games []Game `json:"games,omitempty"`
RoundsPerGame int `json:"rounds_per_game,omitempty"`
LettersPerWord int `json:"letters_per_word,omitempty"`
}
// InitMatch generates all the games for the match and populates player information and other match level metadata
func InitMatch(log logrus.FieldLogger, allWords, commonWords []string, playerURIs []string, numLetters, numRounds, numGames int) (*Match, error) {
id := uuid.NewString()
log = log.WithField("match_id", id)
mu := &sync.Mutex{}
// this could be a large number so preallocate for speed
games := make([]Game, numGames)
for i := 0; i < numGames; i++ {
games[i] = InitGame(commonWords, numLetters, numRounds)
}
var wgGenerate, wgListen sync.WaitGroup
playerCHAN := make(chan *Player)
errCHAN := make(chan error)
var players []*Player
var errors []error
wgListen.Add(1)
go func() {
defer wgListen.Done()
for player := range playerCHAN {
log.
WithFields(logrus.Fields{
"player_definition": player.Definition,
}).
Debug("got player info")
players = append(players, player)
}
}()
wgListen.Add(1)
go func() {
defer wgListen.Done()
for err := range errCHAN {
errors = append(errors, err)
}
}()
for _, playerURI := range playerURIs {
wgGenerate.Add(1)
go func(playerURI string) {
defer wgGenerate.Done()
player, err := InitPlayer(mu, log, playerURI)
if err != nil {
log.
WithFields(logrus.Fields{
"uri": playerURI,
}).
WithError(err).
Error("player failed to respond")
errCHAN <- err
return
}
playerCHAN <- player
}(playerURI)
}
wgGenerate.Wait()
close(playerCHAN)
close(errCHAN)
wgListen.Wait()
if len(errors) > 0 {
return nil, fmt.Errorf("failed to contact %d players: %+v", len(errors), errors)
}
return &Match{
uuid: id,
players: players,
games: games,
numLetters: numLetters,
numRounds: numRounds,
allWords: allWords,
commonWords: commonWords,
mu: mu,
log: log,
}, nil
}
// Start kicks off all the games as goroutines and waits for them to complete
func (m *Match) Start(ctx context.Context) {
m.log.Info("match started")
m.start = time.Now()
var wg sync.WaitGroup
for _, player := range m.players {
wg.Add(1)
go func(player *Player) {
defer wg.Done()
player.PlayMatch(ctx, m.games)
}(player)
}
wg.Wait()
m.end = time.Now()
m.log.Info("match finished")
}
func (m *Match) Snapshot() MatchSnapshot {
m.mu.Lock()
defer m.mu.Unlock()
// JSON isolator
body, err := json.Marshal(m.players)
if err != nil {
m.log.WithError(err).Error("wtf")
return MatchSnapshot{}
}
var decoupledPlayers []Player
err = json.Unmarshal(body, &decoupledPlayers)
if err != nil {
m.log.Error("wtf")
return MatchSnapshot{}
}
return MatchSnapshot{
UUID: m.uuid,
Games: m.games,
Players: decoupledPlayers,
RoundsPerGame: m.numRounds,
LettersPerWord: m.numLetters,
Start: m.start,
End: m.end,
}
}
// Broadcast takes the results of the match and sends it to all players
func (m *Match) Broadcast() {
var wg sync.WaitGroup
snapshot := m.Snapshot()
for _, player := range m.players {
wg.Add(1)
go func(player *Player) {
defer wg.Done()
err := player.BroadcastMatch(snapshot)
if err != nil {
m.log.WithError(err).Error("failed to broadcast")
}
}(player)
}
wg.Wait()
}