Files
cheesy-arena-lite/static/js/match_timing.js
2018-04-11 23:26:36 -07:00

69 lines
2.0 KiB
JavaScript

// Copyright 2014 Team 254. All Rights Reserved.
// Author: pat@patfairbank.com (Patrick Fairbank)
//
// Shared client-side logic for interpreting match state and timing notifications.
var matchStates = {
0: "PRE_MATCH",
1: "START_MATCH",
2: "WARMUP_PERIOD",
3: "AUTO_PERIOD",
4: "PAUSE_PERIOD",
5: "TELEOP_PERIOD",
6: "ENDGAME_PERIOD",
7: "POST_MATCH"
};
var matchTiming;
// Handles a websocket message containing the length of each period in the match.
var handleMatchTiming = function(data) {
matchTiming = data;
};
// Converts the raw match state and time into a human-readable state and per-period time. Calls the provided
// callback with the result.
var translateMatchTime = function(data, callback) {
var matchStateText;
switch (matchStates[data.MatchState]) {
case "PRE_MATCH":
matchStateText = "PRE-MATCH";
break;
case "START_MATCH":
case "WARMUP_PERIOD":
matchStateText = "WARMUP";
break;
case "AUTO_PERIOD":
matchStateText = "AUTONOMOUS";
break;
case "PAUSE_PERIOD":
matchStateText = "PAUSE";
break;
case "TELEOP_PERIOD":
case "ENDGAME_PERIOD":
matchStateText = "TELEOPERATED";
break;
case "POST_MATCH":
matchStateText = "POST-MATCH";
break;
}
callback(matchStates[data.MatchState], matchStateText, getCountdown(data.MatchState, data.MatchTimeSec));
};
// Returns the per-period countdown for the given match state and overall time into the match.
var getCountdown = function(matchState, matchTimeSec) {
switch (matchStates[matchState]) {
case "PRE_MATCH":
case "START_MATCH":
case "WARMUP_PERIOD":
return matchTiming.AutoDurationSec;
case "AUTO_PERIOD":
return matchTiming.WarmupDurationSec + matchTiming.AutoDurationSec - matchTimeSec;
case "TELEOP_PERIOD":
case "ENDGAME_PERIOD":
return matchTiming.WarmupDurationSec + matchTiming.AutoDurationSec + matchTiming.TeleopDurationSec +
matchTiming.PauseDurationSec - matchTimeSec;
default:
return 0;
}
};