Fixed server error when buying ragman clothing with lega medals

This commit is contained in:
Dev 2024-09-15 22:45:13 +01:00
parent e53cff5737
commit 4f393a5eec
2 changed files with 42 additions and 27 deletions

View File

@ -1,7 +1,10 @@
import { ProfileHelper } from "@spt/helpers/ProfileHelper"; import { ProfileHelper } from "@spt/helpers/ProfileHelper";
import { IPmcData } from "@spt/models/eft/common/IPmcData"; import { IPmcData } from "@spt/models/eft/common/IPmcData";
import { ISuit } from "@spt/models/eft/common/tables/ITrader"; import { ISuit } from "@spt/models/eft/common/tables/ITrader";
import { ClothingItem, IBuyClothingRequestData } from "@spt/models/eft/customization/IBuyClothingRequestData"; import {
IBuyClothingRequestData,
IPaymentItemForClothing,
} from "@spt/models/eft/customization/IBuyClothingRequestData";
import { IWearClothingRequestData } from "@spt/models/eft/customization/IWearClothingRequestData"; import { IWearClothingRequestData } from "@spt/models/eft/customization/IWearClothingRequestData";
import { IItemEventRouterResponse } from "@spt/models/eft/itemEvent/IItemEventRouterResponse"; import { IItemEventRouterResponse } from "@spt/models/eft/itemEvent/IItemEventRouterResponse";
import { ILogger } from "@spt/models/spt/utils/ILogger"; import { ILogger } from "@spt/models/spt/utils/ILogger";
@ -148,17 +151,17 @@ export class CustomizationController {
* Update output object and player profile with purchase details * Update output object and player profile with purchase details
* @param sessionId Session id * @param sessionId Session id
* @param pmcData Player profile * @param pmcData Player profile
* @param clothingItems Clothing purchased * @param itemsToPayForClothingWith Clothing purchased
* @param output Client response * @param output Client response
*/ */
protected payForClothingItems( protected payForClothingItems(
sessionId: string, sessionId: string,
pmcData: IPmcData, pmcData: IPmcData,
clothingItems: ClothingItem[], itemsToPayForClothingWith: IPaymentItemForClothing[],
output: IItemEventRouterResponse, output: IItemEventRouterResponse,
): void { ): void {
for (const sellItem of clothingItems) { for (const inventoryItemToProcess of itemsToPayForClothingWith) {
this.payForClothingItem(sessionId, pmcData, sellItem, output); this.payForClothingItem(sessionId, pmcData, inventoryItemToProcess, output);
} }
} }
@ -166,47 +169,59 @@ export class CustomizationController {
* Update output object and player profile with purchase details for single piece of clothing * Update output object and player profile with purchase details for single piece of clothing
* @param sessionId Session id * @param sessionId Session id
* @param pmcData Player profile * @param pmcData Player profile
* @param clothingItem Clothing item purchased * @param paymentItemDetails Payment details
* @param output Client response * @param output Client response
*/ */
protected payForClothingItem( protected payForClothingItem(
sessionId: string, sessionId: string,
pmcData: IPmcData, pmcData: IPmcData,
clothingItem: ClothingItem, paymentItemDetails: IPaymentItemForClothing,
output: IItemEventRouterResponse, output: IItemEventRouterResponse,
): void { ): void {
const relatedItem = pmcData.Inventory.items.find((x) => x._id === clothingItem.id); const inventoryItem = pmcData.Inventory.items.find((x) => x._id === paymentItemDetails.id);
if (!relatedItem) { if (!inventoryItem) {
this.logger.error( this.logger.error(
this.localisationService.getText( this.localisationService.getText(
"customisation-unable_to_find_clothing_item_in_inventory", "customisation-unable_to_find_clothing_item_in_inventory",
clothingItem.id, paymentItemDetails.id,
), ),
); );
return; return;
} }
if (clothingItem.del === true) { if (paymentItemDetails.del) {
output.profileChanges[sessionId].items.del.push(relatedItem); output.profileChanges[sessionId].items.del.push(inventoryItem);
pmcData.Inventory.items.splice(pmcData.Inventory.items.indexOf(relatedItem), 1); pmcData.Inventory.items.splice(pmcData.Inventory.items.indexOf(inventoryItem), 1);
} }
if (!relatedItem.upd || !relatedItem.upd.StackObjectsCount) { // No upd, add a default
throw new Error( inventoryItem.upd ||= {
this.localisationService.getText("customisation-suit_lacks_upd_or_stack_property", relatedItem._tpl), StackObjectsCount: 1,
); };
// Nullguard
if (typeof inventoryItem.upd.StackObjectsCount === "undefined") {
inventoryItem.upd.StackObjectsCount = 1;
} }
if (relatedItem.upd.StackObjectsCount > clothingItem.count) { // Needed count to buy is same as current stack
relatedItem.upd.StackObjectsCount -= clothingItem.count; if (inventoryItem.upd.StackObjectsCount === paymentItemDetails.count) {
output.profileChanges[sessionId].items.del.push(inventoryItem);
pmcData.Inventory.items.splice(pmcData.Inventory.items.indexOf(inventoryItem), 1);
return;
}
if (inventoryItem.upd.StackObjectsCount > paymentItemDetails.count) {
inventoryItem.upd.StackObjectsCount -= paymentItemDetails.count;
output.profileChanges[sessionId].items.change.push({ output.profileChanges[sessionId].items.change.push({
_id: relatedItem._id, _id: inventoryItem._id,
_tpl: relatedItem._tpl, _tpl: inventoryItem._tpl,
parentId: relatedItem.parentId, parentId: inventoryItem.parentId,
slotId: relatedItem.slotId, slotId: inventoryItem.slotId,
location: relatedItem.location, location: inventoryItem.location,
upd: { StackObjectsCount: relatedItem.upd.StackObjectsCount }, upd: { StackObjectsCount: inventoryItem.upd.StackObjectsCount },
}); });
} }
} }

View File

@ -1,10 +1,10 @@
export interface IBuyClothingRequestData { export interface IBuyClothingRequestData {
Action: "CustomizationBuy"; Action: "CustomizationBuy";
offer: string; offer: string;
items: ClothingItem[]; items: IPaymentItemForClothing[];
} }
export interface ClothingItem { export interface IPaymentItemForClothing {
del: boolean; del: boolean;
id: string; id: string;
count: number; count: number;