-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathPage.js
417 lines (336 loc) · 14.4 KB
/
Page.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
function Page() {
// square highlight duration
const _stimulus_time = 500;
// total time to wait for user input
const _total_trial_time = 3000;
var self = this; // for event handlers
var startButton = document.getElementById('start-button');
var continueButton = document.getElementById('continue-button');
this.blockCreator = new BlockCreator(); // is a static class in the original silverlight app
// our list of trials
this.m_Trials = [];
// current block we're working on
this._blockNum = 0;
// current trial we're working on
this._trialNum = 0;
// starting value of n
this._starting_N = 2;
// value of n for the current block
this._n = this._starting_N;
// keeps track of the user's score
this._score = new Score();
// set to true whenever the game is running
this._playingGame = false;
// timers - from Page.xaml
// initial wait - gap before the trial starts so the user can get ready
// Timer_1 - for the stimulus, i.e. the square highlights
// TrialTimer - total trial time, trial is ended when complete
// _continurTimer - a timer to wait after the "continue" button has been clicked
this.initialWait;
this.Timer_1;
this.TrialTimer;
this._continueTimer;
DisplayN(this._starting_N);
setProgress(0);
// EVENTS
startButton.addEventListener('click', function(event) {
// check for start/pause via changing the text value of the button itself
if(event.target.value == "Start") {
event.target.value = "Pause";
event.target.textContent = "Pause";
// original timer = 675 * 10000 (675ms)
// in setTimeout() the delay is in milliseconds
// 'this' in an event handler is the object that fired the event
self.initialWait = new Timer(self.Start_Training, 675);
} else if(event.target.value == "Pause") {
event.target.value = "Resume";
event.target.textContent = "Resume";
// pause all animations/timers
self.Timer_1.pause();
self.TrialTimer.pause();
} else if(event.target.value == "Resume") {
// continue from pause
event.target.value = "Pause";
event.target.textContent = "Pause";
self.Timer_1.resume();
self.TrialTimer.resume();
}
});
document.addEventListener("keyup", function(event) {
if(self._playingGame === true) {
self._score.recordButtonPress(event.key);
}
}, false);
continueButton.addEventListener('click', function(event) {
if(event.target.value == "Continue") {
// Start a new block...
setProgress(0);
self._blockNum++;
document.getElementById("session-number").innerHTML = (self._blockNum + 1).toString() +
" / " + self.blockCreator.GetDefaultBlockSize().toString();
self.m_Trials = self.blockCreator.createBlock(self._n);
self._trialNum = 0;
// Tell the user the n they're using
DisplayN(self._n);
self._score.startBlock(self._n);
// Give the user a chance to catch their breath before starting the next block.
self._continueTimer = new Timer(self.startBlock, 675);
} else if(event.target.value == "Reset") {
event.target.value = "Continue";
event.target.textContent = "Continue";
startButton.value = "Start";
startButton.textContent = "Start";
self._blockNum = 0;
self._playingGame = false;
self._trialNum = 0;
self._n = self._starting_N;
self._score._score = 0;
setProgress(0);
DisplayN(self._n);
// reset session number
document.getElementById("session-number").innerHTML = "1 / " +
self.blockCreator.GetDefaultBlockSize().toString();
document.getElementById("score-text").innerHTML = "0";
}
});
// FUNCTIONS
// callback for when the timer expires for the stimulus, hide the stimulus
this.hideStimulus = function() {
// just clear all grid squares for ease
document.getElementById("bottom-left").style.backgroundColor = "";
document.getElementById("bottom-middle").style.backgroundColor = "";
document.getElementById("bottom-right").style.backgroundColor = "";
document.getElementById("middle-left").style.backgroundColor = "";
document.getElementById("middle-right").style.backgroundColor = "";
document.getElementById("top-left").style.backgroundColor = "";
document.getElementById("top-middle").style.backgroundColor = "";
document.getElementById("top-right").style.backgroundColor = "";
}
this.Start_Training = function() {
self._playingGame = true;
// we're starting, get the list of trials and clear the extra stuff
self._n = self._starting_N;
self._blockNum = 0;
self._trialNum = 0;
self.m_Trials = self.blockCreator.createBlock(self._n);
self._score.startBlock(self._n);
// present the first trial to the user
self.presentTrialInfoToUser(self.m_Trials[self._trialNum]);
self._score.startNewTrial(self.m_Trials[self._trialNum].GetSecondTrialInTarget);
// start the timers for the first trial
self.Timer_1 = new Timer(self.hideStimulus, _stimulus_time);
self.TrialTimer = new Timer(self.trialTimeUp, _total_trial_time);
}
// called whenever the total time for an individual trial has expired
this.trialTimeUp = function() {
self._score.endTrial();
self._trialNum++;
// clear feedback boxes
// TODO - should be handled with an animation so the boxes
// are only shaded for a brief period, fade-in-out
document.getElementById("left-hand-feedback").style.backgroundColor = "";
document.getElementById("right-hand-feedback").style.backgroundColor = "";
var progress = self._trialNum / self.m_Trials.length;
setProgress(progress * 100);
// are we at the end of a block?
if(self._trialNum >= self.m_Trials.length) {
// are we at the end of an entire session (20 blocks)?
if(self._blockNum == (self.blockCreator.GetNumBlocksTotal() - 1)) {
// what was the average n level?
var averageN = self._score.getMeanN();
document.getElementById("next-level-info").innerHTML = "You have completed all of the sessions. Congratulations!<br>" +
"You had an average N level of " + averageN.toString();
if(self._score.getPercentGFIncrease() > 0) {
var gFIncrease = self._score.getPercentGFIncrease();
document.getElementById("next-level-info").innerHTML += "<br>" + gFIncrease.toString();
}
// output last block score, note the original version doesn't do this
document.getElementById("correct-audio-results").innerHTML = "Correct Audio Results: " +
(self.blockCreator.GetDefaultBlockSize() - self._score.audioMistakes()).toString() +
" / " + self.blockCreator.GetDefaultBlockSize().toString();
document.getElementById("correct-visual-results").innerHTML = "Correct Visual Results: " +
(self.blockCreator.GetDefaultBlockSize() - self._score.visualMistakes()).toString() +
" / " + self.blockCreator.GetDefaultBlockSize().toString();
// change button to reset to start a brand new trial
continueButton.value = "Reset";
continueButton.textContent = "Reset";
// save progress
addSessionToHistory(Date.now(), averageN);
$('#scoreModal').modal('show');
return;
}
// end the block, output results
document.getElementById("correct-audio-results").innerHTML = "Correct Audio Results: " +
(self.blockCreator.GetDefaultBlockSize() - self._score.audioMistakes()).toString() +
" / " + self.blockCreator.GetDefaultBlockSize().toString();
document.getElementById("correct-visual-results").innerHTML = "Correct Visual Results: " +
(self.blockCreator.GetDefaultBlockSize() - self._score.visualMistakes()).toString() +
" / " + self.blockCreator.GetDefaultBlockSize().toString();
var deltaN = self._score.endBlock();
if((deltaN + self._n) >= 2) {
self._n = self._n + deltaN;
}
// display the next trial level (N number) in the popup
document.getElementById("next-level-info").innerHTML = "Next level: " + self._n.toString();
// display results dialog
$('#scoreModal').modal('show');
return;
}
// start a new trial
self._score.startNewTrial(self.m_Trials[self._trialNum].GetSecondTrialInTarget());
self.presentTrialInfoToUser(self.m_Trials[self._trialNum]);
self.Timer_1 = new Timer(self.hideStimulus, _stimulus_time);
self.TrialTimer = new Timer(self.trialTimeUp, _total_trial_time);
}
this.startBlock = function() {
// start a new trial
self._score.startNewTrial(self.m_Trials[self._trialNum].GetSecondTrialInTarget());
self.presentTrialInfoToUser(self.m_Trials[self._trialNum]);
window.clearTimeout(self.Timer_1);
window.clearTimeout(self.TrialTimer);
self.Timer_1 = new Timer(self.hideStimulus, _stimulus_time);
self.TrialTimer = new Timer(self.trialTimeUp, _total_trial_time);
}
// display trial information (square, audio, etc.) to the user
this.presentTrialInfoToUser = function(t) {
switch(t.GetLetter()) {
case Consonant.Letter1:
document.getElementById("letter1").play();
break;
case Consonant.Letter2:
document.getElementById("letter2").play();
break;
case Consonant.Letter3:
document.getElementById("letter3").play();
break;
case Consonant.Letter4:
document.getElementById("letter4").play();
break;
case Consonant.Letter5:
document.getElementById("letter5").play();
break;
case Consonant.Letter6:
document.getElementById("letter6").play();
break;
case Consonant.Letter7:
document.getElementById("letter7").play();
break;
case Consonant.Letter8:
document.getElementById("letter8").play();
break;
default:
break;
}
// display the visual information
switch(t.GetPosition()) {
case SquarePosition.BottomLeft:
document.getElementById("bottom-left").style.backgroundColor = "orange";
break;
case SquarePosition.BottomMiddle:
document.getElementById("bottom-middle").style.backgroundColor = "orange";
break;
case SquarePosition.BottomRight:
document.getElementById("bottom-right").style.backgroundColor = "orange";
break;
case SquarePosition.MiddleLeft:
document.getElementById("middle-left").style.backgroundColor = "orange";
break;
case SquarePosition.MiddleRight:
document.getElementById("middle-right").style.backgroundColor = "orange";
break;
case SquarePosition.TopLeft:
document.getElementById("top-left").style.backgroundColor = "orange";
break;
case SquarePosition.TopMiddle:
document.getElementById("top-middle").style.backgroundColor = "orange";
break;
case SquarePosition.TopRight:
document.getElementById("top-right").style.backgroundColor = "orange";
break;
default:
break;
}
}
}
function handleScores(totalScore) {
document.getElementById("score-text").innerHTML = totalScore.toString();
}
// called via an event to say if there was a trial success or failure
// highlights the left/right areas of the grid for success/failure feedback
// Page & Score access need access to this function
function handleTrialResult(result) {
if(result == TrialResult.Visual_Success) {
document.getElementById("left-hand-feedback").style.backgroundColor = "green";
} else if(result == TrialResult.Visual_Failure) {
document.getElementById("left-hand-feedback").style.backgroundColor = "red";
}
if(result == TrialResult.Audio_Success) {
document.getElementById("right-hand-feedback").style.backgroundColor = "green";
} else if(result == TrialResult.Audio_Failure) {
document.getElementById("right-hand-feedback").style.backgroundColor = "red";
}
}
// display appropriate N to user
// from NDisplayGrid.xaml.cs
function DisplayN(n) {
document.getElementById("N1").style.display = "none";
document.getElementById("N2").style.display = "none";
document.getElementById("N3").style.display = "none";
document.getElementById("N4").style.display = "none";
document.getElementById("N5").style.display = "none";
document.getElementById("N6").style.display = "none";
document.getElementById("N7").style.display = "none";
if (n > 7)
{
var n7 = document.getElementById("N7");
n7.style.display = "inline-block";
n7.textContent = n.toString();
} else {
if (n > 0) {
document.getElementById("N1").style.display = "inline-block";
}
if (n > 1) {
document.getElementById("N2").style.display = "inline-block";
}
if (n > 2) {
document.getElementById("N3").style.display = "inline-block";
}
if (n > 3) {
document.getElementById("N4").style.display = "inline-block";
}
if (n > 4) {
document.getElementById("N5").style.display = "inline-block";
}
if (n > 5) {
document.getElementById("N6").style.display = "inline-block";
}
if (n > 6) {
document.getElementById("N7").style.display = "inline-block";
}
}
}
// set progress bar value
// from ProgressBar.xaml.cs
function setProgress(prog) {
if((prog > 100) || (prog < 0)) {
return;
}
document.getElementById("prog-bar").style.width = prog + "%";
}
// setTimeout wrapper to include pause functionality
// source: http://stackoverflow.com/questions/3969475/javascript-pause-settimeout
function Timer(callback, delay) {
var timerId;
var start;
var remaining = delay;
this.pause = function() {
window.clearTimeout(timerId);
remaining -= new Date() - start;
};
this.resume = function() {
start = new Date();
window.clearTimeout(timerId);
timerId = window.setTimeout(callback, remaining);
};
this.resume();
}