This repository has been archived by the owner on Nov 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathserver.js
executable file
·87 lines (69 loc) · 2.5 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
#!/usr/bin/env node
'use strict';
var express = require("express");
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server, {log: false});
var players = require('./lib/players.js').setSocket(io);
var logger = require('./lib/logger.js');
var map = require('./lib/map.js').setHttp(app);
var corruption = require('./lib/corruption.js').setMap(map).setSocket(io);
var daynight = require('./lib/daynight.js').setMap(map).setSocket(io);
var earthquake = require('./lib/earthquake.js').setMap(map).setSocket(io);
var terrain = require('./lib/terrain.js').setMap(map);
var npcs = require('./lib/npcs.js').setMap(map).setSocket(io).setPlayers(players);
// Web Server Configuration
var server_port = parseInt(process.argv[2], 10) || 80; // most OS's will require sudo to listen on 80
var server_host = process.argv[3];
map.loadMap(function(err) {
if (err) {
logger.error(err);
throw err;
}
npcs.spawn(80);
corruption.execute();
daynight.execute();
earthquake.execute();
npcs.execute();
});
// HTTP
logger.notice("Express", "Attempting to listen on: " + (server_host||'0.0.0.0') + ':' + server_port);
server.listen(server_port, server_host);
app.on('error', function (e) {
if (e.code == 'EADDRINUSE') {
logger.error("Express", "Address in use. Quitting...");
process.exit();
} else if (e.code == 'EACCES') {
logger.error("Express", "You don't have permissions to bind to this address. Try running via sudo.");
} else {
logger.error("Express", e);
process.exit();
}
});
// User requests root, return HTML
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html');
});
app.get('/favicon.ico', function (req, res) {
res.sendFile(__dirname + '/favicon.ico');
});
// User requests a file in the assets folder, read it and return it
app.get('/assets/*', function (req, res) {
// is this secure? in PHP land it would be pretty bad
res.sendFile(__dirname + '/assets/' + req.params[0]);
});
// SOCKETS
io.sockets.on('connection', function (socket) {
logger.action("Player", "Connected");
socket.emit('chat', {
name: 'Server',
message: 'Socket Established',
priority: 'server'
});
players.sendData(socket);
daynight.sendData(socket);
corruption.sendData(socket);
npcs.sendData(socket);
players.handleSocketEvents(socket);
map.handleSocketEvents(socket);
});