-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
90 lines (75 loc) · 2.75 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
var express = require('express');
var app = express()
var server = require('http').createServer(app);
var Game = require('./game.js')
var players = { };
var io = require('socket.io').listen(server);
var _ = require('underscore');
server.listen(process.env.PORT || 3000);
app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
app.use('/public', express.static('public'));
function json(o, res) {
res.writeHead(200, { 'Content-Type': 'application/json' });
res.write(JSON.stringify(o));
res.end();
}
function returnGame(gameId, res) { json(gameViewModel(gameId), res); }
function broadcastGame(gameId) {
for(var player in players[gameId]) {
players[gameId][player].emit("updateGame", gameViewModel(gameId));
}
}
function gameViewModel(gameId) {
var game = Game.getGame(gameId);
var viewModel = JSON.parse(JSON.stringify(game));
delete viewModel.deck;
return viewModel;
}
io.sockets.on('connection', function(socket) {
socket.on('connectToGame', function(data) {
if(!players[data.gameId]) players[data.gameId] = { };
socket.gameId = data.gameId;
socket.playerId = data.playerId;
players[data.gameId][data.playerId] = socket;
broadcastGame(data.gameId);
});
socket.on('disconnect', function() {
delete players[socket.gameId][socket.playerId];
});
});
app.get('/', function (req, res) { res.render('index'); });
app.get('/game', function (req, res) { res.render('game'); });
app.get('/list', function (req, res) { json(Game.list(), res); });
app.get('/listall', function (req, res) { json(Game.listAll(), res); });
app.post('/add', function (req, res) { json(Game.addGame(req.body), res); });
app.get('/gamebyid', function (req, res) { json(Game.getGame(req.query.id), res); });
app.post('/joingame', function (req, res) {
var game = Game.getGame(req.body.gameId);
if(game.isStarted) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ error: "too many players" }));
res.end();
return null;
}
game = Game.joinGame(game, { id: req.body.playerId, name: req.body.playerName });
returnGame(req.body.gameId, res);
});
app.post('/selectcard', function(req, res) {
Game.selectCard(req.body.gameId, req.body.playerId, req.body.whiteCardId);
broadcastGame(req.body.gameId);
returnGame(req.body.gameId, res);
});
app.post('/selectWinner', function(req, res) {
Game.selectWinner(req.body.gameId, req.body.cardId);
broadcastGame(req.body.gameId);
returnGame(req.body.gameId, res);
});
app.post('/readyForNextRound', function(req, res){
Game.readyForNextRound(req.body.gameId, req.body.playerId);
broadcastGame(req.body.gameId);
returnGame(req.body.gameId, res);
});