forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_result.go
311 lines (282 loc) · 8.6 KB
/
match_result.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
// Copyright 2014 Team 254. All Rights Reserved.
// Author: [email protected] (Patrick Fairbank)
//
// Model and datastore CRUD methods for the results (score and fouls) from a match at an event.
package main
import (
"encoding/json"
)
type MatchResult struct {
Id int
MatchId int
PlayNumber int
RedScore Score
BlueScore Score
RedFouls []Foul
BlueFouls []Foul
RedCards map[string]string
BlueCards map[string]string
}
type MatchResultDb struct {
Id int
MatchId int
PlayNumber int
RedScoreJson string
BlueScoreJson string
RedFoulsJson string
BlueFoulsJson string
RedCardsJson string
BlueCardsJson string
}
type Score struct {
AutoMobilityBonuses int
AutoHighHot int
AutoHigh int
AutoLowHot int
AutoLow int
AutoClearHigh int
AutoClearLow int
AutoClearDead int
Cycles []Cycle
ElimTiebreaker int
ElimDq bool
}
type Cycle struct {
Assists int
Truss bool
Catch bool
ScoredHigh bool
ScoredLow bool
DeadBall bool
}
type Foul struct {
TeamId int
Rule string
TimeInMatchSec float64
IsTechnical bool
}
type ScoreSummary struct {
AutoPoints int
AssistPoints int
TrussCatchPoints int
GoalPoints int
TeleopPoints int
FoulPoints int
Score int
}
// Returns a new match result object with empty slices instead of nil.
func NewMatchResult() *MatchResult {
matchResult := new(MatchResult)
matchResult.RedScore.Cycles = []Cycle{}
matchResult.BlueScore.Cycles = []Cycle{}
matchResult.RedFouls = []Foul{}
matchResult.BlueFouls = []Foul{}
matchResult.RedCards = make(map[string]string)
matchResult.BlueCards = make(map[string]string)
return matchResult
}
func (database *Database) CreateMatchResult(matchResult *MatchResult) error {
matchResultDb, err := matchResult.serialize()
if err != nil {
return err
}
err = database.matchResultMap.Insert(matchResultDb)
if err != nil {
return err
}
matchResult.Id = matchResultDb.Id
return nil
}
func (database *Database) GetMatchResultForMatch(matchId int) (*MatchResult, error) {
var matchResults []MatchResultDb
query := "SELECT * FROM match_results WHERE matchid = ? ORDER BY playnumber DESC LIMIT 1"
err := database.matchResultMap.Select(&matchResults, query, matchId)
if err != nil {
return nil, err
}
if len(matchResults) == 0 {
return nil, nil
}
matchResult, err := matchResults[0].deserialize()
if err != nil {
return nil, err
}
return matchResult, err
}
func (database *Database) SaveMatchResult(matchResult *MatchResult) error {
matchResultDb, err := matchResult.serialize()
if err != nil {
return err
}
_, err = database.matchResultMap.Update(matchResultDb)
return err
}
func (database *Database) DeleteMatchResult(matchResult *MatchResult) error {
matchResultDb, err := matchResult.serialize()
if err != nil {
return err
}
_, err = database.matchResultMap.Delete(matchResultDb)
return err
}
func (database *Database) TruncateMatchResults() error {
return database.matchResultMap.TruncateTables()
}
// Calculates and returns the summary fields used for ranking and display for the red alliance.
func (matchResult *MatchResult) RedScoreSummary() *ScoreSummary {
return scoreSummary(&matchResult.RedScore, matchResult.BlueFouls)
}
// Calculates and returns the summary fields used for ranking and display for the blue alliance.
func (matchResult *MatchResult) BlueScoreSummary() *ScoreSummary {
return scoreSummary(&matchResult.BlueScore, matchResult.RedFouls)
}
// Checks the score for disqualifications or a tie and adjusts it appropriately.
func (matchResult *MatchResult) CorrectEliminationScore() {
matchResult.RedScore.ElimDq = false
for _, card := range matchResult.RedCards {
if card == "red" {
matchResult.RedScore.ElimDq = true
}
}
for _, card := range matchResult.BlueCards {
if card == "red" {
matchResult.BlueScore.ElimDq = true
}
}
matchResult.RedScore.ElimTiebreaker = 0
matchResult.BlueScore.ElimTiebreaker = 0
redScore := matchResult.RedScoreSummary()
blueScore := matchResult.BlueScoreSummary()
if redScore.Score != blueScore.Score {
return
}
// Tiebreakers, in order: foul points, assist points, auto points, truss/catch points.
if redScore.FoulPoints > blueScore.FoulPoints {
matchResult.RedScore.ElimTiebreaker = 1
return
} else if redScore.FoulPoints < blueScore.FoulPoints {
matchResult.BlueScore.ElimTiebreaker = 1
return
}
if redScore.AssistPoints > blueScore.AssistPoints {
matchResult.RedScore.ElimTiebreaker = 1
return
} else if redScore.AssistPoints < blueScore.AssistPoints {
matchResult.BlueScore.ElimTiebreaker = 1
return
}
if redScore.AutoPoints > blueScore.AutoPoints {
matchResult.RedScore.ElimTiebreaker = 1
return
} else if redScore.AutoPoints < blueScore.AutoPoints {
matchResult.BlueScore.ElimTiebreaker = 1
return
}
if redScore.TrussCatchPoints > blueScore.TrussCatchPoints {
matchResult.RedScore.ElimTiebreaker = 1
return
} else if redScore.TrussCatchPoints < blueScore.TrussCatchPoints {
matchResult.BlueScore.ElimTiebreaker = 1
return
}
}
// Calculates and returns the summary fields used for ranking and display.
func scoreSummary(score *Score, opponentFouls []Foul) *ScoreSummary {
summary := new(ScoreSummary)
// Leave the score at zero if the team was disqualified.
if score.ElimDq {
return summary
}
// Calculate autonomous score.
summary.AutoPoints = 5*score.AutoMobilityBonuses + 20*score.AutoHighHot + 15*score.AutoHigh +
11*score.AutoLowHot + 6*score.AutoLow
// Calculate teleop score.
summary.GoalPoints = 10*score.AutoClearHigh + 1*score.AutoClearLow
for _, cycle := range score.Cycles {
if cycle.Truss {
summary.TrussCatchPoints += 10
if cycle.Catch {
summary.TrussCatchPoints += 10
}
}
if cycle.ScoredHigh {
summary.GoalPoints += 10
} else if cycle.ScoredLow {
summary.GoalPoints += 1
}
if cycle.ScoredHigh || cycle.ScoredLow {
if cycle.Assists == 2 {
summary.AssistPoints += 10
} else if cycle.Assists == 3 {
summary.AssistPoints += 30
}
}
}
// Calculate foul score.
summary.FoulPoints = 0
for _, foul := range opponentFouls {
if foul.IsTechnical {
summary.FoulPoints += 50
} else {
summary.FoulPoints += 20
}
}
// Fill in summed values.
summary.TeleopPoints = summary.AssistPoints + summary.TrussCatchPoints + summary.GoalPoints
summary.Score = summary.AutoPoints + summary.TeleopPoints + summary.FoulPoints + score.ElimTiebreaker
return summary
}
// Converts the nested struct MatchResult to the DB version that has JSON fields.
func (matchResult *MatchResult) serialize() (*MatchResultDb, error) {
matchResultDb := MatchResultDb{Id: matchResult.Id, MatchId: matchResult.MatchId, PlayNumber: matchResult.PlayNumber}
if err := serializeHelper(&matchResultDb.RedScoreJson, matchResult.RedScore); err != nil {
return nil, err
}
if err := serializeHelper(&matchResultDb.BlueScoreJson, matchResult.BlueScore); err != nil {
return nil, err
}
if err := serializeHelper(&matchResultDb.RedFoulsJson, matchResult.RedFouls); err != nil {
return nil, err
}
if err := serializeHelper(&matchResultDb.BlueFoulsJson, matchResult.BlueFouls); err != nil {
return nil, err
}
if err := serializeHelper(&matchResultDb.RedCardsJson, matchResult.RedCards); err != nil {
return nil, err
}
if err := serializeHelper(&matchResultDb.BlueCardsJson, matchResult.BlueCards); err != nil {
return nil, err
}
return &matchResultDb, nil
}
func serializeHelper(target *string, source interface{}) error {
bytes, err := json.Marshal(source)
if err != nil {
return err
}
*target = string(bytes)
return nil
}
// Converts the DB MatchResult with JSON fields to the nested struct version.
func (matchResultDb *MatchResultDb) deserialize() (*MatchResult, error) {
matchResult := MatchResult{Id: matchResultDb.Id, MatchId: matchResultDb.MatchId, PlayNumber: matchResultDb.PlayNumber}
if err := json.Unmarshal([]byte(matchResultDb.RedScoreJson), &matchResult.RedScore); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(matchResultDb.BlueScoreJson), &matchResult.BlueScore); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(matchResultDb.RedFoulsJson), &matchResult.RedFouls); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(matchResultDb.BlueFoulsJson), &matchResult.BlueFouls); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(matchResultDb.RedCardsJson), &matchResult.RedCards); err != nil {
return nil, err
}
if err := json.Unmarshal([]byte(matchResultDb.BlueCardsJson), &matchResult.BlueCards); err != nil {
return nil, err
}
return &matchResult, nil
}