-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
341 lines (313 loc) · 9.25 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
var ids = ["green", "blue", "purple", "pink", "yellow", "orange"];
var letters = ["A", "C", "K", "O", "W", "S"];
var allLetters = ["A B C D E", "F G H I J", "K L M N O", "P Q R S T", "U V W X Y Z"];
var colors = ["#c2fb73", "#8ccdfc", "#cb93f9", "#feaab1", "#fcf273", "#fb9e5a"];
var words = ["HI", "LOOK", "I", "CAN", "SPELL", "IT", "IS", "FUN", "AND", "WONDERFUL"];
var score = 0,
goalScore = 30,
index = 0,
letterIndex = 0,
wordIndex = 0,
keyNum = 4,
failCount = 0,
speed = 1500,
inSession = false,
curColor = "",
curKey = "",
prevKey = "";
var barTimer, rightKeyTimer, failTimer;
//sequences for letters mode
var mode = "letters";
var seq4 = [2, 3, 1, 0, 3, 0, 1, 2, 1, 0]; //10
var seq5 = [3, 4, 2, 0, 4, 0, 1, 2, 3, 2, 4, 3, 0]; //13
var seq6 = [3, 5, 2, 0, 5, 0, 1, 2, 3, 2, 4, 0, 4, 2, 1, 0]; //16
$(document).ready(function() {
$(document).on('touchstart touchmove mousemove', function(e) {
if (inSession == false) {
return;
}
e.preventDefault();
curColor = $("#header").attr("data-color"); //the current header color
curKey = getCurKey(e); //the current key
//spelling mode
if (mode == "spelling") {
if (curKey != false && curKey != prevKey) { //user moves to a new key
if (prevKey != "") {
resetBar(prevKey);
}
prevKey = curKey;
animateBar(curKey);
} else if (curKey == false) { //user moves to blank areas
if (prevKey != "") {
resetBar(prevKey);
}
prevKey = false;
clearTimeout(barTimer);
clearTimeout(rightKeyTimer);
}
}
//other modes
if (mode != "spelling") {
if (curKey != false && curKey != prevKey) { //user moves to a new key
prevKey = curKey;
if (curKey == curColor) { //user moves to the right key
rightKeyAction();
}
} else if (curKey == false) { //user moves to blank areas
prevKey = false;
}
}
});
$("#start").on('click', function() {
$("#start").hide();
$("#end").show();
inSession = true;
mode = $("#mode").val();
if ($("#speed").val() > 0) {
speed = $("#speed").val() * 1000;
} else {
speed = 1500;
}
//posting logs to google sheet
$.ajax({
url: "https://script.google.com/macros/s/AKfycbzcjmIPchxdXsJkEfb5S82-t98hwoxo3wNG8RPl16PCx5oOEzs/exec",
type: "post",
data: {
timestamp: $.now(),
datetime: getDatetime(),
action: "start",
data: mode + " " + keyNum + " " + speed
}
});
});
$("#end").on('click', function() {
endGame();
});
$('#mode').on('change', function(e) {
//change page layout accordding to the selected mode
if ($('#mode').val() == "spelling") {
$('#speedDiv').show();
$('#keyNumDiv').hide();
$("#yellow").show();
$("#orange").hide();
$(".key").css("width", "30%");
for (let i = 0; i < 5; i++) {
$("#" + ids[i] + "> p").text(allLetters[i]);
}
constructWord(words[0]);
$("#header").css("background-color", colors[1]);
$("#header").attr("data-color", "blue");
keyNum = 5;
goalScore = 34;
$("#score").text("Score: " + 0 + " / " + goalScore);
} else if ($('#mode').val() == "letters") {
$('#speedDiv').hide();
$('#keyNumDiv').show();
for (let i = 0; i < 6; i++) {
$("#" + ids[i] + "> p").text(letters[i]);
}
adjustKeyNum();
goalScore = 30;
$("#score").text("Score: " + 0 + " / " + goalScore);
}
});
$('#keyNum').on('change', function(e) {
adjustKeyNum();
});
});
function getDatetime() {
let currentdate = new Date();
let datetime = currentdate.getDate() + "/" +
(currentdate.getMonth() + 1) + "/" +
currentdate.getFullYear() + " " +
currentdate.getHours() + ":" +
currentdate.getMinutes() + ":" +
currentdate.getSeconds();
return datetime;
}
function rightKeyAction() {
//update score
score++;
$("#score").text("Score: " + score + " / " + goalScore);
if (score == goalScore) {
alert("Yay you win!");
endGame();
}
if (mode == "letters") {
//change header to a new color
let num = 0;
if (keyNum == 4) {
num = seq4[index % (seq4.length)];
} else if (keyNum == 5) {
num = seq5[index % (seq5.length)];
} else if (keyNum == 6) {
num = seq6[index % (seq6.length)];
}
index++;
$("#header").attr("data-color", ids[num]);
$("#header").css("background-color", colors[num]);
$("#letter").text(letters[num]);
}
if (mode == "spelling") {
//move to the next letter or next word
letterIndex++;
if (letterIndex >= words[wordIndex].length) {
wordIndex++;
letterIndex = 0;
$("#word").empty();
constructWord(words[wordIndex]);
}
//move the black border
let letterNum = letterIndex + 1;
$("#word p:nth-child(" + letterIndex + ")").css("border", "none");
$("#word p:nth-child(" + letterNum + ")").css("border", "2px solid black");
//change header color
let curLetter = words[wordIndex].charAt(letterIndex);
let groupIndex = getGroupIndex(curLetter);
$("#header").attr("data-color", ids[groupIndex]);
$("#header").css("background-color", colors[groupIndex]);
curColor = $("#header").attr("data-color");
}
//posting logs to google sheet
$.ajax({
url: "https://script.google.com/macros/s/AKfycbzcjmIPchxdXsJkEfb5S82-t98hwoxo3wNG8RPl16PCx5oOEzs/exec",
type: "post",
data: {
timestamp: $.now(),
datetime: getDatetime(),
action: "match success",
data: curKey
}
});
}
function animateBar(id) {
$("#" + id + "> .bar").css("visibility", "visible");
$("#" + id + "> .bar").animate({
width: "100%"
}, speed);
barTimer = setTimeout(function() {
resetBar(id)
}, speed + 20);
if (curKey == curColor) { //user moves to the right key
clearTimeout(failTimer);
rightKeyTimer = setTimeout(rightKeyAction, speed);
} else {
clearTimeout(rightKeyTimer);
failTimer = setTimeout(failLog, speed);
}
}
function resetBar(id) {
$("#" + id + "> .bar").stop();
$("#" + id + "> .bar").css("width", "0%");
$("#" + id + "> .bar").css("visibility", "hidden");
if (curKey == prevKey) {
animateBar(curKey);
} else {
clearTimeout(barTimer);
clearTimeout(failTimer);
}
}
function getCurKey(e) {
for (var i = 0; i < keyNum; i++) {
let left = $("#" + ids[i]).position().left,
top = $("#" + ids[i]).position().top;
let right = left + $("#" + ids[i]).width(),
bottom = top + $("#" + ids[i]).height();
if (e.type != "mousemove") {
for (var j = 0; j < e.touches.length; j++) {
if (e.touches[j].pageX > left && e.touches[j].pageX < right && e.touches[j].pageY > top && e.touches[j].pageY < bottom) {
return ids[i];
}
}
} else {
if (e.pageX > left && e.pageX < right && e.pageY > top && e.pageY < bottom) {
return ids[i];
}
}
}
return false;
}
function endGame() {
$("#end").hide();
$("#start").show();
inSession = false;
clearTimeout(barTimer);
clearTimeout(rightKeyTimer);
$("#" + curKey + "> .bar").stop();
$("#" + curKey + "> .bar").css("width", "0%");
$("#" + curKey + "> .bar").css("visibility", "hidden");
//posting logs to google sheet
$.ajax({
url: "https://script.google.com/macros/s/AKfycbzcjmIPchxdXsJkEfb5S82-t98hwoxo3wNG8RPl16PCx5oOEzs/exec",
type: "post",
data: {
timestamp: $.now(),
datetime: getDatetime(),
action: "end",
data: failCount
}
});
//reset index, score and header color
index = 0;
letterIndex = 0;
wordIndex = 0;
score = 0;
failCount = 0;
$("#score").text("Score: " + score + " / " + goalScore);
if (mode == "letters") {
$("#letter").text("A");
$("#header").attr("data-color", "green");
$("#header").css("background-color", "#c2fb73");
}
if (mode == "spelling") {
constructWord(words[0]);
$("#header").attr("data-color", "blue");
$("#header").css("background-color", colors[1]);
}
}
function adjustKeyNum() {
//change layout accordding to how many keys user want
keyNum = $("#keyNum").val();
if (keyNum == 4) {
$("#yellow").hide();
$("#orange").hide();
$(".key").css("width", "43%");
} else if (keyNum == 5) {
$("#yellow").show();
$("#orange").hide();
$(".key").css("width", "30%");
} else if (keyNum == 6) {
$("#yellow").show();
$("#orange").show();
$(".key").css("width", "30%");
}
$("#word").empty();
$("#word").append("<p id='letter'>A</p>");
$("#header").attr("data-color", "green");
$("#header").css("background-color", "#c2fb73");
}
function constructWord(word) {
$("#word").empty();
for (let i = 0; i < word.length; i++) {
$("#word").append("<p>" + word.charAt(i) + "</p>");
}
$("#word p:nth-child(1)").css("border", "2px solid black");
}
function getGroupIndex(letter) {
if (letter == 'Z') return 4;
let num = letter.charCodeAt(0) - ('A').charCodeAt(0);
return Math.floor(num / 5);
}
function failLog() {
failCount++;
$.ajax({
url: "https://script.google.com/macros/s/AKfycbzcjmIPchxdXsJkEfb5S82-t98hwoxo3wNG8RPl16PCx5oOEzs/exec",
type: "post",
data: {
timestamp: $.now(),
datetime: getDatetime(),
action: "match fail",
data: curKey + " " + words[wordIndex].charAt(letterIndex)
}
});
}