Add generation and sending of game-specific data.

This commit is contained in:
Patrick Fairbank
2018-04-11 23:26:36 -07:00
parent 423baf992c
commit 0c475b0a20
18 changed files with 228 additions and 67 deletions

View File

@@ -4,6 +4,8 @@
package web
import (
"fmt"
"github.com/Team254/cheesy-arena/game"
"github.com/gorilla/websocket"
"github.com/stretchr/testify/assert"
"sync"
@@ -59,7 +61,11 @@ func TestAllianceStationDisplayWebsocket(t *testing.T) {
web.arena.AllianceStations["B3"].Bypass = true
web.arena.StartMatch()
web.arena.Update()
readWebsocketType(t, ws, "matchTime")
web.arena.MatchStartTime = time.Now().Add(-time.Duration(game.MatchTiming.WarmupDurationSec) * time.Second)
web.arena.Update()
messages := readWebsocketMultiple(t, ws, 2)
fmt.Println(messages)
_, ok := messages["status"]
assert.True(t, ok)
_, ok = messages["matchTime"]

View File

@@ -55,7 +55,7 @@ func TestAudienceDisplayWebsocket(t *testing.T) {
}
sound, ok := messages["playSound"]
if assert.True(t, ok) {
assert.Equal(t, "match-start", sound)
assert.Equal(t, "match-warmup", sound)
}
_, ok = messages["matchTime"]
assert.True(t, ok)

View File

@@ -333,7 +333,8 @@ func (web *Web) matchPlayWebsocketHandler(w http.ResponseWriter, r *http.Request
web.arena.AllianceStations[station].Bypass = !web.arena.AllianceStations[station].Bypass
case "startMatch":
args := struct {
MuteMatchSounds bool
MuteMatchSounds bool
GameSpecificData string
}{}
err = mapstructure.Decode(data, &args)
if err != nil {
@@ -341,6 +342,7 @@ func (web *Web) matchPlayWebsocketHandler(w http.ResponseWriter, r *http.Request
continue
}
web.arena.MuteMatchSounds = args.MuteMatchSounds
web.arena.CurrentMatch.GameSpecificData = args.GameSpecificData
err = web.arena.StartMatch()
if err != nil {
websocket.WriteError(err.Error())

View File

@@ -351,43 +351,47 @@ func TestMatchPlayWebsocketNotifications(t *testing.T) {
web.arena.AllianceStations["B3"].Bypass = true
web.arena.StartMatch()
web.arena.Update()
messages := readWebsocketMultiple(t, ws, 4)
statusReceived, matchTime := getStatusMatchTime(t, messages)
assert.Equal(t, true, statusReceived)
assert.Equal(t, 2, matchTime.MatchState)
assert.Equal(t, 0, matchTime.MatchTimeSec)
_, ok := messages["setAudienceDisplay"]
messages := readWebsocketMultiple(t, ws, 3)
_, ok := messages["matchTime"]
assert.True(t, ok)
_, ok = messages["setAudienceDisplay"]
assert.True(t, ok)
_, ok = messages["setAllianceStationDisplay"]
web.arena.MatchStartTime = time.Now().Add(-time.Duration(game.MatchTiming.WarmupDurationSec) * time.Second)
web.arena.Update()
messages = readWebsocketMultiple(t, ws, 2)
statusReceived, matchTime := getStatusMatchTime(t, messages)
assert.Equal(t, true, statusReceived)
assert.Equal(t, 3, matchTime.MatchState)
assert.Equal(t, 3, matchTime.MatchTimeSec)
assert.True(t, ok)
web.arena.ScoringStatusNotifier.Notify(nil)
readWebsocketType(t, ws, "scoringStatus")
// Should get a tick notification when an integer second threshold is crossed.
web.arena.MatchStartTime = time.Now().Add(-time.Second + 10*time.Millisecond) // Not crossed yet
web.arena.Update()
web.arena.MatchStartTime = time.Now().Add(-time.Second - 10*time.Millisecond) // Crossed
web.arena.Update()
err = mapstructure.Decode(readWebsocketType(t, ws, "matchTime"), &matchTime)
assert.Nil(t, err)
assert.Equal(t, 3, matchTime.MatchState)
assert.Equal(t, 1, matchTime.MatchTimeSec)
web.arena.MatchStartTime = time.Now().Add(-2*time.Second + 10*time.Millisecond) // Not crossed yet
web.arena.Update()
web.arena.MatchStartTime = time.Now().Add(-2*time.Second - 10*time.Millisecond) // Crossed
web.arena.Update()
err = mapstructure.Decode(readWebsocketType(t, ws, "matchTime"), &matchTime)
assert.Nil(t, err)
assert.Equal(t, 2, matchTime.MatchState)
assert.Equal(t, 1, matchTime.MatchTimeSec)
err = mapstructure.Decode(readWebsocketType(t, ws, "matchTime"), &matchTime)
assert.Nil(t, err)
assert.Equal(t, 2, matchTime.MatchState)
assert.Equal(t, 3, matchTime.MatchState)
assert.Equal(t, 2, matchTime.MatchTimeSec)
// Check across a match state boundary.
web.arena.MatchStartTime = time.Now().Add(-time.Duration(game.MatchTiming.AutoDurationSec) * time.Second)
web.arena.MatchStartTime = time.Now().Add(-time.Duration(game.MatchTiming.WarmupDurationSec+
game.MatchTiming.AutoDurationSec) * time.Second)
web.arena.Update()
statusReceived, matchTime = readWebsocketStatusMatchTime(t, ws)
assert.Equal(t, true, statusReceived)
assert.Equal(t, 3, matchTime.MatchState)
assert.Equal(t, game.MatchTiming.AutoDurationSec, matchTime.MatchTimeSec)
assert.Equal(t, 4, matchTime.MatchState)
assert.Equal(t, game.MatchTiming.WarmupDurationSec+game.MatchTiming.AutoDurationSec, matchTime.MatchTimeSec)
}
// Handles the status and matchTime messages arriving in either order.