forked from petrosdawit/brown-clps-research-prototype
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
534 lines (410 loc) · 14.7 KB
/
game.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
var socket = io.connect();
var round = 1;
var gameArr = new Array(10);
var t0 = 0;
var t1 = 0;
//keeps track of who is supposed to win each round
for (var i = 0; i<gameArr.length; i++){
if (i == 3 || i == 7 || i == 10){
gameArr[i] = 0;
} else {
gameArr[i] = 1;
}
}
var index = 0;
var sound = new Audio("Mariokart.mp3");
var i = 0;
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d");
var ctx1 = canvas.getContext("2d");
ctx.fillStyle = "#222";
ctx.strokeStyle="#111";
ctx.lineWidth=6;
ctx.strokeRect(0,0,90,140);
ctx.fillRect(4,4,82,132);
//set height of game body div
var bodyheight = $(document).height()-130;
$("#gameBody").height(bodyheight);
//instructions modal
var instModal = document.getElementById('inst');
// Get the button that opens the modal
var instBtn = document.getElementById("instBtn");
// Get the span for instructions
var span1 = document.getElementsByClassName("close1")[0];
// Get the win modal
var winModal = document.getElementById('win');
// Get the lose modal
var loseModal = document.getElementById('lose');
//get convertModal
var convertModal = document.getElementById('convertModal');
// Get the <span> element that closes the convert modal
var span2 = document.getElementsByClassName("close2")[0];
//get keep Modal
var deductModal = document.getElementById('deductModal');
// Get the <span> element that closes the deduct modal
var span3 = document.getElementsByClassName("close3")[0];
//get the message modal
var messageModal = document.getElementById('messageModal');
// Get the <span> element that closes the message modal
var span4 = document.getElementsByClassName("close4")[0];
// When the user clicks the button, open the modal
instBtn.onclick = function() {
instModal.style.display = "block";
}
// When the user clicks on <span> (x), close the instructions modal
span1.onclick = function() {
instModal.style.display = "none";
}
// When the user clicks on <span> (x), close the convert modal
span2.onclick = function() {
convertModal.style.display = "none";
messageModal.style.display = "block";
}
// When the user clicks on <span> (x), close the deduct modal
span3.onclick = function() {
deductModal.style.display = "none";
messageModal.style.display = "block";
}
// When the user clicks on <span> (x), close the messages modal
span4.onclick = function() {
messageModal.style.display = "none";
reset();
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if($(event.target).is('#instBtn')){
event.preventDefault();
}
else if (event.target == instModal) {
instModal.style.display = "none";
}
else if(event.target == convertModal){
messageModal.style.display = "block";
convertModal.style.display = "none";
} else if(event.target == messageModal){
messageModal.style.display = "none";
reset();
}
}
$(document).ready(function() {
$(window).resize(function() {
var bodyheight = $(document).height()-130;
var player1= bodyheight-130;
$("#gameBody").height(bodyheight);
});
$('#spacebar').hide();
//Let the server know you are on game apge
socket.emit('gameStart', {});
// Receive the userid and username of current player
socket.on('names', function(data){
console.log(data.username);
console.log(data.userid);
setUpInfoBar(data.username, data.compName);
});
//Display computer messages sent at specific rounds
socket.on('sendCompMessage', function(data){
console.log(data);
var li = $('<li></li>');
li.html('<span style="font-weight:bold; color:red;"">' + data.compName + '</span>: ' + data.message);
$("#messagesList").append(li);
});
});
/* Resets the gamepage to get ready for next round */
function reset(){
sound.play();
var changeColors = window.setInterval(function(){
$('#spacebar').hide();
colors = ["red", "yellow", "green"];
pos_x=[45, 45, 45];
pos_y=[30, 70, 110];
// Stoplight color switcher
if (i < 3){
ctx1.fillStyle = "#222";
ctx1.fill();
ctx.strokeStyle = colors[i];
ctx.strokeRect(0,0,90,140);
ctx1.beginPath();
ctx1.arc(pos_x[i],pos_y[i],20,0,Math.PI*2,true);
ctx1.closePath();
ctx1.fillStyle=colors[i];
ctx1.strokeStyle="#222";
ctx1.fill();
ctx1.stroke();
i += 1;
}
else {
window.clearInterval(changeColors);
$('#spacebar').show();
//$("#img1").css("animation-name", "example3");
$("#img1").css("animation-duration", 0);
//$("#img2").css("animation-name", "example3");
$("#img2").css("animation-duration", 0);
//$("#spacebar").append("Hit the spacebar!");
var hitSpace = window.setInterval(function(){
$('#spacebar').hide();
}, 1000);
}
}, 830);
}
$(function(){
$(document).keypress(function(e){
// If spacebar is hit
if (e.keyCode == 32 && i > 2){
console.log("Spacebar hit");
$('#spacebar').hide();
// If user wins the round
if (gameArr[round] == 1){
// Give user an extra token
$("#tokensPly").val($("#tokensPly").val()+1);
$("#stoplight").css("background-color", "red");
ctx1.fillStyle="#222";
ctx1.strokeStyle="#222";
ctx1.fill();
ctx1.stroke();
ctx.fillStyle = "#222";
ctx.strokeStyle="#222";
ctx.lineWidth=6;
ctx.strokeRect(0,0,90,140);
ctx.fillRect(4,4,82,132);
index++;
i = 0;
$("#img1").css("animation-name", "example");
$("#img1").css("animation-duration", "4s");
$("#img2").css("animation-name", "example2");
$("#img2").css("animation-duration", "4s");
window.setTimeout(function(){
//Update player tokens
$("#tokensPly").html('Tokens: '+'</strong>'+ $("#tokensPly").val() + "/5");
winModal.style.display = "block";
t0 = performance.now();
}, 1500);
} else {
//Computer wins
console.log("Computer wins!");
//Subtract 100 points from player
var newval = $("#pointsPly").val()-100;
console.log(newval);
$("#pointsPly").val(newval);
$("#stoplight").css("background-color", "red");
ctx1.fillStyle="#222";
ctx1.strokeStyle="#222";
ctx1.fill();
ctx1.stroke();
ctx.strokeStyle="#111";
ctx.lineWidth=6;
ctx.strokeRect(0,0,90,140);
ctx.fillRect(4,4,82,132);
index++;
i = 0;
$("#img1").css("animation-name", "example2");
$("#img1").css("animation-duration", "4s");
$("#img2").css("animation-name", "example");
$("#img2").css("animation-duration", "4s");
window.setTimeout(function(){
// Update player score
$("#pointsPly").html('Points: '+'</strong>'+ $("#pointsPly").val());
loseModal.style.display = "block";
t0 = performance.now();
}, 1500);
}
}
})
})
/* Click event handlers */
$('#submitId').on("click", sendMessage);
$("#convertBtn1").click(convertPopUp);
$("#convertBtn2").click(convertPopUp);
$("#deductBtn1").click(deductPopUp);
$("#deductBtn2").click(deductPopUp);
$("#keepBtn1").click(keepOption);
$("#keepBtn2").click(keepOption);
$("#enterId").on('click', convertPoints);
$("#deductId").on('click', deductPoints);
$("#nextBtn").on('click', function(){
messageModal.style.display = "none";
reset();
});
function convertPoints(event){
event.preventDefault();
t1 = performance.now();
var time = t1-t0;
console.log("Tried to convert points");
console.log(time);
console.log($("#tokensConvert").val());
var conversion = $("#tokensConvert").val();
var pointsAdd = $("#tokensConvert").val()*100;
convertModal.style.display = "none";
messageModal.style.display = "block";
// If they try to convert negative amounts of tokens
if(conversion < 0){
conversion = 0;
pointsAdd = 0;
}
// If they try to convert more tokens than they have
if(conversion > $("#tokensPly").val()){
conversion = $("#tokensPly").val();
pointsAdd = conversion*100;
}
// Convert tokens to points
var newToken = $("#tokensPly").val()-conversion;
$("#tokensPly").val(newToken);
$("#pointsPly").val($("#pointsPly").val()+pointsAdd);
// Let the backend know convert was the action taken
socket.emit('action',
{
rounds: round,
action: conversion,
time: time,
pointsPlyPre: ($("#pointsPly").val()-pointsAdd),
pointsPly: $("#pointsPly").val(),
pointsOppPre: $("#pointsOpp").val(),
pointsOpp: $("#pointsOpp").val(),
tokensPlyPre: $("#tokensPly").val()+conversion,
tokensPly: $("#tokensPly").val(),
tokensOppPre: $("#tokensOpp").val(),
tokensOpp: $("#tokensOpp").val(),
winner: gameArr[round]
});
// Update tokens and points
$("#tokensPly").html('Tokens: '+'</strong>'+ $("#tokensPly").val() + "/5");
$("#pointsPly").html('Points: '+'</strong>'+ $("#pointsPly").val());
round += 1;
//reset();
}
/* Same as convert but subtracts points instead of converting */
function deductPoints(event){
event.preventDefault();
t1 = performance.now();
var time = t1-t0;
console.log("Tried to deduct points");
console.log(time);
console.log($("#pointsDeduct").val());
var deduct = $("#pointsDeduct").val()
var conversion = deduct/100;
deductModal.style.display = "none";
messageModal.style.display = "block";
$("#pointsOpp").val($("#pointsOpp").val()-deduct);
$("#tokensPly").val($("#tokensPly").val()-conversion);
socket.emit('action',
{
rounds: round,
action: (-conversion),
time, time,
pointsPlyPre: $("#pointsPly").val(),
pointsPly: $("#pointsPly").val(),
pointsOppPre: $("#pointsOpp").val()+deduct,
pointsOpp: $("#pointsOpp").val(),
tokensPlyPre: $("#tokensPly").val()+conversion,
tokensPly: $("#tokensPly").val(),
tokensOppPre: $("#tokensOpp").val(),
tokensOpp: $("#tokensOpp").val(),
winner: gameArr[round]
});
$("#tokensPly").html('Tokens: '+'</strong>'+ $("#tokensPly").val() + "/5");
$("#pointsOpp").html('Points: '+'</strong>' + $("#pointsOpp").val());
round += 1;
//reset();
}
function setUpInfoBar(namePly, nameOpp){
//setting up player info
$("#namePly").val(namePly);
$("#usernameId").val(namePly);
$("#tokensPly").val(0);
$("#pointsPly").val(0);
var tokensPly = $("#tokensPly").val();
var pointsPly = $("#pointsPly").val();
$("#namePly").html('<strong>'+namePly+'</strong>');
$("#tokensPly").html('Tokens: '+'</strong>'+ tokensPly + "/5");
$("#pointsPly").html('Points: '+'</strong>'+pointsPly + "/5");
//seting up opponent info
$("#nameOpp").val(nameOpp);
$("#tokensOpp").val(0);
$("#pointsOpp").val(0);
var tokensOpp = $("#tokensOpp").val();
var pointsOpp = $("#pointsOpp").val();
$("#nameOpp").html('<strong>'+nameOpp+'</strong>' );
$("#tokensOpp").html('Tokens: '+ tokensOpp + "/5");
$("#pointsOpp").html('Points: ' + pointsOpp + "/5");
}
function convertPopUp(){
console.log("inside convert popup");
convertModal.style.display = "block";
loseModal.style.display = "none";
winModal.style.display = "none";
}
function deductPopUp(){
console.log("inside deduct popup");
deductModal.style.display = "block";
loseModal.style.display = "none";
winModal.style.display = "none";
$("#tokensOpp").val($("#tokensOpp").val()-1);
}
function keepOption(){
t1 = performance.now();
var time = t1-t0;
console.log("inside keep popup");
console.log(time);
loseModal.style.display = "none";
winModal.style.display = "none";
messageModal.style.display = "block";
// Let backend know user just kept tokens
socket.emit('action', {
rounds: round,
action: 0,
time: time,
pointsPlyPre: $("#pointsPly").val(),
pointsPly: $("#pointsPly").val(),
pointsOppPre: $("#pointsOpp").val(),
pointsOpp: $("#pointsOpp").val(),
tokensPlyPre: $("#tokensPly").val(),
tokensPly: $("#tokensPly").val(),
tokensOppPre: $("#tokensOpp").val(),
tokensOpp: $("#tokensOpp").val(),
winner: gameArr[round]
});
// Ask for computer message in these rounds
if(round == 3 || round == 5 || round == 8){
socket.emit('sendCompMessage', {rounds:round});
}
round += 1;
}
function sendMessage(event) {
event.preventDefault();
var username = $('#usernameId').val();
var message = $('#messageId').val();
socket.emit("message", {message:message, rounds: round-1});
console.log(message);
//clear the message input
$('#messageId').val('');
var li = $('<li></li>');
li.html('<span style="font-weight:bold; color:red;"">' + username + '</span>: ' + message);
$("#messagesList").append(li);
messageModal.style.display = "none";
reset();
}
function loading() {
setTimeout(reset, 12000);
setTimeout(changeLoad, 7000);
setTimeout(foundOpponent, 12000);
}
function changeLoad() {
document.getElementById("searching").style.display = "none";
document.getElementById("loader").style.display = "none";
document.getElementById("foundOpponent").style.display = "block";
document.getElementById("countDown").style.display = "block";
var count = 5;
document.getElementById("countDown").innerHTML = count;
count--;
var x = setInterval(function() {
document.getElementById("countDown").innerHTML = count;
count--;
if (count <= 0){
clearInterval(x);
}
}, 1000);
}
function foundOpponent() {
document.getElementById("foundOpponent").style.display = "none";
document.getElementById("countDown").style.display = "none";
document.getElementById("game").style.display = "block";
}