11var trivia = {
22 questions : [ ] ,
3+ currentCategory : null ,
4+ categoriesEnabled : false ,
35 currentQuestion : { } ,
46 questionIndex : 0 ,
57 totalQuestions : 0 ,
68 totalCorrect : 0 ,
79 totalAnswered : 0 ,
810 state : "welcome" ,
9- loadGoogleSheet : function ( link ) {
11+ loadGoogleSheet : function ( link ) {
1012 return new Promise ( ( resolve , reject ) => {
1113 Tabletop . init ( {
1214 key : link ,
@@ -25,33 +27,71 @@ var trivia = {
2527 } ) ;
2628 } ) ;
2729 } ,
28- advanceQuestion : function ( ) {
29- trivia . questionIndex ++ ;
30+ getCategories : function ( ) {
31+ var cats = [ ] ;
32+ this . questions . filter ( ( q ) => {
33+ if ( ! cats . includes ( q . category ) ) cats . push ( q . category ) ;
34+ } ) ;
35+ return cats ;
36+ } ,
37+ getUnfinishedCategories : function ( ) {
38+ var cats = [ ] ;
39+ this . questions . filter ( ( q ) => {
40+ if ( ! cats . includes ( q . category ) && ! q . response ) cats . push ( q . category ) ;
41+ } ) ;
42+ return cats ;
43+ } ,
44+ gotoNextQuestion : function ( ) { //this just forwards a "deprecated function"
45+ displayQuestion ( ) ;
46+ } ,
47+ insertCategoriesInfo : function ( ) {
48+ var cats = this . getCategories ( ) ;
49+ var unfcats = this . getUnfinishedCategories ( ) ;
50+ $ ( '#category-set' ) . html ( '' ) ;
51+ cats . forEach ( ( c ) => {
52+ var $catbtn = $ ( `<button class="category-btn">${ c } </button>` ) ;
53+ if ( unfcats . includes ( c ) ) {
54+ $catbtn . on ( 'click' , function ( e ) {
55+ trivia . currentCategory = c ;
56+ onClickedCategory ( ) ;
57+ } ) ;
58+ }
59+ else $catbtn . attr ( "disabled" , true ) ;
60+ $ ( '#category-set' ) . append ( $catbtn ) ;
61+ } )
62+ } ,
63+ insertQuestionInfo : function ( ) {
3064 trivia . state = "question" ;
31- if ( trivia . questions [ trivia . questionIndex ] )
65+ $ ( ".answer-btn" ) . attr ( "disabled" , null ) ;
66+ trivia . questionIndex = trivia . questions . findIndex ( ( q ) => {
67+ if ( ! this . categoriesEnabled ) return ! q . response ;
68+ else return ! q . response && q . category == this . currentCategory ;
69+ } ) ;
70+ if ( trivia . questions [ trivia . questionIndex ] ) {
3271 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" ;
72+ for ( var prop in trivia . currentQuestion ) {
73+ $ ( "#" + prop ) . html ( trivia . currentQuestion [ prop ] ) ;
74+ }
4175 }
42- } ,
43- insertQuestionInfo : function ( ) {
44- for ( var prop in trivia . currentQuestion ) {
45- $ ( "#" + prop ) . html ( trivia . currentQuestion [ prop ] ) ;
76+ else {
77+ if ( this . totalAnswered == this . totalQuestions ) {
78+ trivia . state = "thankyou" ;
79+ displayThankyou ( ) ; //game over
80+ }
81+ else if ( this . categoriesEnabled ) {
82+ trivia . state = "categories" ;
83+ displayCategories ( ) ;
84+ }
85+ else alert ( 'Yikes' ) ;
4686 }
4787 } ,
48- shuffleAnswers : function ( ) {
88+ shuffleAnswers : function ( ) {
4989 $ ( "#answer-set" ) . html ( shuffle ( $ ( "#answer-set" ) . children ( ) ) ) ; //shuffle answers
5090 } ,
51- startClickListeners : function ( ) {
91+ startClickListeners : function ( ) {
5292 //listen for answer button clicks
5393 $ ( ".screen" ) . on ( "click" , ".answer-btn" , ev => {
54- $ ( "button " ) . attr ( "disabled" , "disabled" ) ; //turn off buttons to prohibit cheating :)
94+ $ ( ".answer-btn " ) . attr ( "disabled" , "disabled" ) ; //turn off buttons to prohibit cheating :)
5595 if ( $ ( ev . target ) . is ( "#correctAnswer" ) ) {
5696 trivia . currentQuestion . response = "correct" ;
5797 trivia . state = "correct" ;
@@ -70,9 +110,11 @@ var trivia = {
70110
71111 //listen for restart button click
72112 $ ( ".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
113+ this . questions . forEach ( function ( q ) { delete q . response } ) ;
114+ trivia . questionIndex = 0 ;
115+ if ( ! this . categoriesEnabled ) trivia . state = "question" ;
116+ else trivia . state = "categories" ;
117+ trivia . currentQuestion = trivia . questions [ 0 ] ; //reset to the first question
76118 onClickedStart ( ) ;
77119 } ) ;
78120 }
0 commit comments