-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
53 lines (45 loc) · 1.47 KB
/
server.js
File metadata and controls
53 lines (45 loc) · 1.47 KB
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
var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var game = require('./game.js');
var game_room = require('./game_room.js');
var _ = require('./public/js/underscore-min.js');
var server_port = (process.env.PORT || 3000);
var game_rooms = [];
setInterval(function() {
game_rooms = _.reject(game_rooms, function(room) {
var should_remove = room.should_be_removed();
if (should_remove) {
console.log("removing room: " + room.id + " from room pool");
}
return should_remove;
});
}, 5000);
app.use("/public", express.static(__dirname + '/public'));
app.get('/', function(req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/maze/', function(req, res) {
res.sendFile(__dirname + '/maze.html');
});
io.on('connection', function(socket) {
console.log("player: " + socket.id + " is looking for room");
var room = _.find(game_rooms, function(room) { return room.has_room(); });
if (!room) {
console.log("no open rooms. making a new one");
room = new game_room.game_room({ name: "harg", io: io });
game_rooms.push(room);
}
console.log("calling add player for: " + socket.id);
room.join(socket);
});
io.on('connection', function(socket){
socket.on('chat message', function(msg){
io.emit('chat message', msg);
console.log('message: ' + msg);
});
});
http.listen(server_port, function() {
console.log('listening on *:' + String(server_port));
});