word/quiz: report table to save user actions
[sheet.git] / word / memory.js
index 375b7c185bab825ba6eb63b5ea80862952ac6cf0..89fd853c3cb89210c2a1d3e02e1b9fdaaaec2b55 100644 (file)
@@ -5,6 +5,7 @@ class WordMemory extends WordQuiz {
                        // show an open card
                        this.turned.push(target);
                        put(target, '.turn');
+                       this.log('pick', target.id, target.index);
                }
                else if (this.turned.length < 2) {
                        return; // keep open
@@ -15,8 +16,10 @@ class WordMemory extends WordQuiz {
                }
 
                // compare two cards
-               let match = this.pairs[this.turned[0].id] == this.turned[1].id
-                       || this.pairs[this.turned[1].id] == this.turned[0].id;
+               let match = !this.pairs ? this.turned[0].id == this.turned[1].id : (
+                       this.pairs[this.turned[0].id] == this.turned[1].id
+                       || this.pairs[this.turned[1].id] == this.turned[0].id
+               );
                if (!match && !this.turned[0].classList.contains('bad')) {
                        put(this.turned[0], '.bad'); // indicate failure on first card
                        return;
@@ -28,6 +31,7 @@ class WordMemory extends WordQuiz {
                        this.turned = [];
                        if (Array.from(this.form.children).every(card => card.classList.contains('good'))) {
                                put(this.form, '.good');
+                               this.stop('done');
                        }
                        return;
                }
@@ -38,20 +42,46 @@ class WordMemory extends WordQuiz {
        }
 
        load(dataurl) {
-               this.dataurl = '/data/wordpairs.json';
-               fetch(this.dataurl).then(res => res.json()).then(pairs => {
-                       this.turned = [];
-                       this.pairs = pairs;
-                       this.form = document.getElementById('quiz');
-                       this.cards = Object.entries(pairs).flat()
-                               .map(e => e.toString())
-                               .sort(() => {return .5 - Math.random()}) // shuffle
-                       this.cards.forEach(word => {
-                               put(this.form,
-                                       'figure>img[src=$]<', `/data/word/en/${word}.jpg`,
-                                       {onclick: e => this.turn(e), id: word}
-                               );
+               if (dataurl) {
+                       super.load(dataurl);
+               }
+               else {
+                       this.dataurl = '/data/wordpairs.json';
+                       fetch(this.dataurl).then(res => res.json()).then(pairs => {
+                               this.pairs = pairs;
+                               this.setup();
                        });
+               }
+       }
+
+       setup() {
+               this.turned = [];
+               this.form = document.getElementById('quiz');
+
+               let cards;
+               if (this.words) {
+                       const formstyle = window.getComputedStyle(this.form)
+                       const gridsize = [
+                               formstyle['grid-template-rows'], formstyle['grid-template-columns']
+                       ].map(val => val.match(/ /g).length + 1).reduce((x, y) => x * y) / 2;
+                       cards = this.words.splice(0, gridsize || 6).map(row => row[2]);
+                       cards.push(...cards.map(val => -val));
+               }
+               else {
+                       cards = Object.entries(this.pairs).flat()
+                               .map(e => e.toString())
+               }
+
+               cards.shuffle().forEach((word, seq) => {
+                       let ref = Math.abs(word);
+                       put(this.form,
+                               'figure>img[src=$]<', `/data/word/32/${ref}.jpg`, {
+                                       onclick: e => this.turn(e),
+                                       id: ref,
+                                       className: word < 0 ? 'mirror' : '',
+                                       index: seq,
+                               }
+                       );
                });
        }
 };