|
|
@@ -166,32 +166,20 @@ const onMessage = (thread, message) => { |
|
|
|
messageHandlers[message.method](thread, message); |
|
|
|
}; |
|
|
|
|
|
|
|
const spawnThread = async ({ name, path, instanced, destroyWhenEmptyForMs = -1 }, obj) => { |
|
|
|
const spawnThread = async ({ map: { name, path, instanced, destroyWhenEmptyForMs = -1 }, obj }) => { |
|
|
|
let cbOnInitialized; |
|
|
|
|
|
|
|
const promise = new Promise(resolveOnReady => { |
|
|
|
cbOnInitialized = resolveOnReady; |
|
|
|
}); |
|
|
|
|
|
|
|
const worker = childProcess.fork('./world/worker', [name]); |
|
|
|
|
|
|
|
let id = instanced ? _.getGuid() : name; |
|
|
|
const emBeforeGetThreadId = { |
|
|
|
id, |
|
|
|
obj, |
|
|
|
name, |
|
|
|
instanced |
|
|
|
}; |
|
|
|
events.emit('beforeGetThreadId', emBeforeGetThreadId); |
|
|
|
|
|
|
|
id = emBeforeGetThreadId.id; |
|
|
|
|
|
|
|
const thread = { |
|
|
|
id, |
|
|
|
name, |
|
|
|
instanced, |
|
|
|
path, |
|
|
|
worker, |
|
|
|
worker: null, |
|
|
|
isReady: false, |
|
|
|
promise, |
|
|
|
cbOnInitialized, |
|
|
@@ -199,10 +187,25 @@ const spawnThread = async ({ name, path, instanced, destroyWhenEmptyForMs = -1 } |
|
|
|
playersCurrent: [], |
|
|
|
birthEpoch: +new Date(), |
|
|
|
destroyWhenEmptyForMs, |
|
|
|
emptySinceEpoch: null |
|
|
|
emptySinceEpoch: null, |
|
|
|
sendArgsToWorker: ['name', 'id'] |
|
|
|
}; |
|
|
|
|
|
|
|
worker.on('message', onMessage.bind(null, thread)); |
|
|
|
const emBeforeSpawnThread = { |
|
|
|
thread, |
|
|
|
spawnForObject: obj |
|
|
|
}; |
|
|
|
events.emit('beforeSpawnThread', emBeforeSpawnThread); |
|
|
|
|
|
|
|
const workerArgs = JSON.stringify( |
|
|
|
Object.fromEntries( |
|
|
|
thread.sendArgsToWorker.map(a => [a, thread[a]]) |
|
|
|
) |
|
|
|
); |
|
|
|
|
|
|
|
thread.worker = childProcess.fork('./world/worker', [workerArgs]); |
|
|
|
|
|
|
|
thread.worker.on('message', onMessage.bind(null, thread)); |
|
|
|
|
|
|
|
threads.push(thread); |
|
|
|
|
|
|
@@ -221,16 +224,19 @@ const getThread = async ({ zoneName, zoneId, obj }) => { |
|
|
|
map = mapList.find(m => m.name === clientConfig.config.defaultZone); |
|
|
|
|
|
|
|
let thread = threads.find(t => t.id === zoneId && t.name === zoneName); |
|
|
|
|
|
|
|
|
|
|
|
//Maybe this player has been in a thread for this map before |
|
|
|
if (!thread) |
|
|
|
if (!thread) |
|
|
|
thread = threads.find(t => t.name === zoneName && t.players.includes(obj.id)); |
|
|
|
|
|
|
|
if (!thread) { |
|
|
|
if (map.instanced) |
|
|
|
result.resetObjPosition = true; |
|
|
|
|
|
|
|
thread = await spawnThread(map, obj); |
|
|
|
thread = await spawnThread({ |
|
|
|
map, |
|
|
|
obj |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
if (!thread.isReady) |
|
|
|