-
Notifications
You must be signed in to change notification settings - Fork 1
/
goals17.html
235 lines (230 loc) · 7.15 KB
/
goals17.html
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
<!DOCTYPE html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width">
<title>SDGs GOAL17QUIZ / ゴール17クイズ</title>
<meta property="og:title" content="SDGs GOAL17QUIZ / ゴール17クイズ">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://code4sabae.github.io/goalquiz/goals17.png">
<meta property="og:image" content="https://code4sabae.github.io/goalquiz/goals17.png">
<style>
body {
text-align: center;
margin: 0;
padding: 0;
font-family: sans-serif;
}
h1 {
margin: 0;
padding: .8em;
background-color: #f1f1f1;
margin-bottom: 1vw;
}
.result {
font-size: 3vw;
height: 2em;
}
.quiz {
display: inline-block;
text-align: left;
padding: 2vw;
font-size: 4vw;
margin: 0;
min-height: 16em;
box-sizing: border-box;
border: .3vw solid gray;
width: 93vw;
}
.logos {
display: inline-block;
text-align: left;
margin-top: 1vw;
width: 93vw;
line-height: 0;
}
.panel {
display: inline-block;
width: 15vw;
height: 15vw;
margin: .2vw .2vw;
text-align: center;
line-height: 15vw;
font-size: 7vw;
}
a {
color: gray !important;
}
@media (min-aspect-ratio: 1/1) {
.result {
font-size: 3vh;
}
.quiz {
font-size: 3vh;
min-height: 12em;
}
.panel {
width: 9.8vw;
height: 9.8vw;
line-height: 10vw;
font-size: 5vw;
}
}
</style>
<script type="module">
import { CSV } from "https://code4sabae.github.io/js/CSV.js";
import { shuffle } from "https://code4sabae.github.io/js/shuffle.js";
import { sleep } from "https://code4sabae.github.io/js/sleep.js";
import {} from "https://code4sabae.github.io/leaflet-mjs/qr-code.mjs";
const adujustColor = (ele) => {
const bg = ele.style.backgroundColor;
let dark = false;
const n = bg.match(/rgb\((\d+), (\d+), (\d+)\)/);
if (n) {
dark = (parseInt(n[1]) + parseInt(n[2]) + parseInt(n[3])) / 3 < 120;
}
ele.style.color = dark ? "white" : "black";
};
window.onload = async () => {
const baseurl = "https://code4sabae.github.io/SDGs_ja/";
const goals = CSV.toJSON(await CSV.fetch(baseurl + "SDG-goals.csv"));
const body = document.body;
add(body, "h1", "SDGs " + lang("ゴール17クイズ", "GOAL17QUIZ"));
const result = add(body, "div");
result.className = "result";
const quiz = add(body, "div");
quiz.className = "quiz";
const logos = add(body, "div");
logos.className = "logos";
const sels = [];
for (const goal of goals) {
console.log(goal);
const div = document.createElement("div");
div.textContent = goal.goal;
div.style.backgroundColor = goal.color;
adujustColor(div);
div.className = "panel";
logos.appendChild(div);
sels.push(div);
}
const credit = add(body, "div");
credit.className = "credit";
add(credit, "span", "App: CC BY @taisukef", "https://fukuno.jig.jp/3615");
add(credit, "span", " ");
add(credit, "span", "(src on GitHub)", "https://github.com/code4sabae/goalquiz");
add(credit, "br");
add(credit, "div", "Data: SDGs_ja on GitHub", "https://github.com/code4sabae/SDGs_ja");
add(credit, "span", "Japanese", "#ja");
add(credit, "span", " / ");
add(credit, "span", "English", "#en");
add(body, "qr-code");
for (;;) {
const qgoals = [];
for (const g of goals) {
qgoals.push(g);
}
shuffle(qgoals);
let point = 0;
for (let i = 0; i < 10; i++) {
quiz.innerHTML = " ";
quiz.style.backgroundColor = "white";
quiz.style.color = "black";
const goal = qgoals[i];
const desc = lang("description_ja", "description");
const title = lang("title_ja", "title");
const tid = startShowText(quiz, goal[desc], lang(50, 25));
const ans = await waitClick(sels) + 1;
clearInterval(tid);
quiz.textContent = goal[desc];
quiz.innerHTML += "<br>" + lang("あなたの回答", "Your Choice") + ":" + ans + " 「" + goals[ans - 1][title] + "」";
await sleep(300);
quiz.style.backgroundColor = goal.color;
quiz.style.color = "white";
if (ans == goal.goal) {
point++;
}
result.textContent = lang(
(i + 1) + "問中" + point + "問正解 正解率" + (point / (i + 1) * 100).toFixed(1) + "%",
"Q cnt " + (i + 1) + ", Corrects " + point + ", Rate " + (point / (i + 1) * 100).toFixed(1) + "%"
);
if (ans == goal.goal) {
quiz.innerHTML += "<br>" + lang("正解!", "Correct!");
await sleep(1000);
} else {
quiz.innerHTML += "<br>" + lang("まちがい! 正解は ", "Wrong.. Correct answer is ") + goal.goal + " 「" + goal[title] + "」";
quiz.innerHTML += "<br>" + lang("正解をタップして次の問題へ", "Go to click the correct answer.");
while (await waitClick(sels) + 1 != goal.goal);
}
}
const mes = point == 10 ? lang("全問正解!もう一度する?", "ALL CLEAR! RETRY?") : lang(point + "問正解!もう一度する?", point + " points, GAME SET! RETRY?");
if (!confirm(mes)) {
break;
}
}
};
const lang = (ja, en) => {
return getLang() == "ja" ? ja : en;
};
const getLang = () => {
const hash = document.location.hash;
if (hash.indexOf('en') >= 0) {
return "en";
}
if (hash.indexOf('ja') >= 0) {
return "ja";
}
const lang = (window.navigator.languages && window.navigator.languages[0]) || window.navigator.language || window.navigator.userLanguage || window.navigator.browserLanguage;
return lang;
};
const waitClick = async (sels) => {
return new Promise(resolve => {
if (Array.isArray(sels)) {
for (let i = 0; i < sels.length; i++) {
const sel = sels[i];
sel.wcid = i;
sel.onclick = function() {
resolve(this.wcid);
};
}
} else {
sels.onclick = function() {
resolve();
};
}
});
};
const startShowText = (div, text, waitms) => {
let n = 1;
return setInterval(() => {
if (n <= text.length) {
div.textContent = text.substring(0, n);
n++;
}
}, waitms);
};
const add = (parent, tag, txt, link) => {
const ele = (() => {
if (tag.indexOf("-") < 0) {
return document.createElement(tag);
} else {
return new (customElements.get(tag))();
}
})();
if (txt) {
if (link) {
const a = add(ele, "a", txt);
a.href = link;
if (link.startsWith("#")) {
a.onclick = () => {
location.href = a.href;
location.reload();
};
}
} else {
ele.textContent = txt;
}
}
parent.appendChild(ele);
return ele;
};
</script>
</head>
<body>
</body>
</html>