woozle-scoreboard/scoreboard.js

500 lines
12 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>
*
* 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 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
*/
2011-11-22 22:45:09 -07:00
/* State names */
2011-12-07 21:47:58 -07:00
var SETUP = 0; // !P 30:00 !J 2:00
2011-12-06 22:39:54 -07:00
var JAM = 1; // P J 2:00
2011-12-12 19:36:09 -07:00
var LINEUP = 2; // P J 1:00
2011-12-06 22:39:54 -07:00
var TIMEOUT = 3; // !P J 1:00
var periodtext = ["Period 1", "Halftime", "Period 2", "Break"];
2012-04-07 21:41:09 -06:00
var jamtext = ["Jam", "Lineup", "Timeout", "Setup"];
2011-12-06 22:39:54 -07:00
var period = 0;
2012-07-06 18:30:37 -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() {
for (i in timer_updates) {
var u = timer_updates[i];
u();
}
}
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) {
var startTime;
2011-12-08 13:20:30 -07:00
var running = false;
2012-06-27 22:01:27 -06:00
var set_duration = 0;
2011-11-22 22:45:09 -07:00
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
2011-12-08 13:20:30 -07:00
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 ((! className) && (remain <= 20000)) {
element.className += " lowtime";
2011-11-11 23:20:31 -07:00
}
2011-12-08 13:20:30 -07:00
if (! running) {
element.className += " paused";
}
2011-11-11 23:20:31 -07:00
2011-11-22 22:45:09 -07:00
// Has the timer run out?
2012-06-27 22:01:27 -06:00
if ((set_duration > 0) && (remain <= 0)) {
2011-11-17 17:53:49 -07:00
duration = 0;
2011-11-22 22:45:09 -07:00
sec = 0;
2011-12-08 13:20:30 -07:00
running = false;
2011-11-22 22:45:09 -07:00
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() {
2011-12-08 13:20:30 -07:00
if (running) {
2011-11-22 22:45:09 -07:00
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();
2012-06-27 22:01:27 -06:00
set_duration = t;
2011-11-22 22:45:09 -07:00
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() {
2011-12-08 13:20:30 -07:00
if (! running) {
2011-11-22 22:45:09 -07:00
startTime = (new Date()).getTime();
2011-12-08 13:20:30 -07:00
running = true;
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() {
2011-12-08 13:20:30 -07:00
if (running) {
2011-11-22 22:45:09 -07:00
duration = element.remaining();
2011-12-08 13:20:30 -07:00
running = false;
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-12-08 13:20:30 -07:00
timer_updates.push(refresh);
2011-11-11 23:20:31 -07:00
}
// Transition state machine based on state
function transition(newstate) {
2012-04-07 09:35:17 -06:00
var jt = e("jam");
var pt = e("period");
var jtext = e("jamtext");
2012-07-06 18:30:37 -06:00
var jno = e("jamno");
2011-11-11 23:20:31 -07:00
2011-12-07 21:47:58 -07:00
if ((newstate == undefined) || (newstate == state)) {
return;
}
if ((state == SETUP) && window.penalties) {
penalties_duck();
}
2011-12-07 21:47:58 -07:00
state = newstate;
2011-11-11 23:20:31 -07:00
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();
2012-04-07 21:41:09 -06:00
jtext.innerHTML = jamtext[0];
2012-07-06 18:30:37 -06:00
jamno += 1;
jno.innerHTML = jamno;
2011-12-12 19:36:09 -07:00
} else if (state == LINEUP) {
2011-11-22 22:45:09 -07:00
pt.start();
2011-12-20 22:42:06 -07:00
jt.set(30000, "lineup");
2011-11-14 22:41:03 -07:00
jt.start();
2012-04-07 21:41:09 -06:00
jtext.innerHTML = jamtext[1];
2011-11-11 23:20:31 -07:00
} 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();
2012-04-07 21:41:09 -06:00
jtext.innerHTML = jamtext[2];
2011-11-11 23:20:31 -07:00
}
2011-12-08 13:20:30 -07:00
// Reset lead jammer indicators
e("jammer-a").className = "";
e("jammer-b").className = "";
2011-11-11 23:20:31 -07:00
}
/***********************************
* Notices
*/
var notice_timer;
function notice_expire(n) {
var p = document.getElementById("notice-vanish");
var pClassName = "";
for (var i = 0; i < 10; i += 1) {
var e = document.getElementById("notice-" + i);
if (! e) continue;
if (i == n) {
e.className = "active";
pClassName = "notice";
} else {
e.className = "";
}
}
if (p) p.className = pClassName;
}
function notice(n) {
clearTimeout(notice_timer);
notice_timer = setTimeout(function() {notice_expire()}, 8000);
notice_expire(n);
}
2011-11-14 22:41:03 -07:00
function e(id) {
2012-04-07 09:35:17 -06:00
ret = document.getElementById(id);
if (! ret) {
return Array();
}
return ret;
2011-11-14 22:41:03 -07:00
}
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;
}
/***********************************
* 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) {
tgt = e("jammer-" + team);
var on = ! tgt.className;
e("jammer-a").className = "";
e("jammer-b").className = "";
if (on) tgt.className = "lead";
}
2011-11-14 22:41:03 -07:00
function handle(event) {
var tgt = event.target || window.event.srcElement;
2011-12-08 13:20:30 -07:00
var team = tgt.id.substr(tgt.id.length - 1);
var adj = event.shiftKey?-1:1;
var mod = (event.ctrlKey || event.altKey);
var newstate;
2011-11-11 23:20:31 -07:00
2011-12-08 13:20:30 -07:00
switch (tgt.id) {
2011-11-22 00:45:52 -07:00
case "name-a":
case "name-b":
2011-12-02 17:55:47 -07:00
if (state == SETUP) {
2011-12-08 13:20:30 -07:00
var tn = prompt("Enter team " + team + " name", tgt.innerHTML);
2011-12-02 17:55:47 -07:00
if (tn) {
2011-12-08 13:20:30 -07:00
tgt.innerHTML = tn;
2011-11-23 21:43:09 -07:00
}
if (window.penalties) {
penalties_setTeamName(team, tn);
}
2011-11-22 00:45:52 -07:00
}
break;
case "logo-a":
case "logo-b":
2011-12-02 17:55:47 -07:00
if (state == SETUP) {
if (mod) {
2011-11-22 23:28:31 -07:00
var u = prompt("Enter URL to team " + team + " logo");
2011-11-23 21:43:09 -07:00
if (u) {
2011-12-08 13:20:30 -07:00
tgt.src = u;
2011-11-22 23:28:31 -07:00
}
2011-11-23 21:43:09 -07:00
} else {
2011-12-08 13:20:30 -07:00
var t;
logo[team] = (teams.length + logo[team] + adj) % teams.length;
t = teams[logo[team]];
e("name-" + team).innerHTML = t[0];
tgt.src = "logos/" + t[1];
2011-12-12 19:36:09 -07:00
if (window.penalties) {
penalties_setTeamName(team, t[0]);
}
2011-11-22 23:28:31 -07:00
}
2011-12-06 22:39:54 -07:00
} else {
score(team, -adj);
2011-11-22 00:45:52 -07:00
}
break;
2011-12-08 13:20:30 -07:00
case "jammer-a":
case "jammer-b":
leadJammer(team);
2011-12-08 13:20:30 -07:00
break;
2011-12-06 22:39:54 -07:00
case "timeouts-a":
case "timeouts-b":
// Allow for timeouts > 3
2011-12-08 13:20:30 -07:00
var v = Number(tgt.innerHTML);
v -= adj;
if (v == -1) v = 3;
2011-12-08 13:20:30 -07:00
tgt.innerHTML = v;
2011-12-06 22:39:54 -07:00
break;
2011-11-22 00:45:52 -07:00
case "period":
2011-12-02 17:55:47 -07:00
if ((state == SETUP) || (state == TIMEOUT)) {
2011-12-08 13:20:30 -07:00
var r = prompt("Enter new time for period clock", tgt.innerHTML);
2011-11-14 22:41:03 -07:00
if (! r) return;
var t = r.split(":");
var sec = 0;
if (t.length > 3) {
2011-12-08 13:20:30 -07:00
tgt.innerHTML = "What?";
2011-11-14 22:41:03 -07:00
return;
}
for (var i in t) {
var v = t[i];
sec = (sec * 60) + Number(v);
}
2011-12-08 13:20:30 -07:00
tgt.set(sec*1000);
2011-11-22 00:45:52 -07:00
} else {
newstate = TIMEOUT;
}
break;
case "periodtext":
var pt;
if (mod) {
2011-12-08 13:20:30 -07:00
pt = prompt("Enter new period indicator text", tgt.innerHTML);
} else {
var ptl = periodtext.length;
period = (period + ptl + adj) % ptl;
pt = periodtext[period];
}
2011-12-08 13:20:30 -07:00
if (pt) tgt.innerHTML = pt;
2011-11-22 00:45:52 -07:00
break;
case "jam":
if (state == JAM) {
2011-12-12 19:36:09 -07:00
newstate = LINEUP;
2011-11-22 00:45:52 -07:00
} else {
newstate = JAM;
2011-11-14 22:41:03 -07:00
}
2011-11-22 00:45:52 -07:00
break;
2012-07-06 18:30:37 -06:00
case "jamno":
jamno -= adj;
tgt.innerHTML = jamno;
break;
2011-11-22 00:45:52 -07:00
case "score-a":
case "score-b":
2011-12-07 21:47:58 -07:00
if (state == SETUP) {
2011-12-08 13:20:30 -07:00
var s = prompt("Enter score for team " + team, tgt.innerHTML);
2011-11-23 21:43:09 -07:00
if (s) {
2011-12-08 13:20:30 -07:00
tgt.innerHTML = s;
2011-11-23 21:43:09 -07:00
}
2011-11-22 00:45:52 -07:00
} else {
score(team, adj);
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-12-08 13:20:30 -07:00
function key(event) {
2011-12-20 22:38:56 -07:00
var c = String.fromCharCode(event.which || 0);
var newstate;
2011-11-15 21:56:33 -07:00
2011-12-20 22:38:56 -07:00
switch (c) {
2011-11-15 21:56:33 -07:00
case " ":
if (state == JAM) {
2011-12-12 19:36:09 -07:00
newstate = LINEUP;
2011-11-15 21:56:33 -07:00
} else {
newstate = JAM;
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 "[":
2011-11-15 21:56:33 -07:00
score('a', 1);
break;
case "b":
case "]":
2011-11-15 21:56:33 -07:00
score('b', 1);
break;
case "A":
case "{":
2011-11-15 21:56:33 -07:00
score('a', -1);
break;
case "B":
case "}":
2011-11-15 21:56:33 -07:00
score('b', -1);
break;
case ",":
leadJammer('a');
break;
case ".":
leadJammer('b');
break;
2011-12-20 22:38:56 -07:00
case "1":
case "2":
case "3":
case "4":
case "5":
case "6":
case "7":
case "8":
case "9":
case "0":
2011-12-20 22:38:56 -07:00
var n = Number(c);
window.notice(n);
2011-11-15 21:56:33 -07:00
}
transition(newstate);
2011-11-15 21:56:33 -07:00
}
2011-12-06 22:39:54 -07:00
function dfl(v, d) {
if (v == undefined) {
return d;
} else {
return v;
}
}
2012-04-07 09:35:17 -06:00
function store(k, v) {
if ((v == undefined) || ! localStorage) {
return;
} else {
localStorage["rdsb_" + k] = v;
}
}
function save() {
var ls = localStorage || {};
2012-04-07 09:35:17 -06:00
store("period_clock", e("period").remaining());
store("name_a", e("name-a").innerHTML);
store("name_b", e("name-b").innerHTML);
store("logo_a", e("logo-a").src);
store("logo_b", e("logo-b").src);
store("score_a", e("score-a").innerHTML);
store("score_b", e("score-b").innerHTML);
store("timeout_a", e("timeouts-a").innerHTML);
store("timeout_b", e("timeouts-b").innerHTML);
store("period", period);
}
2011-11-14 22:41:03 -07:00
function start() {
var p = document.getElementById("period");
var j = document.getElementById("jam");
2012-03-18 23:29:48 -06:00
var ls = localStorage || {};
2012-03-18 23:25:41 -06:00
var c;
2012-03-18 23:29:48 -06:00
// IE8 doesn't have localStorage for file:// URLs :<
e("name-a").innerHTML = dfl(ls.rdsb_name_a, "Home");
e("name-b").innerHTML = dfl(ls.rdsb_name_b, "Vis");
e("logo-a").src = dfl(ls.rdsb_logo_a, "logos/black.png");
e("logo-b").src = dfl(ls.rdsb_logo_b, "logos/white.png");
e("score-a").innerHTML = dfl(ls.rdsb_score_a, 0);
e("score-b").innerHTML = dfl(ls.rdsb_score_b, 0);
e("timeouts-a").innerHTML = dfl(ls.rdsb_timeout_a, 3);
e("timeouts-b").innerHTML = dfl(ls.rdsb_timeout_b, 3);
period = Number(ls.rdsb_period) || 0;
2012-03-18 23:25:41 -06:00
2012-03-18 23:29:48 -06:00
if (localStorage) {
2012-03-18 23:25:41 -06:00
save_itimer = setInterval(save, 1000);
}
if (window.penalties) {
penalties_init();
}
2011-11-14 22:41:03 -07:00
e("periodtext").innerHTML = periodtext[period];
2012-04-07 21:41:09 -06:00
e("jamtext").innerHTML = jamtext[3];
transition();
2011-11-14 22:41:03 -07:00
2012-03-18 23:29:48 -06:00
c = Number(ls.rdsb_period_clock || 1800000);
2011-11-22 22:45:09 -07:00
startTimer(p);
p.set(c);
2011-12-12 19:36:09 -07:00
var j = document.getElementById("jam");
2012-04-07 09:35:17 -06:00
startTimer(j, window.tenths);
2011-11-22 22:45:09 -07:00
j.set(120000);
2011-11-14 22:41:03 -07:00
2011-12-08 13:20:30 -07:00
save_timer = setInterval(save, 1000);
update_itimer = setInterval(update, 33);
2011-11-14 22:41:03 -07:00
}
window.onload = start;
2012-04-07 09:35:17 -06:00
window.tenths = true;