Files
cheesy-arena-lite/static/js/match_timing.js

74 lines
2.2 KiB
JavaScript
Raw Normal View History

2014-07-27 16:41:09 -07:00
// 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: "POST_MATCH",
7: "TIMEOUT_ACTIVE",
8: "POST_TIMEOUT"
2014-07-27 16:41:09 -07:00
};
var matchTiming;
// Handles a websocket message containing the length of each period in the match.
2014-07-27 16:41:09 -07:00
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.
2014-07-27 16:41:09 -07:00
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;
2014-07-27 16:41:09 -07:00
case "AUTO_PERIOD":
matchStateText = "AUTONOMOUS";
break;
case "PAUSE_PERIOD":
matchStateText = "PAUSE";
break;
case "TELEOP_PERIOD":
matchStateText = "TELEOPERATED";
break;
case "POST_MATCH":
matchStateText = "POST-MATCH";
break;
case "TIMEOUT_ACTIVE":
case "POST_TIMEOUT":
matchStateText = "TIMEOUT";
break;
2014-07-27 16:41:09 -07:00
}
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.
2014-07-27 16:41:09 -07:00
var getCountdown = function(matchState, matchTimeSec) {
switch (matchStates[matchState]) {
case "PRE_MATCH":
case "START_MATCH":
case "WARMUP_PERIOD":
return matchTiming.AutoDurationSec;
2014-07-27 16:41:09 -07:00
case "AUTO_PERIOD":
return matchTiming.WarmupDurationSec + matchTiming.AutoDurationSec - matchTimeSec;
2014-07-27 16:41:09 -07:00
case "TELEOP_PERIOD":
return matchTiming.WarmupDurationSec + matchTiming.AutoDurationSec + matchTiming.TeleopDurationSec +
matchTiming.PauseDurationSec - matchTimeSec;
case "TIMEOUT_ACTIVE":
return matchTiming.TimeoutDurationSec - matchTimeSec;
2014-07-27 16:41:09 -07:00
default:
return 0;
}
};