-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
456 lines (444 loc) · 12.9 KB
/
server.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
let gameobjs = require('./gameobjs');
let Player = gameobjs.Player;
let Ball = gameobjs.Ball;
let Border = gameobjs.Border;
let express = require('express');
let cookieParser = require('cookie-parser');
let Matter = require('matter-js');
let Engine = Matter.Engine,
World = Matter.World,
Bodies = Matter.Bodies,
Body = Matter.Body,
Vector = Matter.Vector;
let app = express();
app.set('port', (process.env.PORT || 5000));
let server = app.listen(app.get('port'),listen);
// This call back just tells us that the server has started
function listen() {
console.log("The amazing KadooRegel server");
console.log('Node app is running on port', app.get('port'));
}
// need cookieParser middleware before we can do anything with cookies
app.use(cookieParser());
app.use(function (req, res, next) {
res.cookie('Kport',app.get('port'), { maxAge: 900000, httpOnly: true });
next(); // <-- important!
});
app.use(express.static('public'));
//
let socket = require('socket.io');
let io = socket(server);
//
function cleanName(name) {
if (name.length < 1) {
name = 'Player';
} else {
name = (name.length > 19) ? name.substring(0, 19) : name;
}
// this is the list of the curse words.
let words = ['fuck','retard','shit','whore','ass','asshole','idiot','dumb','fucker',
'bitch','stupid','pusy','pussy','cock','dick','gay','fag','faggot','fagot','nigga',
'nigger','damn','darn','piss','douche','slut','bastard','crap','cunt'];
// This function gets a string and replaces all the curses with '*'.
for (let i = 0; i < words.length; i++) {
let curse = words[i];
let reg = new RegExp(curse, 'gi');
name = name.replace(reg,'*'.repeat(curse.length));
}
return name;
}
//
function pushBorders(TheWorld) {
let bordersWidth = 140,
bordersHeight = 350;
borders.push(
// the left & right borders.
new Border(0 , height/10 , bordersWidth, bordersHeight, -1),
new Border(0 , height - height/10 , bordersWidth, bordersHeight, -1),
new Border(width , height/10 , bordersWidth, bordersHeight, 1),
new Border(width , height - height/10 , bordersWidth, bordersHeight, 1),
// the upper & lower borders.
new Border(width/2 , 0 - 50 , width + bordersWidth, bordersWidth, 0),
new Border(width/2 , height + 50 , width + bordersWidth, bordersWidth, 0),
// the left & right long borders.
new Border(0 - bordersWidth, height/2 , bordersWidth, height + bordersWidth + 100, -1),
new Border(width + bordersWidth, height/2 , bordersWidth, height + bordersWidth + 100, 1)
);
for (let i = 0; i < borders.length; i++) {
World.add(TheWorld, borders[i].Body);
}
}
// resets the Players and the Ball to their original location.
function resetObj(xOffSet, yOffSet, angle, mass) {
// Ball Reset
let ballPosVector = Vector.create(width/2, height/2);
let ballVelVector = Vector.create(0, 0);
Body.setPosition(ball.body, ballPosVector);
Body.setVelocity(ball.body, ballVelVector);
// Players Reset
let BluePlayersPosVector = Vector.create(width/2 - xOffSet, height/2 - yOffSet);
let RedPlayersPosVector = Vector.create(width/2 + xOffSet, height/2 - yOffSet);
let BluePlayersAngle = angle;
let RedPlayersAngle = -angle;
for (let i=0; i < players.length; i++) {
if (players[i].body != undefined) {
Body.setStatic(players[i].body, true);
if (players[i].team == 'teamBlue') {
Body.setPosition(players[i].body, BluePlayersPosVector);
Body.setAngle(players[i].body, BluePlayersAngle);
} else {
Body.setPosition(players[i].body, RedPlayersPosVector);
Body.setAngle(players[i].body, RedPlayersAngle);
}
}
}
}
// restarts the game.
function restart() {
console.log("Restart Game");
needRestart = false;
goalStopWatch = 0;
goalMode = false;
resetObj(width/3, 0, Math.PI/2, Player.PlayerMass);
countdown = 0;
sendCount = 3;
countdownMode = true;
blueTeamScore = 0;
redTeamScore = 0;
onesecstopper = 0;
clock = 300;
stopCountdownSent = false;
io.sockets.emit('start', worldMap());
gameLoop();
}
// Map message
function worldMap() {
let score = [blueTeamScore, redTeamScore];
let states = [];
for (let i=0; i < borders.length; i++) {
states.push(borders[i].border);
}
states.push(ball.r);
states.push(score);
return states;
}
// update status message
function worldStatus() {
let players_state = [];
for (let i = 0; i < players.length; i++) {
if (players[i].body != undefined) {
let vertices = [];
for (let j = 0;j < players[i].body.vertices.length; j++) {
vertices.push([players[i].body.vertices[j].x, players[i].body.vertices[j].y]);
}
let player_state = {points: vertices,
PosX: players[i].body.position.x,
PosY: players[i].body.position.y,
team: players[i].team,
name: players[i].nickname};
players_state.push(player_state);
}
}
let state = {ballposx: ball.body.position.x,
ballposy: ball.body.position.y,
players: players_state,
clock: clock};
return state;
}
// check who scored
function whoScored() {
let posx = ball.body.position.x;
let rad = ball.r;
if (posx <= borders[0].border.x + borders[0].border.w/2 - rad) {
redTeamScore++;
return 'Red Team';
} else if (posx >= borders[2].border.x - borders[2].border.w/2 + rad) {
blueTeamScore++;
return 'Blue Team';
}
return null;
}
// gets the player Id out of the list of players.
function getPlayer(Id) {
for(let i = 0; i < players.length; i++) {
if (players[i].Id == Id) {
return players[i];
}
}
return null;
}
//
function numReadyPlayers() {
let readyPlayers = 0;
for (let i = 0; i < players.length; i++) {
if (players[i].body != undefined) {
readyPlayers++;
}
}
return readyPlayers;
}
//
function removePlayer(ID){
let player = getPlayer(ID);
if (player != null) {
if (player.team == "teamBlue") {
blueTeamPlayers--;
} else if (player.team == "teamRed") {
redTeamPlayers--;
}
// remove the player obj from our list of players.
let removedPlayer = players.splice(players.indexOf(player),1);
// remove the player body from matter.js physics world.
if (removedPlayer[0].body != undefined) {
World.remove(engine.world, removedPlayer[0].body);
}
}
}
// ----- Variables -----
// 40 tick server a brodacast update 40 times a second
let fps = 40;
// The maximum player capacity
// The remaining players will be spectators
let playerCap = 6;
let blueTeamScore = 0,
redTeamScore = 0;
let blueTeamPlayers = 0,
redTeamPlayers = 0;
let space = 0,
up = 1,
shift = 2,
rightKey = 3,
leftKey = 4;
let rightDir = 1,
leftDir = -1;
let countdown = 0;
// this is the message that we send to our clients.
let sendCount = 3;
let countdownTime = fps * 3;
// asks whether we told our clients that the countdown is over.
let stopCountdownSent = false;
let countdownMode = true;
let goalMode = false;
let goalStopWatch = 0;
let goalStopWatchTime = fps * 5;
let startLoop = true;
let gameLoopRunning = false;
let needRestart = false;
// a stop watch for one sec.
let onesecstopper = 0;
let clock = 300;
let readyCount = 0;
let IntervalId;
let players = [];
let width = 1600,
height = 900;
let engine;
// Creates the Engine.
engine = Engine.create();
engine.world.gravity.x = 0;
engine.world.gravity.y = 0;
engine.world.bounds = { min: { x: -width - 100, y: -height - 100}, max: { x: width + 100, y: height + 100} };
// Borders.
let borders = [];
pushBorders(engine.world);
// Ball.
let ball = new Ball(width/2, height/2, 50, engine);
World.add(engine.world, ball.Body);
// the game loop.
function gameLoop() {
gameLoopRunning = true;
IntervalId = setInterval(
function() {
onesecstopper++;
if (onesecstopper % fps == 0) {
onesecstopper = 0;
if (!stopCountdownSent) {
io.sockets.emit('countdownStop');
stopCountdownSent = true;
}
if (!goalMode && !countdownMode) {
if (clock <= 0) {
clearInterval(IntervalId);
gameLoopRunning = false;
let winner;
if (blueTeamScore == redTeamScore) {
winner = 'Tie';
} else {
winner = (blueTeamScore > redTeamScore)? 'Blue Team Wins': 'Red Team Wins';
}
io.sockets.emit('endMatch',winner);
needRestart = true;
} else {
clock--;
}
}
}
for (let i = 0; i < players.length; i++) {
players[i].turn();
players[i].move();
}
if (goalMode) {
goalStopWatch++;
if (goalStopWatch >= goalStopWatchTime) {
goalStopWatch = 0;
goalMode = false;
resetObj(width/3, 0, Math.PI/2, Player.PlayerMass);
io.sockets.emit('goalStop');
countdown = 0;
countdownMode = true;
}
}
else {
let whoScore = whoScored();
if (whoScore != null) {
io.sockets.emit('goalStart', (whoScore + ' Scored A Goal!!!'));
goalMode = true;
}
}
if (countdownMode) {
countdown++;
io.sockets.emit('countdownStart', sendCount);
if (countdown % fps == 0) {
sendCount--;
io.sockets.emit('countdownStart', sendCount);
}
if(countdown >= countdownTime) {
for (let i = 0; i < players.length; i++) {
if (players[i].body != undefined) {
Body.setStatic(players[i].body, false);
Body.setMass(players[i].body, Player.PlayerMass);
}
}
sendCount = 'Go!';
io.sockets.emit('countdownStart', sendCount);
stopCountdownSent = false;
sendCount = 3;
countdown = 0;
countdownMode = false;
}
}
Engine.update(engine, 1000/fps);
io.sockets.emit('update', worldStatus());
},1000/fps);
}
//
io.sockets.on('connection',
// We are given a websocket object in our function
function(socket) {
console.log('We have a new client: ' + socket.id);
players.push(new Player(socket.id, countdownMode, width, height));
io.sockets.emit('connected', players.length);
socket.on('isReady', function(name) {
let totalPlayers = redTeamPlayers + blueTeamPlayers;
if (totalPlayers < playerCap) {
let team = 'teamBlue';
if (redTeamPlayers < blueTeamPlayers) {
team = 'teamRed';
redTeamPlayers++;
} else {
blueTeamPlayers++;
}
// Give the player his nickname and his team
// And Set his physical body.
let player = getPlayer(socket.id);
player.setTeam(team, cleanName(name));
World.add(engine.world, player.Body);
socket.emit('start', worldMap());
// check if we have enough players to start the game loop.
if (numReadyPlayers() >= 2) {
if (needRestart) {
restart();
} else if (!gameLoopRunning) {
gameLoop();
}
}
socket.on('PressedEvents', function(key) {
player = getPlayer(socket.id)
if (player != null) {
switch (key) {
case space:
player.isBoosting = true;
break;
case up:
player.isMoving = true;
break;
case shift:
player.isDrifting = true;
if (player.rightKeyPressed) {
player.setRot(rightDir);
} else if(player.leftKeyPressed) {
player.setRot(leftDir);
}
break;
case rightKey:
player.setRot(rightDir);
player.rightKeyPressed = true;
player.lastKeyPressed = rightDir;
break;
case leftKey:
player.setRot(leftDir);
player.leftKeyPressed = true;
player.lastKeyPressed = leftDir;
break;
}
}
});
socket.on('ReleasedEvents', function(key) {
player = getPlayer(socket.id);
if (player != null) {
switch (key) {
case space:
player.isBoosting = false;
break;
case up:
player.isMoving = false;
break;
case shift:
player.isDrifting = false;
if (player.rightKeyPressed) {
player.setRot(rightDir);
} else if (player.leftKeyPressed) {
player.setRot(leftDir);
} else {
if (!player.lastKeyPressed == 0) {
player.setLastRot(player.lastKeyPressed)
} else {
player.setRot(0)
}
}
break;
case rightKey:
if (player.leftKeyPressed) {
player.setRot(leftDir);
} else {
player.setLastRot(rightDir);
}
player.rightKeyPressed = false;
if (player.leftKeyPressed) {
player.lastKeyPressed = leftDir;
}
break;
case leftKey:
if (player.rightKeyPressed) {
player.setRot(rightDir);
} else {
player.setLastRot(leftDir);
}
player.leftKeyPressed = false;
if (player.rightKeyPressed) {
player.lastKeyPressed = rightDir;
}
break;
}
}
});
}
else {
socket.emit('start', worldMap());
}
});
socket.on('disconnect', function() {
removePlayer(socket.id);
console.log("Client " + socket.id + " has disconnected");
});
});