word/finder: debug styling for extended levels
[sheet.git] / word / memory.js
index 94836c282b54666093c396631cc56fe17c8840db..a70ec06638ac20ce07a2ec60bc2aff3803617ff8 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
@@ -30,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;
                }
@@ -39,26 +41,35 @@ class WordMemory extends WordQuiz {
                .forEach(card => put(card, '!.turn!.bad'));
        }
 
-       load(dataurl) {
-               if (dataurl) {
-                       super.load(dataurl);
-               }
-               else {
+       load() {
+               this.configure();
+               if (this.preset.pairs) {
                        this.dataurl = '/data/wordpairs.json';
                        fetch(this.dataurl).then(res => res.json()).then(pairs => {
                                this.pairs = pairs;
                                this.setup();
                        });
                }
+               else {
+                       super.load();
+               }
        }
 
        setup() {
+               super.setup();
                this.turned = [];
-               this.form = document.getElementById('quiz');
+               this.form.innerHTML = '';
+               this.form.className = '';
 
                let cards;
                if (this.words) {
-                       cards = this.words.splice(0, 6).map(row => row[2]);
+                       const aspect = this.form.clientWidth / window.innerHeight;
+                       //TODO image ratio
+                       let count = parseInt(this.preset.n) || 35;
+                       let cols = Math.round(Math.sqrt(count) * aspect**.5);
+                       count = cols * Math.ceil(count / cols);
+                       this.form.style['grid-template-columns'] = `repeat(${cols}, 1fr)`;
+                       cards = this.words.splice(0, count>>1).map(row => row[2]);
                        cards.push(...cards.map(val => -val));
                }
                else {
@@ -66,11 +77,15 @@ class WordMemory extends WordQuiz {
                                .map(e => e.toString())
                }
 
-               cards.shuffle().forEach(word => {
+               cards.shuffle().forEach((word, seq) => {
                        let ref = Math.abs(word);
                        put(this.form,
-                               'figure>img[src=$]<', `/data/word/en/${ref}.jpg`,
-                               {onclick: e => this.turn(e), id: ref, className: word < 0 ? 'mirror' : ''}
+                               'figure>img[src=$]<', `/data/word/32/${ref}.jpg`, {
+                                       onclick: e => this.turn(e),
+                                       id: ref,
+                                       className: word < 0 ? 'mirror' : '',
+                                       index: seq,
+                               }
                        );
                });
        }