-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
270 lines (218 loc) · 6.82 KB
/
Copy pathscript.js
File metadata and controls
270 lines (218 loc) · 6.82 KB
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
// Constants
var BASE_VOLUME = 0.100;
var TEST_VOLUME = 0.050;
var BASE_LEN_SEC = 40;
var TEST_GRACE_SEC = 6;
var PERIOD_FACTOR = 1.3;
// Game States
var STATE_IDLE = 0;
var STATE_RUNNING = 1;
var STATE_ENDED = 2;
// Global state
var rain = document.getElementById('rain');
var bell = document.getElementById('bell');
var rain_gain = null;
var timer = null;
var test_is_ongoing = false;
var session_start_time = 0;
var period_len_sec = 0;
var current_state = STATE_IDLE;
// Global input listener
document.addEventListener('keydown', function(event) {
if (event.code === 'Space') {
event.preventDefault();
if (current_state === STATE_IDLE) {
start_button_pressed();
} else if (current_state === STATE_RUNNING) {
key_was_pressed();
} else if (current_state === STATE_ENDED) {
restart_session();
}
}
});
function start_button_pressed()
{
assert(current_state === STATE_IDLE, "start_button_pressed called while not IDLE");
log_message('Start button was pressed');
current_state = STATE_RUNNING;
// Switch containers
display_feedback('top_container', false);
display_feedback('bottom_container', true);
session_start();
}
function stop_session_early()
{
assert(current_state === STATE_RUNNING, "stop_session_early called while not RUNNING");
log_message('Session stopped manually by user');
if (timer) {
window.clearTimeout(timer);
timer = null;
}
session_end(null);
}
function restart_session()
{
assert(current_state === STATE_ENDED, "restart_session called while not ENDED");
log_message('Restarting session...');
// Hide buttons and messages
toggle_display('restart_button', false);
display_feedback('fb_theend', false);
display_feedback('fb_wander', false);
display_feedback('fb_eager', false);
display_feedback('fb_score', false);
current_state = STATE_RUNNING;
session_start();
}
// Helper to show/hide elements without animation (mostly for buttons)
function toggle_display(id, show) {
var el = document.getElementById(id);
if (el) el.style.display = show ? 'block' : 'none';
}
function display_feedback(name, onoff)
{
var elem = document.getElementById(name);
if (onoff)
{
elem.style.animation = 'blurFadeIn 0.9s forwards';
elem.style.animationDelay = '1s';
}
else
{
elem.style.animation = 'blurFadeOut 2s forwards';
elem.style.animationDelay = '0s';
}
}
function rain_begin_playing()
{
if (!rain_gain) {
var context = new AudioContext();
var source = context.createBufferSource();
rain_gain = context.createGain();
rain_gain.gain.value = BASE_VOLUME;
source.connect(rain_gain);
rain_gain.connect(context.destination);
var request = new XMLHttpRequest();
request.open('GET', rain.innerHTML, true);
request.responseType = 'arraybuffer';
request.onload = function()
{
context.decodeAudioData(request.response, function(response)
{
source.buffer = response;
source.start(0);
source.loop = true;
}, function () { console.error('The request failed.'); } );
};
request.send();
}
rain_volume(BASE_VOLUME);
}
function rain_volume(vol, duration)
{
assert(rain_gain !== null, "rain_volume called before rain_gain initialized");
var now = rain_gain.context.currentTime;
rain_gain.gain.cancelScheduledValues(now);
rain_gain.gain.setValueAtTime(rain_gain.gain.value, now);
if (duration && duration > 0) {
rain_gain.gain.linearRampToValueAtTime(vol, now + duration);
} else {
rain_gain.gain.setValueAtTime(vol, now);
}
}
function session_start()
{
log_message('Meditation session begins...');
timer = null;
session_start_time = Date.now();
period_len_sec = BASE_LEN_SEC;
log_message(rain.currentSrc);
rain_begin_playing();
window.setTimeout(function() { bell.play(); }, 1000);
// Feedback and Buttons
display_feedback('fb_listen', true);
// Show Stop and Trigger buttons
toggle_display('stop_button', true);
toggle_display('trigger_button', true);
toggle_display('restart_button', false);
period_begin_now(period_len_sec);
}
function period_begin_now(length_sec)
{
log_message('Period of length ' + length_sec + ' begins...');
rain_volume(BASE_VOLUME);
test_is_ongoing = false;
period_len_sec = length_sec;
display_feedback('fb_listen', true);
// Ensure controls are visible
toggle_display('stop_button', true);
toggle_display('trigger_button', true);
timer = window.setTimeout(test_begin_now, length_sec * 1000);
}
function test_begin_now()
{
log_message('Test period started');
test_is_ongoing = true;
rain_volume(TEST_VOLUME, TEST_GRACE_SEC);
timer = window.setTimeout(test_end_now, TEST_GRACE_SEC * 1000);
}
function test_end_now()
{
log_message('Test period finished without user pressing any key');
test_is_ongoing = false;
session_end('fb_wander');
}
function session_end(feedback)
{
current_state = STATE_ENDED;
rain_volume(TEST_VOLUME);
bell.pause();
bell.currentTime = 0;
bell.play();
// HIDE active game elements
display_feedback('fb_listen', false);
toggle_display('stop_button', false);
toggle_display('trigger_button', false); // Hide the interaction button
// SHOW end game elements
session_length = Math.ceil((Date.now() - session_start_time) / (60 * 1000));
document.getElementById('score_value').innerHTML = (session_length == 1)? '1 minute' : session_length + ' minutes';
if (feedback) {
display_feedback(feedback, true);
}
display_feedback('fb_theend', true);
display_feedback('fb_score', true);
// Show restart button
toggle_display('restart_button', true);
display_feedback('restart_button', true);
}
function key_was_pressed()
{
if (timer) {
window.clearTimeout(timer);
timer = null;
}
if (test_is_ongoing)
{
log_message('User passed the test')
var goodjob = document.getElementById('fb_goodjob');
var cloned = goodjob.cloneNode(true);
goodjob.parentNode.replaceChild(cloned, goodjob);
cloned.style.animation = 'blurFadeInThenOut 4s forwards';
period_len_sec *= PERIOD_FACTOR;
period_begin_now(period_len_sec);
}
else
{
log_message('User pressed a key too early')
session_end('fb_eager');
}
}
function log_message(msg)
{
console.log(msg);
}
function assert(condition, message)
{
if (!condition) {
throw new Error("Assertion failed: " + message);
}
}