From 1d091e3c4c8157081903afcb0648e23430706507 Mon Sep 17 00:00:00 2001 From: Sainan Date: Mon, 17 Mar 2025 05:10:28 -0700 Subject: [PATCH] chore: remove consumables, recipes, etc. from array when their ItemCount becomes 0 (#1216) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1216 --- src/services/inventoryService.ts | 79 +++++++--------------- src/types/inventoryTypes/inventoryTypes.ts | 19 ++---- 2 files changed, 30 insertions(+), 68 deletions(-) diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 9c087f89..2886bf00 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -8,7 +8,6 @@ import { HydratedDocument, Types } from "mongoose"; import { SlotNames, IInventoryChanges, IBinChanges, slotNames } from "@/src/types/purchaseTypes"; import { IChallengeProgress, - IConsumable, IFlavourItem, IMiscItem, IMission, @@ -378,7 +377,7 @@ export const addItem = async ( { ItemType: typeName, ItemCount: quantity - } satisfies IConsumable + } satisfies ITypeCount ]; addConsumables(inventory, consumablesChanges); return { @@ -1102,74 +1101,42 @@ export const addMiscItems = (inventory: TInventoryDatabaseDocument, itemsArray: }); }; -export const addShipDecorations = (inventory: TInventoryDatabaseDocument, itemsArray: IConsumable[]): void => { - const { ShipDecorations } = inventory; +const applyArrayChanges = (arr: ITypeCount[], changes: ITypeCount[]): void => { + for (const change of changes) { + if (change.ItemCount != 0) { + let itemIndex = arr.findIndex(x => x.ItemType === change.ItemType); + if (itemIndex == -1) { + itemIndex = arr.push({ ItemType: change.ItemType, ItemCount: 0 }) - 1; + } - itemsArray.forEach(({ ItemCount, ItemType }) => { - const itemIndex = ShipDecorations.findIndex(miscItem => miscItem.ItemType === ItemType); - - if (itemIndex !== -1) { - ShipDecorations[itemIndex].ItemCount += ItemCount; - } else { - ShipDecorations.push({ ItemCount, ItemType }); + arr[itemIndex].ItemCount += change.ItemCount; + if (arr[itemIndex].ItemCount == 0) { + arr.splice(itemIndex, 1); + } else if (arr[itemIndex].ItemCount <= 0) { + logger.warn(`account now owns a negative amount of ${change.ItemType}`); + } } - }); + } }; -export const addConsumables = (inventory: TInventoryDatabaseDocument, itemsArray: IConsumable[]): void => { - const { Consumables } = inventory; +export const addShipDecorations = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => { + applyArrayChanges(inventory.ShipDecorations, itemsArray); +}; - itemsArray.forEach(({ ItemCount, ItemType }) => { - const itemIndex = Consumables.findIndex(i => i.ItemType === ItemType); - - if (itemIndex !== -1) { - Consumables[itemIndex].ItemCount += ItemCount; - } else { - Consumables.push({ ItemCount, ItemType }); - } - }); +export const addConsumables = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => { + applyArrayChanges(inventory.Consumables, itemsArray); }; export const addCrewShipRawSalvage = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => { - const { CrewShipRawSalvage } = inventory; - - itemsArray.forEach(({ ItemCount, ItemType }) => { - const itemIndex = CrewShipRawSalvage.findIndex(i => i.ItemType === ItemType); - - if (itemIndex !== -1) { - CrewShipRawSalvage[itemIndex].ItemCount += ItemCount; - } else { - CrewShipRawSalvage.push({ ItemCount, ItemType }); - } - }); + applyArrayChanges(inventory.CrewShipRawSalvage, itemsArray); }; export const addCrewShipAmmo = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => { - const { CrewShipAmmo } = inventory; - - itemsArray.forEach(({ ItemCount, ItemType }) => { - const itemIndex = CrewShipAmmo.findIndex(i => i.ItemType === ItemType); - - if (itemIndex !== -1) { - CrewShipAmmo[itemIndex].ItemCount += ItemCount; - } else { - CrewShipAmmo.push({ ItemCount, ItemType }); - } - }); + applyArrayChanges(inventory.CrewShipAmmo, itemsArray); }; export const addRecipes = (inventory: TInventoryDatabaseDocument, itemsArray: ITypeCount[]): void => { - const { Recipes } = inventory; - - itemsArray.forEach(({ ItemCount, ItemType }) => { - const itemIndex = Recipes.findIndex(i => i.ItemType === ItemType); - - if (itemIndex !== -1) { - Recipes[itemIndex].ItemCount += ItemCount; - } else { - Recipes.push({ ItemCount, ItemType }); - } - }); + applyArrayChanges(inventory.Recipes, itemsArray); }; export const addMods = (inventory: TInventoryDatabaseDocument, itemsArray: IRawUpgrade[]): void => { diff --git a/src/types/inventoryTypes/inventoryTypes.ts b/src/types/inventoryTypes/inventoryTypes.ts index 4a06de67..869cd4e7 100644 --- a/src/types/inventoryTypes/inventoryTypes.ts +++ b/src/types/inventoryTypes/inventoryTypes.ts @@ -236,8 +236,8 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu FusionTreasures: IFusionTreasure[]; WebFlags: IWebFlags; CompletedAlerts: string[]; - Consumables: IConsumable[]; - LevelKeys: IConsumable[]; + Consumables: ITypeCount[]; + LevelKeys: ITypeCount[]; TauntHistory?: ITaunt[]; StoryModeChoice: string; PeriodicMissionCompletions: IPeriodicMissionCompletionDatabase[]; @@ -265,7 +265,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu Drones: IDroneClient[]; StepSequencers: IStepSequencer[]; ActiveAvatarImageType: string; - ShipDecorations: IConsumable[]; + ShipDecorations: ITypeCount[]; DiscoveredMarkers: IDiscoveredMarker[]; CompletedJobs: ICompletedJob[]; FocusAbility?: string; @@ -293,7 +293,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu Settings: ISettings; PersonalTechProjects: IPersonalTechProject[]; PlayerSkills: IPlayerSkills; - CrewShipAmmo: IConsumable[]; + CrewShipAmmo: ITypeCount[]; CrewShipSalvagedWeaponSkins: IUpgradeClient[]; CrewShipWeapons: ICrewShipWeaponClient[]; CrewShipSalvagedWeapons: IEquipmentClient[]; @@ -303,7 +303,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu SubscribedToEmailsPersonalized: number; InfestedFoundry?: IInfestedFoundryClient; BlessingCooldown?: IMongoDate; - CrewShipRawSalvage: IConsumable[]; + CrewShipRawSalvage: ITypeCount[]; CrewMembers: ICrewMember[]; LotusCustomization: ILotusCustomization; UseAdultOperatorLoadout?: boolean; @@ -417,11 +417,6 @@ export interface ICompletedJob { StageCompletions: number[]; } -export interface IConsumable { - ItemCount: number; - ItemType: string; -} - export interface ICrewMember { ItemType: string; NemesisFingerprint: number; @@ -891,7 +886,7 @@ export enum GettingSlotOrderInfo { } export interface IGiving { - RawUpgrades: IConsumable[]; + RawUpgrades: ITypeCount[]; _SlotOrderInfo: GivingSlotOrderInfo[]; } @@ -924,7 +919,7 @@ export interface IPersonalTechProject { State: number; ReqCredits: number; ItemType: string; - ReqItems: IConsumable[]; + ReqItems: ITypeCount[]; CompletionDate?: IMongoDate; ItemId: IOid; ProductCategory?: string;