-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
559 lines (458 loc) · 16.5 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
var canvas = document.querySelector('canvas');
var socket = new WebSocket("ws://99.241.147.138:1345");
canvas.width = 600;
canvas.height = 600;
var c = canvas.getContext('2d');
const name = prompt("What is your username?");
const username = Math.random().toString();
const grid = 10;
const BOARDWIDTH = canvas.width;
const BOARDHEIGHT = canvas.height;
const PIXELWIDTH = BOARDWIDTH / grid;
const PIXELHEIGHT = BOARDHEIGHT / grid;
const LADDERDIST = 20;
const VEL = 5;
const QUESTIONS = ["What is the opposite of conjunct music? (Luka);disjunct;antijunct;unjunct;dejunct",
"What does Mezzo Forte (mf) mean? (Luka);Moderately loud;Moderately quiet;Very loud;Very quiet",
"Which element of music best describes how 'high' or 'low' notes sound? (Luka);Pitch;Rhythm;Texture;Timbre",
"How fast is 60 bpm? (Luka);1 beat per second;2 beats per second;half a beat per second;4 beats per second",
"What is a polyphonic song? (Luka);A song with two or more melodic lines;A song with one melodic line;A melody supported by harmony;A harmony with no melody",
"What is timbre? (Luca);The pitch quality of a note.;How loud you play.;How long you hold a note.;The name of a note.",
"What is the order of the treble clef notes? (Luca);E,F,G,A,B,C,D,E,F;E,E,G,A,B,C,D,E,F;E,F,G,A,B,C,D,E,E;E,G,F,A,B,D,C,E,F",
"What does a Tie do? (Luca);Connects two notes of the same pitch;Make’s a note be longer by half its value.;Connects a bar of notes.;Connects three notes.",
"How long do you hold a dotted quarter note for? (Luca);1.5 beats.;Three beats.;Two beats.;0.5 beats",
"What does a dot beside a note do? (Luca);Adds half the length of the note to the total length.;Doubles the note length.;Adds quarter the length of the note to the total length.;Makes the note half the length.",
"_____ is the sound when 2 or more notes are played at the same time (David);Harmony;Melody;Rhythm;Meter",
"How many different elements of music are there (David);7;10;5;3",
"_____ is the first string instrument ever made that’s recorded;Lyres of Ur;Zeusaphone;Pikasso guitar;Cello horn",
"What is this sign called 𝄻 (David);Whole Rest;Half Rest;Staff;Measure",
"How many different notes are there in music (David);12;10;7;15",
"What is the smallest note (Gabriel);256th note;128th note;eight note;1 6th note",
"How many beats does a whole note last (Gabriel);4 beats;2 beats;1 beat;half a beat",
"What is another word for melody? (Louis);Tune;Rhythm;Dictation;Melody's just melody, duh",
"What is the word that conveys the length of a note? (Louis);Note value;Overtone;Form;Rhythm",
"If you were to portray soft, sad music, what would you use? (Louis);Mezzo Piano;Mezzo Forte;Fortissimo;Fortississimo",
"What is texture a mix of? (Louis);Tempo melodic;Form and timbre;Form and dynamics.;Dynamics and pitch"
];
var localPlayer = "";
var users = {};
var rawQuestion;
var question;
var correctIndex;
var roll;
var turn = false;
var rolled = false;
const pawns = ["Data/pawn_blue.png", "Data/pawn_green.png", "Data/pawn_orange.png",
"Data/pawn_pink.png", "Data/pawn_purple.png", "Data/pawn_red.png",
"Data/pawn_yellow.png"];
function User(colour, x, y) {
this.username = username;
this.name = name;
this.colour = colour;
this.x = x;
this.y = y;
}
socket.onopen = function(e) {
console.log("Connection established.");
if (name == undefined) {
name = "guest";
}
socket.send("COLR" + username);
};
socket.onmessage = function(event) {
var category = event.data.slice(0,4);
var data = event.data.slice(4);
if (category == "COLR") {
localPlayer = new User(data, 0, 540);
socket.send("JOIN" + JSON.stringify(localPlayer));
}
else if (category == "DATA") {
users = JSON.parse(data);
resetPlayer();
document.getElementById("users").innerHTML = "";
Object.entries(users).forEach(([key, value]) => {
document.getElementById("users").innerHTML += "<br><img src=\"Data/" + value.colour + ".png\" alt=\"User pawn\" height=\"42\" width=\"42\">\n" + value.name;
});
}
else if (category == "NPLR") {
var player = JSON.parse(data);
users[player["username"]] = player;
document.getElementById("users").innerHTML += "<br><img src=\"Data/" + player.colour + ".png\" alt=\"User pawn\" height=\"42\" width=\"42\">\n" + player.name;
}
else if (category == "MOVE") {
var player = JSON.parse(data);
users[player["username"]] = player;
}
else if (category == "PTUR") {
var player = JSON.parse(data);
if (player.username == username) {
turn = true;
document.getElementById("alert1").innerHTML = "It's your turn! Roll to begin.";
resetQuestions();
document.getElementById('answer1').innerHTML = '';
document.getElementById('answer2').innerHTML = '';
document.getElementById('answer3').innerHTML = '';
document.getElementById('answer4').innerHTML = '';
document.getElementById('question').innerHTML = 'Roll to get a question!';
} else {
document.getElementById("alert1").innerHTML = "It's " + player.name + "'s turn!";
}
}
else if (category == "WINR") {
var plyr = JSON.parse(data);
if (plyr.username == username) {
document.getElementById("alert1").innerHTML = "You won the game! Starting new game in 10 seconds...";
} else {
document.getElementById("alert1").innerHTML = plyr.name + " won the game! Starting new game in 10 seconds...";
}
setTimeout(() => {
resetPlayer();
if (turn) {
document.getElementById("alert1").innerHTML = "It's your turn! Roll to begin.";
}
}, 10000);
}
};
socket.onclose = function(event) {
if (event.wasClean) {
console.log(`[close] Connection closed cleanly, code=${event.code} reason=${event.reason}`);
} else {
// e.g. server process killed or network down
// event.code is usually 1006 in this case
console.log('[close] Connection died');
}
};
socket.onerror = function(error) {
alert('[Error] Could not connect to the server. Please try again later.');
};
function movement() {
socket.send("MOVE" + JSON.stringify(new User(localPlayer.colour, player.x, player.y)));
}
function endTurn() {
socket.send("DONE_");
turn = false;
rolled = false;
}
function win() {
socket.send("WINR" + username);
}
function shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
function ask_question() {
resetQuestions();
rawQuestion = choice(QUESTIONS).split(";");
question = rawQuestion[0];
var shuffled = shuffleArray([rawQuestion[1], rawQuestion[2], rawQuestion[3], rawQuestion[4]]);
correctIndex = shuffled.indexOf(rawQuestion[1]) + 1;
document.getElementById("question").innerHTML = question;
document.getElementById("answer1").innerHTML = shuffled[0];
document.getElementById("answer2").innerHTML = shuffled[1];
document.getElementById("answer3").innerHTML = shuffled[2];
document.getElementById("answer4").innerHTML = shuffled[3];
}
function optionClicked(option) {
resetQuestions();
if (turn && rolled) {
document.getElementById('option-' + option).checked = true;
if (correctIndex == option) {
player.move(roll);
} else {
document.getElementById('answer' + option).style.color = 'red';
}
document.getElementById('answer' + correctIndex).style.color = 'lime';
endTurn();
}
}
function resetQuestions() {
document.getElementById('option-1').checked = false;
document.getElementById('option-2').checked = false;
document.getElementById('option-3').checked = false;
document.getElementById('option-4').checked = false;
document.getElementById('answer1').style.color = 'white';
document.getElementById('answer2').style.color = 'white';
document.getElementById('answer3').style.color = 'white';
document.getElementById('answer4').style.color = 'white';
}
function notify(notification) {
document.getElementById("alert1").innerHTML = notification;
}
function randint(min, max) {
return Math.floor(Math.random() * Math.floor(max - min)) + min;
}
function choice(iterable) {
return iterable[randint(0, iterable.length)];
}
function get_floor(square) {
return Math.floor(square / grid) + 1;
}
function degToRad(d) {
return d * 0.01745;
}
function rollDice() {
if (turn && !rolled) {
roll = randint(1, 7);
notify("You rolled a " + roll.toString() + "!\nGet the question correct to move!");
ask_question();
rolled = true;
}
}
function draw_line(x1, y1, x2, y2, colour, thickness) {
c.strokeStyle = colour;
c.lineWidth = thickness;
c.beginPath();
c.moveTo(x1, y1);
c.lineTo(x2, y2);
c.stroke();
c.lineWidth = 1;
}
function find_angle(pos, vertex) {
var sX = vertex[0];
var sY = vertex[1];
try {
var angle = Math.atan((sY - pos[1]) / (sX - pos[0]))
} catch (err) {
var angle = (Math.pi / 2)
}
if (pos[1] <= sY && pos[0] >= sX) {
angle = Math.abs(angle);
} else if (pos[1] <= sY && pos[0] <= sX) {
angle = Math.PI - angle;
} else if (pos[1] >= sY && pos[0] <= sX) {
angle = Math.PI + Math.abs(angle);
} else if (pos[1] >= sY && pos[0] >= sX) {
angle = (Math.PI * 2) - angle;
}
angle -= 90 * Math.PI / 180;
return angle
}
// unused, but its too good to delete
function drawRotated(x, y, degrees, sprite, width, height) {
c.save();
c.translate(x, y);
c.rotate(degrees);
c.translate(-x, -y);
c.drawImage(sprite.image, sprite.src_x, sprite.src_y, sprite.src_width, sprite.src_height, x, y, width, height);
c.restore();
}
function Pixel(x, y, width, height, number) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.number = number;
this.draw = function() {
if (this.number % 2 == 0) {
c.fillStyle = "red";
c.fillRect(this.x, this.y, this.width, this.height);
}
c.fillStyle = "black";
c.strokeStyle = "black";
c.strokeRect(this.x, this.y, this.width, this.height);
c.font = "30px Arial";
var textwidth = c.measureText(this.number.toString()).width
c.fillText(this.number.toString(), (this.x + width / 2) - (textwidth / 2), this.y + this.width / 2 + 15);
}
}
function Sprite(image, width, height, src_x, src_y, src_width, src_height) {
// x, y, width, height is used to resize the image
// for src things you put the actual size of the image
this.image = new Image();
this.image.src = image;
this.width = width;
this.height = height;
this.src_x = src_x;
this.src_y = src_y;
this.src_width = src_width;
this.src_height = src_height;
this.draw = function(x, y) {
c.drawImage(this.image, this.src_x, this.src_y, this.src_width, this.src_height, x, y, this.width, this.height);
}
}
function Player(sprite) {
this.x = pixels[0].x;
this.y = pixels[0].y;
this.moved = false;
this.square = 1;
this.floor = 1;
this.perm_goal = 1;
this.temp_goal = 1;
this.sprite = sprite;
this.incline = undefined;
this.move = function(boardloc) {
if (this.moved || boardloc == 1) {
this.perm_goal = this.square + boardloc;
} else {
this.perm_goal = this.square + boardloc - 1;
}
this.temp_goal = this.square + 1;
this.moved = true;
}
this.update = function() {
this.floor = get_floor(this.square);
var temp_goal_floor = get_floor(this.temp_floor);
if (this.perm_goal != this.square) {
if (this.square >= 99) {
win();
} else {
if (this.floor % 2 == 1) {
if (this.x <= pixels[this.temp_goal].x || this.y <= pixels[this.temp_goal].y) {
if ((this.square + 1) % 10 == 0) {
this.y -= VEL;
movement();
} else {
this.x += VEL;
movement();
}
}
if (this.x >= pixels[this.temp_goal].x) {
if ((this.square + 1) % 10 != 0 || this.y <= pixels[this.temp_goal].y) {
this.square++;
this.temp_goal = this.square + 1;
this.x = pixels[this.temp_goal - 1].x;
this.y = pixels[this.temp_goal - 1].y;
}
}
}
else if (this.floor % 2 == 0) {
try {
if (this.x >= pixels[this.temp_goal].x || this.y <= pixels[this.temp_goal].y) {
if ((this.square + 1) % 10 == 0) {
this.y -= VEL;
movement();
} else {
this.x -= VEL;
movement();
}
}
if (this.x <= pixels[this.temp_goal].x) {
if ((this.square + 1) % 10 != 0 || this.y <= pixels[this.temp_goal].y) {
this.square++;
this.temp_goal = this.square + 1;
this.x = pixels[this.temp_goal - 1].x;
this.y = pixels[this.temp_goal - 1].y;
}
}
} catch (err) {
win();
}
}
}
} else {
for (var i = 0; i < inclines.length; i++) {
if (inclines[i].start == this.square + 1) {
this.incline = inclines[i];
}
}
}
if (this.incline != undefined) {
var y2 = pixels[this.incline.end - 1].y;
var y1 = pixels[this.incline.start - 1].y;
var x2 = pixels[this.incline.end - 1].x;
var x1 = pixels[this.incline.start - 1].x;
var angle = find_angle([x1, y1], [x2, y2]);
this.x += Math.sin(angle) * VEL;
this.y += Math.cos(angle) * VEL;
movement()
if (this.incline.type == 0 && this.y <= y2) {
this.square = this.incline.end - 1;
this.perm_goal = this.square;
this.incline = undefined;
this.x = x2;
this.y = y2;
}
if (this.incline.type == 1 && this.y >= y2) {
this.square = this.incline.end - 1;
this.perm_goal = this.square;
this.incline = undefined;
this.x = x2;
this.y = y2;
}
}
}
this.draw = function() {
this.sprite.draw(this.x + 9, this.y);
}
}
function Incline(start, end, sprite, type) {
this.start = start;
this.end = end;
this.type = type;
this.floor_difference = get_floor(this.end) - get_floor(this.start);
this.sprite = sprite;
this.x = Math.min([pixels[this.start - 1].x], pixels[this.end - 1].x);
this.y = Math.min([pixels[this.start - 1].y], pixels[this.end - 1].y);
this.draw = function() {
// bar 1
var x1 = pixels[this.start - 1].x + PIXELWIDTH / 2;
var y1 = pixels[this.start - 1].y + PIXELHEIGHT / 2 + LADDERDIST / 2;
var x2 = pixels[this.end - 1].x + PIXELWIDTH / 2;
var y2 = pixels[this.end - 1].y + PIXELHEIGHT / 2 + LADDERDIST / 2;
//draw_line(x1, y1, x2, y2, "blue", 5);
// bar 2
x1 = pixels[this.start - 1].x + PIXELWIDTH / 2;
y1 = pixels[this.start - 1].y + PIXELHEIGHT / 2 - LADDERDIST / 2;
x2 = pixels[this.end - 1].x + PIXELWIDTH / 2;
y2 = pixels[this.end - 1].y + PIXELHEIGHT / 2 - LADDERDIST / 2;
//draw_line(x1, y1, x2, y2, "blue", 5);
sprite.draw(this.x + PIXELWIDTH / 2, this.y + PIXELWIDTH / 2);
}
}
function resetPlayer() {
player = new Player(new Sprite("Data/" + localPlayer.colour + ".png", PIXELWIDTH, PIXELHEIGHT, 0, 0, 128, 128));
movement();
}
var pixels = [];
var counter = 0;
var direction = true;
for (var y = 10; y > 0; y--) {
if (direction) {
for (var x = 0; x < 10; x++) {
counter++;
pixels.push(new Pixel(x * PIXELWIDTH, y * PIXELHEIGHT - PIXELHEIGHT, PIXELWIDTH, PIXELHEIGHT, counter));
}
} else {
for (var x = 10; x > 0; x--) {
counter++;
pixels.push(new Pixel(x * PIXELWIDTH - PIXELWIDTH, y * PIXELHEIGHT - PIXELHEIGHT, PIXELWIDTH, PIXELHEIGHT, counter));
}
}
direction = !direction;
}
var inclines = [new Incline(83, 99, new Sprite("Data/ladder-2x2.png", 60, 60, 0, 0, 60, 60), 0),
new Incline(21, 42, new Sprite("Data/ladder-2x3.png", 60, 120, 0, 0, 60, 120), 0),
new Incline(28, 76, new Sprite("Data/ladder-4x6.png", 180, 600, 0, 0, 180, 600), 0),
new Incline(4, 14, new Sprite("Data/ladder-4x2.png", 180, 60, 0, 0, 180, 60), 0),
new Incline(29, 8, new Sprite("Data/snake-2x3.png", 60, 180, 0, 0, 60, 180), 1),
new Incline(77, 42, new Sprite("Data/snake-3x4.png", 120, 180, 0, 0, 120, 180), 1),
new Incline(44, 15, new Sprite("Data/snake-3x4-2.png", 180, 240, 0, 0, 180, 240), 1),
new Incline(92, 73, new Sprite("Data/snake-2x3-2.png", 60, 120, 0, 0, 60, 120), 1),
new Incline(86, 68, new Sprite("Data/snake-3x3.png", 180, 180, 0, 0, 180, 180), 1)];
var player = new Player(new Sprite("Data/pawn_blue.png", PIXELWIDTH, PIXELHEIGHT, 0, 0, 128, 128));
var connected = false;
function animate() {
requestAnimationFrame(animate);
c.clearRect(0, 0, innerWidth, innerHeight);
if (localPlayer != "" && !connected) {
connected = true;
player.sprite = new Sprite("Data/" + localPlayer.colour + ".png", PIXELWIDTH, PIXELHEIGHT, 0, 0, 128, 128)
}
for (var i = 0; i < pixels.length; i++) {
pixels[i].draw();
}
Object.entries(users).forEach(([key, value]) => {
var img = new Sprite("Data/" + value.colour + ".png", PIXELWIDTH, PIXELHEIGHT, 0, 0, 128, 128)
img.draw(value.x + 9, value.y);
});
player.update();
for (var i = 0; i < inclines.length; i++) {
inclines[i].draw();
}
}
animate();