-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
228 lines (201 loc) · 6.76 KB
/
main.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
218
219
220
221
222
223
224
225
226
227
228
"use strict";
class SlideShow {
constructor(SETTINGS, data) {
this.LIMIT = SETTINGS.limit;
// slides is a list of URLs
this.slides = [];
this.position = -1; // this will be immediately incremented
this.PRELOAD_LIMIT = 5;
this.data = data;
this.urls = data.urls;
}
// generator function - generates elements for a randomized array of urls
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
*
generateUrls() {
while (true) {
if (this.urls.length == 0) {
this.urls = this.data.urls;
console.debug("reset");
}
// select a random url
let randChoice = this.urls[Math.floor(Math.random() * this.urls.length)];
// remove it from the list
this.urls = this.urls.filter((url) => url !== randChoice);
// if we have used all the urls, reset the list
console.debug(randChoice);
yield randChoice;
}
}
// asynchronous image preload
_preloadImage(url) {
(async() => {
var img = new Image();
img.src = url;
})();
}
_shouldEndGame() {
switch (this.LIMIT.type) {
case "slides":
// remember this.position is zero-indexed
return this.position >= this.LIMIT.amount;
// the game will be ended by a timer, and slides are infinite
case "minutes":
return false;
// no other types are supported
default:
return true;
}
}
_update() {
// make sure we always have 5 preloaded slides ready
while (this.position > this.slides.length - this.PRELOAD_LIMIT) {
let url = this.generateUrls().next().value;
this._preloadImage(url);
this.slides.push(url);
}
if (this._shouldEndGame()) {
endGame();
}
if (this.LIMIT.type === "slides") {
showProgress(`${this.position + 1} / ${this.LIMIT.amount}`);
}
}
_setImageToCurrentPosition() {
const display = document.getElementById("game-display");
display.src = this.slides[this.position];
}
next() {
this.position++;
this._update();
this._setImageToCurrentPosition();
}
last() {
if (this.position > 0) {
this.position--;
}
this._update();
this._setImageToCurrentPosition();
}
}
// Note that a const binding != const values
const settings = {
ALLOWED_TYPES: ["slides", "minutes"],
// note that limit needs to be an object in order to avoid being passed by value
limit: {
type: "slides",
amount: 15,
},
};
function showInputError(message) {
document.getElementById("input-error-message").textContent = message;
}
function showProgress(text) {
document.getElementById("progress").textContent = text;
}
function startTimer(endTime /* as a Date */ ) {
var x = setInterval(function() {
// Get today's date and time
var now = new Date().getTime();
// Find the distance between now and the count down date
var distance = endTime.getTime() - now;
// TODO: Fix when distance >= 60min
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
showProgress(minutes + "m " + seconds + "s ");
// If the count down is finished, write some text
if (distance < 0) {
clearInterval(x);
showProgress("TIME'S UP");
endGame();
}
}, 900);
}
function milliseconds(min, sec = 0) {
return (min * 60 + sec) * 1000;
}
// state management
function startGame(type, amount) {
loadSlideshow();
//const type = document.getElementById("limit-type").value;
//const amount = document.getElementById("amount").value;
if (parseInt(amount) == NaN || parseInt(amount) < 1) {
showInputError("Please enter a number amount greater than 1");
return;
}
if (!settings.ALLOWED_TYPES.includes(type)) {
showInputError("Please choose an allowed type");
return;
}
showInputError(""); // clear errors
settings.limit.amount = parseInt(amount);
settings.limit.type = type;
if (type === "minutes") {
// end game in a few minutes
showProgress(`${settings.limit.amount} m 00s`);
startTimer(new Date(Date.now() + milliseconds(settings.limit.amount)));
} else if (type === "slides") {
showProgress(`1 / ${settings.limit.amount}`);
}
// show the user the game
hideElementIds( /*"MainMenuContainer",*/ "start-screen", "end-screen");
showElementIds("progress", "main");
showElementIdsAsFlex("game-window", "footer");
}
function endGame() {
hideElementIds("start-screen", "game-window", "progress", "footer");
showElementIdsAsFlex("end-screen");
//I bestow my garbage code upon yours! HUZZAH!
let elementendscreen = document.getElementById('end-screen');
elementendscreen.classList.add('intrologo');
setTimeout(function() { reopenMenu(); }, 1500);
}
function hideElementIds(...ids) {
for (const id of ids) {
styleElement(id, "none");
}
}
function showElementIds(...ids) {
for (const id of ids) {
styleElement(id, "block");
}
}
function showElementIdsAsFlex(...ids) {
for (const id of ids) {
styleElement(id, "flex");
}
}
function styleElement(id, style) {
for (const ele of document.querySelectorAll(`#${id}`)) {
ele.style.display = style;
}
}
// game logic
// loadSlideshow is a bodge - Go back and fix later
function loadSlideshow() {
(async() => {
const response = await fetch("./urls.json");
if (response.status !== 200) {
console.error(
"Looks like there was a problem. Status Code: " + response.status
);
return;
}
const data = await response.json();
// TODO: remove
let urls = data.urls;
console.debug(data);
console.debug(urls);
const presentation = new SlideShow(settings, data);
console.log("Loaded slideshow");
// note the anonymous function is necessary to maintain the proper this context
document
.getElementById("game-display")
.addEventListener("click", () => presentation.next());
document
.getElementById("last")
.addEventListener("click", () => presentation.last());
// set initial picture
presentation.next();
})();
}