-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
187 lines (164 loc) · 5.56 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
players = []
scores = {}
var sheetId = '1_tPHJtHXFOisVWk5H3WKrg1wsy30SHy_5XDQ35bHmgo';
var round = 1;
var visualization;
google.load('visualization', '1', {
packages: ['table']
});
function init() {
$("#board").hide();
$("#next-round").hide();
handleSetup(function() {
loadData();
});
}
function handleSetup(callback) {
// todo validate stuff
$("#start").click(function (e) {
// assigns inputs to global vars cause idgaf
var setup = $("#setup");
setup.find("#player-setup input").each(function(e) {
players.push($(this).val());
});
players.forEach(function(p) {
scores[p] = 0;
}, this);
var re = /spreadsheets\/d\/([^\/]+)/
var sheetUrl = setup.find("#sheet").val();
sheetId = re.exec(sheetUrl)[1];
$('#setup').hide();
callback();
});
}
function displayScores() {
var scoreDiv = $("#scores")
scoreDiv.text("")
var newText = "";
for (var key in scores) {
newText += '<div class="blue score" id="' + key + '_score">' + key + " = " + scores[key] + "</div>";
}
scoreDiv.append(newText)
}
function scoreIt(player, score) {
scores[player] += score;
displayScores();
maybeShowNextRound();
}
function maybeShowNextRound(board) {
board = board || $("#board");
if (board.find(".clicked").length == 30 && round == 1) {
$("#next-round").show();
}
}
function getRound() {
return round;
}
function getSheet() {
if (getRound() == 1) {
return "Round 1"
} else {
return "Round 2"
}
}
function loadData() {
var query = new google.visualization.Query('https://spreadsheets.google.com/tq?key=' + sheetId + '&sheet='+ getSheet() + '&output=html&usp=sharing');
query.setQuery('SELECT A, B, C label A "Topic", B "Question", C "Answer"');
query.send(handleQueryResponse);
}
function handleQueryResponse(response) {
console.log("handle response")
if (response.isError()) {
alert('There was a problem with your query: ' + response.getMessage() + ' ' + response.getDetailedMessage());
return;
}
var data = response.getDataTable();
var topics = data.getDistinctValues("Topic")
var tQuestions = {};
for (var i = 0; i < data.getNumberOfRows(); i++) {
var question = data.getValue(i, 1) ;
var answer = data.getValue(i, 2);
var currentTopic = data.getValue(i, 0);
if (question != null) {
if (currentTopic) {
topic = currentTopic;
}
var answers = tQuestions[topic]
if (!answers) {
tQuestions[topic] = [{"q": question, "a": answer}]
} else {
tQuestions[topic].push({"q": question, "a": answer})
}
}
}
var board = $("#board")
board.text("");
var topicHtml = '<div class="row">';
for(id in topics) {
var topic = topics[id];
if (topic != null) {
topicHtml += '<div class="col-1 blue">' + topics[id] + '</div>';
}
}
board.append(topicHtml);
for (i = 0; i < 5; i++) {
var score = (i + 1) * 100 * round;
var rowHtml = '<div class="row">';
for (t in topics) {
var topic = topics[t];
if (topic == null) {
continue;
}
qna = tQuestions[topic][i];
if (qna) {
var xIt = '<div class="control"><button id="' + t + '_' + i + '_cancel">Return</button>'
xIt += '<button id="' + t + '_' + i + '_no-answer">No answer</button></div>'
var scoreIt = '<br/><br/><br/><div class="ok">Correct! ->';
players.forEach(function(player) {
scoreIt += '<button onClick=\'scoreIt(\"' + player + '\",' + score + ')\' id="' + t + '_' + i + '_' + player + '_ok">' + player + '</button>'
}, this);
scoreIt += "</div>"
scoreIt += '<div class="not-ok">Wrong! ->';
players.forEach(function(player) {
scoreIt += '<button onClick=\'scoreIt(\"' + player + '\",-' + score + ')\' id="' + t + '_' + i + '_' + player + '_not-ok">' + player + '</button>'
}, this);
scoreIt += "</div>"
questionDiv = '<div id="' + t + '_' + i + '_question" class="question">' + qna.q + scoreIt + xIt + '</div>'
rowHtml += '<div id="' + t + '_' + i + '" class="col-1 blue score">' + score + '</div>' + questionDiv;
}
}
board.append(rowHtml);
}
$(".question").hide();
$(".score").click(function() {
var rootId = this.id;
var rootElement = $("#" + this.id);
var question = $("#" + rootId + "_question");
question.show();
$("#" + rootId + "_question .ok").click(function(e) {
rootElement.addClass("clicked")
question.hide();
maybeShowNextRound(board);
})
$("#" + rootId + "_cancel").click(function(e) {
question.hide();
});
$("#" + rootId + "_no-answer").click(function(e) {
rootElement.addClass("clicked")
question.hide();
maybeShowNextRound(board);
});
})
$("#next-round").hide();
if (round == 2) {
$("#next-round").text("")
} else {
$("#next-round").click(function (e) {
round+=1;
$("#table").text("")
loadData();
})
}
board.show();
displayScores();
}