forked from mozilla/BrowserQuest
-
Notifications
You must be signed in to change notification settings - Fork 220
/
Copy pathmain.js
152 lines (133 loc) · 4.82 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
var fs = require('fs');
var Metrics = require('./metrics');
var ProductionConfig = require('./productionconfig');
var _ = require('underscore');
function main(config) {
var Log = require('log');
switch(config.debug_level) {
case "error":
log = new Log(Log.ERROR); break;
case "debug":
log = new Log(Log.DEBUG); break;
case "info":
log = new Log(Log.INFO); break;
};
var production_config = new ProductionConfig(config);
if(production_config.inProduction()) {
_.extend(config, production_config.getProductionSettings());
}
var ws = require("./ws");
var WorldServer = require("./worldserver");
var server = new ws.WebsocketServer(config.port, config.use_one_port, config.ip);
var metrics = config.metrics_enabled ? new Metrics(config) : null;
var worlds = [];
var lastTotalPlayers = 0;
var DatabaseSelector = require("./databaseselector");
var checkPopulationInterval = setInterval(function() {
if(metrics && metrics.isReady) {
metrics.updateWorldCount();
metrics.getTotalPlayers(function(totalPlayers) {
if(totalPlayers !== lastTotalPlayers) {
lastTotalPlayers = totalPlayers;
_.each(worlds, function(world) {
world.updatePopulation(totalPlayers);
});
}
});
}
}, 1000);
log.info("Starting BrowserQuest game server...");
var selector = DatabaseSelector(config);
databaseHandler = new selector(config);
server.onConnect(function(connection) {
var world; // the one in which the player will be spawned
var connect = function() {
if(world) {
world.connect_callback(new Player(connection, world, databaseHandler));
}
};
if(metrics) {
metrics.getOpenWorldCount(function(open_world_count) {
// choose the least populated world among open worlds
world = _.min(_.first(worlds, open_world_count), function(w) { return w.playerCount; });
connect();
});
}
else {
// simply fill each world sequentially until they are full
world = _.find(worlds, function(world) {
return world.playerCount < config.nb_players_per_world;
});
world.updatePopulation();
connect();
}
});
server.onError(function() {
log.error(Array.prototype.join.call(arguments, ", "));
});
var onPopulationChange = function() {
metrics.updatePlayerCounters(worlds, function(totalPlayers) {
_.each(worlds, function(world) {
world.updatePopulation(totalPlayers);
});
});
metrics.updateWorldDistribution(getWorldDistribution(worlds));
};
_.each(_.range(config.nb_worlds), function(i) {
var world = new WorldServer('world'+ (i+1), config.nb_players_per_world, server, databaseHandler);
world.run(config.map_filepath);
worlds.push(world);
if(metrics) {
world.onPlayerAdded(onPopulationChange);
world.onPlayerRemoved(onPopulationChange);
}
});
server.onRequestStatus(function() {
return JSON.stringify(getWorldDistribution(worlds));
});
if(config.metrics_enabled) {
metrics.ready(function() {
onPopulationChange(); // initialize all counters to 0 when the server starts
});
}
process.on('uncaughtException', function (e) {
// Display the full error stack, to aid debugging
log.error('uncaughtException: ' + e.stack);
});
}
function getWorldDistribution(worlds) {
var distribution = [];
_.each(worlds, function(world) {
distribution.push(world.playerCount);
});
return distribution;
}
function getConfigFile(path, callback) {
fs.readFile(path, 'utf8', function(err, json_string) {
if(err) {
console.info("This server can be customized by creating a configuration file named: " + err.path);
callback(null);
} else {
callback(JSON.parse(json_string));
}
});
}
var defaultConfigPath = './server/config.json';
var customConfigPath = './server/config_local.json';
process.argv.forEach(function (val, index, array) {
if(index === 2) {
customConfigPath = val;
}
});
getConfigFile(defaultConfigPath, function(defaultConfig) {
getConfigFile(customConfigPath, function(localConfig) {
if(localConfig) {
main(localConfig);
} else if(defaultConfig) {
main(defaultConfig);
} else {
console.error("Server cannot start without any configuration file.");
process.exit(1);
}
});
});