Skip to content

Commit

Permalink
feat: live service check for real time mutliplayer app (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
a2937 authored Nov 9, 2023
1 parent 16aeb6c commit e143945
Showing 1 changed file with 37 additions and 20 deletions.
57 changes: 37 additions & 20 deletions apps/secure-real-time-multiplayer-game/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const app = express();
app.use(helmet());
app.use(helmet.noCache());
app.use(helmet.hidePoweredBy({ setTo: 'PHP 7.4.3' }));
app.use(cors({origin: '*'})); //For FCC testing purposes only
app.use(cors({ origin: '*' })); //For FCC testing purposes only

app.use('/public', express.static(process.cwd() + '/public'));
app.use('/assets', express.static(process.cwd() + '/assets'));
Expand All @@ -22,27 +22,28 @@ app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

// Index page (static HTML)
app.route('/')
.get(function (req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});
app.route('/').get(function (req, res) {
res.sendFile(process.cwd() + '/views/index.html');
});

app.get('/status/ping', (req, res) => {
res.send({ msg: 'pong' }).status(200);
});

//For FCC testing purposes
fccTestingRoutes(app);

// 404 Not Found Middleware
app.use(function(req, res) {
res.status(404)
.type('text')
.send('Not Found');
app.use(function (req, res) {
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') {
if (process.env.NODE_ENV === 'test') {
console.log('Running Tests...');
setTimeout(function () {
try {
Expand All @@ -56,7 +57,7 @@ const server = app.listen(portNum, () => {
});

// Socket.io setup:
// Start app and bind
// Start app and bind
// Socket.io to the same port
const io = socket(server);
const Collectible = require('./public/Collectible');
Expand All @@ -77,13 +78,21 @@ const generateCoin = () => {
coinValue = 3;
}

return new Collectible({
x: generateStartPos(canvasCalcs.playFieldMinX, canvasCalcs.playFieldMaxX, 5),
y: generateStartPos(canvasCalcs.playFieldMinY, canvasCalcs.playFieldMaxY, 5),
return new Collectible({
x: generateStartPos(
canvasCalcs.playFieldMinX,
canvasCalcs.playFieldMaxX,
5
),
y: generateStartPos(
canvasCalcs.playFieldMinY,
canvasCalcs.playFieldMaxY,
5
),
value: coinValue,
id: Date.now()
});
}
};

let coin = generateCoin();

Expand All @@ -104,7 +113,11 @@ io.sockets.on('connection', socket => {
movingPlayer.x = obj.x;
movingPlayer.y = obj.y;

socket.broadcast.emit('move-player', { id: socket.id, dir, posObj: { x: movingPlayer.x, y: movingPlayer.y } });
socket.broadcast.emit('move-player', {
id: socket.id,
dir,
posObj: { x: movingPlayer.x, y: movingPlayer.y }
});
}
});

Expand All @@ -114,10 +127,14 @@ io.sockets.on('connection', socket => {
stoppingPlayer.x = obj.x;
stoppingPlayer.y = obj.y;

socket.broadcast.emit('stop-player', { id: socket.id, dir, posObj: { x: stoppingPlayer.x, y: stoppingPlayer.y } });
socket.broadcast.emit('stop-player', {
id: socket.id,
dir,
posObj: { x: stoppingPlayer.x, y: stoppingPlayer.y }
});
}
});

socket.on('destroy-item', async ({ playerId, coinValue, coinId }) => {
if (!destroyedCoins.includes(coinId)) {
const scoringPlayer = currPlayers.find(obj => obj.id === playerId);
Expand All @@ -135,7 +152,7 @@ io.sockets.on('connection', socket => {
if (scoringPlayer.score >= 100) {
sock.emit('end-game', 'win');
sock.broadcast.emit('end-game', 'lose');
}
}

// Generate new coin and send it to all players
coin = generateCoin();
Expand Down

0 comments on commit e143945

Please sign in to comment.