-
Notifications
You must be signed in to change notification settings - Fork 41
/
main.js
345 lines (270 loc) · 10.3 KB
/
main.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
This file is part of Ironbane MMO.
Ironbane MMO is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Ironbane MMO is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Ironbane MMO. If not, see <http://www.gnu.org/licenses/>.
*/
var path = require('path'),
config = require('./nconf'),
pkg = require('./package.json'),
log = require('util').log,
Q = require('q');
// track root path of this file, so no matter where the game is launched from we have the base for require
global.APP_ROOT_PATH = __dirname;
var isProduction = config.get('isProduction');
var cryptSalt = config.get('cryptSalt');
// profiling...
if (!isProduction && config.get('use_nodetime')) {
require('nodetime').profile(config.get('nodetime'));
}
var clientDir = config.get('buildTarget') + 'game/';
var assetDir = config.get('assetDir');
if (!cryptSalt) {
var warn = function() {
console.log("Warning: no password hash set! Your server works, but is very insecure. Edit config.json and set the cryptSalt variable.");
};
if (isProduction) {
setInterval(warn, 1000);
} else {
setTimeout(warn, 3000);
}
}
// System start
var SERVER = true;
var fs = require('fs');
var spawn = require('child_process').spawn;
var window = {};
// Custom
var NameGen = require('./External/namegen.js');
var wrench = require('wrench');
var crypto = require('crypto');
var util = require('./Engine/ibutil.js');
var fsi = require('./External/fsi.js');
// https://github.com/chriso/node-validator
var check = require('validator').check,
sanitize = require('validator').sanitize;
var _ = require('underscore');
var THREE = require('./src/client/game/lib/three/three.js');
// Everything runs on one database, since the db is not hurt that bad by performance
// We cut the middle man and only use mysql occasionally to save/load data
// Start MySQL - has to be here for global access below...
var mysql = require('./src/server/db');
var GameEngine = require('./src/server/game/engine');
// create game server, do it first so that the other 2 "servers" can query it
var engine;
// create express.io server
var HttpServer,
httpServer;
// for the global access coming...todo: refactor
var io,
ioApp;
// setup REPL for console server mgmt
var startREPL = function() {
if(config.get('use_repl') === true){
var repl = require('repl');
// Not game stuff, this is for the server executable
process.stdin.setEncoding('utf8');
// startup a full node repl for javascript awesomeness
var serverREPL = repl.start({
prompt: "ironbane> ",
input: process.stdin,
output: process.stdout
});
serverREPL.on('exit', function() {
// todo: other shutdown stuff, like stop db, etc.
process.exit();
});
// context variables get attached to "global" of this instance
serverREPL.context.version = pkg.version;
serverREPL.context.httpServer = httpServer;
}
if(config.get('use_netrepl') === true){
// Add UNIX socket to REPL for awesome realtime debugging
// We take the sweetest auto-complete with colors available module
var repl = require('net-repl');
var options = {
prompt: 'ironbane> ',
deleteSocketOnStart: true,
useGlobal: true,
useColors: true
}
var socketPath = "/tmp/ibs-" + config.get('mysql_database');
var srv = repl.createServer(options).listen(socketPath);
}
};
// Necessary to prevent 'Mysql has gone away' errors
// Use it check for restarting on git push
var shuttingDown = false;
function keepAlive() {
//mysql.query('SELECT 1');
if (shuttingDown) {
return;
}
mysql.query('SELECT value FROM ib_config WHERE name = ?', ["restart"], function(err, results, fields) {
if (err) {
throw err;
}
if (results.length) {
shuttingDown = true;
global.chatHandler.announce("<Server> New update available!", "red");
global.chatHandler.announce("<Server> Auto-restarting in 10 seconds...", "red");
// Disconnect all clients first
io.sockets.emit("disconnect");
setTimeout(function() {
mysql.query('DELETE FROM ib_config WHERE name = ?', ["restart"], function(err, results, fields) {
if (err) {
throw err;
}
process.exit();
});
}, 20000);
}
});
return;
}
var LoadActorScripts = function() {
var scriptPath = assetDir + 'scripts',
fs = require('q-io/fs'),
path = require('path'),
actorScripts = {};
return fs.listTree(scriptPath, function(file) {
return path.extname(file) === '.js';
}).then(function(files) {
console.log('loading actor scripts...' + files.length);
_.each(files, function(file) {
console.log("loading... " + path.basename(file));
try {
var script = require(path.resolve(file, '.')); // does this fix both nix & windows?
// script should be an object to add
_.each(_.keys(script), function(key) {
global.actorScripts[key] = script[key];
});
} catch(e) {
console.error("error loading script!!" + e);
}
});
return actorScripts;
}, function(err) {
console.log('error loading asset scripts! ', err);
return {};
});
};
function start(scripts) {
// create express.io server
HttpServer = require('./src/server/http/server').Server;
httpServer = new HttpServer({
db: mysql
});
// for the global access coming...todo: refactor
io = httpServer.server.io;
ioApp = httpServer.server;
// load constants (was Shared.js)
var IB = require('./src/common/constants');
// inject into global until rest is modular
_.extend(global, IB);
// temporarily global services
global.itemTemplateService = require('./src/server/services/itemTemplate');
global.itemService = require('./src/server/services/item');
// temporarily global entities
global.ItemTemplate = require('./src/server/entity/itemTemplate');
global.Item = require('./src/server/entity/item');
// Load DataHandler global for now (holds memory DB of item and unit templates)
global.dataHandler = require('./src/server/game/dataHandler');
// load AI as a module
var AI = require('./src/server/game/ai');
// temp pass them on to global for access below
global.StateMachine = AI.StateMachine;
global.State = AI.State;
_.extend(global, AI.States);
// READ REST OF OLD GLOBAL APP HERE
for (var f = 0; f < includes.length; f++) {
log("Loading: " + includes[f]);
eval(fs.readFileSync(includes[f]) + '');
}
// temp hack
global.worldHandler = worldHandler;
global.actorScripts = {};
LoadActorScripts()
.then(function(scripts) {
_.extend(global.actorScripts, scripts);
// Load Chat module - after worldHandler, there is a dep
global.chatHandler = require('./src/server/game/chat')(io, global.dataHandler.units, global.worldHandler);
// All set! Tell WorldHandler to load
return worldHandler.loadWorld();
}).then(function() {
return worldHandler.Awake(); // Awaken units after loading
}).then(function() {
// create game server, do it first so that the other 2 "servers" can query it
engine = new GameEngine();
engine.on('start', function() {
engine.tick();
});
engine.on('tick', function(elapsed) {
server.tick(elapsed);
setTimeout(function() {
engine.tick.bind(engine);
}, 100 );
});
engine.start();
// start it up, todo: only per config?
startREPL();
setInterval(keepAlive, 10000);
setInterval(function() {
worldHandler.autoSave();
}, 60 * 1 * 1000);
// Schedule an auto-restart to prevent EM readfile errors
// We can only prevent this by upgrading to a real dedicated server, which we will do
// when we have more people. Currently IB runs on a VPS.
setTimeout(function() {
global.chatHandler.announce("This server needs to restart in order to keep it running smoothly. The restart will happen in 1 minute. Your progress will be saved.", "red");
setTimeout(function() {
process.exit();
}, 60000);
}, 60 * 60 * 24 * 1000);
});
}
// These are all to be converted to modules
var includes = [
'/Shared/seedrandom.js',
'/Shared/Buffs.js',
'/Shared/Util.js',
'/Engine/worldhandler/snapshots.js',
'/Engine/worldhandler/zones.js',
'/Engine/ConsoleCommand.js',
'/Engine/ConsoleHandler.js',
'/Engine/Switch.js',
'/Engine/SocketHandler.js',
'/Engine/worldhandler/index.js',
'/Game/SteeringBehaviour.js',
'/Game/Unit.js',
'/Game/MovingUnit.js',
'/Game/Actor.js',
'/Game/Fighter.js',
'/Game/NPC.js',
'/Game/Lootable.js',
'/Game/Player.js',
'/Game/Special/MovingObstacle.js',
'/Game/Special/ToggleableObstacle.js',
'/Game/Special/Train.js',
'/Game/Special/Lever.js',
'/Game/Special/TeleportEntrance.js',
'/Game/Special/TeleportExit.js',
'/Game/Special/Sign.js',
'/Game/Special/Waypoint.js',
'/Game/Special/trigger.js',
'/Game/bank.js', // subclass of trigger
'/Game/Special/HeartPiece.js',
'/Game/Special/MusicPlayer.js',
'/Server.js'
];
includes = _.map(includes, function(include) {
return APP_ROOT_PATH + include;
});
start(includes);