-
Notifications
You must be signed in to change notification settings - Fork 3
/
enter.js
240 lines (223 loc) · 7.92 KB
/
enter.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
/*jslint plusplus: false, bitwise: true, eqeq: true, unparam: true, white: false, browser: true, onevar: false */
/*global console: true, window: true, chrome: true, $: true, require: true, exports: true, process: true, alert: true*/
$(function() {
var KEY_ENTER = 13;
var KEY_SPACE = 32;
/**
* Plays a sound file. Invokes a callback afterwards.
*
* Will play the empty sound if sound is not forced by and it is
* disabled by the user.
*/
function playSound(sound, callback, forceUseSound){
if(forceUseSound || $("#sound").is(":checked")){
sound = sound || sounds.error;
}else{
sound = sounds.none;
}
if(callback){
function handler(){
sound.removeEventListener("ended", handler);
callback();
}
sound.addEventListener("ended", handler)
}
sound.play();
}
/**
* Pretty prints the state in tabular form.
*/
function prettyPrint(){
// TODO improve structure here
try{
// show the most recent answers first
var lines = $("#out").val().split("\n").reverse();
var maxAnswers = 0;
var tableRows = $.map(
lines,
function(line){
var components = line.trim().split(/ /g);
var studentId = components[0];
var setId = components[1];
var answers = components[2];
var answersArray = answers? answers.trim().split(/\d+/g).slice(1): [];
maxAnswers = Math.max(maxAnswers, answersArray.length);
var lineEntries = [studentId, setId, $("<button>Playback</button>").click(
function(){
var toPlay = answersArray;
playAnswers(toPlay);
}
)];
lineEntries = lineEntries.concat(answersArray);
var tableRow = $("<tr></tr>");
$.each(lineEntries,
function(i, entry){
var tableData = $("<td></td>").append(entry);
tableRow.append(tableData);
});
return tableRow;
}
);
var header = $("<thead/>");
header.append($("<td>Student ID</td>"));
header.append($("<td>Set ID</td>"));
header.append($("<td>Sound</td>"));
for(var entryNumber = 0; entryNumber < maxAnswers; entryNumber++){
header.append($("<td>" + (entryNumber + 1) + "</td>"));
}
var $table = $("<table/>");
$table.append(header);
$.each(tableRows, function(i, row){
$table.append(row);
});
// insert
$("#out-pretty").empty();
$("#out-pretty").append($table);
$("#out-pretty tr:even").css("background-color", "#DDDDDD");
$("#out-pretty tr:odd").css("background-color", "#BBBBBB");
$("#out-pretty td:empty").css("background-color", "#FF0000");
}catch(e){
console.error("Could not pretty print state, illegal content in state.");
console.error(e.stack);
}
}
/**
* Creates a new entry in the state area, depending on the current
* values of student id and set id.
*/
var newEntry = function() {
var out = $('#out');
var student = $('#student');
var id = $('#id');
var studentVal = student.val();
var idVal = id.val();
if (studentVal === "") {
student.focus();
return;
}
if (idVal === "") {
id.focus();
return;
}
var prefix = ((out.val() === '') ? '' : out.val() + '\n');
out.val(prefix + studentVal + ' ' + idVal + ' ');
out.change();
student.val('');
id.val('');
$('#answer').focus().val('');
$('#number').val(1);
playSound(sounds.next_set);
};
/**
* Callback for when the user does a keypress in the answer input area:
* - space: next question
* - enter: next set
* - number: error (ignored)
* - char: appended to the other answers to the question
*/
var answerInputKeyPress = function(e) {
var input = $('#answer');
var number = $('#number');
if (e.keyCode === KEY_SPACE || e.keyCode === KEY_ENTER) {
var out = $('#out');
var inputVal = input.val();
if (inputVal === '') {
inputVal = '-';
}
out.val(out.val() + number.val() + inputVal);
prettyPrint();
out.scrollTop(
out[0].scrollHeight - out.height()
);
var numberValue = parseInt(number.val(), 10) + 1;
number.val(numberValue);
if(numberValue in sounds){
playSound(sounds[numberValue]);
}
input.val('');
if (e.keyCode === KEY_SPACE) {
playSound(sounds.next_question);
e.preventDefault();
return;
}
}
if (e.keyCode === KEY_ENTER) {
$('#number input').val(1);
newEntry();
e.preventDefault();
return;
}
if (e.keyCode >= 48 && e.keyCode <= 57) { // number
playSound(sounds.error);
alert("You have entered a number!");
e.preventDefault();
return;
}
var answer = String.fromCharCode(e.keyCode).toLowerCase();
if(answer in sounds){
playSound(sounds[answer]);
}else{
playSound(sounds.no_letter);
}
};
/**
* enter/space event hook for creating a new entry in the state.
*/
var studentInputKeyPress = function(e) {
if (e.keyCode === KEY_ENTER || e.keyCode === KEY_SPACE) {
newEntry();
e.preventDefault();
return;
};
};
/**
* Plays the sounds for an array of answer strings.
*/
function playAnswers(answersArray){
function playAnswer(answerArray, callback){
if(answerArray.length === 0){
playSound(sounds.next_question, callback, true);
return;
}
var head = answerArray[0];
var tail = answerArray.slice(1);
var sound = sounds[head] || sounds.no_letter;
playSound(sound,
function(){
playAnswer(tail, callback);
},
true
);
}
if(answersArray.length === 0){
playSound(sounds.next_set, undefined, true);
return;
}
var head = answersArray[0];
var tail = answersArray.slice(1);
var answerArray = head.split("").sort();
playAnswer(answerArray, function(){playAnswers(tail);});
}
$('#number').val(1);
$("#student").add('#id').keydown(studentInputKeyPress);
$('#answer').keydown(answerInputKeyPress);
$("input").add("textarea").attr("tabindex", -1);
$('#out').on("change keyup", prettyPrint);
var sounds = {};
$.each("a b c d e f g h".split(' '),
function(i, v){
var file = 'letter-'+ v +'.mp3';
sounds[v] = new Audio(file);
}
);
for(var i = 10; i <= 100; i+=10){
var file = 'number-'+ i +'.mp3';
sounds[i] = new Audio(file);
}
sounds.next_question = new Audio('beep.mp3');
sounds.next_set = new Audio('wubup.mp3');
sounds.no_letter = new Audio('blunk.mp3');
sounds.no_answers = new Audio('buzz.mp3');
sounds.error = new Audio('long-buzz.mp3');
sounds.none = new Audio('none.mp3');
});