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 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() { 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); } } // Game screen is the one with the question in question function switchToGame() { var i, q = questions[currentQuestion]; var activePlayer = null, choice = null; // can be null var updateTier = function() { var s = q.tier; if (activePlayer !== null) s += ' — ' + playerNames[activePlayer]; $('#tier').text(s); }; updateTier(); $('#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('