-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
107 lines (89 loc) · 3.31 KB
/
app.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
// reads in our .env file and makes those values available as environment variables
require('dotenv').config();
const express = require('express');
const bodyParser = require('body-parser');
// const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const passport = require('passport');
const routes = require('./routes/main');
const secureRoutes = require('./routes/secure');
const passwordRoutes = require('./routes/password');
// setup mongo connection
// const uri = process.env.MONGO_CONNECTION_URL;
// mongoose.connect(uri, { useNewUrlParser : true, useCreateIndex: true });
// mongoose.connection.on('error', (error) => {
// console.log(error);
// process.exit(1);
// });
// mongoose.connection.on('connected', function () {
// console.log('connected to mongo');
// });
// mongoose.set('useFindAndModify', false);
// create an instance of an express app
const app = express();
const server = require('http').Server(app);
const io = require('socket.io').listen(server);
const players = {};
io.on('connection', function (socket) {
console.log('a user connected: ', socket.id);
// create a new player and add it to our players object
players[socket.id] = {
flipX: false,
x: Math.floor(Math.random() * 400) + 50,
y: Math.floor(Math.random() * 500) + 50,
playerId: socket.id
};
// send the players object to the new player
socket.emit('currentPlayers', players);
// update all other players of the new player
socket.broadcast.emit('newPlayer', players[socket.id]);
// when a player disconnects, remove them from our players object
socket.on('disconnect', function () {
console.log('user disconnected: ', socket.id);
delete players[socket.id];
// emit a message to all players to remove this player
io.emit('disconnect', socket.id);
});
// when a plaayer moves, update the player data
socket.on('playerMovement', function (movementData) {
players[socket.id].x = movementData.x;
players[socket.id].y = movementData.y;
players[socket.id].flipX = movementData.flipX;
// emit a message to all players about the player that moved
socket.broadcast.emit('playerMoved', players[socket.id]);
});
});
// update express settings
app.use(bodyParser.urlencoded({ extended: false })); // parse application/x-www-form-urlencoded
app.use(bodyParser.json()); // parse application/json
app.use(cookieParser());
// require passport auth
require('./auth/auth');
/*
app.get('/game.html', passport.authenticate('jwt', { session : false }), function (req, res) {
res.sendFile(__dirname + '/public/game.html');
});
*/
app.get('/game.html', function (req, res) {
res.sendFile(__dirname + '/public/game.html');
});
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
// main routes
app.use('/', routes);
app.use('/', passwordRoutes);
app.use('/', passport.authenticate('jwt', { session : false }), secureRoutes);
// catch all other routes
app.use((req, res, next) => {
res.status(404).json({ message: '404 - Not Found' });
});
// handle errors
app.use((err, req, res, next) => {
console.log(err.message);
res.status(err.status || 500).json({ error: err.message });
});
server.listen(process.env.PORT || 3000, () => {
console.log(`Server started on port ${process.env.PORT || 3000}`);
});