pngs/quiz.js

190 lines
5.4 KiB
JavaScript
Raw Normal View History

2010-09-20 02:27:28 +02:00
var keyHandler;
$(document).bind('keydown', function(event) {
if (keyHandler)
2010-09-20 03:17:57 +02:00
keyHandler(String.fromCharCode(event.keyCode).toLowerCase(), event.keyCode);
2010-09-20 02:27:28 +02:00
});
2010-09-20 00:55:40 +02:00
$(window).bind('load', function() {
$('#game').hide();
$('#scoreboard').hide();
2010-09-20 02:10:26 +02:00
loadQuizData(function() {
2010-09-20 03:17:57 +02:00
// Quiz data has initialized
$('#setup').show();
$('#start').bind('click', function() {
try {
startQuiz();
} catch (e) {
console.error(e.stack);
}
return false; // don't submit <form>
});
2010-09-20 02:10:26 +02:00
});
2010-09-20 00:55:40 +02:00
});
2010-09-20 02:10:26 +02:00
var questions;
var currentQuestion = 0;
function loadQuizData(done) {
$.ajax({ url: 'data/questions.json',
2010-09-20 03:17:57 +02:00
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);
}
});
2010-09-20 02:10:26 +02:00
}
2010-09-20 02:27:28 +02:00
var playerNames = [], playerScores = [];
2010-09-20 00:55:40 +02:00
function startQuiz() {
var i;
console.log('startQuiz');
2010-09-20 02:10:26 +02:00
questions.forEach(function(q) {
2010-09-20 03:17:57 +02:00
$('#tiers').append('<li></li>');
$('#tiers li').last().text(q.tier);
2010-09-20 02:10:26 +02:00
});
2010-09-20 00:55:40 +02:00
2010-09-20 02:10:26 +02:00
for(i = 0; i < 5; i++) {
2010-09-20 03:17:57 +02:00
var name = $('#playername' + i).val();
if (name) {
playerNames[i] = name;
playerScores[i] = 0;
$('#scoreboard dl').append('<dt></dt><dd>0</dd>');
$('#scoreboard dl dt').last().text(name);
$('#players').append('<li class="player'+i+'"><span class="name"></span><span class="score">0</span></li>');
$('#players li.player'+i+' span.name').text(name);
}
2010-09-20 02:10:26 +02:00
}
2010-09-20 02:27:28 +02:00
$('#setup').fadeOut(700, function() {
2010-09-20 03:17:57 +02:00
switchToScoreboard();
2010-09-20 02:10:26 +02:00
});
2010-09-20 00:55:40 +02:00
}
2010-09-20 02:10:26 +02:00
function switchToScoreboard() {
2010-09-20 02:27:28 +02:00
keyHandler = function(key) {
2010-09-20 03:17:57 +02:00
if (key === ' ') {
$('#scoreboard').fadeOut(500, function() {
switchToGame();
});
}
2010-09-20 02:27:28 +02:00
};
$('#scoreboard').fadeIn(300);
}
2010-09-20 03:23:13 +02:00
function updateScores() {
for(var i = 0; i < playerNames.length; i++) {
2010-09-21 02:15:20 +02:00
if (playerNames[i]) {
// FIXME: eq(i) is bad when first player is empty
$('#scoreboard dl dd').eq(i).text(playerScores[i]);
$('#players .player'+i+' .score').text(playerScores[i]);
}
2010-09-20 03:23:13 +02:00
}
}
2010-09-20 02:27:28 +02:00
// Game screen is the one with the question in question
function switchToGame() {
var i, q = questions[currentQuestion];
2010-09-21 02:07:27 +02:00
var activePlayer = null, choice = null; // can be null
2010-09-20 02:27:28 +02:00
2010-09-20 03:17:57 +02:00
var updateTier = function() {
var s = q.tier;
if (activePlayer !== null)
s += ' — ' + playerNames[activePlayer];
$('#tier').text(s);
};
updateTier();
2010-09-20 02:27:28 +02:00
$('#question').empty();
if (q.text) {
2010-09-20 03:17:57 +02:00
$('#question').append('<p></p>');
$('#question p').text(q.text);
2010-09-20 02:27:28 +02:00
}
2010-09-21 01:37:51 +02:00
if (q.image) {
$('#question').append('<img>');
$('#question img').attr('src', q.image);
2010-09-21 01:37:51 +02:00
}
if (q.video) {
$('#question').append('<video controls autoplay>');
$('#question video').attr('src', q.video);
2010-09-21 01:37:51 +02:00
}
2010-09-20 02:27:28 +02:00
for(i = 0; i < 4; i++) {
2010-09-20 03:17:57 +02:00
var answer = q.answers[i];
var liEl = $('#answers li').eq(i);
liEl.text(answer.text);
liEl.removeClass('selected right wrong');
2010-09-20 02:27:28 +02:00
}
2010-09-20 03:17:57 +02:00
keyHandler = function(key, keyCode) {
if (keyCode === 27) {
2010-09-20 03:17:57 +02:00
// Shortcut: cancel this state
$('#game').hide();
switchToScoreboard();
} else if (activePlayer === null &&
key > 0 && key <= playerNames.length) {
// No active player yet, but somebody hit a button!
2010-09-21 02:07:27 +02:00
var player = parseInt(key, 10) - 1; // operating 0-indexed
if (playerNames[player]) {
activePlayer = player;
updateTier();
}
2010-09-20 03:17:57 +02:00
} else if (activePlayer !== null &&
"abcd".indexOf(key) >= 0) {
// player pronounced the answer
if (choice !== null)
$('#answer' + choice).removeClass('selected');
choice = "abcd".indexOf(key);
$('#answer' + choice).addClass('selected');
} else if (activePlayer !== null &&
keyCode === 13) {
// player confirmed answer or gave up
var answerEl;
2010-09-21 02:07:27 +02:00
if (choice !== null) {
answerEl = $('#answer' + choice);
answerEl.removeClass('selected');
}
2010-09-20 03:17:57 +02:00
var isRight = choice !== null && q.answers[choice].right === true;
if (isRight) {
playerScores[activePlayer] += q.tier;
updateScores();
2010-09-21 02:07:27 +02:00
} else if (choice !== null) {
// Hilight the wrong choice
answerEl.addClass('wrong');
}
// Hilight all right choices
var i = 0;
q.answers.forEach(function(answer) {
if (answer.right === true)
$('#answer' + i).addClass('right');
i++;
});
keyHandler = function(key) {
if (key === " ") {
// next question:
currentQuestion++;
$('#game').fadeOut(500, function() {
switchToScoreboard();
});
2010-09-20 03:17:57 +02:00
}
};
}
2010-09-20 02:27:28 +02:00
};
// Instantly show the question:
$('#game').show();
}