1aaab61cb6e56f537f75ad9da554dbab820723a0
[sheet.git] / word / quiz.js
1 Array.prototype.shuffle = function () {
2         for (let i = this.length - 1; i > 0; i--) {
3                 const j = Math.floor(Math.random() * (i + 1)); // random index 0..i
4                 [this[i], this[j]] = [this[j], this[i]]; // swap elements
5         }
6         return this;
7 };
8
9 Set.prototype.filter = function (f) {
10         return new Set([...this].filter(f));
11 };
12
13 class WordQuiz {
14         dataselect(json) {
15                 this.data = this.datafilter(json);
16                 let rows = Object.values(this.data);
17                 rows = rows.filter(row => !row[3].length); // remove referenced categories
18                 return rows.shuffle();
19         }
20
21         datafilter(json) {
22                 // find viable rows from json data
23                 let ids = new Set(Object.keys(json));
24                 const selection = {...json}; // clone
25
26                 if (this.preset.cat !== undefined) {
27                         ids.clear();
28                         let children = [this.preset.cat];
29                         for (let loop = 0; children.length && loop < 20; loop++) {
30                                 for (let child of children) ids.add(child.toString());
31                                 children = children.map(cat => json[cat][3]).filter(is => is).flat()
32                         }
33                 }
34                 if (this.preset.image) {
35                         ids = ids.filter(id => json[id][2]);
36                 }
37                 if (this.preset.level !== undefined) {
38                         ids = ids.filter(id => json[id][1] <= this.preset.level);
39                 }
40
41                 // keep only wanted ids
42                 for (let id in selection) {
43                         if (id && !ids.has(id)) {
44                                 delete selection[id];
45                         }
46                 }
47
48                 // retain orphaned references in grandparent categories
49                 for (let id in selection) {
50                         selection[id][3] = function subresolve(subs) {
51                                 //console.log(subs);
52                                 return (subs || []).flatMap(sub =>
53                                         sub in selection ? [sub] : subresolve(json[sub][3])
54                                 );
55                         }(selection[id][3]);
56                 }
57                 return selection;
58         }
59
60         load(dataurl) {
61                 this.preset = {};
62                 let input;
63                 if (input = window.location.hash.match(/\d+/)) {
64                         this.preset.cat = parseInt(input[0]);
65                 }
66                 if (window.location.hash.match(/a/)) {
67                         this.preset.level = 3;
68                 }
69
70                 fetch(dataurl).then(res => res.json()).then(json => {
71                         this.words = this.dataselect(json)
72                         this.setup();
73                 });
74         }
75
76         log(...args) {
77                 this.history.push([new Date().toISOString(), ...args]);
78         }
79
80         stop(...args) {
81                 this.log(...args);
82                 window.onbeforeunload = null;
83                 fetch('/word/report', {method: 'POST', body: JSON.stringify(this.history)});
84         }
85
86         constructor(dataurl) {
87                 this.load(dataurl);
88                 this.history = [];
89                 window.onbeforeunload = e => {
90                         this.stop('abort');
91                 };
92         }
93 }