-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·100 lines (85 loc) · 2.52 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
/**
* Module dependencies.
*/
var express = require('express')
, $ = require('jquery')
, routes = require('./routes')
, app = express()
, http = require('http').createServer(app)
, io = require('socket.io').listen(http)
, path = require('path');
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
app.get('/', routes.index);
app.get('/game', routes.game);
http.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
var players = [];
var maximumNumberOfPlayers = 2;
io.sockets.on('connection', function (socket) {
// Check if there is enough place in the current game for a new player
if(checkNumberOfPlayers()) {
socket.emit('info', {info: 'waiting player'});
socket.on('newPlayer', function (data) {
if(data.hasOwnProperty("name")) {
var player = createNewPlayer(socket, data.name);
// Send an ACK to the client
socket.emit('confirmPlayer', {name: player.name});
// Check if we're ready to play
if(!checkNumberOfPlayers()) {
var playersName = [];
$.each(players, function(index, value) {
playersName.push(value.name);
});
informPlayers('info',
{'info': 'ready to play',
'players': playersName}
);
}
socket.on('disconnect', function () {
players = $.grep(players, function(row) {
return row != player;
});
console.log('Player nb: ' + players.length);
});
}
});
} else {
socket.emit('info', {info: 'game full'});
}
});
function createNewPlayer(socket, name) {
var player = new Object();
player.socket = socket;
player.name = name;
player.emit = function(dataName, data) {
player.socket.emit(dataName, data);
};
players.push(player);
return player;
}
function checkNumberOfPlayers() {
if(players.length < maximumNumberOfPlayers) {
return true;
} else {
return false;
}
}
function informPlayers(dataName, data) {
for(var i = 0; i < players.length; i++) {
players[i].emit(dataName, data);
}
}