woozle-scoreboard/res/scoreboard.js

616 lines
11 KiB
JavaScript
Raw Normal View History

2011-11-17 17:53:49 -07:00
/*
2014-01-18 23:33:43 -07:00
LADD Roller Derby Scoreboard
Copyright © 2014 Neale Pickett <neale@woozle.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
2011-11-17 17:53:49 -07:00
*/
2011-11-22 23:28:31 -07:00
/* You can only have one scoreboard per page. This limitation is mostly
* because elements need specific id= attributes, and these attribumets
* must be unique within a page.
*
* Corollary: don't change element ids without making a corresponding
* change in this file.
2011-11-22 23:28:31 -07:00
*/
2014-01-18 23:33:43 -07:00
/* Times for various rulesets */
var presets = [
2014-01-19 18:25:38 -07:00
["WFTDA", 30 * 60 * 1000, 120 * 1000, 30 * 1000, 3],
["USARS", 30 * 60 * 1000, 90 * 1000, 30 * 1000, 3],
["RDCL", 15 * 60 * 1000, 60 * 1000, 30 * 1000, 3],
2014-01-20 20:19:36 -07:00
["MADE", 15 * 60 * 1000, 90 * 1000, 30 * 1000, 1],
["JRDA", 30 * 60 * 1000, 120 * 1000, 30 * 1000, 2]
2014-01-18 23:33:43 -07:00
];
2014-01-19 18:25:38 -07:00
var period_time;
var jam_time;
var lineup_time;
var timeouts;
2011-11-22 23:28:31 -07:00
2011-11-22 22:45:09 -07:00
/* State names */
2014-01-18 23:33:43 -07:00
var SETUP = 0;
var JAM = 1;
var LINEUP = 2;
var TIMEOUT = 3;
2011-12-06 22:39:54 -07:00
2014-01-18 21:53:52 -07:00
var periodtext = [
chrome.i18n.getMessage("period1"),
chrome.i18n.getMessage("halftime"),
2014-01-19 18:25:38 -07:00
chrome.i18n.getMessage("period2"),
chrome.i18n.getMessage("timeToGame")
2014-01-18 21:53:52 -07:00
];
var jamtext = [
chrome.i18n.getMessage("jam"),
chrome.i18n.getMessage("lineup"),
chrome.i18n.getMessage("timeout"),
chrome.i18n.getMessage("setup")
];
2011-12-06 22:39:54 -07:00
var period = 0;
2012-06-27 21:56:17 -06:00
var jamno = 0;
2011-11-11 23:20:31 -07:00
2011-12-02 17:55:47 -07:00
var state = SETUP;
2011-11-11 23:20:31 -07:00
2011-12-08 13:20:30 -07:00
var timer_updates = [];
function update() {
2013-11-02 14:35:21 -06:00
for (var i in timer_updates) {
var u = timer_updates[i];
2011-12-08 13:20:30 -07:00
2013-11-02 14:35:21 -06:00
u();
}
2011-12-08 13:20:30 -07:00
}
2014-01-19 18:25:38 -07:00
function e(id) {
ret = document.getElementById(id);
if (! ret) {
return Array();
}
return ret;
}
2011-11-22 22:45:09 -07:00
// Create a timer on [element].
2014-01-19 18:25:38 -07:00
function startTimer(element) {
2013-11-02 14:35:21 -06:00
var startTime;
var running = false;
var set_duration = 0;
var duration = 0;
var className;
2014-01-19 20:26:34 -07:00
// Heartbeat
function pulse() {
if (! running) {
element.className = className + " paused";
return;
}
refresh();
}
2013-11-02 14:35:21 -06:00
// Re-calculate and update displayed time
2014-01-19 20:26:34 -07:00
function refresh() {
2014-01-19 18:25:38 -07:00
var remain = Math.ceil(element.remaining() / 1000);
var min = Math.floor(Math.abs(remain) / 60);
var sec = Math.abs(remain) % 60;
2013-11-02 14:35:21 -06:00
// Set classes
2014-01-18 21:53:52 -07:00
if ((! className) && (remain <= 20)) {
2014-01-19 20:26:34 -07:00
element.className = "lowtime";
2014-01-19 18:25:38 -07:00
} else {
element.className = className;
2013-11-02 14:35:21 -06:00
}
// Has the timer run out?
if ((set_duration > 0) && (remain <= 0)) {
duration = 0;
sec = 0;
2014-01-19 18:25:38 -07:00
element.stop();
2013-11-02 14:35:21 -06:00
}
2014-01-18 21:53:52 -07:00
sec = Math.ceil(sec);
2013-11-02 14:35:21 -06:00
// Zero-pad
if (sec < 10) {
sec = "0" + sec;
}
var t = min + ":" + sec;
2014-01-19 18:25:38 -07:00
if (t != element.value) {
element.value = t;
}
}
function inputHandler() {
var t = element.value.split(":");
var sec = (Number(t[0]) * 60) + Number(t[1]);
if (isNaN(sec) || (sec <= 0) || (sec > (59 * 60))) {
// Ignore goofy values
return;
2013-11-02 14:35:21 -06:00
}
2014-01-19 18:25:38 -07:00
2014-01-19 20:26:34 -07:00
element.set(sec * 1000, className, true);
2013-11-02 14:35:21 -06:00
}
// Return remaining time in milliseconds
element.remaining = function() {
if (running) {
var now = (new Date()).getTime();
return duration - (now - startTime);
} else {
return duration;
}
}
// Set timer to [d] milliseconds.
// Put element into class [cn], if set.
2014-01-19 20:26:34 -07:00
// If [stealth] is set, don't refresh
element.set = function(t, cn, stealth) {
2013-11-02 14:35:21 -06:00
startTime = (new Date()).getTime();
set_duration = t;
duration = t;
className = cn;
2014-01-19 18:25:38 -07:00
2014-01-19 20:26:34 -07:00
if (! stealth) {
refresh();
}
2013-11-02 14:35:21 -06:00
}
// Start timer
element.start = function() {
if (! running) {
startTime = (new Date()).getTime();
running = true;
}
refresh();
}
// Stop timer
element.stop = function() {
if (running) {
duration = element.remaining();
running = false;
}
}
2014-01-19 18:25:38 -07:00
element.readOnly = true;
element.addEventListener("input", inputHandler);
2014-01-19 20:26:34 -07:00
timer_updates.push(pulse);
2011-11-11 23:20:31 -07:00
}
// Transition state machine based on state
function transition(newstate) {
2013-11-02 14:35:21 -06:00
var jt = e("jam");
var pt = e("period");
var jtext = e("jamtext");
var jno = e("jamno");
if ((newstate === undefined) || (newstate == state)) {
return;
}
state = newstate;
if (state == JAM) {
pt.start();
2014-01-18 21:53:52 -07:00
jt.set(jam_time);
2013-11-02 14:35:21 -06:00
jt.start();
jtext.innerHTML = jamtext[0];
jamno += 1;
jno.innerHTML = jamno;
2014-01-19 18:25:38 -07:00
pt.readOnly = true;
2013-11-02 14:35:21 -06:00
} else if (state == LINEUP) {
pt.start();
2014-01-18 21:53:52 -07:00
jt.set(lineup_time, "lineup");
2013-11-02 14:35:21 -06:00
jt.start();
jtext.innerHTML = jamtext[1];
2014-01-19 18:25:38 -07:00
pt.readOnly = true;
2013-11-02 14:35:21 -06:00
} else if (state == TIMEOUT) {
pt.stop();
if (pt.remaining() <= 0) {
2014-01-18 21:53:52 -07:00
pt.set(period_time);
2013-11-02 14:35:21 -06:00
}
jt.set(0, "timeout");
jt.start();
jtext.innerHTML = jamtext[2];
2014-01-19 18:25:38 -07:00
pt.readOnly = false;
2013-11-02 14:35:21 -06:00
}
// Reset lead jammer indicators
e("jammer-a").className = "";
e("jammer-b").className = "";
2014-01-19 18:25:38 -07:00
e("preset").style.display = "none";
2014-01-18 23:33:43 -07:00
save();
2011-11-11 23:20:31 -07:00
}
/***********************************
* Notices
*/
2012-11-10 20:23:06 -07:00
2014-01-19 18:25:38 -07:00
var notices = {
"banana": '<img src="res/banana.gif">'
};
2012-11-10 20:23:06 -07:00
var notice_timer;
2012-11-10 20:23:06 -07:00
function notice_expire() {
2014-01-19 18:25:38 -07:00
var c = e("notice");
2012-11-10 20:23:06 -07:00
c.innerHTML = "";
2013-04-13 21:36:00 -06:00
c.style.display = "none";
}
function notice(n) {
2014-01-19 18:25:38 -07:00
var c = e("notice");
2013-11-02 14:35:21 -06:00
if (notices[n]) {
2014-01-19 18:25:38 -07:00
if (c.innerHTML != notices[n]) {
c.innerHTML = notices[n];
c.style.display = "block";
}
2013-11-02 14:35:21 -06:00
clearTimeout(notice_timer);
notice_timer = setTimeout(function() {notice_expire()}, 8000);
} else {
notice_expire();
}
}
2011-11-14 22:41:03 -07:00
2011-11-11 23:20:31 -07:00
function score(team, points) {
2014-01-19 18:25:38 -07:00
var te = e("score-" + team);
2013-11-02 14:35:21 -06:00
var ts = Number(te.innerHTML);
2011-11-11 23:20:31 -07:00
2013-11-02 14:35:21 -06:00
ts += points;
te.innerHTML = ts;
2011-11-11 23:20:31 -07:00
}
/***********************************
* Event handlers
*/
2011-12-08 13:20:30 -07:00
var logo = {a:-1, b:-1};
2011-11-11 23:20:31 -07:00
function leadJammer(team) {
2013-11-02 14:35:21 -06:00
tgt = e("jammer-" + team);
var on = ! tgt.className;
2013-11-02 14:35:21 -06:00
e("jammer-a").className = "";
e("jammer-b").className = "";
if (on) tgt.className = "lead";
}
2011-11-14 22:41:03 -07:00
function handle(event) {
2013-11-02 14:35:21 -06:00
var tgt = event.target || window.event.srcElement;
var team = tgt.id.substr(tgt.id.length - 1);
var adj = event.shiftKey?-1:1;
var mod = (event.ctrlKey || event.altKey);
var newstate;
switch (tgt.id) {
case "logo-a":
case "logo-b":
if (state == SETUP) {
2014-01-18 21:53:52 -07:00
var t, name;
2013-11-02 14:35:21 -06:00
2014-01-18 21:53:52 -07:00
logo[team] = (teams.length + logo[team] + adj) % teams.length;
t = teams[logo[team]];
2013-11-02 14:35:21 -06:00
2014-01-18 21:53:52 -07:00
name = t[0];
2013-11-02 14:35:21 -06:00
2014-01-18 21:53:52 -07:00
tgt.src = "logos/" + t[1];
2013-11-02 14:35:21 -06:00
} else {
score(team, -adj);
}
break;
case "jammer-a":
case "jammer-b":
leadJammer(team);
break;
case "timeouts-a":
case "timeouts-b":
// Allow for timeouts > 3
var v = Number(tgt.innerHTML);
v -= adj;
2014-01-19 18:25:38 -07:00
if (v == -1) {
v = timeouts;
}
2013-11-02 14:35:21 -06:00
tgt.innerHTML = v;
break;
case "period":
if ((state == SETUP) || (state == TIMEOUT)) {
// Nothin'
} else {
newstate = TIMEOUT;
}
break;
case "periodtext":
var pt;
2014-01-18 23:33:43 -07:00
var ptl = periodtext.length;
2013-11-02 14:35:21 -06:00
2014-01-18 23:33:43 -07:00
period = (period + ptl + adj) % ptl;
pt = periodtext[period];
2013-11-02 14:35:21 -06:00
if (pt) {
tgt.innerHTML = pt;
if (state == TIMEOUT) {
jamno = 0;
e("jamno").innerHTML = jamno;
}
}
break;
case "jam":
if (state == JAM) {
newstate = LINEUP;
} else {
newstate = JAM;
}
break;
case "jamno":
jamno -= adj;
tgt.innerHTML = jamno;
break;
case "score-a":
case "score-b":
if (state == SETUP) {
2014-01-18 23:33:43 -07:00
e(tgt.id).innerHTML = 0;
2013-11-02 14:35:21 -06:00
} else {
score(team, adj);
}
break;
2014-01-19 18:25:38 -07:00
case "preset":
load_preset(+1);
break;
2014-01-18 21:53:52 -07:00
case "close":
window.close();
break;
2013-11-02 14:35:21 -06:00
}
transition(newstate);
2011-11-11 23:20:31 -07:00
}
2011-12-08 13:20:30 -07:00
function key(event) {
2013-11-02 14:35:21 -06:00
var e = event || window.event;
var k = e.which || e.keyCode || 0;
var c;
var newstate;
switch (k) {
case 32:
c = " ";
break;
2013-11-02 09:47:46 -06:00
case 38:
c = "up";
break;
case 40:
c = "down";
break;
2013-11-02 14:35:21 -06:00
case 188:
c = ",";
break;
case 190:
c = ".";
break;
2014-01-19 18:25:38 -07:00
case 191:
c = "/";
2013-11-02 14:35:21 -06:00
break;
case 219:
c = e.shiftKey ? "{" : "[";
break;
2014-01-19 18:25:38 -07:00
case 221:
c = e.shiftKey ? "}" : "]";
break;
case 222:
c = e.shiftKey ? "\"" : "'";
break;
2013-11-02 09:47:46 -06:00
default:
2013-11-02 14:35:21 -06:00
if ((k >= 48) && (k <= 90)) {
c = String.fromCharCode(k);
if (! e.shiftKey) {
c = c.toLowerCase();
}
} else {
c = null;
}
2013-11-02 09:47:46 -06:00
break;
}
2013-11-02 14:35:21 -06:00
bige = e;
switch (c) {
2013-11-02 09:47:46 -06:00
case "up":
if ((state == TIMEOUT) || (state == SETUP)) {
2014-01-19 18:25:38 -07:00
var pt = e("period");
2013-11-02 09:47:46 -06:00
var rem = pt.remaining();
pt.set(rem + 1000);
}
break;
case "down":
if ((state == TIMEOUT) || (state == SETUP)) {
2014-01-19 18:25:38 -07:00
var pt = e("period");
2013-11-02 09:47:46 -06:00
var rem = pt.remaining();
pt.set(rem - 1000);
}
break;
2013-11-02 14:35:21 -06:00
case " ":
if (state == JAM) {
newstate = LINEUP;
} else {
newstate = JAM;
}
break;
case "t":
newstate = TIMEOUT;
break;
case "a":
case "[":
score('a', 1);
break;
2014-01-19 18:25:38 -07:00
case "'":
2013-11-02 14:35:21 -06:00
case "]":
score('b', 1);
break;
2014-01-19 18:25:38 -07:00
case "z":
2013-11-02 14:35:21 -06:00
case "{":
score('a', -1);
break;
2014-01-19 18:25:38 -07:00
case "/":
2013-11-02 14:35:21 -06:00
case "}":
score('b', -1);
break;
case ",":
leadJammer('a');
break;
case ".":
leadJammer('b');
break;
2014-01-19 18:25:38 -07:00
case "g":
window.notice("banana");
break;
2013-11-02 14:35:21 -06:00
}
transition(newstate);
2011-11-15 21:56:33 -07:00
}
2012-04-07 09:35:17 -06:00
function save() {
2013-11-02 14:35:21 -06:00
chrome.storage.local.set(
2013-11-02 09:47:46 -06:00
{
2014-01-19 18:25:38 -07:00
"preset": e("preset").innerHTML,
2014-01-18 23:33:43 -07:00
"logo_a": e("logo-a").src,
"logo_b": e("logo-b").src,
"score_a": e("score-a").innerHTML,
"score_b": e("score-b").innerHTML,
"timeouts_a": e("timeouts-a").innerHTML,
"timeouts_b": e("timeouts-b").innerHTML,
"period_clock": e("period").remaining(),
2013-11-02 09:47:46 -06:00
}
);
}
2014-01-18 23:33:43 -07:00
2014-01-19 18:25:38 -07:00
function load_preset(preset_name) {
var inc = false;
var pn = 0;
if (preset_name == +1) {
preset_name = e("preset").innerHTML;
inc = true;
}
for (var i in presets) {
if (presets[i][0] == preset_name) {
pn = Number(i);
break;
}
}
if (inc) {
pn = (pn + 1) % presets.length;
}
preset_name = presets[pn][0];
period_time = presets[pn][1];
jam_time = presets[pn][2];
lineup_time = presets[pn][3];
timeouts = presets[pn][4];
e("preset").innerHTML = preset_name;
e("jam").set(jam_time);
e("period").set(period_time);
e("timeouts-a").innerHTML = timeouts;
e("timeouts-b").innerHTML = timeouts;
e("score-a").innerHTML = 0;
e("score-b").innerHTML = 0;
}
2014-01-18 23:33:43 -07:00
function load() {
function load_cb(state) {
2014-01-19 18:25:38 -07:00
load_preset(state.preset);
e("period").set((state.period_clock >= 0) ? state.period_clock : period_time);
2014-01-18 23:33:43 -07:00
e("logo-a").src = state.logo_a;
e("logo-b").src = state.logo_b;
e("score-a").innerHTML = state.score_a;
e("score-b").innerHTML = state.score_b;
2014-01-19 18:25:38 -07:00
e("timeouts-a").innerHTML = (state.timeouts_a >= 0) ? state.timeouts_a : timeouts;
e("timeouts-b").innerHTML = (state.timeouts_b >= 0) ? state.timeouts_b : timeouts;
2014-01-18 23:33:43 -07:00
}
2013-11-02 14:35:21 -06:00
2014-01-18 23:33:43 -07:00
chrome.storage.local.get({
2014-01-19 18:25:38 -07:00
"preset": presets[0][0],
"period_clock": -1,
2014-01-18 23:33:43 -07:00
"score_a": 0,
"score_b": 0,
"logo_a": "logos/black.png",
"logo_b": "logos/white.png",
2014-01-19 18:25:38 -07:00
"timeouts_a": -1,
"timeouts_b": -1
2014-01-18 23:33:43 -07:00
}, load_cb);
}
2013-11-02 09:47:46 -06:00
function ei(name) {
el = e(name);
if (el.addEventListener) {
el.addEventListener("click", handle, false);
}
return el;
}
2013-11-01 17:32:32 -06:00
2011-11-14 22:41:03 -07:00
function start() {
2013-11-02 14:35:21 -06:00
resize();
2014-01-18 23:33:43 -07:00
load();
2012-07-16 22:51:42 -06:00
2014-01-18 23:33:43 -07:00
ei("logo-a");
ei("logo-b");
ei("score-a");
ei("score-b")
2013-11-02 14:35:21 -06:00
ei("jammer-a");
ei("jammer-b");
2014-01-18 23:33:43 -07:00
ei("timeouts-a");
ei("timeouts-b");
2013-11-02 14:35:21 -06:00
ei("period");
ei("jam");
2014-01-18 21:53:52 -07:00
ei("prefs");
ei("close");
2014-01-19 18:25:38 -07:00
ei("preset");
2013-11-02 14:35:21 -06:00
ei("periodtext").innerHTML = periodtext[period];
ei("jamtext").innerHTML = jamtext[3];
transition();
2011-11-14 22:41:03 -07:00
2014-01-19 18:25:38 -07:00
var p = e("period");
startTimer(p);
p.readOnly = false;
2014-01-18 23:33:43 -07:00
var j = e("jam");
2014-01-18 21:53:52 -07:00
startTimer(j);
2011-11-14 22:41:03 -07:00
2014-01-18 21:53:52 -07:00
update_itimer = setInterval(update, 200); // 5 times a second
2012-07-06 23:45:03 -06:00
}
function resize() {
2013-11-02 14:35:21 -06:00
var w, h;
2014-01-18 21:53:52 -07:00
w = window.innerWidth / 7;
h = window.innerHeight / 5;
2012-07-06 23:45:03 -06:00
2014-01-18 21:53:52 -07:00
document.body.style.fontSize = Math.min(w, h) + 'px';
2011-11-14 22:41:03 -07:00
}
window.onload = start;
2013-11-02 09:47:46 -06:00
document.onkeydown = key; // IE requires document, not window
2012-07-06 23:45:03 -06:00
window.onresize = resize;