-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
94 lines (76 loc) · 2.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
//
// Simple SocketIO server.
// Adu Bhandaru
//
var connect = require('connect-assets'),
express = require('express'),
http = require('http'),
socketio = require('socket.io');
var actions = require('./app/actions'),
config = require('./app/config'),
Game = require('./app/game').Game,
User = require('./app/user').User,
util = require('./app/util');
// create app, server, and bind sockets
var app = express();
var server = http.createServer(app);
var io = socketio.listen(server, {log: false});
// log environment and config
console.log('[info] starting');
console.log('[info] environment', process.env.NODE_ENV, config.production);
console.log('[info] ports', config.prodPort, config.port);
// app config
server.listen(config.port);
app.configure(function() {
app.use(connect());
app.use(express.static(__dirname + '/static'));
app.use(express.static(__dirname + '/assets'));
});
// track all the games and users
var users = new util.Map();
var games = new util.Map();
var gameID = 0;
var game = null;
// set up connections
io.sockets.on('connection', function(socket) {
// create user object
var me = new User(socket.id, users.size(), socket);
users.put(me.id, me);
console.log('[info] connection', me.json());
// resolve which game to place user in
if (!game || !game.open()) {
game = new Game(gameID++);
games.put(game.id, game);
}
game.add(me);
me.game = game;
// report user to clients
socket.emit('user', me.json());
me.game.players.forEach(function(id, user) {
if (user.is(me)) return;
socket.emit('player', { id: user.id, order: user.order });
user.socket.emit('player', me.json());
});
// start the game if we have enough players
if (me.game.closed()) {
console.log('[info] start game', me.game.id);
me.game.players.forEach(function(id, user) {
user.socket.emit('start');
});
}
// broadcast the move
socket.on('move', function (move) {
console.log('[info] received move', move);
actions.move(me, move);
});
// user forfeit
socket.on('forfeit', function() {
console.log('[info] user forfeit', me.json());
actions.forfeit(me);
});
// user disconnection
socket.on('disconnect', function() {
console.log('[info] user disconnected', me.json());
actions.disconnect(games, users, me);
});
});