|
|
@@ -1,3 +1,7 @@ |
|
|
|
//This lock system is so stupid. But until we rewrite mail (no more auto-fetch) |
|
|
|
// We need a way to stop race conditions (listener picking up mail when we just want to getMail normally) |
|
|
|
const locks = {}; |
|
|
|
|
|
|
|
module.exports = { |
|
|
|
init: function (instance) { |
|
|
|
this.instance = instance; |
|
|
@@ -18,6 +22,11 @@ module.exports = { |
|
|
|
if (!player) |
|
|
|
return; |
|
|
|
|
|
|
|
if (locks[player.name]) |
|
|
|
return; |
|
|
|
|
|
|
|
locks[player.name] = 1; |
|
|
|
|
|
|
|
let items = doc.value; |
|
|
|
let inventory = player.inventory; |
|
|
|
let stash = player.stash; |
|
|
@@ -66,13 +75,79 @@ module.exports = { |
|
|
|
key: doc.id, |
|
|
|
table: 'mail' |
|
|
|
}); |
|
|
|
|
|
|
|
delete locks[player.name]; |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
|
|
|
|
getMail: async function (playerName) { |
|
|
|
//This function is left here because SQLite has one and the server will crash if it's not here. |
|
|
|
// That said, we don't need it because the listener sorts out acquisition. |
|
|
|
let items = await io.getAsync({ |
|
|
|
key: playerName, |
|
|
|
table: 'mail' |
|
|
|
}); |
|
|
|
|
|
|
|
if (!items || !(items instanceof Array)) |
|
|
|
return; |
|
|
|
|
|
|
|
let player = this.instance.objects.objects.find(o => (o.name === playerName)); |
|
|
|
if (!player) |
|
|
|
return; |
|
|
|
|
|
|
|
if (locks[playerName]) |
|
|
|
return; |
|
|
|
|
|
|
|
locks[playerName] = 1; |
|
|
|
|
|
|
|
let inventory = player.inventory; |
|
|
|
let stash = player.stash; |
|
|
|
|
|
|
|
let sentMessages = []; |
|
|
|
|
|
|
|
items.forEach(function (r) { |
|
|
|
if (r.removeAll) { |
|
|
|
for (let i = 0; i < inventory.items.length; i++) { |
|
|
|
let item = inventory.items[i]; |
|
|
|
if ((r.nameLike) && (item.name.indexOf(r.nameLike) > -1)) { |
|
|
|
inventory.destroyItem(item.id, item.quantity ? item.quantity : null); |
|
|
|
i--; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if (stash) { |
|
|
|
for (let i = 0; i < stash.items.length; i++) { |
|
|
|
let item = stash.items[i]; |
|
|
|
if ((r.nameLike) && (item.name.indexOf(r.nameLike) > -1)) { |
|
|
|
stash.destroyItem(item.id); |
|
|
|
i--; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} else { |
|
|
|
if ((r.msg) && (!sentMessages.some(s => (s === r.msg)))) { |
|
|
|
player.instance.syncer.queue('onGetMessages', { |
|
|
|
id: player.id, |
|
|
|
messages: [{ |
|
|
|
class: 'color-greenB', |
|
|
|
message: r.msg, |
|
|
|
type: 'info' |
|
|
|
}] |
|
|
|
}, [player.serverId]); |
|
|
|
|
|
|
|
sentMessages.push(r.msg); |
|
|
|
delete r.msg; |
|
|
|
} |
|
|
|
|
|
|
|
inventory.getItem(r); |
|
|
|
} |
|
|
|
}); |
|
|
|
|
|
|
|
await io.deleteAsync({ |
|
|
|
key: playerName, |
|
|
|
table: 'mail' |
|
|
|
}); |
|
|
|
|
|
|
|
delete locks[playerName]; |
|
|
|
}, |
|
|
|
|
|
|
|
sendMail: async function (playerName, items, callback) { |
|
|
|