-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
181 lines (167 loc) · 5.47 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
const instructionsDiv = document.getElementById("instructions");
const gameDiv = document.getElementById("game_div");
const optionsDisplay = document.getElementById("options_display");
const imageDisplay = document.getElementById("image_display");
const timerDisplay = document.getElementById("timer_display");
const postGameDiv = document.getElementById("post_game_div");
const postScore = document.getElementById("post_score");
const scoreDisplay = document.getElementById("score");
const hintsDisplay = document.getElementById("hints_left");
const livesDisplay = document.getElementById("health");
let cachedRounds = [];
let articles;
let chosenArticleIndex;
let gameData;
let health;
let points;
let options;
let hintPos;
let timerStart;
let timer;
let hints;
//Load game data and start image caching
let gameDataRequest = new XMLHttpRequest();
gameDataRequest.onreadystatechange = function() {
if (gameDataRequest.readyState === 4 && gameDataRequest.status === 200) {
gameData = JSON.parse(gameDataRequest.responseText);
cacheRounds(5, 2);
let startButton = document.getElementById('main_start_button');
startButton.innerText = "Play";
startButton.disabled = false;
}
}
gameDataRequest.open("GET", "/wikihow_guess/articles_dir.txt");
gameDataRequest.send();
options = 6;
function startGame() {
instructionsDiv.style.display = "none";
postGameDiv.style.display = "none";
gameDiv.style.display = "flex";
hints = 4;
health = 3;
points = 0;
timerStart = 45;
hintsDisplay.innerText = hints;
livesDisplay.innerText = health;
startRound();
startTimer();
}
function optionClick(x) {
if (x == chosenArticleIndex) {
points++;
startRound();
} else {
--health;
livesDisplay.innerText = health;
let option = document.getElementById(`option${x}`)
option.className = "option btn btn-danger";
option.disabled = true;
if (health < 1)
endGame()
}
}
function startRound() {
scoreDisplay.innerText = points;
optionsDisplay.innerHTML = "";
imageDisplay.innerHTML = "";
let cachedArticles = cachedRounds.shift();
articles = cachedArticles["articles"];
chosenArticleIndex = cachedArticles["correct_index"];
hintPos = 0;
cacheRounds(1, 2);
displayHint(false);
for (let articleIndex in articles) {
let optionButton = document.createElement("button");
optionButton.onclick = function () {optionClick(articleIndex)}
optionButton.id = `option${articleIndex}`;
optionButton.className = "option btn btn-secondary";
optionButton.innerText = articles[articleIndex]["title"];
optionsDisplay.appendChild(optionButton);
}
}
//TODO Lock display hint button when appropriate
function displayHint(deductHint=true) {
if (deductHint)
if (hints > 0){
--hints;
hintsDisplay.innerText = hints;
} else {
return null;
}
let image = document.createElement("img");
image.src = articles[chosenArticleIndex]["images"][hintPos];
hintPos++;
imageDisplay.appendChild(image);
}
function endGame() {
clearTimeout(timer);
gameDiv.style.display = "none";
postGameDiv.style.display = "block";
postScore.innerText = points;
}
function cacheRounds(rounds=5, imagesPerRound=2) {
let chosenArticleIndexCache;
let articlesCache;
for (let round=0; round<rounds; round++) {
articlesCache = getRandomArticles(options);
chosenArticleIndexCache = Math.floor(Math.random() * articlesCache.length);
articlesCache[chosenArticleIndexCache]["images"] = shuffleArray(articlesCache[chosenArticleIndexCache]["images"]);
for (let image=0; image<imagesPerRound; image++)
cacheImg(articlesCache[chosenArticleIndexCache]["images"][image]);
cachedRounds.push({"articles": articlesCache, "correct_index": chosenArticleIndexCache});
}
}
function cacheImg(src) {
let temp_img = new Image();
temp_img.src = src;
temp_img.onerror = function () {cacheImgErrorHandler(src)};
}
function cacheImgErrorHandler(src) {
for (let key in cachedRounds) {
if (cachedRounds[key]["articles"][cachedRounds[key]["correct_index"]]["images"].includes(src)) {
cachedRounds.splice(key, 1);
cacheRounds(1, 2);
break;
}
}
}
function shuffleArray(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
function getRandomArticles(n) {
let x;
let requiredLen = 3;
var result = new Array(n),
len = gameData.length,
taken = new Array(len);
if (n > len)
throw new RangeError("getRandom: more elements taken than available");
while (n--) {
while (typeof result[n] === 'undefined' || result[n]["images"].length < requiredLen) {
x = Math.floor(Math.random() * len);
result[n] = gameData[x in taken ? taken[x] : x];
}
taken[x] = --len in taken ? taken[len] : len;
}
return result;
}
function startTimer() {
timerDisplay.innerText = timerStart;
timer = setTimeout(timerTimeoutHandler, 1000);
}
function timerTimeoutHandler() {
let presentTime = parseInt(timerDisplay.innerText);
if (presentTime > 0) {
timerDisplay.innerText = --presentTime;
timer = setTimeout(timerTimeoutHandler, 1000);
} else {
endGame();
}
}