-
Notifications
You must be signed in to change notification settings - Fork 13
/
script.js
217 lines (167 loc) · 5.63 KB
/
script.js
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
const store = {
deaths: [],
score: 0,
highScore: 0,
question: {},
};
function setDeaths(deaths) {
store.deaths = deaths;
return deaths;
}
function setQuestion(question) {
store.question = question;
return question;
}
function incrementScore() {
const newScore = store.score + 1;
store.score = newScore;
return newScore;
}
function saveHighScore() {
if (store.score <= store.highScore) {
return;
}
localStorage.setItem('highScore', store.score)
}
function showHighScore() {
const newHighScore = localStorage.getItem('highScore') || 0;
store.highScore = newHighScore;
document.querySelector('.js-high-streak').innerText = newHighScore;
return newHighScore;
}
function ajaxGetDeaths() {
return new Promise((resolve, reject) => {
fetch('/deaths.json').then(async res => {
const deaths = await res.json();
resolve(deaths);
}).catch(reject);
})
}
function getCharDiedBefore(compareCharIndex) {
const { deaths } = store;
const randomCharIndex = Math.floor(Math.random() * compareCharIndex);
return deaths[randomCharIndex];
}
function getCharDiedAfter(compareCharIndex) {
const { deaths } = store;
const numDeaths = deaths.length;
const randomCharIndex = Math.floor(Math.random() * (numDeaths - compareCharIndex)) + compareCharIndex - 1;
return deaths[randomCharIndex];
}
function getRandomQuestion() {
const { deaths } = store;
const numDeaths = deaths.length;
/* never give the first person who died and the last person who died.
This is done to ensure that both options are not correct.
*/
const randomCompareCharIndex = Math.floor(Math.random() * numDeaths) + 1;
const compareChar = deaths[randomCompareCharIndex];
const deathTime = Math.random() >= 0.5 ? 'after' : 'before';
const charDiedBefore = getCharDiedBefore(randomCompareCharIndex);
const charDiedAfter = getCharDiedAfter(randomCompareCharIndex);
const answer = deathTime === 'before' ? charDiedBefore : charDiedAfter;
return {
deathTime,
compareChar,
charDiedBefore,
charDiedAfter,
answer,
};
}
function handleClickOption(event) {
const { question } = store;
const selectedOptionElem = event.target;
const selectedOptionText = event.target.innerText;
if (!selectedOptionElem.classList.contains('js-option')) {
return;
}
const isCorrect = selectedOptionText === question.answer;
// const isCorrect = true;
const buttonClass = isCorrect ? 'is-option-correct' : 'is-option-incorrect';
selectedOptionElem.classList.add(buttonClass);
if (!isCorrect) {
endGame();
return;
}
incrementScore();
const newScore = store.score;
document.querySelector('.js-streak').innerText = newScore;
setTimeout(() => {
playGame();
}, 300);
}
function renderQuestion() {
const { question } = store;
var questionHeader = document.querySelector('.js-quiz-question');
var questionOptions = document.querySelector('.js-options-list');
questionHeader.classList.add('anim-fade-in');
questionOptions.classList.add('anim-fade-in-buttons');
setTimeout(function() {
questionHeader.classList.remove('anim-fade-in');
questionOptions.classList.remove('anim-fade-in-buttons');
}, 2000);
document.querySelector('.js-death-time').innerText = question.deathTime;
document.querySelector('.js-death-character').innerText = question.compareChar;
const buttonElems = document.querySelectorAll('.js-option');
const randomNum = Math.random();
const optionAIndex = randomNum >= 0.5 ? 1 : 0;
const optionBIndex = randomNum >= 0.5 ? 0 : 1;
buttonElems.forEach(elem => {
elem.classList.remove('is-option-correct');
elem.classList.remove('is-option-incorrect');
});
buttonElems[optionAIndex].innerText = question.charDiedBefore;
buttonElems[optionBIndex].innerText = question.charDiedAfter;
}
function renderGameOver() {
document.querySelector('.js-options-list').classList.add('is-hidden');
document.querySelector('.js-quiz-question').classList.add('is-hidden');
document.querySelector('.js-tweet-block').classList.remove('is-hidden');
document.querySelector('.js-tweet').href = 'https://twitter.com/intent/tweet?text=Play%20GOT%20Death%20Quiz.%20I%20have%20scored%20%23' + store.score + '&url=https%3A%2F%2Fgot-death-quiz.netlify.com&hashtags=roadtohacktober,hacktoberfest';
document.querySelector('.js-game-over').classList.remove('is-hidden');
document.querySelector('.js-game-restart').addEventListener('click', (event) => {
restartGame(event);
});
}
function renderRestartGame() {
document.querySelector('.js-streak').innerText = store.score;
document.querySelector('.js-game-over').classList.add('is-hidden');
document.querySelector('.js-options-list').classList.remove('is-hidden');
document.querySelector('.js-quiz-question').classList.remove('is-hidden');
}
function resetScore() {
store.score = 0;
}
function restartGame() {
resetScore();
renderRestartGame();
playGame();
}
function playGame() {
const question = getRandomQuestion();
setQuestion(question);
// console.log('answer', question.answer); // aditodo remove this
renderQuestion();
}
function endGame() {
// other stuff to do when game over say saving score will be done here.
saveHighScore();
showHighScore();
renderGameOver();
}
function playBackgroundMusic() {
document.body.addEventListener('click', function (event) {
document.getElementById('game-music').play();
}, false);
}
async function init() {
showHighScore();
const deaths = await ajaxGetDeaths();
setDeaths(deaths);
playBackgroundMusic();
playGame();
document.querySelector('.js-options-list').addEventListener('click', (event) => {
handleClickOption(event);
});
}
init();