|
| 1 | +var trivia = { |
| 2 | + questions: [], |
| 3 | + currentQuestion: {}, |
| 4 | + questionIndex: 0, |
| 5 | + totalQuestions: 0, |
| 6 | + totalCorrect: 0, |
| 7 | + totalAnswered: 0, |
| 8 | + state: "welcome", |
| 9 | + loadGoogleSheet: function(link) { |
| 10 | + return new Promise((resolve, reject) => { |
| 11 | + Tabletop.init({ |
| 12 | + key: link, |
| 13 | + callback: data => { |
| 14 | + this.questions = data.Sheet1.elements; |
| 15 | + this.questions = shuffle(this.questions); |
| 16 | + if (this.questions.length) this.currentQuestion = this.questions[0]; |
| 17 | + this.questionIndex = 0; |
| 18 | + this.totalQuestions = this.questions.length; |
| 19 | + this.totalCorrect = 0; |
| 20 | + this.totalAnswered = 0; |
| 21 | + this.state = "welcome"; |
| 22 | + this.startClickListeners(); |
| 23 | + resolve("success"); |
| 24 | + } |
| 25 | + }); |
| 26 | + }); |
| 27 | + }, |
| 28 | + advanceQuestion: function() { |
| 29 | + trivia.questionIndex++; |
| 30 | + trivia.state = "question"; |
| 31 | + if (trivia.questions[trivia.questionIndex]) |
| 32 | + trivia.currentQuestion = trivia.questions[trivia.questionIndex]; |
| 33 | + }, |
| 34 | + gotoNextQuestion: function() { |
| 35 | + trivia.advanceQuestion(); //advance counter to the next question |
| 36 | + $("button").attr("disabled", null); |
| 37 | + if (trivia.totalQuestions > trivia.questionIndex) displayQuestion(); |
| 38 | + else { |
| 39 | + displayThankyou(); //game over |
| 40 | + trivia.state = "thankyou"; |
| 41 | + } |
| 42 | + }, |
| 43 | + insertQuestionInfo: function() { |
| 44 | + for (var prop in trivia.currentQuestion) { |
| 45 | + $("#" + prop).html(trivia.currentQuestion[prop]); |
| 46 | + } |
| 47 | + }, |
| 48 | + shuffleAnswers: function() { |
| 49 | + $("#answer-set").html(shuffle($("#answer-set").children())); //shuffle answers |
| 50 | + }, |
| 51 | + startClickListeners: function() { |
| 52 | + //listen for answer button clicks |
| 53 | + $(".screen").on("click", ".answer-btn", ev => { |
| 54 | + $("button").attr("disabled", "disabled"); //turn off buttons to prohibit cheating :) |
| 55 | + if ($(ev.target).is("#correctAnswer")) { |
| 56 | + trivia.currentQuestion.response = "correct"; |
| 57 | + trivia.state = "correct"; |
| 58 | + } else { |
| 59 | + trivia.currentQuestion.response = "incorrect"; |
| 60 | + trivia.state = "incorrect"; |
| 61 | + } |
| 62 | + trivia.totalCorrect = trivia.questions.filter(item => { |
| 63 | + return item.response == "correct"; |
| 64 | + }).length; |
| 65 | + trivia.totalAnswered = trivia.questions.filter(item => { |
| 66 | + return item.response; |
| 67 | + }).length; |
| 68 | + onClickedAnswer(trivia.currentQuestion.response == "correct"); |
| 69 | + }); |
| 70 | + |
| 71 | + //listen for restart button click |
| 72 | + $(".screen").on("click", ".start-btn", ev => { |
| 73 | + trivia.questionIndex = 0; |
| 74 | + trivia.state = "question"; |
| 75 | + trivia.currentQuestion = trivia.questions[0]; //reset to the first question |
| 76 | + onClickedStart(); |
| 77 | + }); |
| 78 | + } |
| 79 | +}; |
0 commit comments