woozle-scoreboard/scoreboard.js

340 lines
8.3 KiB
JavaScript
Raw Normal View History

2011-11-17 17:53:49 -07:00
/*
* LADD Roller Derby Scoreboard
* Copyright © 2011 Neale Pickett <neale@woozle.org>
2011-11-22 22:45:09 -07:00
* Time-stamp: <2011-11-22 22:35:40 neale>
2011-11-17 17:53:49 -07:00
*
* 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-22 22:45:09 -07:00
/* State names */
2011-11-22 00:45:52 -07:00
var STARTUP = 0;
2011-11-11 23:20:31 -07:00
var JAM = 1;
var ROTATE = 2;
2011-11-22 00:45:52 -07:00
var TIMEOUT = 3;
2011-11-22 22:45:09 -07:00
var BREAK = 4;
2011-11-11 23:20:31 -07:00
2011-11-22 00:45:52 -07:00
var state = STARTUP;
2011-11-11 23:20:31 -07:00
2011-11-22 22:45:09 -07:00
// Create a timer on [element].
// If [tenths] is true, show tenths of a second.
// If [callback] is defined, call it when time runs out.
function startTimer(element, tenths, callback) {
2011-11-11 23:20:31 -07:00
var itimer;
2011-11-22 22:45:09 -07:00
var startTime;
var duration = 0;
var className;
2011-11-11 23:20:31 -07:00
2011-11-22 22:45:09 -07:00
// Re-calculate and update displayed time
function refresh() {
2011-11-14 22:41:03 -07:00
var remain = element.remaining();
2011-11-11 23:20:31 -07:00
var min = Math.floor(Math.abs(remain / 60000));
2011-11-22 22:45:09 -07:00
var sec = (Math.floor(Math.abs(remain / 100)) / 10) % 60;
2011-11-11 23:20:31 -07:00
2011-11-22 22:45:09 -07:00
// Set classes
element.className = className;
if (! itimer) {
element.className += " paused";
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
if ((! className) && (remain <= 20000)) {
element.className += " lowtime";
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
// Has the timer run out?
2011-11-11 23:20:31 -07:00
if ((duration > 0) && (remain <= 0)) {
2011-11-17 17:53:49 -07:00
duration = 0;
2011-11-22 22:45:09 -07:00
sec = 0;
clearInterval(itimer);
itimer = undefined;
if (callback) {
callback();
}
}
// .toFixed() rounds, we want to truncate
if (! tenths) {
sec = Math.floor(sec);
} else {
sec = sec.toFixed(1);
}
// Zero-pad
if (sec < 10) {
sec = "0" + sec;
2011-11-11 23:20:31 -07:00
}
2011-11-21 23:20:25 -07:00
2011-11-22 22:45:09 -07:00
var t = min + ":" + sec;
2011-11-21 23:20:25 -07:00
if (t != element.innerHTML) {
element.innerHTML = t;
}
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
// Return remaining time in milliseconds
element.remaining = function() {
if (itimer) {
var now = (new Date()).getTime();
return duration - (now - startTime);
2011-11-14 22:41:03 -07:00
} else {
return duration;
}
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
// Set timer to [d] milliseconds.
// Put element into class [cn], if set.
element.set = function(t, cn) {
startTime = (new Date()).getTime();
duration = t;
className = cn;
refresh();
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
// Start timer
element.start = function() {
if (! itimer) {
startTime = (new Date()).getTime();
itimer = setInterval(refresh, 33);
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
refresh();
2011-11-11 23:20:31 -07:00
}
2011-11-22 22:45:09 -07:00
// Stop timer
element.stop = function() {
if (itimer) {
duration = element.remaining();
clearInterval(itimer);
itimer = undefined;
2011-11-12 08:24:11 -07:00
}
2011-11-22 22:45:09 -07:00
refresh();
2011-11-11 23:20:31 -07:00
}
2011-11-14 22:41:03 -07:00
2011-11-11 23:20:31 -07:00
}
// Transition state machine based on state
function transition(newstate) {
if ((newstate == undefined) || (newstate == state)) {
return;
}
state = newstate;
2011-11-14 22:41:03 -07:00
var jt = document.getElementById("jam");
var pt = document.getElementById("period");
2011-11-11 23:20:31 -07:00
var ptext = document.getElementById("periodtext");
var jtext = document.getElementById("jamtext");
if (state == JAM) {
2011-11-22 22:45:09 -07:00
pt.start();
jt.set(120000);
2011-11-14 22:41:03 -07:00
jt.start();
2011-11-11 23:20:31 -07:00
jtext.innerHTML = "Jam";
} else if (state == ROTATE) {
2011-11-22 22:45:09 -07:00
pt.start();
jt.set(30000, "rotate");
2011-11-14 22:41:03 -07:00
jt.start();
2011-11-11 23:20:31 -07:00
jtext.innerHTML = "Rotation";
} else if (state == TIMEOUT) {
2011-11-22 22:45:09 -07:00
pt.stop();
2011-11-17 17:53:49 -07:00
if (pt.remaining() <= 0) {
2011-11-22 22:45:09 -07:00
pt.set(1800000);
2011-11-17 17:53:49 -07:00
}
2011-11-22 22:45:09 -07:00
jt.set(0, "timeout");
2011-11-14 22:41:03 -07:00
jt.start();
2011-11-11 23:20:31 -07:00
jtext.innerHTML = "Timeout";
}
ptext.innerHTML = "Period " + period;
}
2011-11-14 22:41:03 -07:00
function save() {
localStorage.rdsb_name_a = e("name-a").innerHTML;
localStorage.rdsb_name_b = e("name-b").innerHTML;
2011-11-17 17:53:49 -07:00
localStorage.rdsb_logo_a = e("logo-a").src;
localStorage.rdsb_logo_b = e("logo-b").src;
2011-11-14 22:41:03 -07:00
localStorage.rdsb_score_a = e("score-a").innerHTML;
localStorage.rdsb_score_b = e("score-b").innerHTML;
localStorage.rdsb_period = period;
localStorage.rdsb_period_clock = e("period").remaining();
}
function e(id) {
return document.getElementById(id);
}
2011-11-11 23:20:31 -07:00
function score(team, points) {
var te = document.getElementById("score-" + team);
var ts = Number(te.innerHTML);
ts += points;
te.innerHTML = ts;
}
2011-11-14 22:41:03 -07:00
function teamname(t, v) {
if (! v) return;
2011-11-17 17:53:49 -07:00
var name = e("name-" + t);
var logo = e("logo-" + t);
if (logo.plastic) {
logo.src = v.toLowerCase() + ".png";
logo.plastic = false;
}
e("name-" + t).innerHTML = v;
2011-11-14 22:41:03 -07:00
}
2011-11-11 23:20:31 -07:00
2011-11-14 22:41:03 -07:00
function handle(event) {
var e = event.target;
2011-11-15 21:56:33 -07:00
var team = e.id.substr(e.id.length - 1);
var newstate;
2011-11-11 23:20:31 -07:00
2011-11-22 00:45:52 -07:00
switch (e.id) {
case "name-a":
case "name-b":
if (state == STARTUP) {
2011-11-14 22:41:03 -07:00
teamname(team, prompt("Enter team " + team + " name", e.innerHTML));
2011-11-22 00:45:52 -07:00
}
break;
case "logo-a":
case "logo-b":
if (state == STARTUP) {
2011-11-21 23:20:25 -07:00
var u = prompt("Enter URL to team " + team + " logo");
2011-11-17 17:53:49 -07:00
if (! u) return;
e.src = u;
e.plastic = false;
2011-11-22 00:45:52 -07:00
}
break;
case "period":
if ((state == STARTUP) || (state == TIMEOUT)) {
2011-11-14 22:41:03 -07:00
var r = prompt("Enter new time for period clock", e.innerHTML);
if (! r) return;
var t = r.split(":");
var sec = 0;
if (t.length > 3) {
e.innerHTML = "What?";
return;
}
for (var i in t) {
var v = t[i];
sec = (sec * 60) + Number(v);
}
2011-11-22 22:45:09 -07:00
e.set(sec*1000);
2011-11-22 00:45:52 -07:00
} else {
newstate = TIMEOUT;
}
break;
case "periodtext":
period = 3 - period;
e.innerHTML = "Period " + period;
break;
case "jam":
if (state == JAM) {
newstate = ROTATE;
} else {
newstate = JAM;
2011-11-14 22:41:03 -07:00
}
2011-11-22 00:45:52 -07:00
break;
case "score-a":
case "score-b":
if (event.shiftKey == 1) {
score(team, -1);
} else {
score(team, 1);
2011-11-11 23:20:31 -07:00
}
2011-11-22 00:45:52 -07:00
break;
2011-11-11 23:20:31 -07:00
}
transition(newstate);
2011-11-11 23:20:31 -07:00
}
2011-11-17 17:53:49 -07:00
function imgfail(team) {
var logo = e("logo-" + team);
var url = e("name-" + team).innerHTML.toLowerCase() + ".png";
if (logo.plastic && (logo.src != url)) {
logo.src = url;
} else {
logo.src = "skate.png";
logo.plastic = true;
}
}
2011-11-15 21:56:33 -07:00
function key(e) {
var newstate;
2011-11-15 21:56:33 -07:00
switch (String.fromCharCode(e.which || 0)) {
case " ":
if (state == JAM) {
newstate = ROTATE;
2011-11-15 21:56:33 -07:00
} else {
newstate = JAM;
2011-11-15 21:56:33 -07:00
}
break;
case "j":
2011-11-21 23:20:25 -07:00
newstate = JAM;
2011-11-15 21:56:33 -07:00
break;
case "r":
case "l": // WFTDA TV uses this
2011-11-21 23:20:25 -07:00
newstate = ROTATE;
2011-11-15 21:56:33 -07:00
break;
case "t":
2011-11-21 23:20:25 -07:00
newstate = TIMEOUT;
2011-11-15 21:56:33 -07:00
break;
case "a":
case ",":
score('a', 1);
break;
case "b":
case ".":
score('b', 1);
break;
case "A":
case "<":
score('a', -1);
break;
case "B":
case ">":
score('b', -1);
break;
}
transition(newstate);
2011-11-15 21:56:33 -07:00
}
2011-11-14 22:41:03 -07:00
function start() {
var p = document.getElementById("period");
var j = document.getElementById("jam");
teamname("a", localStorage.rdsb_name_a || "Home");
teamname("b", localStorage.rdsb_name_b || "Visitor");
2011-11-17 17:53:49 -07:00
e("logo-a").src = localStorage.rdsb_logo_a || "skate.png";
e("logo-b").src = localStorage.rdsb_logo_b || "skate.png";
2011-11-14 22:41:03 -07:00
e("score-a").innerHTML = localStorage.rdsb_score_a || 0;
e("score-b").innerHTML = localStorage.rdsb_score_b || 0;
period = localStorage.rdsb_period || 1;
2011-11-17 17:53:49 -07:00
e("periodtext").innerHTML = "Period " + period;
2011-11-14 22:41:03 -07:00
c = Number(localStorage.rdsb_period_clock || 1800000);
2011-11-22 22:45:09 -07:00
startTimer(p);
p.set(c);
startTimer(j, true);
j.set(120000);
2011-11-14 22:41:03 -07:00
save_itimer = setInterval(save, 1000);
}
window.onload = start;