$(call cmdsave,$<)
data/wordlist.%.inc.pl: tools/mkwordlist data/wordlist.version.txt
$(call cmdsave,$< $*)
+data/wordlist.%.json: data/wordlist.%.inc.pl
+ $(call cmdsave,perl -MJSON=encode_json -E "print encode_json(do \$$ARGV[0])" ./$<)
.SECONDARY: data/font/%.ttf
data/font/%.ttf:
exit;
}
+if ($Request and $Request =~ s{^quiz/?}{}) {
+ Html({
+ raw => <<'EOT',
+<script src="/wordquiz.js"></script>
+<style>
+.wrong {background: red}
+.good {background: green}
+</style>
+EOT
+ });
+ say '<h1>quiz</h1><p id="quiz">test</p>';
+ exit;
+}
+
my $lang = $get{lang} || 'en';
my $wordlist = "data/wordlist.$lang.inc.pl";
my $limit = $get{v} // (exists $get{v} ? 4 : 3);
--- /dev/null
+let quiz = {
+dataurl: '/data/wordlist.nl.json',
+
+next: () => {
+ let word = quiz.words.shift();
+ let question = document.createElement('img');
+ question.src = `/data/word/en/${word[0]}.jpg`;
+ question.style.maxWidth = '50%';
+
+ let answers = [word[2], quiz.words[1][2], quiz.words[2][2], quiz.words[3][2]]
+ .sort(() => {return .5 - Math.random()}) // shuffle
+ let form = document.createElement('ul');
+ answers.forEach(suggest => {
+ let option = document.createElement('li');
+ option.onclick = () => {
+ if (suggest != word[2]) {
+ // incorrect
+ option.classList.add('wrong');
+ return;
+ }
+ option.classList.add('good');
+ window.setTimeout(quiz.next, 500);
+ };
+ option.append(suggest);
+ form.append(option);
+ });
+ quiz.form.append(question, form);
+},
+
+setup: () => {
+ fetch(quiz.dataurl).then(res => res.json()).then(json => {
+ quiz.form = document.getElementById('quiz');
+ quiz.words = Object.values(json)
+ .sort(() => {return .5 - Math.random()}) // shuffle
+ .map(row => row.split(/:/))
+ quiz.next();
+ });
+},
+};
+
+quiz.setup();