-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
276 lines (238 loc) · 7.96 KB
/
main.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
/*jshint globalstrict: true*/
"use strict";
window.onload = function() {
main();
};
function main() {
var game = document.getElementById("game");
var problems = [
["I try", "προσπαθώ", "προσπαδώ", "πρόσπαθω", "πρόσπαδω"],
];
var auto_problems = make_translate_problems();
shuffle_array(auto_problems);
problems = problems.concat(auto_problems);
var ui = make_ui();
var current_correct_answer = 0;
var current_problem = 0;
var next_problem = 0;
var min_level = get_min_level(problems);
var game_state = {
correct_answer: 0,
};
var do_next_problem = function() {
var level = null;
do {
if (next_problem == problems.length) {
next_problem = 0;
min_level = get_min_level(problems);
}
current_problem = next_problem;
next_problem++;
level = get_problem_level(problems[current_problem][0]);
}
while (level > min_level);
display_problem(
ui, problems[current_problem], game_state, level);
};
var process_answer_typed = function() {
// update score
var delay = 2000;
ui.textinput_expected.innerHTML = game_state.correct_answer;
if (ui.textinput.value == game_state.correct_answer) {
update_problem_on_success(problems[current_problem]);
// sucess animation
animate_bgcolor(
ui.textinput_div, [0, 255, 128], [64, 64, 64], delay);
} else {
update_problem_on_fail(problems[current_problem]);
// fail animation
delay = 6000;
animate_bgcolor(
ui.textinput_div, [255, 0, 64], [64, 64, 64], delay);
}
// transition to next problem
setTimeout(do_next_problem, delay);
};
var process_answer_click = function(i) {
// update score
var delay = 1000;
if (i == game_state.correct_answer) {
update_problem_on_success(problems[current_problem]);
// sucess animation
animate_bgcolor(
ui.answers[i], [0, 255, 128], [64, 64, 64], delay);
} else {
update_problem_on_fail(problems[current_problem]);
// fail animation
delay = 5000;
animate_bgcolor(
ui.answers[game_state.correct_answer],
[0, 255, 128],
[64, 64, 64],
delay);
animate_bgcolor(
ui.answers[i],
[255, 0, 64],
[64, 64, 64],
delay / 5);
}
// transition to next problem
setTimeout(do_next_problem, delay);
};
ui.click_callback = process_answer_click;
ui.textinput_callback = process_answer_typed;
// setup the game elements
game.appendChild(ui.div);
do_next_problem();
}
function get_min_level(problems) {
var min_level = null;
for (var i = 0; i < problems.length; ++i) {
var level = get_problem_level(problems[i][0]);
if (min_level == null || level < min_level) {
min_level = level;
}
}
assert(min_level != null);
return min_level;
}
function update_problem_on_success(problem) {
var problem_name = problem[0];
var problem_level = get_problem_level(problem_name);
problem_level += 1;
set_problem_level(problem_name, problem_level);
}
function update_problem_on_fail(problem) {
var problem_name = problem[0];
var problem_level = get_problem_level(problem_name);
problem_level -= 1;
if (problem_level < 0) {
problem_level = 0;
}
set_problem_level(problem_name, problem_level);
}
function set_problem_level(problem_name, value) {
var problem_data = localStorage.getItem(problem_name);
if (problem_data == null) {
problem_data = {};
} else {
problem_data = JSON.parse(problem_data);
}
problem_data["level"] = value;
localStorage.setItem(problem_name, JSON.stringify(problem_data));
}
function get_problem_level(problem_name) {
var problem_data = localStorage.getItem(problem_name);
if (problem_data == null) {
return 0;
}
problem_data = JSON.parse(problem_data);
if (!("level" in problem_data)) {
return 0;
}
return problem_data["level"];
}
function make_ui() {
var ui = {
div: null,
question: null,
level: null,
answers: [],
answers_div: null,
textinput_div: null,
textinput: null,
textinput_expected: null,
textinput_callback: null,
click_callback: null
};
ui.div = document.createElement("div");
ui.question = document.createElement("div");
ui.question.setAttribute("class", "question");
ui.level = document.createElement("div");
ui.level.setAttribute("class", "level");
ui.level.innerHTML = "No level";
ui.div.appendChild(ui.question);
var make_click_callback = function(index) {
return function() {
ui.click_callback(index);
}
};
ui.textinput_div = document.createElement("div");
ui.textinput_div.setAttribute("class", "entry");
ui.textinput = document.createElement("input");
ui.textinput.type = "text";
ui.textinput.setAttribute("autocomplete", "off");
ui.textinput.setAttribute("autocorrect", "off");
ui.textinput.setAttribute("autocapitalize", "off");
ui.textinput.setAttribute("spellcheck", "off");
ui.textinput.onkeyup = function(e) {
var enter_key = 13;
if (e.which == enter_key){
ui.textinput_callback();
}
};
ui.textinput_expected = document.createElement("div");
ui.textinput_expected.setAttribute("class", "expected");
ui.textinput_div.appendChild(ui.textinput);
ui.textinput_div.appendChild(ui.textinput_expected);
ui.textinput_div.style.display = 'none';
ui.div.appendChild(ui.textinput_div);
var num_answers = 4;
ui.answers_div = document.createElement("div");
for (var i = 0; i < num_answers; i++) {
var a = document.createElement("div");
a.setAttribute("class", "answer");
a.onclick = make_click_callback(i);
ui.answers.push(a);
ui.answers_div.appendChild(a);
}
ui.answers_div.style.display = 'none';
ui.div.appendChild(ui.answers_div);
ui.div.appendChild(ui.level);
ui.show_answers = function() {
ui.answers_div.style.display = 'block';
ui.textinput_div.style.display = 'none';
document.activeElement.blur();
};
ui.show_textinput = function() {
ui.textinput_div.style.display = 'block';
ui.answers_div.style.display = 'none';
// looks like this won't help on Apple mobile devices:
// http://stackoverflow.com/questions/12204571/mobile-safari-
// javascript-focus-method-on-inputfield-only-works-with-click
ui.textinput.focus();
};
return ui;
}
function display_problem(ui, problem, game_state, level) {
ui.question.innerHTML = problem[0];
if (level == 0) {
set_bgcolor(ui.question, [0, 0, 0]);
} else if (level == 1) {
set_bgcolor(ui.question, [0, 64, 32]);
} else if (level == 2) {
set_bgcolor(ui.question, [64, 0, 32]);
} else if (level == 3) {
set_bgcolor(ui.question, [32, 0, 64]);
} else {
set_bgcolor(ui.question, [64, 0, 64]);
}
if (level == 0) {
var ordering = [0, 1, 2, 3];
shuffle_array(ordering);
for (var i=0; i<4; ++i) {
var shuffled_i = ordering[i];
ui.answers[i].innerHTML = problem[shuffled_i + 1];
if (shuffled_i === 0) {
game_state.correct_answer = i;
}
}
ui.show_answers();
} else {
game_state.correct_answer = problem[1];
ui.textinput_expected.innerHTML = "";
ui.textinput.value = "";
ui.show_textinput();
}
ui.level.innerHTML = "level: " + level.toString();
}