Skip to content

Commit

Permalink
Fix bug with amplified countdown not being propagated.
Browse files Browse the repository at this point in the history
  • Loading branch information
patfair committed Jun 9, 2024
1 parent ac1b477 commit 833e5c8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
9 changes: 8 additions & 1 deletion field/arena.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package field
import (
"fmt"
"log"
"math"
"reflect"
"time"

Expand Down Expand Up @@ -918,8 +919,10 @@ func (arena *Arena) handlePlcInputOutput() {
// Handle in-match PLC functions.
redScore := &arena.RedRealtimeScore.CurrentScore
oldRedScore := *redScore
oldRedAmplifiedTimeRemainingSec := arena.RedRealtimeScore.AmplifiedTimeRemainingSec
blueScore := &arena.BlueRealtimeScore.CurrentScore
oldBlueScore := *blueScore
oldBlueAmplifiedTimeRemainingSec := arena.BlueRealtimeScore.AmplifiedTimeRemainingSec
matchStartTime := arena.MatchStartTime
currentTime := time.Now()
teleopGracePeriod := matchStartTime.Add(
Expand Down Expand Up @@ -977,13 +980,17 @@ func (arena *Arena) handlePlcInputOutput() {
blueAmpSpeaker.UpdateState(
blueAmpNoteCount, blueSpeakerNoteCount, blueAmplifyButton, blueCoopButton, matchStartTime, currentTime,
)
if !oldRedScore.Equals(redScore) || !oldBlueScore.Equals(blueScore) {
if !oldRedScore.Equals(redScore) || !oldBlueScore.Equals(blueScore) ||
oldRedAmplifiedTimeRemainingSec != arena.RedRealtimeScore.AmplifiedTimeRemainingSec ||
oldBlueAmplifiedTimeRemainingSec != arena.BlueRealtimeScore.AmplifiedTimeRemainingSec {
arena.RealtimeScoreNotifier.Notify()
}

// Handle the amp outputs.
redAmplifiedTimeRemaining := redAmpSpeaker.AmplifiedTimeRemaining(currentTime)
arena.RedRealtimeScore.AmplifiedTimeRemainingSec = int(math.Ceil(redAmplifiedTimeRemaining))
blueAmplifiedTimeRemaining := blueAmpSpeaker.AmplifiedTimeRemaining(currentTime)
arena.BlueRealtimeScore.AmplifiedTimeRemainingSec = int(math.Ceil(blueAmplifiedTimeRemaining))
if arena.MatchState == AutoPeriod || arena.MatchState == PausePeriod || arena.MatchState == TeleopPeriod {
redLowAmpLight := redAmpSpeaker.BankedAmpNotes >= 1
redHighAmpLight := redAmpSpeaker.BankedAmpNotes >= 2
Expand Down
18 changes: 18 additions & 0 deletions field/arena_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,17 +1095,30 @@ func TestPlcMatchCycleGameSpecific(t *testing.T) {
arena.Update()
arena.MatchStartTime = time.Now().Add(-durationToTeleopStart - 10200*time.Millisecond)
arena.Update()
assert.Equal(t, 0, arena.RedRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 10, arena.BlueRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, false, plc.redSpeakerLight)
assert.Equal(t, false, plc.redSubwooferCountdown)
assert.Equal(t, true, plc.blueSpeakerLight)
assert.Equal(t, true, plc.blueSubwooferCountdown)
assert.Equal(t, [3]bool{false, true, true}, plc.blueAmpLights)
blueAmpSpeaker.LastAmplifiedTime = time.Now().Add(-750 * time.Millisecond)
arena.Update()
assert.Equal(t, 0, arena.RedRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 10, arena.BlueRealtimeScore.AmplifiedTimeRemainingSec)
plc.blueAmpButtons = [2]bool{false, false}
plc.blueNoteCounts = [2]int{4, 9}
arena.Update()
assert.Equal(t, 2, blueAmpSpeaker.TeleopUnamplifiedSpeakerNotes)
assert.Equal(t, 3, blueAmpSpeaker.TeleopAmplifiedSpeakerNotes)
blueAmpSpeaker.LastAmplifiedTime = time.Now().Add(-9990 * time.Millisecond)
arena.Update()
assert.Equal(t, 0, arena.RedRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 1, arena.BlueRealtimeScore.AmplifiedTimeRemainingSec)
blueAmpSpeaker.LastAmplifiedTime = time.Now().Add(-11 * time.Second)
arena.Update()
assert.Equal(t, 0, arena.RedRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 0, arena.BlueRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, false, plc.blueSpeakerLight)
assert.Equal(t, false, plc.blueSubwooferCountdown)
plc.blueNoteCounts = [2]int{4, 11}
Expand All @@ -1120,14 +1133,19 @@ func TestPlcMatchCycleGameSpecific(t *testing.T) {
arena.Update()
arena.MatchStartTime = time.Now().Add(-durationToTeleopStart - 20001*time.Millisecond)
arena.Update()
assert.Equal(t, 10, arena.RedRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 0, arena.BlueRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, true, plc.redSpeakerLight)
assert.Equal(t, true, plc.redSubwooferCountdown)
assert.Equal(t, [3]bool{false, true, true}, plc.redAmpLights)
assert.Equal(t, false, plc.blueSpeakerLight)
assert.Equal(t, false, plc.blueSubwooferCountdown)
arena.MatchStartTime = time.Now().Add(-durationToTeleopStart - 23000*time.Millisecond)
plc.redNoteCounts = [2]int{3, 6}
plc.redAmpButtons = [2]bool{false, false}
arena.Update()
assert.Equal(t, 0, arena.RedRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 0, arena.BlueRealtimeScore.AmplifiedTimeRemainingSec)
assert.Equal(t, 4, redAmpSpeaker.TeleopAmplifiedSpeakerNotes)
assert.Equal(t, false, plc.redSpeakerLight)
assert.Equal(t, false, plc.redSubwooferCountdown)
Expand Down

0 comments on commit 833e5c8

Please sign in to comment.