diff --git a/server/routes/games.js b/server/routes/games.js index ce9d1d2..3f16346 100755 --- a/server/routes/games.js +++ b/server/routes/games.js @@ -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; }