-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
413 lines (378 loc) Β· 10.7 KB
/
index.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
let places = [
{
name: 'Admin Office',
x: 482.0,
y: 390.0,
z: 3.0
},
{
name: 'Lower Auditorium',
x: 482.0,
y: 390.0,
z: 4.0
},
{
name: 'Upper Auditorium',
x: 482.0,
y: 390.0,
z: 5.0
},
{
name: 'Seminar Room 2A',
x: 386.0,
y: 368.0,
z: 6.0
},
{
name: 'Chemistry Lab 3',
x: 386.0,
y: 368.0,
z: 3.0
},
{
name: 'Seminar Room 2B',
x: 386.0,
y: 337.0,
z: 6.0
},
{
name: 'Chemistry Lab 2',
x: 386.0,
y: 327.0,
z: 3.0
},
{
name: 'Seminar Room 2C',
x: 386.0,
y: 317.0,
z: 6.0
},
{
name: 'Seminar Room 1',
x: 450.0,
y: 316.0,
z: 6.0
},
{
name: 'Chemistry Lab 1',
x: 450.0,
y: 316.0,
z: 3.0
},
{
name: 'Math ICT Lab',
x: 330.0,
y: 364.0,
z: 6.0
},
{
name: 'Biology Lab 1',
x: 330.0,
y: 364.0,
z: 3.0
},
{
name: 'Applied Technology Lab',
x: 269.0,
y: 373.0,
z: 6.0
},
{
name: 'Biology Lab 2',
x: 269.0,
y: 373.0,
z: 3.0
},
{
name: 'Clean Energy Lab',
x: 284.0,
y: 326.0,
z: 6.0
},
{
name: 'Biology Lab 3',
x: 284.0,
y: 326.0,
z: 3.0
},
{
name: 'Netball Court',
x: 151.0,
y: 426.0,
z: 5.0
},
{
name: 'Hall',
x: 151.0,
y: 426.0,
z: 2.0
},
{
name: 'Lab Manager Room',
x: 232.0,
y: 340.0,
z: 6.0
},
{
name: 'Physics Lab 1',
x: 232.0,
y: 330.0,
z: 3.0
},
{
name: 'Computer Lab 3',
x: 232.0,
y: 330.0,
z: 4.0
},
{
name: 'Synthetic Chemistry Lab',
x: 232.0,
y: 320.0,
z: 6.0
},
{
name: 'IT Helpdesk',
x: 197.0,
y: 301.0,
z: 4.0
},
{
name: 'Computer Lab 2',
x: 197.0,
y: 282.0,
z: 4.0
},
{
name: 'Analytical Chemistry Lab',
x: 197.0,
y: 291.0,
z: 6.0
},
{
name: 'Physics Lab 2',
x: 197.0,
y: 291.0,
z: 3.0
},
{
name: 'Life Sciences Lab',
x: 173.0,
y: 330.0,
z: 6.0
},
{
name: 'Physics Lab 3',
x: 173.0,
y: 330.0,
z: 3.0
},
{
name: 'Computer Lab 1',
x: 173.0,
y: 330.0,
z: 4.0
},
{
name: 'Canteen',
x: 154.0,
y: 604.0,
z: 1.0
},
{
name: 'Concourse',
x: 323.0,
y: 410.0,
z: 3.0
},
{
name: 'Staffroom',
x: 323.0,
y: 410.0,
z: 2.0
}
];
let scaleFactor = 1.7353;
let maxDistance = 414;
let activeGuess = 1;
let win = false;
let date = getDate();
let correct = places[Math.floor((mulberry32(cyrb32(date))()*100000))%32];
for (let i = 1; i <= 6; i++) {
let element = document.getElementById(`guess${i}`);
// add event listener
element.addEventListener('keyup', onKeyPress);
// 'disable' all but the first guess
if (i != activeGuess) element.disabled = true;
else element.disabled = false;
}
let activeInput = document.getElementById('guess1');
//cookie shenanigans
if(!checkCookieExists("date") || getCookieValue("date") != date.toString()){
deleteCookies();
document.cookie = "date=" + date.toString(0);
}
else{
for (let i = 1; i <= 6; i++) {
if(checkCookieExists('guess' + i.toString())){
answerEntered(getCookieValue('guess' + i.toString()));
}
}
}
// actual functions
function onKeyPress(event) {
let val = this.value; // get string in the textbox
if (event.code === 'Enter') {
// find place that matches half-filled text - autocomplete algorithm is used here btw:
let selectedPlace = places.find((place) => place.name.substr(0, val.length).toUpperCase() == val.toUpperCase());
if (selectedPlace) {
this.value = selectedPlace.name;
closeAllLists();
return answerEntered(selectedPlace.name);
}
}
// update autocomplete
// close any already open lists of autocompleted values
closeAllLists();
// if input box is empty dont show anything
if (!val) return;
// create a div that will contain all the autocomplete values...
let div = document.createElement('DIV');
div.setAttribute('id', this.id + 'autocomplete-list');
div.setAttribute('class', 'autocomplete-items max-h-80 overflow-auto');
// ... and append it as a child of the autocomplete container:
this.parentNode.parentNode.appendChild(div);
// for each prompt to be shown...
for (i = 0; i < places.length; i++) {
// check if the prompt starts with the same letters as input as autocomplete algorithm - can replace with more fancy fuzzy searching later
let obj = places[i];
let name = obj.name;
if (name.substr(0, val.length).toUpperCase() == val.toUpperCase()) {
// create a div for the matching prompt
let subdiv = document.createElement('DIV');
// make the matching letters bold - can be changed if needed
subdiv.innerHTML = `<strong>${name.substr(0, val.length)}</strong>${name.substr(val.length)}`;
// insert a input field that will hold the current array item's value:
subdiv.innerHTML += `<input type='hidden' value='${name}'>`;
// execute a function when clicked on the item
subdiv.addEventListener('click', function (e) {
//insert the value for the autocomplete text field:
activeInput.value = this.getElementsByTagName('input')[0].value;
// close the list of autocompleted values, (or any other open lists of autocompleted values:
closeAllLists();
// enter the answer
answerEntered(activeInput.value);
});
div.appendChild(subdiv);
}
}
}
function answerEntered(val) {
// deactivate earlier input box
let currentElement = document.getElementById(`guess${activeGuess}`);
currentElement.value = val;
currentElement.disabled = true;
let answer = places.find((e) => e.name == val);
let dx = correct.x - answer.x;
let dy = correct.y - answer.y;
let dz = correct.z - answer.z;
// temp scaling:
let distance = Math.sqrt(dx ** 2 + dy ** 2)/scaleFactor;
let trueDistance = Math.sqrt(dx ** 2 + dy ** 2 + dy ** 2 * 10)/scaleFactor;
let win = distance == 0;
let exactDist = document.getElementById(`dist${activeGuess}`);
let floor = document.getElementById(`floor${activeGuess}`);
exactDist.innerText = `${distance.toFixed(1)}m`;
floor.innerText = `${Math.abs(dz.toFixed(0))}`; // add plus sign if positive
if (win && dz == 0) {
console.log('win');
floor.innerText = 'π';
exactDist.classList.toggle('invisible');
axios.post('http://peppered-aware-quiet.glitch.me/submit', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
// set green bar:
// scale the distance up, and make lower values of distance better. MUST BE CHANGED TO FIT DATA RANGE
distance = (maxDistance - trueDistance)/maxDistance * 100;
currentElement.style = `background: linear-gradient(to right, #19a7a7 ${distance}%, #374151 ${distance}% 100%)`;
document.cookie = "guess" + activeGuess.toString() + "=" + val + ";";
if (win && dz==0) {
const ans = document.getElementById('ans');
ans.innerText = `ππππππππππππ`;
ans.classList.toggle('hidden');
return;
} else if (activeGuess >= 6) {
// game over
// show the answer:
const ans = document.getElementById('ans');
ans.innerText = `The answer was ${correct.name}`;
ans.classList.toggle('hidden');
return;
} else{
activeGuess++;
};
// 'activate' next input box
currentElement = document.getElementById(`guess${activeGuess}`);
currentElement.disabled = false;
currentElement.classList.remove('placeholder-gray-400');
currentElement.classList.add('placeholder-gray-100');
// set cursor on next input
currentElement.focus();
currentElement.select();
// update activeInput variable
activeInput = document.getElementById(`guess${activeGuess}`);
}
function closeAllLists(elmnt) {
// close all autocomplete lists in the document, except the one passed as an argument:
var x = document.getElementsByClassName('autocomplete-items');
for (var i = 0; i < x.length; i++) {
if (elmnt != x[i] && elmnt != activeInput) x[i].parentNode.removeChild(x[i]);
}
}
// random number generator codes (stolen from https://stackoverflow.com/questions/521295/seeding-the-random-number-generator-in-javascript)
function cyrb32(str) {
let h1 = 1779033703, h2 = 3144134277,
h3 = 1013904242, h4 = 2773480762;
for (let i = 0, k; i < str.length; i++) {
k = str.charCodeAt(i);
h1 = h2 ^ Math.imul(h1 ^ k, 597399067);
h2 = h3 ^ Math.imul(h2 ^ k, 2869860233);
h3 = h4 ^ Math.imul(h3 ^ k, 951274213);
h4 = h1 ^ Math.imul(h4 ^ k, 2716044179);
}
h1 = Math.imul(h3 ^ (h1 >>> 18), 597399067);
h2 = Math.imul(h4 ^ (h2 >>> 22), 2869860233);
h3 = Math.imul(h1 ^ (h3 >>> 17), 951274213);
h4 = Math.imul(h2 ^ (h4 >>> 19), 2716044179);
return (h1^h2^h3^h4)>>>0;
}
function mulberry32(a) {
return function() {
var t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296;
}
}
function getDate() {
const d = new Date();
return d.getUTCDate().toString()+"-"+d.getUTCMonth().toString()+"-"+d.getUTCFullYear().toString();
}
// cookie code
function checkCookieExists(str) {
return document.cookie.split(';').some((item) => item.trim().startsWith(str + '='));
}
function getCookieValue(str) {
return document.cookie.split('; ').find((row) => row.startsWith(str + '='))?.split('=')[1];
}
function deleteCookies() {
document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().toUTCString() + ";path=/"); });
}