-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
543 lines (469 loc) · 11.2 KB
/
game.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
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/**
* Represents a game.
*
* @constructor
*/
function Game ()
{
this.data =
{
groups: {},
elements: {}
};
this.options = [];
this.solution = {};
this.currentGroup = {};
/**
* Adds data to the game.
*
* @param {Object} rawData Raw data.
*/
Game.prototype.addData = function (rawData)
{
// Groups
for (var rawGroup of rawData.groups)
{
// Create group
var group = new Group();
group.id = rawGroup.id;
group.name = rawGroup.name;
// Add group to data
this.data.groups[rawGroup.id] = group;
}
// Elements
for (var rawElement of rawData.elements)
{
// Create element
var element = new Element();
element.id = rawElement.id;
element.name = rawElement.name;
// Add element to data
this.data.elements[rawElement.id] = element;
// Add elements to groups
for (var g of rawElement.group)
{
// Check if group exists
if (this.data.groups[g] == undefined)
{
console.log('ERROR: Group "' + g + '" does not exist');
continue;
}
// Add element to group
this.data.groups[g].elements.push(element);
}
}
// Add similars
for (var rawSimilarArray of rawData.similars)
{
// Generate similar array
var similarArray = [];
for (var elementId of rawSimilarArray)
{
// Get element
var element = this.data.elements[elementId];
// Check if element exists
if (element == undefined)
{
console.log('ERROR: Element "' + element + '" does not exist');
continue;
}
// Add to similar array
similarArray.push(element);
}
// Add similar arrays to elements
for (var elementA of similarArray)
{
for (var elementB of similarArray)
{
if (elementA != elementB)
{
elementA.similars.push(elementB);
}
}
}
}
// Get stats
this._loadStats();
}
/**
* Sets a group as the current one.
*
* @param {String} id Id of the group.
*/
Game.prototype.setCurrentGroup = function (id)
{
// Check if group exists
if (this.data.groups[id] == undefined)
{
console.log('ERROR: Group "' + id + '" does not exist');
return;
}
else
{
this.currentGroup = this.data.groups[id];
}
}
/**
* Gets the longest streak of the current group.
*
* @returns {Number} Number of correct answers in the longest streak of the current group.
*/
Game.prototype.getLongestStreak = function ()
{
var longestStreak = 0;
if (this.currentGroup != undefined)
{
longestStreak = this.currentGroup.getLongestStreak();
}
return longestStreak;
}
/**
* Gets the current streak of the current group.
*
* @returns {Number} Number of correct answers in the current streak of the current group.
*/
Game.prototype.getCurrentStreak = function ()
{
var currentStreak = 0;
if (this.currentGroup != undefined)
{
currentStreak = this.currentGroup.getCurrentStreak();
}
return currentStreak;
}
/**
* Gets a new set of options.
*
* @returns {Object} Object of options.
*/
Game.prototype.getOptions = function ()
{
// Empty options array
this.options = [];
var q = this.currentGroup.getOptions(6);
this.solution = q.solution;
this.options = q.options;
return this.options;
};
/**
* Select an option, updating stats and view.
*
* @param {String} id Id of the selected option.
*/
Game.prototype.selectOption = function (id)
{
var selectedOption = this.data.elements[id];
if (selectedOption == this.solution)
{
// Update element stats
this.solution.right++;
// Update group stats
this.currentGroup.currentStreak++;
if (this.currentGroup.currentStreak > this.currentGroup.longestStreak)
{
this.currentGroup.longestStreak = this.currentGroup.currentStreak;
}
}
else
{
// Update element stats
this.solution.wrong++;
selectedOption.wrong++;
// Update group stats
this.currentGroup.currentStreak = 0;
}
// Save stats
this._saveStats();
// Update view
if (selectedOption == this.solution)
{
$('div#' + id).addClass('correct');
// Set timeout
timeout = 500;
}
else
{
$('div#' + id).addClass('incorrect');
$('div#' + this.solution.id).addClass('correct');
// Set timeout
timeout = 1500;
}
// Load a new question
setTimeout(function(){
reload();
}, timeout);
};
/**
* Reset the stats.
*/
Game.prototype.resetStats = function ()
{
// Reset elements stats
for (var i in this.data.elements)
{
var e = this.data.elements[i];
e.right = 0;
e.wrong = 0;
}
// Reset groups stats
for (var i in this.data.groups)
{
var e = this.data.groups[i];
e.currentStreak = 0;
e.longestStreak = 0;
}
this._saveStats();
}
/**
* Private function to save stats to local storage.
*
* @private
*/
Game.prototype._saveStats = function ()
{
// Save elements stats
var elements = {};
for (var id in this.data.elements)
{
var e = this.data.elements[id];
elements[id] = {};
elements[id].right = e.right;
elements[id].wrong = e.wrong;
}
localStorage.setItem('elements', JSON.stringify(elements));
// Save groups stats
var groups = {};
for (var id in this.data.groups)
{
var e = this.data.groups[id];
groups[id] = {};
groups[id].longestStreak = e.longestStreak;
}
localStorage.setItem('groups', JSON.stringify(groups));
this._loadStats();
}
/**
* Private function to load stats from local storage.
*
* @private
*/
Game.prototype._loadStats = function ()
{
// Load elements stats
var elements = JSON.parse(localStorage.getItem('elements'));
for (var id in elements)
{
this.data.elements[id].right = elements[id].right;
this.data.elements[id].wrong = elements[id].wrong;
}
// Load groups stats
var groups = JSON.parse(localStorage.getItem('groups'));
for (var id in groups)
{
this.data.groups[id].longestStreak = groups[id].longestStreak;
}
}
}
/**
* Represents a group of elements.
*
* @constructor
* @property {String} id Id of the group.
* @property {String} name Name of the group.
* @property {Array} elements Array of elements of the group.
*/
function Group ()
{
this.id = '';
this.name = '';
this.elements = [];
this.currentStreak = 0;
this.longestStreak = 0;
this._prevsolution = {};
this._solution = {};
this._options = [];
/**
* Gets the number of elements in this group.
*
* @returns {Number} Number of elements.
*/
Group.prototype.getNumberOfElements = function ()
{
return this.elements.length;
}
/**
* Gets a solution and an array of options from the group.
*
* @param {Integer} num Number of elements.
* @returns {Object} Object of solution and array of options.
*/
Group.prototype.getOptions = function (num)
{
// Save previous solution
this._prevsolution = this._solution;
// Reset options array
this._options = [];
// Get solution
while (this._solution == this._prevsolution)
{
this._solution = this._getRandomElement();
}
// Include solution in options array
this._options.push(this._solution);
// Fill the rest of the options array
while (this._options.length < num)
{
console.log(this._options.length);
var e = this._getRandomElement();
if (this._options.indexOf(e) == -1)
{
this._options.push(e);
}
}
// Shuffle options array
this._options = shuffle(this._options);
var options = {};
options.solution = this._solution;
options.options = this._options;
return options;
}
/**
* Private function to get random elements.
*
* @returns {Element} Random element.
* @private
*/
Group.prototype._getRandomElement = function ()
{
var weights = [];
var total = 0;
// Calculate weights
for (var e of this.elements)
{
var weight = 100;
if (e.right > e.wrong)
{
// Give less weight to options that have been guessed correctly
weight /= (e.right - e.wrong + 1);
}
else if (e.right < e.wrong)
{
// Give more weight to options that have been guessed incorrectly
weight *= (e.wrong - e.right + 1);
}
/*
weight *= Math.pow(0.5, e.right);
weight *= Math.pow(4, e.wrong);
// Give more weight to options similar to solution
if (this._solution != this._prevsolution)
{
if (this._solution.similars != undefined && this._solution.similars.indexOf(e) != -1)
{
var count = this._solution.similars.length;
weight *= (50 / count);
}
}
*/
weights.push(weight);
total += weight;
}
// Normalize weights
for (var i in weights)
{
weights[i] = weights[i] / total;
}
// Get random number between 0 and 1
random = Math.random();
// Find matching element
var max = 0;
for (var i = 0; i < weights.length; i++)
{
max += weights[i];
if (random < max)
{
break;
}
if (i == weights.length - 1)
{
break;
}
}
// Return element
var keys = Object.keys(this.elements);
return this.elements[keys[i]];
}
/**
* Gets the current streak of the group.
*
* @returns {Number} Number of correct answers in the current streak of the group.
*/
Group.prototype.getCurrentStreak = function ()
{
return this.currentStreak;
}
/**
* Gets the current streak of the group.
*
* @returns {Number} Number of correct answers in the longest streak of the group.
*/
Group.prototype.getLongestStreak = function ()
{
return this.longestStreak;
}
}
/**
* Represents an element.
*
* @constructor
* @property {String} id Id of the element.
* @property {String} name Name of the element.
* @property {Integer} right Number of times this element was selected correctly.
* @property {Integer} wrong Number of times this element was selected incorrectly.
* @property {Array} similar Array of similar elements to this element.
*/
function Element ()
{
this.id;
this.name;
this.right = 0;
this.wrong = 0;
this.similars = [];
/**
* Gets the score of the element.
*
* @returns {Number} Number between 0 and 100 representing a score.
*/
Element.prototype.getScore = function ()
{
var score = 100 * this.right / (this.right + this.wrong);
if (isNaN(score))
{
score = 0;
}
score = Math.round(score * 10) / 10;
return score;
}
}
var getRandomNumber = function (max)
{
return max * Math.random() << 0;
}
function shuffle(array)
{
var currentIndex = array.length, temporaryValue, randomIndex;
// While there remain elements to shuffle...
while (0 !== currentIndex) {
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// And swap it with the current element.
temporaryValue = array[currentIndex];
array[currentIndex] = array[randomIndex];
array[randomIndex] = temporaryValue;
}
return array;
}
function param(name)
{
return (location.search.split(name + '=')[1] || '').split('&')[0];
}