diff --git a/src/server/world/atlas.js b/src/server/world/atlas.js index 0062e06d..7425290a 100644 --- a/src/server/world/atlas.js +++ b/src/server/world/atlas.js @@ -36,9 +36,8 @@ module.exports = { if (map.instanced) { delete obj.x; delete obj.y; - thread = this.spawnMap(map); - await new Promise(res => setTimeout(res, 2000)); + thread = await this.spawnMap(map); } else thread = this.getThreadFromName(map.name); } @@ -171,27 +170,27 @@ module.exports = { .filter(m => !m.disabled && !m.instanced) .forEach(m => this.spawnMap(m)); }, - spawnMap: function ({ name, path, instanced }) { - const worker = childProcess.fork('./world/worker', [name]); - - const id = instanced ? _.getGuid() : name; - - const thread = { - id, - name, - instanced, - path, - worker - }; - - const onMessage = this.onMessage.bind(this, thread); - worker.on('message', function (m) { - onMessage(m); - }); + spawnMap: async function ({ name, path, instanced }) { + return new Promise(resolveOnReady => { + const worker = childProcess.fork('./world/worker', [name]); + + const id = instanced ? _.getGuid() : name; + + const thread = { + id, + name, + instanced, + path, + worker, + cbOnInitialized: resolveOnReady + }; - this.threads.push(thread); + const onMessage = this.onMessage.bind(this, thread); + worker.on('message', function (m) { + onMessage(m); + }); - return thread; + this.threads.push(thread); }, onMessage: function (thread, message) { if (message.module) { @@ -229,6 +228,10 @@ module.exports = { }); }, + onInitialized: function (thread) { + thread.cbOnInitialized(thread); + }, + event: function (thread, message) { objects.sendEvent(message, thread); }, diff --git a/src/server/world/instancer.js b/src/server/world/instancer.js index 7e198dda..8a505556 100644 --- a/src/server/world/instancer.js +++ b/src/server/world/instancer.js @@ -75,6 +75,10 @@ module.exports = { this.clientAck = clientAck; eventEmitter.on('removeObject', unstageZoneIn); + + process.send({ + method: 'onInitialized' + }); }, startRegen: function (respawnMap, respawnPos) {