-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
113 lines (81 loc) · 2.6 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
101
102
103
104
105
106
107
108
109
110
111
112
113
var express = require('express'),
path = require('path');
var Game = require('./game');
var Clients = require('./clients');
/**
* Create Express server.
*/
var app = express();
/**
* Socket
*/
var server = require('http').Server(app),
io = require('socket.io')(server);
/**
* Express configuration.
*/
app.set('port', process.env.PORT || 3001);
app.set('views', path.join(__dirname, 'views'));
app.engine('html', require('ejs').renderFile);
var hour = 3600000,
day = hour * 24,
week = day * 7;
app.use(express['static'](path.join(__dirname, 'public'), {
maxAge: week
}));
app.get('/', function (req, res) {
res.render('index.html', {});
});
var clients = new Clients();
function createGame(user) {
var waiting_game = clients.getWaiting(user);
console.log("--------");
if(waiting_game)
console.log(waiting_game.length);
if (!waiting_game || !waiting_game.length)
return;
var game = new Game(io, waiting_game);
game.on('ended', function () {
var self = this;
this.clients.forEach(function (v) {
clients.setWaiting(v);
v.game = null;
v.is_ready = false;
v.leave(self.room);
});
});
}
//clients.on('add', createGame);
clients.on('setInfo', createGame);
io.sockets.on('connection', function (socket) {
console.log('user.connect' + " " + socket.id);
clients.add(socket);
socket.on('user.info', function (data) {
console.log('user.info' + " " + JSON.stringify(data));
clients.setInfo(this, data);
});
socket.on('user.game.ready', function () {
this.is_ready = true;
console.log('user.game.ready' + " " + this.info.nickname);
//send (in room) message to start game
if (this.game && this.game.isTeamReady() ) {
console.log('game.start' + " " + this.game.room);
io.sockets.in(this.game.room).emit('game.start');
}
});
socket.on('user.game.position', function (data) {
//console.log('user.game.position' + " " + JSON.stringify(data));
if (!this.game) {
return;
}
var p = {};
p[socket.info.nickname] = data;
//console.log('user.game.position' + " " + JSON.stringify(p));
if ( this.game && this.game.checkGameState({client: this.info.nickname, coords: data}))
io.sockets.in(this.game.room).emit('user.game.position', p);
});
});
server.listen(app.get('port'), function () {
console.log('Express server listening on port %d in %s mode', app.get('port'), app.get('env'));
});
module.exports = app;