Skip to content

Commit 84c973e

Browse files
committed
Update TBA integration for 2024.
1 parent 497585e commit 84c973e

File tree

4 files changed

+132
-96
lines changed

4 files changed

+132
-96
lines changed

game/score.go

+11-8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Score struct {
1919
const (
2020
bankedAmpNoteLimit = 2
2121
ensembleBonusPointThreshold = 10
22+
ensembleBonusRobotThreshold = 2
2223
)
2324

2425
// Game-specific settings that can be changed by the user.
@@ -74,15 +75,15 @@ func (score *Score) Summarize(opponentScore *Score) *ScoreSummary {
7475
for _, status := range score.EndgameStatuses {
7576
switch status {
7677
case EndgameParked:
77-
summary.StagePoints += 1
78+
summary.ParkPoints += 1
7879
case EndgameStageLeft:
79-
summary.StagePoints += 3
80+
summary.OnStagePoints += 3
8081
robotsByPosition[StageLeft]++
8182
case EndgameCenterStage:
82-
summary.StagePoints += 3
83+
summary.OnStagePoints += 3
8384
robotsByPosition[CenterStage]++
8485
case EndgameStageRight:
85-
summary.StagePoints += 3
86+
summary.OnStagePoints += 3
8687
robotsByPosition[StageRight]++
8788
default:
8889
}
@@ -95,19 +96,21 @@ func (score *Score) Summarize(opponentScore *Score) *ScoreSummary {
9596

9697
// Handle Harmony (multiple robots climbing on the same chain).
9798
if onstageRobots > 1 {
98-
summary.StagePoints += 2 * (onstageRobots - 1)
99+
summary.HarmonyPoints += 2 * (onstageRobots - 1)
99100
}
100101

101102
// Handle microphones.
102103
if score.MicrophoneStatuses[i] && onstageRobots > 0 {
103-
summary.StagePoints += onstageRobots
104+
summary.SpotlightPoints += onstageRobots
104105
}
105106

106107
// Handle traps.
107108
if score.TrapStatuses[i] {
108-
summary.StagePoints += 5
109+
summary.TrapPoints += 5
109110
}
110111
}
112+
summary.StagePoints = summary.ParkPoints + summary.OnStagePoints + summary.HarmonyPoints + summary.SpotlightPoints +
113+
summary.TrapPoints
111114

112115
summary.MatchPoints = summary.LeavePoints + summary.AmpPoints + summary.SpeakerPoints + summary.StagePoints
113116

@@ -144,7 +147,7 @@ func (score *Score) Summarize(opponentScore *Score) *ScoreSummary {
144147
if summary.NumNotes >= summary.NumNotesGoal {
145148
summary.MelodyBonusRankingPoint = true
146149
}
147-
if summary.StagePoints >= ensembleBonusPointThreshold && totalOnstageRobots >= 2 {
150+
if summary.StagePoints >= ensembleBonusPointThreshold && totalOnstageRobots >= ensembleBonusRobotThreshold {
148151
summary.EnsembleBonusRankingPoint = true
149152
}
150153

game/score_summary.go

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ type ScoreSummary struct {
2222
EnsembleBonusRankingPoint bool
2323
BonusRankingPoints int
2424
NumOpponentTechFouls int
25+
26+
// Fields only needed for TBA.
27+
ParkPoints int
28+
OnStagePoints int
29+
HarmonyPoints int
30+
SpotlightPoints int
31+
TrapPoints int
2532
}
2633

2734
type MatchStatus int

partner/tba.go

+104-83
Original file line numberDiff line numberDiff line change
@@ -53,37 +53,48 @@ type TbaAlliance struct {
5353
}
5454

5555
type TbaScoreBreakdown struct {
56-
MobilityRobot1 string `mapstructure:"mobilityRobot1"`
57-
MobilityRobot2 string `mapstructure:"mobilityRobot2"`
58-
MobilityRobot3 string `mapstructure:"mobilityRobot3"`
59-
AutoMobilityPoints int `mapstructure:"autoMobilityPoints"`
60-
AutoChargeStationRobot1 string `mapstructure:"autoChargeStationRobot1"`
61-
AutoChargeStationRobot2 string `mapstructure:"autoChargeStationRobot2"`
62-
AutoChargeStationRobot3 string `mapstructure:"autoChargeStationRobot3"`
63-
AutoBridgeState string `mapstructure:"autoBridgeState"`
64-
AutoCommunity map[string][9]string `mapstructure:"autoCommunity"`
65-
AutoGamePieceCount int `mapstructure:"autoGamePieceCount"`
66-
AutoGamePiecePoints int `mapstructure:"autoGamePiecePoints"`
67-
AutoPoints int `mapstructure:"autoPoints"`
68-
TeleopCommunity map[string][9]string `mapstructure:"teleopCommunity"`
69-
TeleopGamePieceCount int `mapstructure:"teleopGamePieceCount"`
70-
TeleopGamePiecePoints int `mapstructure:"teleopGamePiecePoints"`
71-
Links []TbaLink `mapstructure:"links"`
72-
LinkPoints int `mapstructure:"linkPoints"`
73-
ExtraGamePieceCount int `mapstructure:"extraGamePieceCount"`
74-
EndGameChargeStationRobot1 string `mapstructure:"endGameChargeStationRobot1"`
75-
EndGameChargeStationRobot2 string `mapstructure:"endGameChargeStationRobot2"`
76-
EndGameChargeStationRobot3 string `mapstructure:"endGameChargeStationRobot3"`
77-
EndGameBridgeState string `mapstructure:"endGameBridgeState"`
78-
TeleopPoints int `mapstructure:"teleopPoints"`
79-
CoopertitionCriteriaMet bool `mapstructure:"coopertitionCriteriaMet"`
80-
SustainabilityBonusAchieved bool `mapstructure:"sustainabilityBonusAchieved"`
81-
ActivationBonusAchieved bool `mapstructure:"activationBonusAchieved"`
82-
FoulCount int `mapstructure:"foulCount"`
83-
TechFoulCount int `mapstructure:"techFoulCount"`
84-
FoulPoints int `mapstructure:"foulPoints"`
85-
TotalPoints int `mapstructure:"totalPoints"`
86-
RP int `mapstructure:"rp"`
56+
AutoLineRobot1 string `mapstructure:"autoLineRobot1"`
57+
AutoLineRobot2 string `mapstructure:"autoLineRobot2"`
58+
AutoLineRobot3 string `mapstructure:"autoLineRobot3"`
59+
AutoLeavePoints int `mapstructure:"autoLeavePoints"`
60+
AutoAmpNoteCount int `mapstructure:"autoAmpNoteCount"`
61+
AutoAmpNotePoints int `mapstructure:"autoAmpNotePoints"`
62+
AutoSpeakerNoteCount int `mapstructure:"autoSpeakerNoteCount"`
63+
AutoSpeakerNotePoints int `mapstructure:"autoSpeakerNotePoints"`
64+
AutoTotalNotePoints int `mapstructure:"autoTotalNotePoints"`
65+
AutoPoints int `mapstructure:"autoPoints"`
66+
TeleopAmpNoteCount int `mapstructure:"teleopAmpNoteCount"`
67+
TeleopAmpNotePoints int `mapstructure:"teleopAmpNotePoints"`
68+
TeleopSpeakerNoteCount int `mapstructure:"teleopSpeakerNoteCount"`
69+
TeleopSpeakerNotePoints int `mapstructure:"teleopSpeakerNotePoints"`
70+
TeleopSpeakerNoteAmplifiedCount int `mapstructure:"teleopSpeakerNoteAmplifiedCount"`
71+
TeleopSpeakerNoteAmplifiedPoints int `mapstructure:"teleopSpeakerNoteAmplifiedPoints"`
72+
TeleopTotalNotePoints int `mapstructure:"teleopTotalNotePoints"`
73+
EndGameRobot1 string `mapstructure:"endGameRobot1"`
74+
EndGameRobot2 string `mapstructure:"endGameRobot2"`
75+
EndGameRobot3 string `mapstructure:"endGameRobot3"`
76+
EndGameParkPoints int `mapstructure:"endGameParkPoints"`
77+
EndGameOnStagePoints int `mapstructure:"endGameOnStagePoints"`
78+
EndGameHarmonyPoints int `mapstructure:"endGameHarmonyPoints"`
79+
MicStageLeft bool `mapstructure:"micStageLeft"`
80+
MicCenterStage bool `mapstructure:"micCenterStage"`
81+
MicStageRight bool `mapstructure:"micStageRight"`
82+
EndGameSpotLightBonusPoints int `mapstructure:"endGameSpotLightBonusPoints"`
83+
TrapStageLeft bool `mapstructure:"trapStageLeft"`
84+
TrapCenterStage bool `mapstructure:"trapCenterStage"`
85+
TrapStageRight bool `mapstructure:"trapStageRight"`
86+
EndGameNoteInTrapPoints int `mapstructure:"endGameNoteInTrapPoints"`
87+
EndGameTotalStagePoints int `mapstructure:"endGameTotalStagePoints"`
88+
TeleopPoints int `mapstructure:"teleopPoints"`
89+
CoopertitionCriteriaMet bool `mapstructure:"coopertitionCriteriaMet"`
90+
MelodyBonusAchieved bool `mapstructure:"melodyBonusAchieved"`
91+
EnsembleBonusAchieved bool `mapstructure:"ensembleBonusAchieved"`
92+
FoulCount int `mapstructure:"foulCount"`
93+
TechFoulCount int `mapstructure:"techFoulCount"`
94+
G424Penalty bool `mapstructure:"g424Penalty"`
95+
FoulPoints int `mapstructure:"foulPoints"`
96+
TotalPoints int `mapstructure:"totalPoints"`
97+
RP int `mapstructure:"rp"`
8798
}
8899

89100
type TbaLink struct {
@@ -148,17 +159,14 @@ type TbaPublishedAward struct {
148159
Awardee string `json:"awardee"`
149160
}
150161

151-
var mobilityMapping = map[bool]string{false: "No", true: "Yes"}
152-
var autoChargeStationMapping = map[bool]string{false: "None", true: "Docked"}
153-
var endGameChargeStationMapping = map[game.EndgameStatus]string{
162+
var leaveMapping = map[bool]string{false: "No", true: "Yes"}
163+
var endGameStatusMapping = map[game.EndgameStatus]string{
154164
game.EndgameNone: "None",
155165
game.EndgameParked: "Parked",
156166
game.EndgameStageLeft: "StageLeft",
157167
game.EndgameCenterStage: "CenterStage",
158168
game.EndgameStageRight: "StageRight",
159169
}
160-
var chargeStationLevelMapping = map[bool]string{false: "NotLevel", true: "Level"}
161-
var gridRowMapping = map[int]string{0: "Bottom", 1: "Mid", 2: "Top"}
162170

163171
func NewTbaClient(eventCode, secretId, secret string) *TbaClient {
164172
return &TbaClient{BaseUrl: tbaBaseUrl, eventCode: eventCode, secretId: secretId, secret: secret,
@@ -459,6 +467,26 @@ func (client *TbaClient) PublishAlliances(database *model.Database) error {
459467
body, _ := io.ReadAll(resp.Body)
460468
return fmt.Errorf("Got status code %d from TBA: %s", resp.StatusCode, body)
461469
}
470+
471+
// Also set the playoff type so that TBA renders the correct bracket.
472+
eventSettings, err := database.GetEventSettings()
473+
if err != nil {
474+
return err
475+
}
476+
playoffType := 0
477+
if eventSettings.PlayoffType == model.DoubleEliminationPlayoff {
478+
playoffType = 10
479+
}
480+
resp, err = client.postRequest("info", "update", []byte(fmt.Sprintf("{\"playoff_type\":%d}", playoffType)))
481+
if err != nil {
482+
return err
483+
}
484+
if resp.StatusCode != 200 {
485+
defer resp.Body.Close()
486+
body, _ := io.ReadAll(resp.Body)
487+
return fmt.Errorf("Got status code %d from TBA: %s", resp.StatusCode, body)
488+
}
489+
462490
return nil
463491
}
464492

@@ -604,59 +632,52 @@ func createTbaScoringBreakdown(
604632
opponentScoreSummary = matchResult.RedScoreSummary()
605633
}
606634

607-
breakdown.MobilityRobot1 = mobilityMapping[score.LeaveStatuses[0]]
608-
breakdown.MobilityRobot2 = mobilityMapping[score.LeaveStatuses[1]]
609-
breakdown.MobilityRobot3 = mobilityMapping[score.LeaveStatuses[2]]
610-
breakdown.AutoMobilityPoints = scoreSummary.LeavePoints
611-
// TODO(pat): Update for 2024.
612-
//breakdown.AutoChargeStationRobot1 = autoChargeStationMapping[score.AutoDockStatuses[0]]
613-
//breakdown.AutoChargeStationRobot2 = autoChargeStationMapping[score.AutoDockStatuses[1]]
614-
//breakdown.AutoChargeStationRobot3 = autoChargeStationMapping[score.AutoDockStatuses[2]]
615-
//breakdown.AutoBridgeState = chargeStationLevelMapping[score.AutoChargeStationLevel]
616-
//breakdown.AutoCommunity = make(map[string][9]string)
617-
//breakdown.TeleopCommunity = make(map[string][9]string)
618-
//for rowIndex, rowName := range gridRowMapping {
619-
// shortRowName := string([]rune(rowName)[0])
620-
// breakdown.AutoCommunity[shortRowName] = createTbaGridRow(&score.Grid, rowIndex, true)
621-
// breakdown.TeleopCommunity[shortRowName] = createTbaGridRow(&score.Grid, rowIndex, false)
622-
//}
623-
//for i := 0; i < 3; i++ {
624-
// for j := 0; j < 9; j++ {
625-
// if score.Grid.Nodes[i][j] != game.Empty {
626-
// if score.Grid.AutoScoring[i][j] {
627-
// breakdown.AutoGamePieceCount++
628-
// }
629-
// breakdown.TeleopGamePieceCount++
630-
// }
631-
// }
632-
//}
633-
//breakdown.Links = make([]TbaLink, 0)
634-
//for _, link := range score.Grid.Links() {
635-
// tbaLink := TbaLink{
636-
// Nodes: [3]int{link.StartColumn, link.StartColumn + 1, link.StartColumn + 2},
637-
// Row: gridRowMapping[int(link.Row)],
638-
// }
639-
// breakdown.Links = append(breakdown.Links, tbaLink)
640-
//}
641-
//breakdown.LinkPoints = score.Grid.LinkPoints()
642-
//breakdown.AutoGamePiecePoints = score.Grid.AutoGamePiecePoints()
643-
//breakdown.TeleopGamePiecePoints = score.Grid.TeleopGamePiecePoints() + score.Grid.SuperchargedPoints()
644-
//breakdown.AutoPoints = scoreSummary.AutoPoints
645-
//breakdown.ExtraGamePieceCount = score.Grid.NumSuperchargedNodes()
646-
//breakdown.EndGameChargeStationRobot1 = endGameChargeStationMapping[score.EndgameStatuses[0]]
647-
//breakdown.EndGameChargeStationRobot2 = endGameChargeStationMapping[score.EndgameStatuses[1]]
648-
//breakdown.EndGameChargeStationRobot3 = endGameChargeStationMapping[score.EndgameStatuses[2]]
649-
//breakdown.EndGameBridgeState = chargeStationLevelMapping[score.EndgameChargeStationLevel]
650-
//breakdown.TeleopPoints = breakdown.TeleopGamePiecePoints + scoreSummary.StagePoints
651-
//breakdown.CoopertitionCriteriaMet = score.Grid.IsCoopertitionThresholdAchieved()
652-
breakdown.SustainabilityBonusAchieved = scoreSummary.MelodyBonusRankingPoint
653-
breakdown.ActivationBonusAchieved = scoreSummary.EnsembleBonusRankingPoint
635+
breakdown.AutoLineRobot1 = leaveMapping[score.LeaveStatuses[0]]
636+
breakdown.AutoLineRobot2 = leaveMapping[score.LeaveStatuses[1]]
637+
breakdown.AutoLineRobot3 = leaveMapping[score.LeaveStatuses[2]]
638+
breakdown.AutoLeavePoints = scoreSummary.LeavePoints
639+
breakdown.AutoAmpNoteCount = score.AmpSpeaker.AutoAmpNotes
640+
breakdown.AutoAmpNotePoints = 2 * breakdown.AutoAmpNoteCount
641+
breakdown.AutoSpeakerNoteCount = score.AmpSpeaker.AutoSpeakerNotes
642+
breakdown.AutoSpeakerNotePoints = 5 * breakdown.AutoSpeakerNoteCount
643+
breakdown.AutoTotalNotePoints = breakdown.AutoAmpNotePoints + breakdown.AutoSpeakerNotePoints
644+
breakdown.AutoPoints = scoreSummary.AutoPoints
645+
breakdown.TeleopAmpNoteCount = score.AmpSpeaker.TeleopAmpNotes
646+
breakdown.TeleopAmpNotePoints = 1 * breakdown.TeleopAmpNoteCount
647+
breakdown.TeleopSpeakerNoteCount = score.AmpSpeaker.TeleopUnamplifiedSpeakerNotes
648+
breakdown.TeleopSpeakerNotePoints = 2 * breakdown.TeleopSpeakerNoteCount
649+
breakdown.TeleopSpeakerNoteAmplifiedCount = score.AmpSpeaker.TeleopAmplifiedSpeakerNotes
650+
breakdown.TeleopSpeakerNoteAmplifiedPoints = 5 * breakdown.TeleopSpeakerNoteAmplifiedCount
651+
breakdown.TeleopTotalNotePoints = breakdown.TeleopAmpNotePoints + breakdown.TeleopSpeakerNotePoints +
652+
breakdown.TeleopSpeakerNoteAmplifiedPoints
653+
breakdown.EndGameRobot1 = endGameStatusMapping[score.EndgameStatuses[0]]
654+
breakdown.EndGameRobot2 = endGameStatusMapping[score.EndgameStatuses[1]]
655+
breakdown.EndGameRobot3 = endGameStatusMapping[score.EndgameStatuses[2]]
656+
breakdown.EndGameParkPoints = scoreSummary.ParkPoints
657+
breakdown.EndGameOnStagePoints = scoreSummary.OnStagePoints
658+
breakdown.EndGameHarmonyPoints = scoreSummary.HarmonyPoints
659+
breakdown.MicStageLeft = score.MicrophoneStatuses[0]
660+
breakdown.MicCenterStage = score.MicrophoneStatuses[1]
661+
breakdown.MicStageRight = score.MicrophoneStatuses[2]
662+
breakdown.EndGameSpotLightBonusPoints = scoreSummary.SpotlightPoints
663+
breakdown.TrapStageLeft = score.TrapStatuses[0]
664+
breakdown.TrapCenterStage = score.TrapStatuses[1]
665+
breakdown.TrapStageRight = score.TrapStatuses[2]
666+
breakdown.EndGameNoteInTrapPoints = scoreSummary.TrapPoints
667+
breakdown.EndGameTotalStagePoints = scoreSummary.StagePoints
668+
breakdown.TeleopPoints = breakdown.TeleopTotalNotePoints + breakdown.EndGameTotalStagePoints
669+
breakdown.CoopertitionCriteriaMet = scoreSummary.CoopertitionCriteriaMet
670+
breakdown.MelodyBonusAchieved = scoreSummary.MelodyBonusRankingPoint
671+
breakdown.EnsembleBonusAchieved = scoreSummary.EnsembleBonusRankingPoint
654672
for _, foul := range score.Fouls {
655673
if foul.IsTechnical {
656674
breakdown.TechFoulCount++
657675
} else {
658676
breakdown.FoulCount++
659677
}
678+
if foul.Rule() != nil && foul.Rule().IsRankingPoint {
679+
breakdown.G424Penalty = true
680+
}
660681
}
661682
breakdown.FoulPoints = scoreSummary.FoulPoints
662683
breakdown.TotalPoints = scoreSummary.Score

partner/tba_test.go

+10-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"io"
1313
"net/http"
1414
"net/http/httptest"
15+
"strings"
1516
"testing"
1617
"time"
1718
)
@@ -111,11 +112,15 @@ func TestPublishAlliances(t *testing.T) {
111112
tbaServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
112113
var reader bytes.Buffer
113114
reader.ReadFrom(r.Body)
114-
assert.Equal(
115-
t,
116-
"[[\"frc254\",\"frc469\",\"frc2848\",\"frc74\",\"frc3175\"],[\"frc1718\",\"frc2451\",\"frc1619\"]]",
117-
reader.String(),
118-
)
115+
if strings.Contains(r.URL.String(), "alliance_selections") {
116+
assert.Equal(
117+
t,
118+
"[[\"frc254\",\"frc469\",\"frc2848\",\"frc74\",\"frc3175\"],[\"frc1718\",\"frc2451\",\"frc1619\"]]",
119+
reader.String(),
120+
)
121+
} else {
122+
assert.Equal(t, "{\"playoff_type\":10}", reader.String())
123+
}
119124
}))
120125
defer tbaServer.Close()
121126
client := NewTbaClient("my_event_code", "my_secret_id", "my_secret")

0 commit comments

Comments
 (0)