-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
277 lines (255 loc) · 8.26 KB
/
app.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Hey there! Curious on how this app worked? Currently attending Varsity?
// Why don't you join the Varsity Coders team! Find out more at
// https://vrsty.info/v-coders
function groupBy(array, key) {
grouped = {};
array.forEach(function (item) {
if (item[key] in grouped) {
grouped[item[key]].push(item);
} else {
grouped[item[key]] = [item];
}
});
return grouped;
}
// https://stackoverflow.com/a/2450976
function shuffle(array) {
let currentIndex = array.length,
temporaryValue,
randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
// https://docs.google.com/spreadsheets/d/e/2PACX-1vTxLC2gDcl3J05egvdWgEt9Jgc5BC299444cf9diFbkD3i5eYcWnUjPHsfwysPIL
let questionsApi = "questions.json";
let tagsApi = "tags.json";
let teamTagsApi = "teamTags.json";
let teamsApi = "teams.json";
function getQuestions() {
return axios.get(questionsApi);
}
function getTags() {
return axios.get(tagsApi);
}
function getTeamTags() {
return axios.get(teamTagsApi);
}
function getTeams() {
return axios.get(teamsApi);
}
console.log("Hey you! Yes, you there, reading the console log!");
console.log(
"If you're attending Varsity, the V Coders are inviting you to join us!"
);
console.log("Visit https://vrsty.info/v-coders for more information!");
axios
.all([getQuestions(), getTags(), getTeamTags(), getTeams()])
.then(
axios.spread(function (questionsResp, tagsResp, teamTagsResp, teamsResp) {
// prepare questions
questionsResponse = groupBy(questionsResp.data.rows, "question");
questions = Object.keys(questionsResponse).map(function (key) {
answers = questionsResponse[key];
imagesCount = 0;
answers.forEach(function (answer) {
if (answer.imageurl) {
imagesCount++;
}
});
return {
question: key,
answers: shuffle(
answers.map(function (answer) {
return {
ans: answer.answer,
tag: answer.tag,
imageurl: answer.imageurl,
};
})
),
hasImages: imagesCount > 0,
option: 0,
selection: null,
};
});
// prepare teams
teamsByTagsGrouped = groupBy(teamTagsResp.data.rows, "tag");
let teamsByTags = {};
Object.keys(teamsByTagsGrouped).forEach(function (tag) {
teams = teamsByTagsGrouped[tag];
teamsByTags[tag] = teams.map(function (team) {
return team.team;
});
});
let teamsData = groupBy(teamsResp.data.rows, "team");
// prepare tags
let tagsData = groupBy(tagsResp.data.rows, "tag");
// the app
new Vue({
el: "#app",
data: {
questions: questions,
name: "",
teamOpened: null,
dark: false,
},
mounted: function () {
document.getElementById("loading-btn").classList.add("hidden");
document.querySelectorAll("ellipse, path").forEach(function(item) {
item.setAttribute("stroke", "#F7F7F7");
});
document.querySelectorAll("ellipse, path").forEach(function(item, index) {
setTimeout(function() {
item.setAttribute("stroke", "rgb(25,25,27)");
}, 50 * (index + 1));
});
document.getElementById("start").classList.remove("hidden");
document.getElementById("quiz").classList.remove("hidden");
document.querySelectorAll("button").forEach(function(item) {
item.classList.add("border-black");
});
},
computed: {
tags: function () {
let total = {};
this.questions.forEach(function (question) {
if (question.option == 0 || question.option == "No Tag") {
//nothing
} else if (question.option in total) {
total[question.option] = total[question.option] + 1;
} else {
total[question.option] = 1;
}
});
return Object.keys(total)
.map(function (key) {
return [key, total[key]];
})
.sort(function (a, b) {
return b[1] - a[1];
})
.map(function (a) {
tagKey = a[0];
tagResult = tagsData[tagKey][0];
tagResult["score"] = total[tagKey];
return tagResult;
});
},
tagResults: function () {
let tagResults = structuredClone(this.tags.slice(0,4));
let randomInt = Math.floor(Math.random() * 100)
if (tagResults.length > 3) {
if (randomInt % 2 === 0) {
tagResults[0]["you"] = tagResults[0]["or"]
tagResults[2]["you"] = tagResults[2]["or"]
} else {
tagResults[1]["you"] = tagResults[1]["or"]
tagResults[3]["you"] = tagResults[3]["or"]
}
}
return tagResults;
},
teams: function () {
// find all the possible teams
teams = {};
this.tags
.filter(function (tag) {
return tag.tag != "No Tag";
})
.forEach(function (tag) {
tagKey = tag.tag;
if (!(tagKey in teamsByTags)) {
// skip
}
teamsByTags[tagKey].forEach(function (team) {
if (team in teams) {
teams[team] = teams[team] + tag.score;
} else {
teams[team] = tag.score;
}
});
});
teamsSorted = [];
for (team in teams) {
teamsSorted.push([team, teams[team]]);
}
return teamsSorted
.sort(function (a, b) {
return b[1] - a[1];
})
.map(function (team) {
return team[0];
})
.slice(0, 2);
},
teamResults: function () {
return this.teams.map(function (team) {
return teamsData[team][0];
});
},
teamOthers: function () {
teamsChosen = this.teams;
return teamsResp.data.rows.filter(function (team) {
return !teamsChosen.includes(team.team);
});
},
},
methods: {
goToNext: function (index) {
nextIndex = index + 1;
document
.getElementById("question-" + nextIndex)
.scrollIntoView({ behavior: "smooth", block: "start" });
},
goToPrev: function (index) {
nextIndex = index - 1;
document
.getElementById("question-" + nextIndex)
.scrollIntoView({ behavior: "smooth", block: "start" });
},
selectAns: function (question, tag, index) {
question.option = tag;
question.selection = index;
},
goToResults: function () {
document
.getElementById("results").classList.remove('hidden');
document
.getElementById("results")
.scrollIntoView({ behavior: "smooth", block: "start" });
i = 1;
this.teams.forEach(function (team) {
ga("send", "event", "Quiz", "Result Team", team + " - " + i);
i++;
});
},
flickAppSwitch: function() {
if (dark) {
dark = false;
} else {
dark = true;
}
},
openTeam: function (index) {
if (this.teamOpened == index) {
this.teamOpened = null;
} else {
this.teamOpened = index;
}
},
},
});
})
)
.catch(function (error) {
console.log(error);
});