Fix player being charged multiple times

Fix issue with  `findBarterItems()` where it would incorrectly wipe out found results each loop
This commit is contained in:
Dev 2024-01-16 18:25:03 +00:00
parent 33d1395dea
commit 93edb3e074
3 changed files with 30 additions and 40 deletions

View File

@ -615,32 +615,36 @@ export class ItemHelper
/**
* Find Barter items from array of items
* @param {string} by tpl or id
* @param {Item[]} items Array of items to iterate over
* @param {string} barterItemId
* @param {Item[]} itemsToSearch Array of items to iterate over
* @param {string} desiredBarterItemIds
* @returns Array of Item objects
*/
public findBarterItems(by: "tpl" | "id", items: Item[], barterItemId: string): Item[]
public findBarterItems(by: "tpl" | "id", itemsToSearch: Item[], desiredBarterItemIds: string | string[]): Item[]
{
// find required items to take after buying (handles multiple items)
const barterIDs = typeof barterItemId === "string" ? [barterItemId] : barterItemId;
// Find required items to take after buying (handles multiple items)
const desiredBarterIds = typeof desiredBarterItemIds === "string"
? [desiredBarterItemIds]
: desiredBarterItemIds;
let barterItems: Item[] = [];
for (const barterID of barterIDs)
const matchingItems: Item[] = [];
for (const barterId of desiredBarterIds)
{
const filterResult = items.filter((item) =>
const filterResult = itemsToSearch.filter((item) =>
{
return by === "tpl" ? (item._tpl === barterID) : (item._id === barterID);
return by === "tpl"
? (item._tpl === barterId)
: (item._id === barterId);
});
barterItems = Object.assign(barterItems, filterResult);
matchingItems.push(...this.jsonUtil.clone(filterResult));
}
if (barterItems.length === 0)
if (matchingItems.length === 0)
{
this.logger.warning(`No items found for barter Id: ${barterIDs}`);
this.logger.warning(`No items found for barter Id: ${desiredBarterIds}`);
}
return barterItems;
return matchingItems;
}
/**

View File

@ -100,15 +100,6 @@ export class TradeHelper
this.traderHelper.addTraderPurchasesToPlayerProfile(sessionID, itemPurchaseDat);
}
/// Pay for item
this.paymentService.payMoney(pmcData, buyRequestData, sessionID, output);
if (output.warnings.length > 0)
{
this.logger.error(`Flea transaction failed: ${output.warnings[0].errmsg}`);
return output;
}
if (assortHasBuyRestrictions)
{
// Increment non-fence trader item buy count
@ -132,16 +123,6 @@ export class TradeHelper
// Decrement trader item count
itemPurchased.upd.StackObjectsCount -= buyCount;
/// Pay for item
this.paymentService.payMoney(pmcData, buyRequestData, sessionID, output);
if (output.warnings.length > 0)
{
const errorMessage = `Transaction failed: ${output.warnings[0].errmsg}`;
this.httpResponse.appendErrorToOutput(output, errorMessage);
return;
}
this.fenceService.removeFenceOffer(buyRequestData.item_id);
};
@ -190,13 +171,6 @@ export class TradeHelper
this.traderHelper.addTraderPurchasesToPlayerProfile(sessionID, itemPurchaseDat);
}
/// Pay for item
this.paymentService.payMoney(pmcData, buyRequestData, sessionID, output);
if (output.warnings.length > 0)
{
return this.httpResponse.appendErrorToOutput(output, output.warnings[0].errmsg, BackendErrorCodes.UNKNOWN_TRADING_ERROR);
}
if (assortHasBuyRestrictions)
{
// Increment non-fence trader item buy count
@ -217,6 +191,8 @@ export class TradeHelper
const itemMaxStackSize = itemDbDetails._props.StackMaxSize;
const itemsToSendTotalCount = buyRequestData.count;
let itemsToSendRemaining = itemsToSendTotalCount;
// Loop until all items have been sent to player stash
while (itemsToSendRemaining > 0)
{
// Handle edge case when remaining items to send < max stack size
@ -240,9 +216,18 @@ export class TradeHelper
{
return;
}
// Remove amount of items added to player stash
itemsToSendRemaining -= itemCountToSend;
}
/// Pay for purchase
this.paymentService.payMoney(pmcData, buyRequestData, sessionID, output);
if (output.warnings.length > 0)
{
const errorMessage = `Transaction failed: ${output.warnings[0].errmsg}`;
this.httpResponse.appendErrorToOutput(output, errorMessage, BackendErrorCodes.UNKNOWN_TRADING_ERROR);
}
}
/**

View File

@ -46,6 +46,7 @@ export class PaymentService
output: IItemEventRouterResponse,
): IItemEventRouterResponse
{
// May need to convert to trader currency
const trader = this.traderHelper.getTrader(request.tid, sessionID);
// Track the amounts of each type of currency involved in the trade.