Add framework for playing game sounds at arbitrary times during the match.

This commit is contained in:
Patrick Fairbank
2019-04-12 18:40:35 -07:00
parent 3f48e4fb92
commit adb0bbada1
6 changed files with 40 additions and 10 deletions

View File

@@ -82,6 +82,7 @@ type Arena struct {
warmupLedMode led.Mode
lastRedAllianceReady bool
lastBlueAllianceReady bool
soundsPlayed map[*game.MatchSound]struct{}
}
type AllianceStation struct {
@@ -208,7 +209,8 @@ func (arena *Arena) LoadMatch(match *model.Match) error {
arena.setupNetwork()
// Reset the realtime scores.
// Reset the arena state and realtime scores.
arena.soundsPlayed = make(map[*game.MatchSound]struct{})
arena.RedRealtimeScore = NewRealtimeScore()
arena.BlueRealtimeScore = NewRealtimeScore()
arena.FieldVolunteers = false
@@ -445,12 +447,10 @@ func (arena *Arena) Update() {
arena.MatchState = WarmupPeriod
enabled = false
sendDsPacket = false
arena.playSound("match-warmup")
} else {
arena.MatchState = AutoPeriod
enabled = true
sendDsPacket = true
arena.playSound("match-start")
}
// Pick an LED warmup mode at random to keep things interesting.
allWarmupModes := []led.Mode{led.WarmupMode, led.Warmup2Mode, led.Warmup3Mode, led.Warmup4Mode}
@@ -463,7 +463,6 @@ func (arena *Arena) Update() {
auto = true
enabled = true
sendDsPacket = true
arena.playSound("match-start")
}
case AutoPeriod:
auto = true
@@ -474,11 +473,9 @@ func (arena *Arena) Update() {
if game.MatchTiming.PauseDurationSec > 0 {
arena.MatchState = PausePeriod
enabled = false
arena.playSound("match-end")
} else {
arena.MatchState = TeleopPeriod
enabled = true
arena.playSound("match-resume")
}
}
case PausePeriod:
@@ -490,7 +487,6 @@ func (arena *Arena) Update() {
auto = false
enabled = true
sendDsPacket = true
arena.playSound("match-resume")
}
case TeleopPeriod:
auto = false
@@ -509,7 +505,6 @@ func (arena *Arena) Update() {
arena.AllianceStationDisplayMode = "logo"
arena.AllianceStationDisplayModeNotifier.Notify()
}()
arena.playSound("match-end")
}
case TimeoutActive:
if matchTimeSec >= float64(game.MatchTiming.TimeoutDurationSec) {
@@ -541,6 +536,8 @@ func (arena *Arena) Update() {
arena.ArenaStatusNotifier.Notify()
}
arena.handleSounds(matchTimeSec)
// Handle field sensors/lights/motors.
arena.handlePlcInput()
arena.handleLeds()
@@ -945,6 +942,17 @@ func (arena *Arena) handleEstop(station string, state bool) {
}
}
func (arena *Arena) handleSounds(matchTimeSec float64) {
for _, sound := range game.MatchSounds {
if _, ok := arena.soundsPlayed[sound]; !ok {
if matchTimeSec > sound.MatchTimeSec {
arena.playSound(sound.Name)
arena.soundsPlayed[sound] = struct{}{}
}
}
}
}
func (arena *Arena) playSound(name string) {
if !arena.MuteMatchSounds {
arena.PlaySoundNotifier.NotifyWithMessage(name)

20
game/match_sounds.go Normal file
View File

@@ -0,0 +1,20 @@
// Copyright 2019 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Game-specific audience sound timings.
package game
type MatchSound struct {
Name string
MatchTimeSec float64
}
// List of sounds and how many seconds into the match they are played.
var MatchSounds = []*MatchSound{
{"match-start", 0},
{"match-resume", 15},
{"match-warning1", 120},
{"match-warning2", 130},
{"match-end", 150},
}

Binary file not shown.

View File

@@ -200,7 +200,8 @@
<audio id="match-force" src="/static/audio/match_force.wav" preload="auto"></audio>
<audio id="match-levitate" src="/static/audio/match_levitate.wav" preload="auto"></audio>
<audio id="match-boost" src="/static/audio/match_boost.wav" preload="auto"></audio>
<audio id="match-endgame" src="/static/audio/match_endgame.wav" preload="auto"></audio>
<audio id="match-warning1" src="/static/audio/match_warning1.wav" preload="auto"></audio>
<audio id="match-warning2" src="/static/audio/match_warning2.wav" preload="auto"></audio>
<script src="/static/js/lib/jquery.min.js"></script>
<script src="/static/js/lib/jquery.json-2.4.min.js"></script>
<script src="/static/js/lib/jquery.websocket-0.0.1.js"></script>

View File

@@ -56,6 +56,7 @@ func TestAudienceDisplayWebsocket(t *testing.T) {
web.arena.AllianceStations["B3"].Bypass = true
web.arena.StartMatch()
web.arena.Update()
web.arena.Update()
messages := readWebsocketMultiple(t, ws, 3)
screen, ok := messages["audienceDisplayMode"]
if assert.True(t, ok) {
@@ -63,7 +64,7 @@ func TestAudienceDisplayWebsocket(t *testing.T) {
}
sound, ok := messages["playSound"]
if assert.True(t, ok) {
assert.Equal(t, "match-warmup", sound)
assert.Equal(t, "match-start", sound)
}
_, ok = messages["matchTime"]
assert.True(t, ok)