-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
executable file
·190 lines (151 loc) · 5.41 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use strict';
(function() {
var express = require('express');
var app = express();
var server = require('http').Server(app);
var io = require('socket.io')(server);
app.set('view options',{layout: false});
app.set('view engine', 'jade');
app.set('port', (process.env.PORT || 3000));
app.get('/', function(req, res) {
res.render('index');
});
app.get('/host/', function(req, res) {
res.render('host/host-home');
});
app.get('/host/:room', function(req, res) {
res.render('host/host-home', {room: req.params.room});
});
app.get('/client/:room', function(req, res) {
res.render('client/client-home', {room: req.params.room});
});
app.use(express.static('public'));
server.listen(app.get('port'));
var Logger = require(__dirname + '/class/Logger.js');
var Player = require(__dirname + '/class/Player.js');
var Room = require(__dirname + '/class/Room.js');
// A Hash to store the existing rooms
var rooms = {};
var mainLogger = new Logger('server');
io.on('connection', function(socket) {
mainLogger.log(socket.conn.remoteAddress + ' is connected');
socket.on('new room', function(data) {
// TODO must be unique
socket.socketName = data.room;
rooms[data.room] = new Room(socket, data);
rooms[data.room].logger = new Logger('room-' + data.room);
socket.join(data.room);
mainLogger.log('new room : ' + data.room);
});
socket.on('connect host', function(data, fn) {
// Check if room exist
if (rooms[data.room] !== undefined) {
var room = rooms[data.room];
socket.socketName = data.room;
room.roomSockets[socket.id] = socket;
room.logger.log('New Host ' + socket.id + ' join Room ' + data.room);
socket.join(data.room);
fn({
players: room.players,
});
} else {
fn({error: 'Room didn\'t exist and was created'});
}
});
socket.on('connect mobile', function(data, fn) {
if (rooms[data.room] !== undefined) {
var room = rooms[data.room];
if (room.started) {
return fn({registered: false, error: 'Game already started'});
}
room.mobileSockets[socket.id] = socket;
socket.join(data.room);
// Store the position of our room that this mobile device belongs to
socket.roomName = data.room;
room.logger.log('New Player ' + socket.id + ' join Room ' + data.room);
var player = new Player(socket.id);
room.players[player.id] = player;
// Return the callback as true with player list
fn({registered: true, playerList: room.players});
socket.broadcast.to(data.room).emit('new user', player);
} else {
// Callback returns false with an error
fn({registered: false, error: 'No live desktop connection found'});
}
});
socket.on('update ready state', function(data, fn) {
if (rooms[socket.roomName] === undefined) {
return;
}
var room = rooms[socket.roomName];
var player = room.players[socket.id];
player.ready = data.ready;
console.log(player);
socket.broadcast.to(socket.roomName).emit('user ready', player);
fn();
});
socket.on('request start', function(fn) {
if (rooms[socket.socketName] === undefined) {
return;
}
var room = rooms[socket.socketName];
room.started = true;
socket.broadcast.to(socket.socketName).emit('game start');
fn();
});
// When a user disconnects
socket.on('disconnect', function() {
var room;
// The lost socket is a room
if (typeof socket.roomName === 'undefined') {
if (typeof rooms[socket.socketName] === 'undefined') {
return;
}
room = rooms[socket.socketName];
if (room.roomSockets[socket.id]) {
delete room.roomSockets[socket.id];
room.logger.log('Host : ' + socket.id + ' disconnected');
if (Object.keys(room.roomSockets).length === 0) {
for (var i in room.mobileSockets) {
if (room.mobileSockets.hasOwnProperty(i)) {
room.mobileSockets[i].disconnect();
}
}
socket.leaveAll();
room.logger.log('Room : ' + socket.socketName + ' DESTROYED');
mainLogger.log('Room : ' + socket.socketName + ' DESTROYED');
delete rooms[socket.socketName];
}
}
}
// Lost socket is a mobile connections
else {
var roomName = socket.roomName;
// Check if room still exist
if (rooms[roomName] !== undefined) {
room = rooms[roomName];
if (room.mobileSockets[socket.id]) {
delete room.mobileSockets[socket.id];
if (room.players[socket.id]) {
socket.broadcast.to(roomName).emit('user removed',
room.players[socket.id]);
delete room.players[socket.id];
room.logger.log('User : ' + socket.id +
' disconnected from : ' + roomName);
}
}
}
}
});
// Custom Events for your Game
socket.on('player increase', function() {
if (rooms[socket.roomName] === undefined) {
return;
}
var room = rooms[socket.roomName];
var player = room.players[socket.id];
player.increase();
socket.broadcast.to(socket.roomName).emit('update player', player);
});
});
})();