forked from coridrew/nodejs-against-humanity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
127 lines (111 loc) · 3.87 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
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
122
123
124
125
126
127
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var Game = require('./game.js');
var routes = require('./routes/routes.js');
var players = { };
var io = require('socket.io').listen(server);
var socketCount = 0;
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 returnGame(gameId, res) { res.json(gameViewModel(gameId)); }
function broadcastGame(gameId) {
var vm = gameViewModel(gameId);
for(var player in players[gameId]) {
players[gameId][player].emit("updateGame", vm);
}
}
function gameViewModel(gameId) {
var game = Game.getGame(gameId);
var viewModel = JSON.parse(JSON.stringify(game));
delete viewModel.deck;
return viewModel;
}
var lobbySocket = io
.of('/lobby')
.on('connection', function(socket) {
console.info('lobby socket connect');
var gameList = Game.list();
socket.emit('lobbyJoin', gameList);
})
io.sockets.on('connection', function(socket) {
socketCount+=1;
console.info('*****SocketCount: ' + socketCount);
socket.on('connectToGame', function(data) {
console.info('server: connectToGame');
var game = Game.getGame(data.gameId);
if(game){
if(!players[data.gameId]) {
players[data.gameId] = { };
}
socket.gameId = data.gameId;
socket.playerId = data.playerId;
players[data.gameId][data.playerId] = socket;
broadcastGame(data.gameId);
} else {
socket.emit('gameError', 'Invalid Game ID');
}
});
socket.on('disconnect', function() {
socketCount-=1;
if(socket.playerId && socket.gameId){
console.info('socket disconnect ' + socket.playerId);
delete players[socket.gameId][socket.playerId];
Game.departGame(socket.gameId, socket.playerId);
lobbySocket.emit('gameAdded', Game.list());
}
});
});
app.get('/', routes.index);
app.get('/views/*', routes.partials);
app.get('/list', function (req, res) { res.json(Game.list()); });
app.get('/listall', function (req, res) { res.json(Game.listAll()); });
app.post('/add', function (req, res) {
var newGame = Game.addGame(req.body);
res.json(newGame);
lobbySocket.emit('gameAdded', Game.list());
});
app.get('/gamebyid', function (req, res) { res.json(Game.getGame(req.query.id)); });
app.post('/joingame', function (req, res) {
var game = Game.getGame(req.body.gameId);
if(!game) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ error: "invalid GameId" }));
res.end();
return null;
}
if(game.isStarted || game.players.length >= 4) {
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);
lobbySocket.emit('gameAdded', Game.list());
});
app.post('/departgame', function(req, res) {
Game.departGame(req.body.gameId, req.body.playerId);
lobbySocket.emit('gameAdded', Game.list());
broadcastGame(req.body.gameId);
});
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);
});