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",
|
2018-04-11 23:26:36 -07:00
|
|
|
2: "WARMUP_PERIOD",
|
|
|
|
|
3: "AUTO_PERIOD",
|
|
|
|
|
4: "PAUSE_PERIOD",
|
|
|
|
|
5: "TELEOP_PERIOD",
|
2019-04-12 17:38:58 -07:00
|
|
|
6: "POST_MATCH",
|
|
|
|
|
7: "TIMEOUT_ACTIVE",
|
|
|
|
|
8: "POST_TIMEOUT"
|
2014-07-27 16:41:09 -07:00
|
|
|
};
|
|
|
|
|
var matchTiming;
|
|
|
|
|
|
2014-09-06 22:25:12 -07:00
|
|
|
// 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;
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-06 22:25:12 -07:00
|
|
|
// 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":
|
2018-04-11 23:26:36 -07:00
|
|
|
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;
|
2018-09-18 23:58:33 -07:00
|
|
|
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));
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-06 22:25:12 -07:00
|
|
|
// 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":
|
2018-04-11 23:26:36 -07:00
|
|
|
case "WARMUP_PERIOD":
|
2018-09-18 23:58:33 -07:00
|
|
|
return matchTiming.AutoDurationSec;
|
2014-07-27 16:41:09 -07:00
|
|
|
case "AUTO_PERIOD":
|
2018-04-11 23:26:36 -07:00
|
|
|
return matchTiming.WarmupDurationSec + matchTiming.AutoDurationSec - matchTimeSec;
|
2014-07-27 16:41:09 -07:00
|
|
|
case "TELEOP_PERIOD":
|
2018-04-11 23:26:36 -07:00
|
|
|
return matchTiming.WarmupDurationSec + matchTiming.AutoDurationSec + matchTiming.TeleopDurationSec +
|
|
|
|
|
matchTiming.PauseDurationSec - matchTimeSec;
|
2018-09-18 23:58:33 -07:00
|
|
|
case "TIMEOUT_ACTIVE":
|
|
|
|
|
return matchTiming.TimeoutDurationSec - matchTimeSec;
|
2014-07-27 16:41:09 -07:00
|
|
|
default:
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
};
|