Move isItemInStash into inventoryHelper

Cleanup of variable names
This commit is contained in:
Dev 2024-01-20 20:15:03 +00:00
parent 4bca0b7747
commit ceba6b3064
2 changed files with 50 additions and 46 deletions

View File

@ -1521,6 +1521,34 @@ export class InventoryHelper
{ {
return this.inventoryConfig; return this.inventoryConfig;
} }
/**
* Recursively checks if the given item is
* inside the stash, that is it has the stash as
* ancestor with slotId=hideout
* @param pmcData Player profile
* @param itemToCheck Item to look for
* @returns True if item exists inside stash
*/
public isItemInStash(pmcData: IPmcData, itemToCheck: Item): boolean
{
let container = itemToCheck;
while ("parentId" in container)
{
if (container.parentId === pmcData.Inventory.stash && container.slotId === "hideout")
{
return true;
}
container = pmcData.Inventory.items.find((item) => item._id === container.parentId);
if (!container)
{
break;
}
}
return false;
}
} }
namespace InventoryHelper namespace InventoryHelper

View File

@ -161,64 +161,65 @@ export class PaymentService
/** /**
* Receive money back after selling * Receive money back after selling
* @param {IPmcData} pmcData * @param {IPmcData} pmcData
* @param {number} amount * @param {number} amountToSend
* @param {IProcessSellTradeRequestData} body * @param {IProcessSellTradeRequestData} request
* @param {IItemEventRouterResponse} output * @param {IItemEventRouterResponse} output
* @param {string} sessionID * @param {string} sessionID
* @returns IItemEventRouterResponse * @returns IItemEventRouterResponse
*/ */
public giveProfileMoney( public giveProfileMoney(
pmcData: IPmcData, pmcData: IPmcData,
amount: number, amountToSend: number,
body: IProcessSellTradeRequestData, request: IProcessSellTradeRequestData,
output: IItemEventRouterResponse, output: IItemEventRouterResponse,
sessionID: string, sessionID: string,
): void ): void
{ {
const trader = this.traderHelper.getTrader(body.tid, sessionID); const trader = this.traderHelper.getTrader(request.tid, sessionID);
const currency = this.paymentHelper.getCurrency(trader.currency); const currency = this.paymentHelper.getCurrency(trader.currency);
let calcAmount = this.handbookHelper.fromRUB(this.handbookHelper.inRUB(amount, currency), currency); let calcAmount = this.handbookHelper.fromRUB(this.handbookHelper.inRUB(amountToSend, currency), currency);
const maxStackSize = this.databaseServer.getTables().templates.items[currency]._props.StackMaxSize; const currencyMaxStackSize = this.databaseServer.getTables().templates.items[currency]._props.StackMaxSize;
let skip = false; let skipSendingMoneyToStash = false;
for (const item of pmcData.Inventory.items) for (const item of pmcData.Inventory.items)
{ {
// item is not currency // Item is not currency
if (item._tpl !== currency) if (item._tpl !== currency)
{ {
continue; continue;
} }
// item is not in the stash // Item is not in the stash
if (!this.isItemInStash(pmcData, item)) if (!this.inventoryHelper.isItemInStash(pmcData, item))
{ {
continue; continue;
} }
if (item.upd.StackObjectsCount < maxStackSize) // Found currency item
if (item.upd.StackObjectsCount < currencyMaxStackSize)
{ {
if (item.upd.StackObjectsCount + calcAmount > maxStackSize) if (item.upd.StackObjectsCount + calcAmount > currencyMaxStackSize)
{ {
// calculate difference // calculate difference
calcAmount -= maxStackSize - item.upd.StackObjectsCount; calcAmount -= currencyMaxStackSize - item.upd.StackObjectsCount;
item.upd.StackObjectsCount = maxStackSize; item.upd.StackObjectsCount = currencyMaxStackSize;
} }
else else
{ {
skip = true; skipSendingMoneyToStash = true;
item.upd.StackObjectsCount = item.upd.StackObjectsCount + calcAmount; item.upd.StackObjectsCount = item.upd.StackObjectsCount + calcAmount;
} }
output.profileChanges[sessionID].items.change.push(item); output.profileChanges[sessionID].items.change.push(item);
if (skip) if (skipSendingMoneyToStash)
{ {
break; break;
} }
} }
} }
if (!skip) if (!skipSendingMoneyToStash)
{ {
const addItemToStashRequest: IAddItemDirectRequest = { const addItemToStashRequest: IAddItemDirectRequest = {
itemWithModsToAdd: [ itemWithModsToAdd: [
@ -238,35 +239,10 @@ export class PaymentService
} }
// set current sale sum // set current sale sum
const saleSum = pmcData.TradersInfo[body.tid].salesSum + amount; const saleSum = pmcData.TradersInfo[request.tid].salesSum + amountToSend;
pmcData.TradersInfo[body.tid].salesSum = saleSum; pmcData.TradersInfo[request.tid].salesSum = saleSum;
this.traderHelper.lvlUp(body.tid, pmcData); this.traderHelper.lvlUp(request.tid, pmcData);
}
/**
* Recursively checks if the given item is
* inside the stash, that is it has the stash as
* ancestor with slotId=hideout
*/
protected isItemInStash(pmcData: IPmcData, item: Item): boolean
{
let container = item;
while ("parentId" in container)
{
if (container.parentId === pmcData.Inventory.stash && container.slotId === "hideout")
{
return true;
}
container = pmcData.Inventory.items.find((i) => i._id === container.parentId);
if (!container)
{
break;
}
}
return false;
} }
/** /**