This repository has been archived by the owner on Feb 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
234 lines (200 loc) · 5.82 KB
/
client.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
229
230
231
232
233
234
/* client.js
* This is the code that is loaded by `index.html`.
* It is responsible for holding the wiki page in an iframe,
* and monitoring the user's progress.
* It might also submit but idk I haven't go that far.
*/
const levelsURL = "http://localhost:8443/levels.json";
var resp;
var xmlHttp;
/* XML Request on main thread is a bad idea. */
resp = "";
xmlHttp = new XMLHttpRequest();
if (xmlHttp != null) {
xmlHttp.open("GET", levelsURL, false);
xmlHttp.send(null);
resp = xmlHttp.responseText;
}
const levels = JSON.parse(resp);
/* end bad idea */
/* globally accessed variables: */
const settings = getLevelSettings();
const frame = document.getElementById("wikipedia-frame");
var viewedPages = [];
var totalLinks = 0;
// Check if an object has all of the correct
// properties to be a level
function validLevel(level) {
if (level === undefined) {
return false;
}
if (
level === levels[level.name] &&
level.startTime !== undefined &&
level.endTime !== undefined &&
level.startPage !== undefined &&
level.endPage !== undefined
) {
return true;
}
return false;
}
// Takes a string, returns last `/` to end
function parseLevelFromString(str) {
const lastSlash = str.lastIndexOf("/") + 1;
return str.slice(lastSlash);
}
// returns the settings for the level if the URL is valid
// otherwise returns undefined
function getLevelSettings() {
const levelName = parseLevelFromString(window.location.href);
if (validLevel(levels[levelName])) {
return levels[levelName];
} else {
return undefined;
}
}
function goTo(page) {
frame.src = `/wiki/${page}`;
}
function setUpCountDown() {
const level = getLevelSettings();
const timer = document.getElementById("time-left");
const endDate = Date.parse(level.endTime);
if (endDate === undefined) return undefined;
let timeLeft = setInterval(function () {
const date = new Date();
let seconds = (endDate - date) / 1000;
let minutes = Math.floor(seconds / 60);
seconds = seconds - minutes * 60;
// Update time on screen
if (minutes > 0) {
timer.textContent = `Time Left: ${minutes} minutes ${Math.round(
seconds
)} seconds.`;
} else if (seconds >= 0) {
timer.textContent = `Time Left: ${Math.round(seconds)} seconds.`;
} else {
timer.textContent = `Time ran out. :(`;
clearInterval(timeLeft);
}
// clear interval when time passes
if (Date.now() - endDate >= 0) {
timer.textContent = `Time ran out. :(`;
clearInterval(timeLeft);
alert("This level is over.");
}
}, 400);
}
setUpCountDown();
function startGame(level) {
const error = document.getElementById("error-text");
// Set iframe url:
if (level !== undefined) {
goTo(level.startPage);
error.textContent = "";
} else {
error.textContent = "Invalid game url.";
}
viewedPages = [];
// Set goal text:
document.getElementById("goal").textContent = `Goal: ${serialize(
level.endPage
)}`;
}
// Sets Iframe location on script load, and when `reset` is clicked
function loadClient() {
const level = getLevelSettings();
const startDate = Date.parse(level.startTime);
const error = document.getElementById("error-text");
let countdown = setInterval(function () {
const date = new Date();
let seconds = (startDate - date) / 1000;
let minutes = Math.floor(seconds / 60);
seconds = seconds - minutes * 60;
// Update time on screen
if (minutes > 0) {
error.textContent = `Starts in ${minutes} minutes ${Math.round(
seconds
)} seconds.`;
} else {
error.innerHTML = `Starts in ${
Math.round(seconds * 10) / 10
} seconds. <br> Goal: Go from ${serialize(
level.startPage
)} to ${serialize(level.endPage)}<br><br>
Remember that the goal is displayed in the bottom right.`;
}
// start game at correct time.
if (Date.now() - startDate >= 0) {
startGame(level);
clearInterval(countdown);
}
}, 100);
}
function submit() {
alert("Everything would have been submitted here.");
}
function serialize(name) {
return name.replaceAll("_", " ").replaceAll("%27", "'");
}
function unSerialize(name) {
return name.replaceAll(" ", "_").replaceAll("'", "%27");
}
// creates unordered list from array
function visualizeHistory(array) {
// Create the list element:
var list = document.createElement("ul");
for (var i = 0; i < array.length; i++) {
// Create the list items:
var item = document.createElement("li");
item.className = "history-element";
// let span = document.createElement("span");
let border = document.createElement("span");
border.className = "history-text";
let text = document.createTextNode(serialize(array[i]));
// append them
border.appendChild(text);
// span.appendChild(border);
item.appendChild(border);
list.appendChild(item);
}
// Finally, return the constructed list:
return list;
}
function reverseHistory(goal) {
let index = viewedPages.indexOf(goal);
if (index < 0) return undefined;
viewedPages.splice(index);
goTo(goal);
}
function setHistory() {
let frame = document.getElementById("history-frame");
frame.innerHTML = visualizeHistory(viewedPages).innerHTML;
frame.scrollLeft = 10000;
}
/* Functions that run at script load: */
loadClient();
document.getElementById("restart").addEventListener("click", loadClient);
// TODO - Run on Iframe start to load, not completion.
frame.addEventListener("load", async () => {
const page = parseLevelFromString(frame.contentWindow.location.href);
if (page === settings.endPage) {
submit();
}
totalLinks++;
if (page != viewedPages[viewedPages.length - 1]) {
viewedPages.push(page);
}
setHistory();
document.getElementById("total").textContent = `Total Links: ${totalLinks}`;
});
// navigate to page when history is clicked
document.getElementById("history-frame").addEventListener("click", (ele) => {
classClicked = ele.target.className;
if (classClicked !== "history-text") {
return undefined;
}
reverseHistory(unSerialize(ele.target.textContent));
});
setHistory();