-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitcher.js
More file actions
515 lines (455 loc) · 16.1 KB
/
switcher.js
File metadata and controls
515 lines (455 loc) · 16.1 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/**
* The Switcher Game
* Javascript code
* @author Erick Levy <erickevyATgmailDOTcom>
* @requires config.js The file with the structure for configuration's options (loaded in index.html)
* @requires strings-xx.js The file with the languaje localized strings where xx: language code [en, es, fr, jp, ...]
*/
// Some Constants
const buttonOff = false;
const buttonOn = true;
// Some paths
const imagePath = './img/';
const soundPath = './raw/';
// Some defaults
var gbPlaySounds = (localStorage.getItem('playSounds') === 'true');
var switchStyle = localStorage.getItem('switchStyle') || "sw";
var colorScheme = localStorage.getItem('colorScheme') || "grn_red";
// Default button images
var imageOn = imagePath + config.styles[switchStyle].colors[colorScheme].on;
var imageOff = imagePath + config.styles[switchStyle].colors[colorScheme].off;
// Default button sound
var soundClick = soundPath + config.styles[switchStyle].sound;
// Other sounds
var soundStart = soundPath + 'start_sound.mp3';
var soundWinner = soundPath + 'winner_sound.mp3';
var soundGameOver = soundPath + 'game_over_sound.mp3';
// Collecions
var allButtons = new Array();
// Game control global variables
var giSteps = 0;
var gbGameOver = true;
// DOM elements mapping
const appLongName = document.getElementById('appLongName');
const lblText = document.getElementById('lblText');
const btnStart = document.getElementById('btnStart');
const btnHelp = document.getElementById('btnHelp');
const btnAbout = document.getElementById('btnAbout');
const btnStats = document.getElementById('btnStats');
const btnConfig = document.getElementById('btnConfig');
const btnSwitch1 = document.getElementById('btnSwitch1');
const btnSwitch2 = document.getElementById('btnSwitch2');
const btnSwitch3 = document.getElementById('btnSwitch3');
const btnSwitch4 = document.getElementById('btnSwitch4');
const btnSwitch5 = document.getElementById('btnSwitch5');
const btnSwitch6 = document.getElementById('btnSwitch6');
const btnSwitch7 = document.getElementById('btnSwitch7');
const btnSwitch8 = document.getElementById('btnSwitch8');
const btnSwitch9 = document.getElementById('btnSwitch9');
// Initialization of the DOM interface
function run() {
// Load strings
lblText.innerText = strings.pressStart || 'Press the START button...';
appLongName.innerText = strings.appLongName || 'The Switcher Game';
btnHelp.title = strings.btnHelp || 'Help';
btnAbout.title = strings.btnAbout || 'About';
btnStats.title = strings.btnStats || 'Statistics';
btnConfig.title = strings.btnConfig || 'Configuration';
btnStart.innerText = strings.btnStart || 'Start';
// Prepare event listeners
btnStart.addEventListener('click', btnStartOnClick);
btnHelp.addEventListener('click', btnHelpOnClick);
btnAbout.addEventListener('click', btnAboutOnClick);
btnStats.addEventListener('click', btnStatsOnClick);
btnConfig.addEventListener('click', btnConfigOnClick);
btnSwitch1.addEventListener('click', btnSwitch1OnClick);
btnSwitch2.addEventListener('click', btnSwitch2OnClick);
btnSwitch3.addEventListener('click', btnSwitch3OnClick);
btnSwitch4.addEventListener('click', btnSwitch4OnClick);
btnSwitch5.addEventListener('click', btnSwitch5OnClick);
btnSwitch6.addEventListener('click', btnSwitch6OnClick);
btnSwitch7.addEventListener('click', btnSwitch7OnClick);
btnSwitch8.addEventListener('click', btnSwitch8OnClick);
btnSwitch9.addEventListener('click', btnSwitch9OnClick);
gameInitialize();
}
// Game initialization
function gameInitialize() {
allButtons = new Array();
allButtons.push(btnSwitch1);
allButtons.push(btnSwitch2);
allButtons.push(btnSwitch3);
allButtons.push(btnSwitch4);
allButtons.push(btnSwitch5);
allButtons.push(btnSwitch6);
allButtons.push(btnSwitch7);
allButtons.push(btnSwitch8);
allButtons.push(btnSwitch9);
allButtons.forEach(element => {
element.src = imageOn;
element.tag = buttonOn;
});
}
function btnHelpOnClick() {
msgbox(strings.helpTitle, strings.helpMessage);
}
function btnAboutOnClick() {
msgbox(strings.aboutTitle, strings.aboutMessage);
}
function btnStatsOnClick() {
statsboxOpen();
}
function btnConfigOnClick() {
configboxOpen();
}
function btnStartOnClick() {
if (!gbGameOver) {
msgbox(strings.restartTitle,
strings.restartMessage,
[strings.btnOk, strings.btnCancel],
msgboxRestart);
} else {
restartGame();
}
}
function msgboxRestart(e) {
msgboxClose();
let btnPressed = e.target.innerText;
if (btnPressed === strings.btnOk) {
restartGame();
}
}
function restartGame() {
randomizeButtons();
giSteps = 0;
lblText.textContent = strings.lblSteps.replace('?', giSteps);
gbGameOver = false;
playSound(soundStart);
// Increment the games played counter
let gamesPlayed = localStorage.getItem('gamesPlayed') || 0;
localStorage.setItem('gamesPlayed', ++gamesPlayed);
}
function randomizeButtons() {
allButtons.forEach(element => {
if (Math.random() < 0.5) {
element.src = imageOff;
element.tag = buttonOff;
} else {
element.src = imageOn;
element.tag = buttonOn;
}
});
}
function redrawButtons() {
soundClick = soundPath + config.styles[switchStyle].sound;
imageOn = imagePath + config.styles[switchStyle].colors[colorScheme].on;
imageOff = imagePath + config.styles[switchStyle].colors[colorScheme].off;
allButtons.forEach(element => {
if (element.tag === buttonOff) {
element.src = imageOff;
element.tag = buttonOff;
} else {
element.src = imageOn;
element.tag = buttonOn;
}
});
}
function switchButton(button) {
let element = allButtons[button - 1];
if (element.tag === buttonOff) {
element.src = imageOn;
element.tag = buttonOn;
} else {
element.src = imageOff;
element.tag = buttonOff;
}
}
function switchButtons(buttons) {
if (!gbGameOver) {
if (Array.isArray(buttons)) {
buttons.forEach(element => {
switchButton(element);
});
updateSteps();
playSound(soundClick);
}
}
}
function btnSwitch1OnClick() {
switchButtons([1, 2, 4]);
}
function btnSwitch2OnClick() {
switchButtons([2, 1, 3, 5]);
}
function btnSwitch3OnClick() {
switchButtons([3, 2, 6]);
}
function btnSwitch4OnClick() {
switchButtons([4, 1, 5, 7]);
}
function btnSwitch5OnClick() {
switchButtons([5, 2, 4, 6, 8]);
}
function btnSwitch6OnClick() {
switchButtons([6, 3, 5, 9]);
}
function btnSwitch7OnClick() {
switchButtons([7, 4, 8]);
}
function btnSwitch8OnClick() {
switchButtons([8, 5, 7, 9]);
}
function btnSwitch9OnClick() {
switchButtons([9, 6, 8]);
}
function updateSteps() {
giSteps++;
lblText.textContent = strings.lblSteps.replace('?', giSteps);
checkButtons();
if (gbGameOver) {
displayGameOver();
}
}
function checkButtons() {
gbGameOver = true;
allButtons.forEach(element => {
if (element.tag === false) {
gbGameOver = false;
}
});
}
function displayGameOver() {
let msg = strings.gameOverScore.replace('?', giSteps);
if (giSteps <= 9) {
playSound(soundWinner);
msg += strings.gameOverWin;
// Store the step counter for stats
let stepsCount = 'stepsCount' + giSteps;
let lastCount = localStorage.getItem(stepsCount) || 0;
localStorage.setItem(stepsCount, ++lastCount);
// Increment the games winned counter
let gamesWinned = localStorage.getItem('gamesWinned') || 0;
localStorage.setItem('gamesWinned', ++gamesWinned);
} else {
playSound(soundGameOver);
msg += strings.gameOverTry;
}
// Use a timer to let the navigator update the display
setTimeout(function () {
msgbox(strings.gameOverTitle, msg);
lblText.textContent = strings.pressStart;
}, 500);
}
function playSound(sound) {
if (gbPlaySounds) {
let audio = new Audio(sound);
audio.play();
}
}
function msgbox(titleText, msgText, arrActions = strings.btnOk, fnOnClick = msgboxClose) {
const modal = document.getElementById('myModal');
const msgbox = document.getElementById('msgbox');
const hText = document.getElementById('hText');
const pText = document.getElementById('pText');
const msgboxButtons = document.getElementById('msgboxButtons');
// Texts in title and message
hText.textContent = titleText;
pText.innerHTML = msgText;
// Force the parameter to an array
if (!Array.isArray(arrActions)) {
arrActions = [arrActions];
}
// Empty the nav tag (no buttons)
msgboxButtons.innerHTML = '';
// Fill the nav tag with all the new buttons
arrActions.forEach(element => {
let msgButton = document.createElement('button');
msgButton.innerText = element;
msgButton.addEventListener('click', fnOnClick);
msgboxButtons.appendChild(msgButton);
});
// Unhide modal screen and box
msgbox.style.display = 'block';
modal.style.display = 'block';
}
function msgboxClose() {
const modal = document.getElementById('myModal');
const msgbox = document.getElementById('msgbox');
modal.style.display = 'none';
msgbox.style.display = 'none';
}
function configboxOpen() {
const modal = document.getElementById('myModal');
const configbox = document.getElementById('configbox');
const configboxOk = document.getElementById('configboxOk');
// Load label strings
document.getElementById('configboxTitle').innerText
= strings.configboxTitle || 'Configuration';
document.getElementById('configboxPlaySoundsLabel').innerText
= strings.configboxPlaySoundsLabel || 'Play sounds:';
document.getElementById('configboxSwitchStyleLabel').innerText
= strings.configboxSwitchStyleLabel || 'Switch style:';
document.getElementById('configboxColorSchemeLabel').innerText
= strings.configboxColorSchemeLabel || 'Color scheme:';
configboxOk.innerText = strings.btnOk;
// Update and display options
updatePlaySounds();
updateStyleOptions();
updateColorOptions();
// Add click event to Ok button to close
configboxOk.addEventListener('click', configboxClose);
// Unhide modal screen and box
modal.style.display = 'block';
configbox.style.display = 'block';
}
function updatePlaySounds() {
const imgPlaySoundOn = imagePath + 'ic_action_sounds_on.png';
const imgPlaySoundOff = imagePath + 'ic_action_sounds_off.png';
const imgPlaySounds = document.getElementById('imgPlaySounds');
imgPlaySounds.src = gbPlaySounds ? imgPlaySoundOn : imgPlaySoundOff;
imgPlaySounds.addEventListener('click', imgPlaySoundsOnClick);
}
function imgPlaySoundsOnClick(e) {
gbPlaySounds = !gbPlaySounds;
localStorage.setItem('playSounds', gbPlaySounds);
updatePlaySounds();
}
function updateStyleOptions() {
let styleOptions = document.getElementById('configboxSwitchStyleOptions');
styleOptions.innerHTML = '';
Object.keys(config.styles).forEach(element => {
let img = document.createElement('img');
img.tag = element;
img.src = imagePath + config.styles[element].cover;
img.title = config.styles[element].title;
if (element === switchStyle) {
img.classList.add('selected');
} else {
img.addEventListener('click', imgStyleOptionOnClick);
}
styleOptions.appendChild(img);
});
}
function imgStyleOptionOnClick(e) {
switchStyle = e.target.tag;
localStorage.setItem('switchStyle', switchStyle);
updateStyleOptions();
updateColorOptions();
}
function updateColorOptions() {
let colorOptions = document.getElementById('configboxColorSchemeOptions');
colorOptions.innerHTML = '';
Object.keys(config.styles[switchStyle].colors).forEach(element => {
let img = document.createElement('img');
img.tag = element;
img.src = imagePath + config.styles[switchStyle].colors[element].on;
img.title = config.styles[switchStyle].colors[element].title;
if (element === colorScheme) {
img.classList.add('selected');
} else {
img.addEventListener('click', imgColorOptionOnClick);
}
colorOptions.appendChild(img);
});
}
function imgColorOptionOnClick(e) {
colorScheme = e.target.tag;
localStorage.setItem('colorScheme', colorScheme);
updateColorOptions();
}
function configboxClose() {
const modal = document.getElementById('myModal');
const configbox = document.getElementById('configbox');
redrawButtons();
modal.style.display = 'none';
configbox.style.display = 'none';
}
function statsboxOpen() {
const modal = document.getElementById('myModal');
const statsbox = document.getElementById('statsbox');
const statsboxOk = document.getElementById('statsboxOk');
// Load label strings
document.getElementById('statsboxTitle').innerText = strings.statsboxTitle || 'Statistics';
document.getElementById('statsboxGamesLabel').innerText = strings.statsboxGamesLabel || 'Games';
document.getElementById('statsboxGraphLabel').innerText = strings.statsboxGraphLabel || 'Steps Distribution Graph';
statsboxOk.innerText = strings.btnOk;
// Update and display options
updateStats();
// Add click event to Ok button to close
statsboxOk.addEventListener('click', statsboxClose);
// Unhide modal screen and box
modal.style.display = 'block';
statsbox.style.display = 'block';
}
function updateStats() {
// Games
const statsboxGames = document.getElementById('statsboxGames');
const played = localStorage.getItem('gamesPlayed') || 0;
const won = localStorage.getItem('gamesWinned') || 0;
const percent = (played > 0) ? Math.round(100 * won / played) : 0;
const stats = {
'gamesPlayed': {
'label': strings.gamesPlayed || 'Played',
'value': played
},
'gamesWinned': {
'label': strings.gamesWinned || 'Winned',
'value': won
},
'porcWinned': {
'label': strings.gamesPercentage || 'Percentage',
'value': percent + '%'
}
};
statsboxGames.innerHTML = '';
Object.keys(stats).forEach(element => {
let div = document.createElement('div');
let label = document.createElement('div');
let value = document.createElement('div');
div.classList.add('stats-container');
label.classList.add('label');
value.classList.add('value');
label.innerText = stats[element].label;
value.innerText = stats[element].value;
div.appendChild(value);
div.appendChild(label);
statsboxGames.appendChild(div);
});
// Graph
const statsboxGraph = document.getElementById('statsboxGraph');
statsboxGraph.innerHTML = '';
let maxCount = 0;
for (i = 1; i <= 9; i++) {
let actualCount = localStorage.getItem('stepsCount' + i) || 0;
if (actualCount > maxCount) {
maxCount = actualCount;
}
}
for (i = 1; i <= 9; i++) {
let div = document.createElement('div');
let label = document.createElement('div');
let bar = document.createElement('div');
let value = document.createElement('div');
div.classList.add('bar-container');
label.classList.add('label');
bar.classList.add('bar');
value.classList.add('value');
let actualCount = localStorage.getItem('stepsCount' + i) || 0;
label.innerText = i;
bar.style.width = Math.floor(90 * actualCount / maxCount) + '%';
value.innerText = actualCount;
div.appendChild(label);
bar.appendChild(value);
div.appendChild(bar);
statsboxGraph.appendChild(div);
}
}
function statsboxClose() {
const modal = document.getElementById('myModal');
const statsbox = document.getElementById('statsbox');
modal.style.display = 'none';
statsbox.style.display = 'none';
}
// Executes initializations
run();
// End of code.