1
0

Trading with bank seems to work

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-02-17 18:20:11 -08:00
parent 8686663d80
commit 5cc69d04bc

View File

@ -998,47 +998,58 @@ router.put("/:id/:action/:value?", async (req, res) => {
break;
}
const offer = req.body;
let target;
/* Verify that the offer sent by the active player matches what
* the latest offer was that was received by the requesting player */
let mismatch = false, target = game.players[offer.color];
offer.gives.forEach(item => {
const isOffered = target.gives.find(
match => match.type === item.type && match.count === item.count);
if (!isOffered) {
mismatch = true;
if (!offer.name || offer.name !== 'The bank') {
let mismatch = false;
target = game.players[offer.color];
offer.gives.forEach(item => {
const isOffered = target.gives.find(
match => match.type === item.type && match.count === item.count);
if (!isOffered) {
mismatch = true;
}
});
offer.gets.forEach(item => {
const isOffered = target.gets.find(
match => match.type === item.type && match.count === item.count);
if (!isOffered) {
mismatch = true;
}
});
if (mismatch) {
error = `Unfortunately, trades were re-negotiated in transit and the deal is invalid!`;
break;
}
});
offer.gets.forEach(item => {
const isOffered = target.gets.find(
match => match.type === item.type && match.count === item.count);
if (!isOffered) {
mismatch = true;
}
});
if (mismatch) {
error = `Unfortunately, trades were re-negotiated in transit and the deal is invalid!`;
break;
}
/* Transfer goods */
offer.gives.forEach(item => {
target[item.type] -= item.count;
player.gets.forEach(item => {
if (target) {
target[item.type] -= item.count;
}
player[item.type] += item.count;
});
offer.gets.forEach(item => {
target[item.type] += item.count;
player.gives.forEach(item => {
if (target) {
target[item.type] += item.count;
}
player[item.type] -= item.count;
});
delete game.turn.offer;
delete target.gives;
delete target.gets;
if (target) {
delete target.gives;
delete target.gets;
}
delete session.player.gives;
delete session.player.gets;
game.turn.actions = [];
addChatMessage(game, session, `${session.name} has accepted a trade offer from ${offer.name}.`);
addChatMessage(game, session, `${session.name} has accepted a trade ` +
`offer from ${(offer.name === 'The bank') ? 'the bank' : offer.name}.`);
break;
}