-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.js
79 lines (63 loc) · 2.25 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
const app = require('express')();
const server = require('http').Server(app);
let ejs = require('ejs');
var favicon = require('serve-favicon')
const path = require('path');
var bodyParser = require('body-parser');
var url = require('url');
const Player = require('./app/models/player')
const { port } = require('./config');
let socket = require('./app/controllers/socker-server')
let roomManager = require('./app/controllers/room-manager')
let playerManager = require('./app/controllers/player-manager')
app.use(bodyParser.urlencoded({ extended: true }));
server.listen(port, () => {
console.log(`Server running at port:${port}`);
socket.initiateSocketConnection(server);
});
app.use(require('express').static(path.join(__dirname, 'public')));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(favicon(path.join(__dirname, 'public', 'assets', 'images', 'favicon.ico')))
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/views/index.html');
});
app.get('/board', (req, res) => {
// res.sendFile(__dirname + '/public/views/board.html');
res.render('board', { url: '/', players: [] });
});
app.post('/createRoom', function (req, res) {
let room = roomManager.createRoom(req.body.noOfRounds, req.body.timeToGuess);
let player = playerManager.createPlayer(req.body.playerName, true);
roomManager.addPlayerToRoom(room.roomName, player);
var data = {
playerName: req.body.playerName,
isAdmin: true,
roomName: room.roomName,
};
res.send(data);
});
app.post('/joinRoom', function (req, res) {
console.log(req.body);
let player = playerManager.createPlayer(req.body.playerName, false);
let status = roomManager.addPlayerToRoom(req.body.roomName, player);
console.log(status);
let data = { status: status };
res.status(status);
res.send(JSON.stringify(data));
});
app.get('/getPlayers', function (req, res) {
var roomName = req.query.roomName;
let players = roomManager.getPlayers(roomName);
res.send(JSON.stringify(players));
});
/*
const app = express();
app.use(express.static('public'))
app.listen(port, () => {
console.log(`listening on port ${port}`)
});
app.get('/', function(req, res){
res.sendFile(__dirname + '/public/views/board.html');
});
*/