if (!window.console) { var stub = function() { }; window.console = { log: stub, error: stub, warn: stub }; } var keyHandler; $(document).bind('keydown', function(event) { console.log('cc: '+event.charCode+'/'+String.fromCharCode(event.keyCode).toLowerCase()+' kc: '+event.keyCode); if (keyHandler) keyHandler(String.fromCharCode(event.keyCode).toLowerCase(), event.keyCode); }); $(window).bind('load', function() { $('#game').hide(); $('#scoreboard').hide(); loadQuizData(function() { // Quiz data has initialized $('#setup').show(); $('#start').bind('click', function() { try { startQuiz(); } catch (e) { console.error(e.stack); } return false; // don't submit
}); }); }); var questions; var currentQuestion = 0; function loadQuizData(done) { $.ajax({ url: 'data/questions.json', contentType: 'json', success: function(data, status) { if (typeof data === 'string') data = JSON.parse(data); console.log(status); questions = data; done(); }, error: function(req, status, e) { console.error(status); console.log(e.stack); } }); } var ws, sendToBackend, onBackendMessage; function setupWs() { var url = 'ws://' + document.location.host + '/'; ws = new WebSocket(url, '*'); ws.onerror = function(e) { console.error(e.message); setupWs(); }; ws.onclose = function() { console.error('WebSocket closed'); setupWs(); }; ws.onmessage = function(event) { try { var data = event.data; console.log({fromBackend: data}); var msg = JSON.parse(data); if (onBackendMessage) onBackendMessage(msg); } catch(e) { console.error(e.message); } }; sendToBackend = function(msg) { console.log('toBackend: ' + JSON.stringify(msg)); ws.send(JSON.stringify(msg)); }; ws.onopen = function() { /* TODO: rm debug */ sendToBackend({ nedap: "ping" }); }; } setupWs(); function Timer() { $('#timer').hide(); this.cb = null; } Timer.prototype.set = function(t, cb) { var that = this; this.clear(); var tick = function() { if (t > 0) { t--; $('#timer').text('' + t); } else { that.halt(); $('#timer').addClass('elapsed'); cb(); } }; this.interval = window.setInterval(tick, 1000); /* appear: */ tick(); $('#timer').fadeIn(1000); }; Timer.prototype.halt = function() { if (this.interval) window.clearInterval(this.interval); }; Timer.prototype.clear = function() { this.halt(); delete this.interval; $('#timer').removeClass('elapsed'); $('#timer').hide(); }; var TIMER_QUESTION = 90; var TIMER_ANSWER = 60; var timer = new Timer(); var playerNames = [], playerScores = [], playerJokers = []; function startQuiz() { var i; console.log('startQuiz'); questions.forEach(function(q) { $('#tiers').append('
  • '); $('#tiers li').last().text(q.tier); }); for(i = 0; i < 5; i++) { var name = $('#playername' + i).val(); if (name) { playerNames[i] = name; playerScores[i] = 0; $('#scoreboard dl').append('
    0
    '); $('#scoreboard dl dt').last().text(name); $('#players').append('
  • 0
  • '); $('#players li.player'+i+' span.name').text(name); } } $('#setup').fadeOut(700, function() { switchToScoreboard(); }); } function switchToScoreboard() { timer.clear(); keyHandler = function(key) { if (key === ' ' && currentQuestion < questions.length) { $('#scoreboard').fadeOut(500, function() { switchToGame(); }); } }; for(var i = 0; i < currentQuestion; i++) { $('#tiers li').eq(i).addClass('done'); } $('#scoreboard').fadeIn(300); } function updateScores() { for(var i = 0; i < playerNames.length; i++) { if (playerNames[i]) { // FIXME: eq(i) is bad when first player is empty $('#scoreboard dl dd').eq(i).find('.score').text(playerScores[i]); $('#players .player'+i+' .score').text(playerScores[i]); } } } function takeJoker(activePlayer, joker) { if (activePlayer === null) // No active player return; if (!playerJokers.hasOwnProperty(activePlayer)) playerJokers[activePlayer] = {}; if (playerJokers[activePlayer][joker]) // Joker already taken return; playerJokers[activePlayer][joker] = true; $('#tier').append(''); $('#scoreboard dd').eq(activePlayer).find('.' + joker).remove(); if (joker === 'fiftyfifty') { var h1, h2, answers = questions[currentQuestion].answers; do { h1 = Math.floor(Math.random() * 4); h2 = Math.floor(Math.random() * 4); } while(answers[h1].right || answers[h2].right || h1 === h2); $('#answer' + h1).fadeTo(500, 0.1); $('#answer' + h2).fadeTo(500, 0.1); } if (joker === 'nedap') { var q = questions[currentQuestion]; sendToBackend({ nedap: { joker: { question: q.text, answers: q.answers } } }); $('#nedap').show(); var scores = [0, 0, 0, 0]; var redraw = function() { var canvas = $('#polls')[0]; var w = canvas.width, h = canvas.height; var ctx = canvas.getContext('2d'); ctx.fillStyle = '#20203f'; ctx.fillRect(0, 0, w, h); var total = 0; for(var i = 0; i < scores.length; i++) { total += scores[i]; } if (total < 1) total = 1; for(var i = 0; i < scores.length; i++) { /* Bounds */ var x1, y1, x2, y2; if (i == 0 || i == 2) { x1 = w * 0.05; x2 = w * 0.45; } if (i == 1 || i == 3) { x1 = w * 0.55; x2 = w * 0.95; } if (i == 0 || i == 1) { y1 = h * 0.05; y2 = h * 0.45; } if (i == 2 || i == 3) { y1 = h * 0.55; y2 = h * 0.95; } /* Fill */ ctx.fillStyle = '#ccc'; var barHeight = (y2 - y1) * scores[i] / total; console.log({x1:x1,y1:y1,x2:x2,y2:y2,barHeight:barHeight}); ctx.fillRect(x1, y2 - barHeight, x2 - x1, barHeight); /* Outline */ ctx.strokeStyle = 'white'; ctx.beginPath(); ctx.moveTo(x1, y1); ctx.lineTo(x2, y1); ctx.lineTo(x2, y2); ctx.lineTo(x1, y2); ctx.lineTo(x1, y1); ctx.stroke(); } }; onBackendMessage = function(msg) { if (msg.nedap && msg.nedap.scores) scores = msg.nedap.scores; console.log('scores: '+JSON.stringify(scores)); redraw(); }; } } function setQuestionContents(q) { $('#question').empty(); if (q.text) { $('#question').append('

    '); $('#question p').text(q.text); } if (q.image) { $('#question').append(''); $('#question img').attr('src', q.image); } if (q.video) { $('#question').append('