-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (102 loc) · 3.38 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
const dotenv = require("dotenv");
const express = require('express');
const flash = require('connect-flash');
const session = require('express-session');
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo');
const morgan = require('morgan');
const methodOverride = require('method-override');
const appRoutes = require('./routes/appRoute');
const favicon = require('serve-favicon');
const path = require('path');
const http = require('http');
const socket = require('socket.io');
const router = express.Router();
const {currentlyGuest, loggedIn} = require('./middlewares/authentication');
var app = express()
const controller = require('./controllers/appController');
const server = http.createServer(app)
const io = socket(server)
const port = process.env.PORT || 3000
var numPlayers = 0
//Whenever someone connects this gets executed
io.on('connection', function(socket){
console.log('User connected');
socket.on('joined', function(msg) {
numPlayers = msg.numPlayers
if (numPlayers === "2")
msg.color = "black"
socket.emit('player', msg)
});
socket.on('play', function (roomId) {
socket.broadcast.emit('play', {roomId, numPlayers});
});
socket.on('move', function (msg) {
socket.broadcast.emit('move', msg);
});
//Whenever someone disconnects this piece of code executed
socket.on('disconnect', function () {
console.log('A user disconnected');
numPlayers = 1
io.emit('roomId_on_disconnect', {})
});
socket.on('game_over', function(msg) {
socket.broadcast.emit(msg)
});
socket.on('roomId_on_disconnect', function(msg) {
//route that directs controller through post request
//to eventually decrease numPlayers by 1
router.post('/disconnected/' + msg.roomId, loggedIn, controller.disconnected)
});
});
app.set('view engine', 'ejs');
dotenv.config();
mongoose.connect(`mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_POINTER}`,
{useNewUrlParser: true, useUnifiedTopology: true})
.then(()=>{
server.listen(port, () => {
console.log('Server is running on port', 3000);
});
})
.catch(err=>console.log(err.message));
app.use(express.static('public'));
app.use(express.urlencoded({extended: true}));
app.use(morgan('tiny'));
app.use(methodOverride('_method'));
app.use(favicon(path.join(__dirname, "public", "images", "favicon.ico")));
app.use(session(
{
secret: `${process.env.SECRET}`,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 60*60*1000, httpOnly: true },
store: new MongoStore(
{
mongoUrl: `mongodb+srv://${process.env.DB_USERNAME}:${process.env.DB_PASSWORD}@${process.env.DB_POINTER}`
})
}));
app.use(flash());
app.use((req, res, next) =>
{
res.locals.user = req.session.user || null;
res.locals.editorPieces = req.session.editorPieces || null;
res.locals.errors = req.flash('error');
res.locals.successes = req.flash('success');
next();
});
app.use('/', appRoutes);
app.use((req, res, next) =>
{
let error = new Error("The server cannot find " + req.url);
error.status = 404;
next(error);
});
app.use((error, req, res, next) =>
{
if (!error.status)
{
error.status = 500;
error.message = "Internal Server Error";
}
res.render("./main/error", { error: error });
});