-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
91 lines (83 loc) · 3.07 KB
/
server.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
var http = require('http');
var fs = require('fs');
var webserver = require('./lib/webserver')();
var io = require('socket.io').listen(webserver.httpServer);
var rooms = require('./rooms.json');
var world = require('./lib/world')(rooms);
const DEBUG = process.env.NODE_ENV !== 'production';
io.set('log level', 1);
io.sockets.on('connection', function(socket) {
socket.on('new player', function(sessionID) {
world.addPlayer(sessionID, function(room){
socket.join(room.id);
socket.emit('room', room);
socket.broadcast.to(room.id).emit('room players', room.players);
});
});
socket.on('new position', function(exportPlayer) {
if (!exportPlayer || !exportPlayer.id || !exportPlayer.position) return;
var player = world.getPlayerById(exportPlayer.id);
if (player) {
player.position.x = exportPlayer.position.x;
player.position.y = exportPlayer.position.y;
player.direction.x = exportPlayer.direction.x;
player.direction.y = exportPlayer.direction.y;
player.speed = exportPlayer.speed;
world.getRoomByPlayerId(player.id, function(err, playerRoom){
socket.broadcast.to(playerRoom.id).emit('player update', player);
});
}
});
socket.on('door collision', function(data) {
var exportPlayer = data.player,
doorIndex = data.doorIndex,
newRoom;
if (!exportPlayer || !exportPlayer.id || !exportPlayer.position) return;
var player = world.getPlayerById(exportPlayer.id);
if (!player) { return; }
world.getRoomByPlayerId(player.id, function(err, oldRoom){
var newRoom = world.changeRoom(player, oldRoom, doorIndex);
socket.leave(oldRoom.id);
socket.broadcast.to(oldRoom.id).emit('room players', oldRoom.players);
socket.join(newRoom.id);
socket.broadcast.to(newRoom.id).emit('room players', newRoom.players);
socket.emit('room', newRoom);
});
});
socket.on('message', function(data) {
if (!data.player || !data.message) return;
var player = world.getPlayerById(data.player);
if (!player) { return; }
world.getRoomByPlayerId(player.id, function(err, room) {
socket.broadcast.to(room.id).emit('message', data);
});
});
});
var browserScripts = require(__dirname + '/browser/scripts.json');
webserver.configure(function(httpApp, express) {
httpApp.set('views', __dirname + '/tpl');
httpApp.use(express.static(__dirname + '/browser'));
httpApp.use(express.cookieParser('srautie nrauet eauits'));
httpApp.use(express.session());
});
webserver.httpApp.get('/', function(req, res){
var params = {
sessionID: req.sessionID,
domain: process.env.PROD_DOMAIN || 'localhost',
scripts: browserScripts,
debugmode: DEBUG
};
res.render('index.html', params, function(err, html){
res.send(html);
});
});
webserver.httpApp.get('/debug', function(req, res){
res.render('debug.html', function(err, html){
res.send(html);
});
});
webserver.httpApp.get('/rooms.json', function(req, res){
res.send(rooms);
});
webserver.listen(8000);
console.log('Server running at http://0.0.0.0:8000/');