-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
175 lines (157 loc) · 5.57 KB
/
script.js
File metadata and controls
175 lines (157 loc) · 5.57 KB
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
const homeButton = document.getElementById("home-button");
const groupContainer = document.getElementById("group-container");
const questionContainer = document.getElementById("question-container");
const answerContainer = document.getElementById("answer-container");
const question = document.getElementById("question");
const questionText = document.getElementById("question-text");
const questionImage = document.getElementById("question-image");
const correctContainer = document.getElementById("correct-container");
const correctText = document.getElementById("correct-text");
const correctImg = document.getElementById("correct-image");
let content = {};
let correctSounds = [];
let wrongSounds = [];
fetch("content.json")
.then((response) => response.json())
.then((data) => {
content = data;
correctSounds = data.correctSounds.map((sound) => new Audio(sound));
wrongSounds = data.wrongSounds.map((sound) => new Audio(sound));
showStartScreen();
});
homeButton.addEventListener("click", showStartScreen);
document.addEventListener("contextmenu", (event) => {
event.preventDefault();
});
if ("serviceWorker" in navigator) {
navigator.serviceWorker
.register("service-worker.js")
.then((registration) => {
console.log(
"Service Worker registered with scope:",
registration.scope
);
})
.catch((error) => {
console.error("Service Worker registration failed:", error);
});
}
function showStartScreen() {
hideAll();
groupContainer.style.display = "block";
for (const group of content.groups) {
const button = document.createElement("button");
button.innerText = group.name;
const img = document.createElement("img");
img.classList.add("grid-item");
img.src = group.image;
img.setAttribute("draggable", false);
button.prepend(document.createElement("br"));
button.prepend(img);
button.addEventListener("click", () => startGroup(group));
groupContainer.appendChild(button);
}
}
function startGroup(group) {
preprocess(group);
group.questions.sort(() => Math.random() - 0.5);
showQuestion(group, 0);
}
function preprocess(group) {
if (group.questions !== "auto") return;
group.questions = [];
for (let i = group.from; i <= group.to; i += group.answers + 1) {
const question = {
question: "",
questionImage: `${group.folder}/${i}.${group.type}`,
answers: [],
correct: "Ja!",
};
for (let j = 1; j <= group.answers; j++) {
question.answers.push(`${group.folder}/${i + j}.${group.type}`);
}
group.questions.push(question);
}
}
function showQuestion(group, questionIndex) {
hideAll();
questionContainer.style.display = "block";
questionText.innerText = group.questions[questionIndex].question;
questionImage.src = group.questions[questionIndex].questionImage;
questionImage.setAttribute("draggable", false);
questionContainer.addEventListener(
"click",
() => {
showAnswers(group, questionIndex);
},
{ once: true }
);
}
function showAnswers(group, questionIndex) {
hideAll();
answerContainer.style.display = "block";
question.style.display = "block";
let correctAnswer;
let answers = [];
question.innerText = group.questions[questionIndex].question;
answers = [...group.questions[questionIndex].answers];
correctAnswer = group.questions[questionIndex].answers[0];
answers.sort(() => Math.random() - 0.5);
for (const answer of answers) {
const img = document.createElement("img");
img.classList.add("grid-item");
img.src = answer;
img.setAttribute("draggable", false);
img.addEventListener("click", () => {
if (img.src.includes(correctAnswer)) {
pickRandom(correctSounds).play();
img.style.filter =
"sepia(100%) saturate(500%) hue-rotate(30deg)";
setTimeout(() => {
showCorrectScreen(group, questionIndex);
}, 500);
} else {
pickRandom(wrongSounds).play();
img.style.filter =
"sepia(100%) saturate(500%) hue-rotate(302deg)";
setTimeout(() => {
showQuestion(group, questionIndex);
}, 500);
}
});
answerContainer.appendChild(img);
}
}
function showCorrectScreen(group, questionIndex) {
hideAll();
correctContainer.style.display = "block";
correctText.innerText = group.questions[questionIndex].correct;
correctImg.src = group.questions[questionIndex].answers[0];
document.addEventListener(
"click",
() => {
if (questionIndex >= group.questions.length - 1) {
showStartScreen();
return;
}
showQuestion(group, questionIndex + 1);
},
{ once: true }
);
}
function hideAll() {
groupContainer.style.display = "none";
questionContainer.style.display = "none";
answerContainer.style.display = "none";
question.style.display = "none";
correctContainer.style.display = "none";
for (const child of answerContainer.children) {
child.style.display = "none";
}
for (const child of groupContainer.children) {
child.style.display = "none";
}
}
function pickRandom(array) {
return array[Math.floor(Math.random() * array.length)];
}