forked from mcamuzat/fcc-secure-real-time-multiplayer-game
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
171 lines (134 loc) · 4.53 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
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
const expect = require('chai');
const socket = require('socket.io');
const helmet = require('helmet');
const fccTestingRoutes = require('./routes/fcctesting.js');
const runner = require('./test-runner.js');
const app = express();
app.use('/public', express.static(process.cwd() + '/public'));
app.use('/assets', express.static(process.cwd() + '/assets'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
// helmet part
app.use(helmet.noSniff());
app.use(helmet.xssFilter());
app.use(helmet.noCache());
app.use(helmet.hidePoweredBy({ setTo: 'PHP 7.4.3' }));
// Index page (static HTML)
app.route('/')
.get(function (req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
//For FCC testing purposes
fccTestingRoutes(app);
// 404 Not Found Middleware
app.use(function(req, res, next) {
res.status(404)
.type('text')
.send('Not Found');
});
const portNum = process.env.PORT || 3000;
// Set up server and tests
const server = app.listen(portNum, () => {
console.log(`Listening on port ${portNum}`);
if (process.env.NODE_ENV==='test') {
console.log('Running Tests...');
setTimeout(function () {
try {
runner.run();
} catch (error) {
console.log('Tests are not valid:');
console.error(error);
}
}, 1500);
}
});
// Socket.io setup:
const Player = require('./public/Player');
const Collectible = require('./public/Collectible');
const {dimension} = require('./public/dimension');
const random = (min, max) => {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
const getRandomPosition = () => {
let x = random(dimension.minX+50, dimension.maxX-50);
let y = random(dimension.minY+50, dimension.maxY-50);
x = Math.floor(x/10) * 10;
y = Math.floor(y/10) * 10;
return [x,y];
}
let playersList = [];
let [oxygenX,oxygenY] = getRandomPosition();
let oxygen = new Collectible({x:oxygenX,y:oxygenY,value:1, id:Date.now()})
let [spikeX,spikeY] = getRandomPosition();
let spike = new Player({x:spikeX,y:spikeY,value:1, id:Date.now()});
let connections = [];
const io = socket(server);
io.sockets.on('connection', socket => {
console.log(`New connection ${socket.id}`);
connections.push(socket);
console.log('Connected: %s sockets connected.',connections.length);
let [positionX,positionY] = getRandomPosition();
let player = new Player({x:positionX,y:positionY,score:0,id:socket.id});
playersList.push(player)
socket.emit('init', {id: socket.id, players: playersList, oxygen: oxygen, spike:spike});
//socket.broadcast.emit('new-player', new Plaobj);
socket.on('update', (updatedUser) => {
playersList.forEach(user => {
if(user.id === socket.id){
user.x = updatedUser.x;
user.y = updatedUser.y;
user.score = updatedUser.score;
user.radius = updatedUser.radius;
}
});
io.emit('update', {players: playersList,spike:spike,oxygen:oxygen,player: null});
});
socket.on('disconnect', () => {
console.log(`deconnection ${socket.id}`);
socket.broadcast.emit('remove-player', socket.id);
connections.splice(connections.indexOf(socket), 1);
playersList = playersList.filter(player => player.id !== socket.id);
console.log('Disconnected: %s sockets connected.', connections.length);
});
});
setInterval(tick, 1000/50);
let vx = 2;
let vy = 2;
function tick() {
// move the spike
if(spike.x+ spike.radius > dimension.maxX) vx = -vx;
if(spike.y+ spike.radius > dimension.maxY) vy = -vy;
if(spike.x-spike.radius <= dimension.minX) vx = -vx;
if(spike.y-spike.radius <= dimension.minY) vy = -vy;
spike.x += vx;
spike.y += vy;
let playerUpdate = null
playersList.forEach(player => {
if(spike.collision(player)) {
let [positionX,positionY] = getRandomPosition();
player.x = positionX;
player.y = positionY;
player.score = 0;
player.radius = 30
playerUpdate = player;
}
let p = new Player(player);
if (p.collision(oxygen)) {
player.score += 1;
player.radius +=10
let [oxygenX,oxygenY] = getRandomPosition();
oxygen = new Collectible({x:oxygenX,y:oxygenY,value:1, id:Date.now()})
playerUpdate = player;
}
});
io.emit('update', {
players: playersList,
spike:spike,
oxygen: oxygen,
player: playerUpdate
});
}
module.exports = app; // For testing