forked from Team254/cheesy-arena
-
Notifications
You must be signed in to change notification settings - Fork 0
/
match_result.go
267 lines (239 loc) · 7.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
// 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
MatchType string
RedScore Score
BlueScore Score
RedCards map[string]string
BlueCards map[string]string
}
type MatchResultDb struct {
Id int
MatchId int
PlayNumber int
MatchType string
RedScoreJson string
BlueScoreJson string
RedCardsJson string
BlueCardsJson string
}
type Score struct {
AutoDefensesReached int
AutoDefensesCrossed [5]int
AutoLowGoals int
AutoHighGoals int
DefensesCrossed [5]int
LowGoals int
HighGoals int
Challenges int
Scales int
Fouls []Foul
ElimDq bool
}
type Foul struct {
TeamId int
Rule string
IsTechnical bool
TimeInMatchSec float64
}
type ScoreSummary struct {
AutoPoints int
DefensePoints int
GoalPoints int
ScaleChallengePoints int
TeleopPoints int
FoulPoints int
BonusPoints int
Score int
Breached bool
Captured bool
TowerStrength int
}
// Returns a new match result object with empty slices instead of nil.
func NewMatchResult() *MatchResult {
matchResult := new(MatchResult)
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.BlueScore.Fouls, matchResult.MatchType)
}
// 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.RedScore.Fouls, matchResult.MatchType)
}
// 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
}
}
// No elimination tiebreakers in 2016 Chezy Champs rules.
}
// Calculates and returns the summary fields used for ranking and display.
func scoreSummary(score *Score, opponentFouls []Foul, matchType string) *ScoreSummary {
summary := new(ScoreSummary)
// Leave the score at zero if the team was disqualified.
if score.ElimDq {
return summary
}
// Calculate autonomous score.
autoDefensePoints := 0
for _, defense := range score.AutoDefensesCrossed {
autoDefensePoints += 10 * defense
}
autoGoalPoints := 5*score.AutoLowGoals + 10*score.AutoHighGoals
summary.AutoPoints = 2*score.AutoDefensesReached + autoDefensePoints + autoGoalPoints
// Calculate teleop score.
teleopDefensePoints := 0
for _, defense := range score.DefensesCrossed {
teleopDefensePoints += 5 * defense
}
summary.ScaleChallengePoints = 5*score.Challenges + 15*score.Scales
teleopGoalPoints := 2*score.LowGoals + 5*score.HighGoals
// Calculate tower strength.
numTechFouls := 0
for _, foul := range score.Fouls {
if foul.IsTechnical {
numTechFouls++
}
}
summary.TowerStrength = eventSettings.InitialTowerStrength + numTechFouls - score.AutoLowGoals -
score.AutoHighGoals - score.LowGoals - score.HighGoals
// Calculate bonuses.
summary.BonusPoints = 0
numDefensesDamaged := 0
for i := 0; i < 5; i++ {
if score.AutoDefensesCrossed[i]+score.DefensesCrossed[i] == 2 {
numDefensesDamaged++
}
}
if numDefensesDamaged >= 4 {
summary.Breached = true
if matchType == "elimination" {
summary.BonusPoints += 20
}
}
if score.Challenges+score.Scales == 3 && summary.TowerStrength <= 0 {
summary.Captured = true
if matchType == "elimination" {
summary.BonusPoints += 25
}
}
summary.TeleopPoints = teleopDefensePoints + teleopGoalPoints + summary.ScaleChallengePoints +
summary.BonusPoints
summary.FoulPoints = 5 * len(opponentFouls)
summary.DefensePoints = autoDefensePoints + teleopDefensePoints
summary.GoalPoints = autoGoalPoints + teleopGoalPoints
summary.Score = summary.AutoPoints + summary.TeleopPoints + summary.FoulPoints
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, MatchType: matchResult.MatchType}
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.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, MatchType: matchResultDb.MatchType}
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.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
}