Skip to content

Commit

Permalink
Update alliance selection timer to better align with new rules from F…
Browse files Browse the repository at this point in the history
…IRST.
  • Loading branch information
patfair committed Sep 8, 2024
1 parent f0336dd commit 6e804f7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 43 deletions.
2 changes: 1 addition & 1 deletion model/event_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (database *Database) GetEventSettings() (*EventSettings, error) {
NumPlayoffAlliances: 8,
SelectionRound2Order: "L",
SelectionRound3Order: "",
SelectionShowUnpickedTeams: false,
SelectionShowUnpickedTeams: true,
TbaDownloadEnabled: true,
ApChannel: 36,
WarmupDurationSec: game.MatchTiming.WarmupDurationSec,
Expand Down
1 change: 1 addition & 0 deletions model/event_settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestEventSettingsReadWrite(t *testing.T) {
NumPlayoffAlliances: 8,
SelectionRound2Order: "L",
SelectionRound3Order: "",
SelectionShowUnpickedTeams: true,
TbaDownloadEnabled: true,
ApChannel: 36,
WarmupDurationSec: 0,
Expand Down
17 changes: 11 additions & 6 deletions static/js/alliance_selection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@

var websocket;

// Sends a websocket message to show the timer.
const showTimer = function() {
websocket.send("showTimer");
// Sends a websocket message to set the timer to the given time limit.
const setTimer = function(timeLimitInput) {
websocket.send("setTimer", parseInt(timeLimitInput.value));
}

// Sends a websocket message to start and show the timer.
const startTimer = function() {
websocket.send("startTimer");
};

// Sends a websocket message to hide the timer.
const hideTimer = function() {
websocket.send("hideTimer");
// Sends a websocket message to stop and hide the timer.
const stopTimer = function() {
websocket.send("stopTimer");
}

// Handles a websocket message to update the alliance selection status.
Expand Down
15 changes: 5 additions & 10 deletions templates/alliance_selection.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,18 @@
<legend id="timer"></legend>
</div>
</div>
<p>
Timer starts automatically after each non-captain assignment and is hidden on the audience overlay until
the button below is pressed.
</p>
<p>Timer is hidden on the audience overlay until the Start/Show button below is pressed.</p>
<div class="row">
<label class="col-lg-5 control-label">Time limit in seconds<br />(0 = disabled)</label>
<div class="col-lg-3">
<input type="text" class="form-control" name="timeLimitSec" value="{{.TimeLimitSec}}">
</div>
<div class="col-lg-4 text-end">
<button type="submit" class="btn btn-primary">Save/Reset</button>
<input type="text" class="form-control" name="timeLimitSec" value="{{.TimeLimitSec}}"
onblur="setTimer(this);">
</div>
</div>
<div class="mt-3 row justify-content-center">
<div class="col-lg-8 text-center">
<button type="button" class="btn btn-success" onclick="showTimer();">Show Timer</button>
<button type="button" class="btn btn-secondary" onclick="hideTimer();">Hide Timer</button>
<button type="button" class="btn btn-success" onclick="startTimer();">Start/Show Timer</button>
<button type="button" class="btn btn-secondary" onclick="stopTimer();">Stop/Hide Timer</button>
</div>
</div>
</div>
Expand Down
49 changes: 27 additions & 22 deletions web/alliance_selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

// Global var to hold configurable time limit for selections. A value of zero disables the timer.
var allianceSelectionTimeLimitSec = 0
var allianceSelectionTimeLimitSec = 45

// Global var to hold a ticker used for the alliance selection timer.
var allianceSelectionTicker *time.Ticker
Expand All @@ -43,9 +43,6 @@ func (web *Web) allianceSelectionPostHandler(w http.ResponseWriter, r *http.Requ
return
}

// Update time limit value.
allianceSelectionTimeLimitSec, _ = strconv.Atoi(r.PostFormValue("timeLimitSec"))

// Reset picked state for each team in preparation for reconstructing it.
for i := range web.arena.AllianceSelectionRankedTeams {
web.arena.AllianceSelectionRankedTeams[i].Picked = false
Expand Down Expand Up @@ -96,19 +93,6 @@ func (web *Web) allianceSelectionPostHandler(w http.ResponseWriter, r *http.Requ
web.arena.AllianceSelectionShowTimer = false
web.arena.AllianceSelectionTimeRemainingSec = 0
}
if _, nextCol := web.determineNextCell(); nextCol > 0 && allianceSelectionTimeLimitSec > 0 {
web.arena.AllianceSelectionTimeRemainingSec = allianceSelectionTimeLimitSec
allianceSelectionTicker = time.NewTicker(time.Second)
go func() {
for range allianceSelectionTicker.C {
web.arena.AllianceSelectionTimeRemainingSec--
web.arena.AllianceSelectionNotifier.Notify()
if web.arena.AllianceSelectionTimeRemainingSec == 0 {
allianceSelectionTicker.Stop()
}
}
}()
}

web.arena.AllianceSelectionNotifier.Notify()
http.Redirect(w, r, "/alliance_selection", 303)
Expand Down Expand Up @@ -296,7 +280,7 @@ func (web *Web) allianceSelectionWebsocketHandler(w http.ResponseWriter, r *http

// Loop, waiting for commands and responding to them, until the client closes the connection.
for {
messageType, _, err := ws.Read()
messageType, data, err := ws.Read()
if err != nil {
if err == io.EOF {
// Client has closed the connection; nothing to do here.
Expand All @@ -307,11 +291,32 @@ func (web *Web) allianceSelectionWebsocketHandler(w http.ResponseWriter, r *http
}

switch messageType {
case "showTimer":
web.arena.AllianceSelectionShowTimer = true
web.arena.AllianceSelectionNotifier.Notify()
case "hideTimer":
case "setTimer":
if timeLimitSec, ok := data.(float64); ok {
allianceSelectionTimeLimitSec = int(timeLimitSec)
} else {
ws.WriteError("Invalid time limit value.")
}
case "startTimer":
if !web.arena.AllianceSelectionShowTimer {
web.arena.AllianceSelectionShowTimer = true
web.arena.AllianceSelectionTimeRemainingSec = allianceSelectionTimeLimitSec
web.arena.AllianceSelectionNotifier.Notify()
allianceSelectionTicker = time.NewTicker(time.Second)
go func() {
for range allianceSelectionTicker.C {
web.arena.AllianceSelectionTimeRemainingSec--
web.arena.AllianceSelectionNotifier.Notify()
if web.arena.AllianceSelectionTimeRemainingSec == 0 {
allianceSelectionTicker.Stop()
}
}
}()
}
case "stopTimer":
allianceSelectionTicker.Stop()
web.arena.AllianceSelectionShowTimer = false
web.arena.AllianceSelectionTimeRemainingSec = 0
web.arena.AllianceSelectionNotifier.Notify()
default:
ws.WriteError(fmt.Sprintf("Invalid message type '%s'.", messageType))
Expand Down
8 changes: 4 additions & 4 deletions web/alliance_selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,17 +318,17 @@ func TestAllianceSelectionWebsocket(t *testing.T) {
// Should get a status update right after connection.
readWebsocketType(t, ws, "allianceSelection")

// Test showing and hiding the timer.
// Test starting and stopping the timer.
allianceSelectionMessage := struct {
ShowTimer bool
}{}
ws.Write("showTimer", nil)
ws.Write("startTimer", nil)
assert.Nil(t, mapstructure.Decode(readWebsocketType(t, ws, "allianceSelection"), &allianceSelectionMessage))
assert.Equal(t, true, allianceSelectionMessage.ShowTimer)
ws.Write("hideTimer", nil)
ws.Write("stopTimer", nil)
assert.Nil(t, mapstructure.Decode(readWebsocketType(t, ws, "allianceSelection"), &allianceSelectionMessage))
assert.Equal(t, false, allianceSelectionMessage.ShowTimer)
ws.Write("showTimer", nil)
ws.Write("startTimer", nil)
assert.Nil(t, mapstructure.Decode(readWebsocketType(t, ws, "allianceSelection"), &allianceSelectionMessage))
assert.Equal(t, true, allianceSelectionMessage.ShowTimer)
}

0 comments on commit 6e804f7

Please sign in to comment.