mirror of
https://github.com/Team254/cheesy-arena-lite.git
synced 2026-03-10 06:06:47 -04:00
127 lines
4.3 KiB
JavaScript
127 lines
4.3 KiB
JavaScript
// Copyright 2014 Team 254. All Rights Reserved.
|
|
// Author: pat@patfairbank.com (Patrick Fairbank)
|
|
//
|
|
// Client-side methods for the alliance station display.
|
|
|
|
var allianceStation = "";
|
|
var blinkInterval;
|
|
var currentScreen = "blank";
|
|
var websocket;
|
|
|
|
// Handles a websocket message to set which alliance station this display represents.
|
|
var handleAllianceStation = function(station) {
|
|
allianceStation = station;
|
|
};
|
|
|
|
// Handles a websocket message to change which screen is displayed.
|
|
var handleAllianceStationDisplayMode = function(targetScreen) {
|
|
currentScreen = targetScreen;
|
|
if (allianceStation === "") {
|
|
// Don't do anything if this screen hasn't been assigned a position yet.
|
|
return;
|
|
}
|
|
$("body").attr("data-mode", targetScreen);
|
|
switch (allianceStation[1]) {
|
|
case "1":
|
|
$("body").attr("data-position", "right");
|
|
break;
|
|
case "2":
|
|
$("body").attr("data-position", "middle");
|
|
break;
|
|
case "3":
|
|
$("body").attr("data-position", "left");
|
|
break;
|
|
}
|
|
};
|
|
|
|
// Handles a websocket message to update the team to display.
|
|
var handleMatchLoad = function(data) {
|
|
if (allianceStation !== "") {
|
|
var team = data.Teams[allianceStation];
|
|
if (team) {
|
|
$("#teamNumber").text(team.Id);
|
|
$("#teamNameText").attr("data-alliance-bg", allianceStation[0]).text(team.Nickname);
|
|
|
|
var ranking = data.Rankings[team.Id];
|
|
if (ranking && data.MatchType === "Qualification") {
|
|
var rankingText = ranking.Rank;
|
|
$("#teamRank").attr("data-alliance-bg", allianceStation[0]).text(rankingText);
|
|
} else {
|
|
$("#teamRank").attr("data-alliance-bg", allianceStation[0]).text("");
|
|
}
|
|
} else {
|
|
$("#teamNumber").text("");
|
|
$("#teamNameText").attr("data-alliance-bg", allianceStation[0]).text("");
|
|
$("#teamRank").attr("data-alliance-bg", allianceStation[0]).text("");
|
|
}
|
|
} else {
|
|
$("body").attr("data-mode", "displayId");
|
|
}
|
|
};
|
|
|
|
// Handles a websocket message to update the team connection status.
|
|
var handleArenaStatus = function(data) {
|
|
stationStatus = data.AllianceStations[allianceStation];
|
|
var blink = false;
|
|
if (stationStatus && stationStatus.Bypass) {
|
|
$("#match").attr("data-status", "bypass");
|
|
} else if (stationStatus) {
|
|
if (!stationStatus.DsConn || !stationStatus.DsConn.DsLinked) {
|
|
$("#match").attr("data-status", allianceStation[0]);
|
|
} else if (!stationStatus.DsConn.RobotLinked) {
|
|
blink = true;
|
|
if (!blinkInterval) {
|
|
blinkInterval = setInterval(function() {
|
|
var status = $("#match").attr("data-status");
|
|
$("#match").attr("data-status", (status === "") ? allianceStation[0] : "");
|
|
}, 250);
|
|
}
|
|
} else {
|
|
$("#match").attr("data-status", "");
|
|
}
|
|
}
|
|
|
|
if (!blink && blinkInterval) {
|
|
clearInterval(blinkInterval);
|
|
blinkInterval = null;
|
|
}
|
|
};
|
|
|
|
// Handles a websocket message to update the match time countdown.
|
|
var handleMatchTime = function(data) {
|
|
translateMatchTime(data, function(matchState, matchStateText, countdownSec) {
|
|
var countdownString = String(countdownSec % 60);
|
|
if (countdownString.length === 1) {
|
|
countdownString = "0" + countdownString;
|
|
}
|
|
countdownString = Math.floor(countdownSec / 60) + ":" + countdownString;
|
|
$("#timeRemaining").text(countdownString);
|
|
$("#match").attr("data-state", matchState);
|
|
});
|
|
};
|
|
|
|
// Handles a websocket message to update the match score.
|
|
var handleRealtimeScore = function(data) {
|
|
$("#redScore").text(data.Red.Score);
|
|
$("#blueScore").text(data.Blue.Score);
|
|
};
|
|
|
|
$(function() {
|
|
if (displayId === "") {
|
|
displayId = Math.floor(Math.random() * 10000);
|
|
window.location = "/displays/alliance_station?displayId=" + displayId;
|
|
}
|
|
$("#displayId").text(displayId);
|
|
|
|
// Set up the websocket back to the server.
|
|
websocket = new CheesyWebsocket("/displays/alliance_station/websocket?displayId=" + displayId, {
|
|
allianceStation: function(event) { handleAllianceStation(event.data); },
|
|
allianceStationDisplayMode: function(event) { handleAllianceStationDisplayMode(event.data); },
|
|
arenaStatus: function(event) { handleArenaStatus(event.data); },
|
|
matchLoad: function(event) { handleMatchLoad(event.data); },
|
|
matchTime: function(event) { handleMatchTime(event.data); },
|
|
matchTiming: function(event) { handleMatchTiming(event.data); },
|
|
realtimeScore: function(event) { handleRealtimeScore(event.data); }
|
|
});
|
|
});
|