forked from rezich/soulsrl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
executable file
·61 lines (55 loc) · 1.48 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
var static = require('node-static');
var http = require('http');
var util = require('util');
var webroot = './public';
var file = new (static.Server)(webroot, {
cache: 600
});
var httpServer = http.createServer(function (req, res) {
req.addListener('end', function () {
file.serve(req, res, function (err, result) {
if (err) {
console.error('Error serving %s - %s', req.url, err.message);
if (err.status === 404 || err.status === 500) {
file.serveFile(util.format('/%d.html', err.status), err.status, {}, req, res);
}
else {
res.writeHead(err.status, err.headers);
res.end();
}
}
else {
console.log('%s - %s', req.url, res.message);
}
});
});
});
httpServer.listen(process.env.PORT || 3000);
var nowjs = require("now");
var everyone = nowjs.initialize(httpServer);
var players = [];
nowjs.on('connect', function () {
players[this.user.clientId] = { x: 1, y: 1 };
})
nowjs.on('disconnect', function () {
for (var i in players) {
if (i == this.user.clientId) {
delete players[i];
break;
}
}
});
everyone.now.updatePlayer = function (x, y) {
players[this.user.clientId].x = x;
players[this.user.clientId].y = y;
var toUpdate = {};
for (var i in players) {
// TODO: limit this to only some players or something
toUpdate[i] = { x: players[i].x, y: players[i].y };
}
for (var i in toUpdate) {
nowjs.getClient(i, function (err) {
this.now.drawPlayers(toUpdate);
});
}
}