function showFSATeamCountTroll() { var fsatctel = document.getElementById('fsateamcounttroll') fsatctel.innerHTML = (state.fsaTeamCountTroll + ' teams have already answered this question') } function updateFSATeamCountTroll() { if (state.fsaTeamCountTroll > (128*Math.random())) { showFSATeamCountTroll() return } // `|| state.submitTime` is a workaround for saved Qs that don't have a time set yet var time = state.questions[state.currentQuestion].time || state.submitTime var timeratio = 1 - (state.submitTimer / time) var slowAnswerChance = Math.random()*2*Math.pow(timeratio, 2) var quickAnswerChance = (Math.random()+Math.sqrt(69/time))/4 var chance = Math.round(slowAnswerChance + quickAnswerChance) var magnitude = Math.round((Math.random()/2+timeratio)*6) var nt = chance * magnitude state.fsaTeamCountTroll += nt showFSATeamCountTroll() } function updateTotalTimer() { var timerEls = document.querySelectorAll('.totaltimer') for (timerEl of timerEls) timerEl.innerHTML = 'Total time: ' + formatTime(state.totalTimer); if (!state.waitNextQuestion) state.totalTimer++ localStorage.setItem('state', JSON.stringify(state)) if (state.style == 'FSA') updateFSATeamCountTroll() } function startTotalTimer() { console.log('Setting totalTimer to ' + state.totalTimer + ' seconds.') updateTotalTimer() state.totalInterval = setInterval(updateTotalTimer, 1000) } function skipWaitNextQuestion(event) { if (!event.ctrlKey || !event.shiftKey) return console.log('Skipping/Bypassing wait timer') state.submitTimer = 0 updateSubmitTimer() } function updateSubmitInfo() { var button = document.getElementById('quizSubmitButton') var si = document.getElementById('submitinfo') if (getRule('submitTimeout') || state.waitNextQuestion) { if (state.submitTimer > 0) { si.innerHTML = state.waitNextQuestion ? 'Waiting for next question' : 'Wait to retry' button.value = 'Wait ' + formatTime(state.submitTimer) button.readOnly = true button.addEventListener('click', skipWaitNextQuestion) return } } si.innerHTML = '' button.readOnly = false button.removeEventListener('click', skipWaitNextQuestion) if (getRule('questionTimeout')) { if (state.submitTimer > 0) { if (getRule('allowQOvertime')) si.innerHTML = ('Losing bonus points in ' + formatTime(state.submitTimer)) else si.innerHTML = ('Forced hand-in in ' + formatTime(state.submitTimer)) } else { if (getRule('allowQOvertime')) document.getElementById('submitinfo').innerHTML = 'Bonus points lost.' } } var lastQuestion = (state.currentQuestion == (state.questions.length - 1)) button.value = lastQuestion || !getRule('sequential') ? 'Submit Answers' : 'Next Question' } function updateSubmitTimer() { if (state.submitTimer > 0) state.submitTimer -= 1 if (state.submitTimer == 0) { clearInterval(state.submitInterval) if (getRule('questionTimeout')) if (state.waitNextQuestion || !getRule('allowQOvertime')) submitQuiz() // Force next question } updateSubmitInfo() } function startSubmitTimer(time) { state.submitTimer = time || state.questions[state.currentQuestion].time || state.submitTime if (state.submitTimer == null) return console.log('Setting submitTimer to ' + state.submitTimer + ' seconds.') updateSubmitTimer() state.submitInterval = setInterval(updateSubmitTimer, 1000) } function updateSequentialQuestion() { var questions = document.querySelectorAll('.question') for (q of questions) q.style.display = null updateSubmitInfo() if (state.currentQuestion !== null && !state.waitNextQuestion) { var q = document.querySelector('#quiz form #question' + state.currentQuestion) q.style.display = 'block' } } function nextQuestion() { // Save time taken state.questions[state.currentQuestion].timeTaken = state.totalTimer - state.questionStartTotalTimer // Last question if (state.currentQuestion == (state.questions.length - 1)) { state.currentQuestion = null updateSequentialQuestion() return } // Waiting for next question if (getRule('questionTimeout') && (state.submitTimer > 0)) { state.waitNextQuestion = true; updateSequentialQuestion() // hide question return } state.waitNextQuestion = false if (state.style == 'FSA') { state.fsaTeamCountTroll = 0 showFSATeamCountTroll() } // Start next question state.questionStartTotalTimer = state.totalTimer state.currentQuestion++ updateSequentialQuestion() if (getRule('questionTimeout')) startSubmitTimer() } function renderQuiz() { var quizForm = document.querySelector('#quiz form') quizForm.className = '' if (getRule('sequential')) quizForm.classList.add('sequential') quizForm.innerHTML = '' for (const [i, q] of state.questions.entries()) { var html = `
Source/Author: ${q.author || '[No source/author provided]'}
${q.explanation || '[No explanation provided]'}
` el.appendChild(meta) var qinfo = (value.correct) ? '✅ Correct' : '❌ Incorrect' if (q.timeTaken) { qinfo += ('. Time taken: ' + formatTime(q.timeTaken)) if (value.correct && getRule('allowQOvertime')) qinfo += (', Bonus points ' + ((q.timeTaken <= q.time) ? 'received' : 'lost')) else if (q.timeTaken >= (q.time - 1) && !getRule('allowQOvertime')) qinfo += ', Time ran out' } el.querySelector('h3').innerHTML += ` ` } document.querySelector('#fsateamcounttroll').innerHTML = '' document.querySelector('#submitinfo').innerHTML = '' document.querySelector('#quizSubmitButton').value = 'Back' } function mergeKeyReducer(acc, entry) { if (!acc[entry[0]]) acc[entry[0]] = {answers: [], correct: false} acc[entry[0]].answers.push(entry[1]) return acc } function submitQuiz() { // If not running, we're most likely in review mode. Just switch back. if (state.running == false) { changeView('postscreen') return } if (getRule('sequential')) { nextQuestion() // Not at the end of the quiz yet if (state.currentQuestion !== null) return } console.log('Submitting quiz. State:') console.log(state) var quizForm = document.querySelector('#quiz form') var data = new FormData(quizForm) var responses = Array.from(data.entries()).reduce(mergeKeyReducer, {}) console.log(responses) var correct = 0 state.responses = [] for (var idx = 0; idx < state.questions.length; idx++) { var value = responses['q'+idx] || {answers: [], correct: false} console.log(idx + ":", value) if (state.questions[idx].type == 'ChooseOne' || state.questions[idx].type == 'ChooseAny') { state.questions[idx].answers.sort() value.answers.sort() } var correctAnswer = JSON.stringify(state.questions[idx].answers) var givenAnswer = JSON.stringify(value.answers) console.log('Comparing: ' + givenAnswer + ' == ' + correctAnswer + '?: ' + (givenAnswer == correctAnswer)) if (givenAnswer == correctAnswer) { value.correct = true correct++ } state.responses.push(value) } console.log(state.responses) var text = '' + correct + '/' + state.questions.length + ' questions answered correctly.' state.success = (correct == state.questions.length) if (!state.success) { state.submitTry++ if (getRule('submitTries') && state.submitTry < state.submitTries) { alert(text + `\nThis was try ${state.submitTry}/${state.submitTries}\nClick OK to Try Again`) renderQuiz() if (getRule('sequential')) { state.currentQuestion = 0 updateSequentialQuestion() } if (getRule('submitTimeout')) startSubmitTimer(state.submitTime) } else { document.querySelector('#postscreen h1').innerHTML = (text + '