Master See merge request Isleward/isleward!614tags/v0.12.0
@@ -414,7 +414,7 @@ module.exports = { | |||||
setAllAmounts: function (amount) { | setAllAmounts: function (amount) { | ||||
this.list.forEach(l => { | this.list.forEach(l => { | ||||
l.amount = amount; | |||||
l.threat = amount; | |||||
}); | }); | ||||
} | } | ||||
}; | }; |
@@ -25,7 +25,7 @@ module.exports = { | |||||
this.players.push(p); | this.players.push(p); | ||||
}, | }, | ||||
onDisconnect: function (socket) { | |||||
onDisconnect: async function (socket) { | |||||
let player = this.players.find(p => p.socket.id === socket.id); | let player = this.players.find(p => p.socket.id === socket.id); | ||||
if (!player) | if (!player) | ||||
@@ -2,14 +2,13 @@ | |||||
const objects = require('../objects/objects'); | const objects = require('../objects/objects'); | ||||
const events = require('../misc/events'); | const events = require('../misc/events'); | ||||
const { | const { | ||||
getThread, killThread, sendMessageToThread, getThreadFromId, returnWhenThreadsIdle | |||||
getThread, killThread, sendMessageToThread, getThreadFromId, returnWhenThreadsIdle, canThreadBeClosed | |||||
} = require('./threadManager'); | } = require('./threadManager'); | ||||
const { registerCallback, removeCallback } = require('./atlas/registerCallback'); | |||||
//Exports | //Exports | ||||
module.exports = { | module.exports = { | ||||
nextId: 0, | nextId: 0, | ||||
lastCallbackId: 0, | |||||
callbacks: [], | |||||
addObject: async function (obj, keepPos, transfer) { | addObject: async function (obj, keepPos, transfer) { | ||||
const serverObj = objects.objects.find(o => o.id === obj.id); | const serverObj = objects.objects.find(o => o.id === obj.id); | ||||
@@ -80,7 +79,7 @@ module.exports = { | |||||
callback(); | callback(); | ||||
}, | }, | ||||
removeObject: function (obj, skipLocal, callback) { | |||||
removeObject: async function (obj, skipLocal, callback) { | |||||
if (!skipLocal) | if (!skipLocal) | ||||
objects.removeObject(obj); | objects.removeObject(obj); | ||||
@@ -88,7 +87,7 @@ module.exports = { | |||||
if (!thread) | if (!thread) | ||||
return; | return; | ||||
if (thread.instanced) { | |||||
if (thread.instanced && await canThreadBeClosed(thread, this.registerCallback.bind(this))) { | |||||
this.removeObjectFromInstancedZone(thread, obj, callback); | this.removeObjectFromInstancedZone(thread, obj, callback); | ||||
return; | return; | ||||
@@ -147,15 +146,11 @@ module.exports = { | |||||
}, | }, | ||||
registerCallback: function (callback) { | registerCallback: function (callback) { | ||||
this.callbacks.push({ | |||||
id: ++this.lastCallbackId, | |||||
callback: callback | |||||
}); | |||||
return this.lastCallbackId; | |||||
return registerCallback(callback); | |||||
}, | }, | ||||
resolveCallback: function (msg) { | resolveCallback: function (msg) { | ||||
let callback = this.callbacks.spliceFirstWhere(c => c.id === msg.msg.id); | |||||
const callback = removeCallback(msg.msg.id); | |||||
if (!callback) | if (!callback) | ||||
return; | return; | ||||
@@ -0,0 +1,22 @@ | |||||
let lastCallbackId = 0; | |||||
const callbacks = []; | |||||
const registerCallback = callback => { | |||||
callbacks.push({ | |||||
id: ++lastCallbackId, | |||||
callback | |||||
}); | |||||
return lastCallbackId; | |||||
}; | |||||
const removeCallback = callbackId => { | |||||
const callback = callbacks.spliceFirstWhere(c => c.id === callbackId); | |||||
return callback; | |||||
}; | |||||
module.exports = { | |||||
registerCallback, | |||||
removeCallback | |||||
}; |
@@ -385,6 +385,18 @@ module.exports = { | |||||
id: callbackId | id: callbackId | ||||
} | } | ||||
}); | }); | ||||
} | |||||
}, | |||||
getPlayerCount: function ({ callbackId }) { | |||||
process.send({ | |||||
module: 'atlas', | |||||
method: 'resolveCallback', | |||||
msg: { | |||||
id: callbackId, | |||||
result: { | |||||
playerCount: objects.objects.filter(o => o.player !== undefined).length | |||||
} | |||||
} | |||||
}); | |||||
} | |||||
}; | }; |
@@ -5,6 +5,7 @@ const childProcess = require('child_process'); | |||||
const objects = require('../objects/objects'); | const objects = require('../objects/objects'); | ||||
const connections = require('../security/connections'); | const connections = require('../security/connections'); | ||||
const { mapList } = require('./mapManager'); | const { mapList } = require('./mapManager'); | ||||
const { registerCallback } = require('./atlas/registerCallback'); | |||||
//Internals | //Internals | ||||
const threads = []; | const threads = []; | ||||
@@ -19,6 +20,21 @@ const getThreadFromId = threadId => { | |||||
return threads.find(t => t.id === threadId); | return threads.find(t => t.id === threadId); | ||||
}; | }; | ||||
const canThreadBeClosed = async thread => { | |||||
const { playerCount } = await new Promise(res => { | |||||
const cb = registerCallback(res); | |||||
thread.worker.send({ | |||||
method: 'getPlayerCount', | |||||
args: { | |||||
callbackId: cb | |||||
} | |||||
}); | |||||
}); | |||||
return playerCount === 0; | |||||
}; | |||||
const messageHandlers = { | const messageHandlers = { | ||||
onReady: function (thread) { | onReady: function (thread) { | ||||
thread.worker.send({ | thread.worker.send({ | ||||
@@ -78,7 +94,7 @@ const messageHandlers = { | |||||
rezone: async function (thread, message) { | rezone: async function (thread, message) { | ||||
const { args: { obj, newZone, keepPos = true } } = message; | const { args: { obj, newZone, keepPos = true } } = message; | ||||
if (thread.instanced) { | |||||
if (thread.instanced && await canThreadBeClosed(thread)) { | |||||
thread.worker.kill(); | thread.worker.kill(); | ||||
threads.spliceWhere(t => t === thread); | threads.spliceWhere(t => t === thread); | ||||
} | } | ||||
@@ -242,5 +258,6 @@ module.exports = { | |||||
spawnMapThreads, | spawnMapThreads, | ||||
messageAllThreads, | messageAllThreads, | ||||
sendMessageToThread, | sendMessageToThread, | ||||
returnWhenThreadsIdle | |||||
returnWhenThreadsIdle, | |||||
canThreadBeClosed | |||||
}; | }; |