-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
410 lines (366 loc) · 14.6 KB
/
server.js
File metadata and controls
410 lines (366 loc) · 14.6 KB
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
// TO DO:
// 1.
// Enhance collision using AABB function
// 2.
// Refactor gameFrames logic so leftpaddle/rightpaddle logic isn't hard coded
var express = require('express');
var WebSocket = require('ws');
var http = require('http');
var path = require('path');
var app = express();
var PORT = process.env.PORT || 8080;
var server = http.createServer(app);
var wss = new WebSocket.Server({ server });
// game objects constructor
function gameObjs(puck, leftpaddle, rightpaddle, players = 0, gamestate = false) {
this.puck =
{
x: 375,
y: 250,
w: 10,
h: 10,
velx: 0,
vely: 0
};
this.leftpaddle =
{
x: 0,
y: 225,
w: 10,
h: 50,
vely: 0,
up: false,
down: false,
deaccelerator: null,
ready: false,
connected: false,
ballSpeedVote: 6,
score: 0
};
this.rightpaddle =
{
x: 740,
y: 225,
w: 10,
h: 50,
vely: 6,
up: false,
down: false,
deaccelerator: null,
ready: false,
connected: false,
ballSpeedVote: 6,
score: 0
};
this.players = players;
this.gamestate = gamestate;
}
// function that calculates puck's Y velocity based on where puck hits the paddle
function puckYVal(paddlepos, puckpos) {
var halfPaddle = paddlepos - 25;
// i.e. top half of the paddle
if (puckpos < halfPaddle) {
var ratio = (halfPaddle - puckpos) / 100;
return -22 * ratio; // -> normally 30
}
// i.e. bottom half of the paddle
else if (puckpos > halfPaddle) {
var ratio = (puckpos - halfPaddle) / 100;
return 22 * ratio; // -> normally 30
}
else
return 0;
}
var concurrentGames = {};
var gameObjsString;
var gameFrames;
var serverOnline = false;
wss.on('connection', function(connection) {
console.log((new Date()) + " Connection accepted.");
function startGame() {
gameFrames = setInterval(function() {
Object.keys(concurrentGames).forEach(gameObjs => {
// gameObjs are the "keys" for the concurrentGames object, i.e. the routes
// acceleration calculations for left paddle
if (concurrentGames[gameObjs].leftpaddle.up && concurrentGames[gameObjs].leftpaddle.y >= 0) {
concurrentGames[gameObjs].leftpaddle.deaccelerator = "-";
concurrentGames[gameObjs].leftpaddle.y -= ((concurrentGames[gameObjs].leftpaddle.vely)^2)/2;
if (concurrentGames[gameObjs].leftpaddle.vely < 13) {
concurrentGames[gameObjs].leftpaddle.vely+=2;
}
}
else if (concurrentGames[gameObjs].leftpaddle.down && concurrentGames[gameObjs].leftpaddle.y <= 450) {
concurrentGames[gameObjs].leftpaddle.deaccelerator = "+";
concurrentGames[gameObjs].leftpaddle.y += ((concurrentGames[gameObjs].leftpaddle.vely)^2)/2;
if (concurrentGames[gameObjs].leftpaddle.vely < 13) {
concurrentGames[gameObjs].leftpaddle.vely+=2;
}
}
else {
if (concurrentGames[gameObjs].leftpaddle.y >= 0 && concurrentGames[gameObjs].leftpaddle.y <= 450) {
if (concurrentGames[gameObjs].leftpaddle.vely > 0) {
concurrentGames[gameObjs].leftpaddle.vely-=2;
if (concurrentGames[gameObjs].leftpaddle.deaccelerator === "+") {
concurrentGames[gameObjs].leftpaddle.y += ((concurrentGames[gameObjs].leftpaddle.vely)^2)/2;
}
if (concurrentGames[gameObjs].leftpaddle.deaccelerator === "-") {
concurrentGames[gameObjs].leftpaddle.y -= ((concurrentGames[gameObjs].leftpaddle.vely)^2)/2;
}
}
}
}
// acceleration calculations for right paddle
if (concurrentGames[gameObjs].rightpaddle.up && concurrentGames[gameObjs].rightpaddle.y >= 0) {
concurrentGames[gameObjs].rightpaddle.deaccelerator = "-";
concurrentGames[gameObjs].rightpaddle.y -= ((concurrentGames[gameObjs].rightpaddle.vely)^2)/2;
if (concurrentGames[gameObjs].rightpaddle.vely < 13) {
concurrentGames[gameObjs].rightpaddle.vely+=2;
}
}
else if (concurrentGames[gameObjs].rightpaddle.down && concurrentGames[gameObjs].rightpaddle.y <= 450) {
concurrentGames[gameObjs].rightpaddle.deaccelerator = "+";
concurrentGames[gameObjs].rightpaddle.y += ((concurrentGames[gameObjs].rightpaddle.vely)^2)/2;
if (concurrentGames[gameObjs].rightpaddle.vely < 13) {
concurrentGames[gameObjs].rightpaddle.vely+=2;
}
}
else {
if (concurrentGames[gameObjs].rightpaddle.y >= 0 && concurrentGames[gameObjs].rightpaddle.y <= 450) {
if (concurrentGames[gameObjs].rightpaddle.vely > 0) {
concurrentGames[gameObjs].rightpaddle.vely-=2;
if (concurrentGames[gameObjs].rightpaddle.deaccelerator === "+") {
concurrentGames[gameObjs].rightpaddle.y += ((concurrentGames[gameObjs].rightpaddle.vely)^2)/2;
}
if (concurrentGames[gameObjs].rightpaddle.deaccelerator === "-") {
concurrentGames[gameObjs].rightpaddle.y -= ((concurrentGames[gameObjs].rightpaddle.vely)^2)/2;
}
}
}
}
// player 1 scores
if (concurrentGames[gameObjs].puck.x >= 740) {
concurrentGames[gameObjs].leftpaddle.score++;
resetPuck(gameObjs);
}
// top and bottom wall collision for puck
if (concurrentGames[gameObjs].puck.y <=0 || concurrentGames[gameObjs].puck.y >= 490) {
concurrentGames[gameObjs].puck.vely = concurrentGames[gameObjs].puck.vely * (-1);
}
// player 2 scores
if (concurrentGames[gameObjs].puck.x <= 0) {
concurrentGames[gameObjs].rightpaddle.score++;
resetPuck(gameObjs);
}
// left paddle collision detection
if (concurrentGames[gameObjs].puck.y + concurrentGames[gameObjs].puck.h >= concurrentGames[gameObjs].leftpaddle.y
&& concurrentGames[gameObjs].puck.y <= concurrentGames[gameObjs].leftpaddle.y + concurrentGames[gameObjs].leftpaddle.h
&& concurrentGames[gameObjs].puck.x <= concurrentGames[gameObjs].leftpaddle.w) {
var paddlePos = concurrentGames[gameObjs].leftpaddle.y + concurrentGames[gameObjs].leftpaddle.h;
var puckPos = concurrentGames[gameObjs].puck.y + concurrentGames[gameObjs].puck.h;
concurrentGames[gameObjs].puck.vely = puckYVal(paddlePos, puckPos);
concurrentGames[gameObjs].puck.velx = concurrentGames[gameObjs].puck.velx * (-1);
}
// right paddle collision detection
if (concurrentGames[gameObjs].puck.y + concurrentGames[gameObjs].puck.h >= concurrentGames[gameObjs].rightpaddle.y
&& concurrentGames[gameObjs].puck.y <= concurrentGames[gameObjs].rightpaddle.y + concurrentGames[gameObjs].rightpaddle.h
&& concurrentGames[gameObjs].puck.x >= 740 - concurrentGames[gameObjs].rightpaddle.w) {
var paddlePos = concurrentGames[gameObjs].rightpaddle.y + concurrentGames[gameObjs].rightpaddle.h;
var puckPos = concurrentGames[gameObjs].puck.y + concurrentGames[gameObjs].puck.h;
concurrentGames[gameObjs].puck.vely = puckYVal(paddlePos, puckPos);
concurrentGames[gameObjs].puck.velx = concurrentGames[gameObjs].puck.velx * (-1);
}
// update the puck position
concurrentGames[gameObjs].puck.x += concurrentGames[gameObjs].puck.velx;
concurrentGames[gameObjs].puck.y += concurrentGames[gameObjs].puck.vely;
gameObjsString = JSON.stringify(concurrentGames);
wss.clients.forEach(client => {
client.send(gameObjsString);
});
})
}, 20);
}
// if a goal is scored this function is trigged
function resetPuck(route) {
// a player scored 11, end game and send resting values
if (concurrentGames[route].leftpaddle.score === 11) {
sendMessage("Player 1 wins!", route);
}
if (concurrentGames[route].rightpaddle.score === 11) {
sendMessage("Player 2 wins!", route);
}
if (concurrentGames[route].leftpaddle.score === 11 || concurrentGames[route].rightpaddle.score === 11) {
concurrentGames[route].puck.x = 375;
concurrentGames[route].puck.y = 240;
concurrentGames[route].leftpaddle.y = 225;
concurrentGames[route].rightpaddle.y = 225;
concurrentGames[route].leftpaddle.deaccelerator = null;
concurrentGames[route].rightpaddle.deaccelerator = null;
concurrentGames[route].leftpaddle.ready = false;
concurrentGames[route].rightpaddle.ready = false;
concurrentGames[route].gamestate = false;
concurrentGames[route].puck.velx = 0;
concurrentGames[route].puck.vely = 0;
}
// each players score is below 11, reset puck position and continue game
if (concurrentGames[route].leftpaddle.score < 11 && concurrentGames[route].rightpaddle.score < 11) {
concurrentGames[route].puck.x = 375;
concurrentGames[route].puck.y = 240;
//var storeVelY = concurrentGames[route].puck.vely;
var storeVelX = concurrentGames[route].puck.velx;
concurrentGames[route].puck.vely = 0;
concurrentGames[route].puck.velx = 0;
setTimeout(function(){
// if undefined then don't execute this block of code or server will die.
if (concurrentGames[route] != undefined) {
concurrentGames[route].puck.vely = randomPuckYVel() * randomDirection();
concurrentGames[route].puck.velx = storeVelX * randomDirection();
}
}, 3000);
}
}
function randomPuckYVel() {
return (Math.random() * 5.5);
}
function randomDirection() {
return (Math.floor(Math.random() * 2) == 0) ? 1 : -1;
}
function sendMessage(msg, route) {
wss.clients.forEach(client => {
client.send(`${route} message ${msg}`);
});
}
connection.on('message', function(message) {
var route = message.substring(0, 5);
if (message.substring(6, 10) == 'join') {
if (concurrentGames[route] == undefined) {
concurrentGames[route] = new gameObjs();
}
concurrentGames[route].players++;
// determines which player is connected, and connects player to leftpaddle controls, rightpaddle controls or makes the third joiner a spectator
if (!concurrentGames[route].leftpaddle.connected && !concurrentGames[route].rightpaddle.connected) {
concurrentGames[route].leftpaddle.connected = true;
connection.send("1");
sendMessage("Player 1 (left paddle) has joined the game!", route);
}
else if (concurrentGames[route].leftpaddle.connected && !concurrentGames[route].rightpaddle.connected) {
concurrentGames[route].rightpaddle.connected = true;
connection.send("2");
sendMessage("Player 2 (right paddle) has joined the game!", route);
}
else if (!concurrentGames[route].leftpaddle.connected && concurrentGames[route].rightpaddle.connected) {
concurrentGames[route].leftpaddle.connected = true;
connection.send("1");
sendMessage("Player 1 (left paddle) has joined the game!", route);
}
else {
connection.send("3");
sendMessage("A spectator has joined the game!", route);
}
}
if (message.substring(6, 18) == '1 disconnect') {
sendMessage("Player 1 has left the game!", route);
connection.terminate();
concurrentGames[route].leftpaddle.connected = false;
concurrentGames[route].players--;
if (concurrentGames[route]) {
if (!concurrentGames[route].rightpaddle.connected) {
delete concurrentGames[route];
}
}
}
if (message.substring(6, 18) == '2 disconnect') {
sendMessage("Player 2 has left the game!", route);
connection.terminate();
concurrentGames[route].rightpaddle.connected = false;
concurrentGames[route].players--;
if (concurrentGames[route]) {
if (!concurrentGames[route].leftpaddle.connected) {
delete concurrentGames[route];
}
}
}
if (message.substring(6, 16) == 'disconnect') {
connection.terminate();
if (concurrentGames[route]) {
if (!concurrentGames[route].leftpaddle.connected && !concurrentGames[route].rightpaddle.connected) {
delete concurrentGames[route];
}
}
}
if (message.substring(6, 10) == '1 up') {
concurrentGames[route].leftpaddle.up = true;
}
if (message.substring(6, 16) == '1 up false') {
concurrentGames[route].leftpaddle.up = false;
}
if (message.substring(6, 12) == '1 down') {
concurrentGames[route].leftpaddle.down = true;
}
if (message.substring(6, 18) == '1 down false') {
concurrentGames[route].leftpaddle.down = false;
}
if (message.substring(6, 10) == '2 up') {
concurrentGames[route].rightpaddle.up = true;
}
if (message.substring(6, 16) == '2 up false') {
concurrentGames[route].rightpaddle.up = false;
}
if (message.substring(6, 12) == '2 down') {
concurrentGames[route].rightpaddle.down = true;
}
if (message.substring(6, 18) == '2 down false') {
concurrentGames[route].rightpaddle.down = false;
}
if (message.substring(6, 11) == 'ready') {
if (message.substring(0, 5) == route && message.substring(12,13) == 1) {
concurrentGames[route].leftpaddle.ballSpeedVote = message.substring(14,15);
concurrentGames[route].leftpaddle.ready = true;
sendMessage("Player 1 is ready with a ball speed vote of " + concurrentGames[route].leftpaddle.ballSpeedVote + "!", route);
}
if (message.substring(0, 5) == route && message.substring(12,13) == 2) {
concurrentGames[route].rightpaddle.ballSpeedVote = message.substring(14,15);
concurrentGames[route].rightpaddle.ready = true;
sendMessage("Player 2 is ready with a ball speed vote of " + concurrentGames[route].rightpaddle.ballSpeedVote + "!", route);
}
if (concurrentGames[route] != undefined) {
if (!concurrentGames[route].gamestate && concurrentGames[route].leftpaddle.ready && concurrentGames[route].rightpaddle.ready) {
concurrentGames[route].gamestate = true;
setTimeout(function() {
sendMessage("Game will start soon!", route);
}, 3500);
setTimeout(function() {
beginGame();
}, 7500);
}
}
}
function beginGame() {
if (concurrentGames[route] != undefined) {
// starting values
concurrentGames[route].puck.x = 375;
concurrentGames[route].puck.y = 240;
concurrentGames[route].leftpaddle.score = 0;
concurrentGames[route].rightpaddle.score = 0;
concurrentGames[route].puck.velx = ((parseInt(concurrentGames[route].leftpaddle.ballSpeedVote) + parseInt(concurrentGames[route].rightpaddle.ballSpeedVote)) / 2) * randomDirection();
concurrentGames[route].puck.vely = randomPuckYVel() * randomPuckYVel();
}
}
});
if (!serverOnline) {
serverOnline = true;
startGame();
}
});
app.use(express.static('./public'));
app.get("/", function(req, res) {
res.sendFile(path.join(__dirname, "/public/home.html"));
});
app.get("/games", function(req, res) {
res.send(concurrentGames);
});
app.get("/:route", function(req, res) {
res.sendFile(path.join(__dirname, "/public/game.html"));
});
server.listen(PORT, function () {
console.log(`app listening on port ${PORT}`);
});