From a1c281572846740a031ab6baf93705a37054283f Mon Sep 17 00:00:00 2001 From: holmityd Date: Wed, 30 Aug 2023 21:15:46 +0400 Subject: [PATCH] missionInventoryUpdate - private functions outside --- src/services/inventoryService.ts | 86 +++++++++++----------- src/types/inventoryTypes/SuitTypes.ts | 8 +- src/types/inventoryTypes/inventoryTypes.ts | 2 +- src/types/inventoryTypes/weaponTypes.ts | 2 + 4 files changed, 53 insertions(+), 45 deletions(-) diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 05c60f2f..d7581ab9 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -5,7 +5,7 @@ import { Types } from "mongoose"; import { ISuitResponse } from "@/src/types/inventoryTypes/SuitTypes"; import { SlotType } from "@/src/types/purchaseTypes"; import { IWeaponResponse } from "@/src/types/inventoryTypes/weaponTypes"; -import { FlavourItem } from "@/src/types/inventoryTypes/inventoryTypes"; +import { FlavourItem, IInventoryDatabaseDocument } from "@/src/types/inventoryTypes/inventoryTypes"; import { MissionInventoryUpdate, MissionInventoryUpdateCard, @@ -112,54 +112,56 @@ export const addCustomization = async (customizatonName: string, accountId: stri return changedInventory.FlavourItems[flavourItemIndex].toJSON(); //mongoose bug forces as FlavourItem }; +const addGearExpByCategory = ( + inventory: IInventoryDatabaseDocument, + gearArray: MissionInventoryUpdateGear[] | undefined, + categoryName: "Pistols" | "LongGuns" | "Melee" | "Suits" +) => { + const category = inventory[categoryName]; + + gearArray?.forEach(({ ItemId, XP }) => { + const itemIndex = category.findIndex(i => i._id?.equals(ItemId.$oid)); + const item = category[itemIndex]; + + if (itemIndex !== -1 && item.XP != undefined) { + item.XP += XP; + inventory.markModified(`${categoryName}.${itemIndex}.XP`); + } + }); +}; + +const addItemsByCategory = ( + inventory: IInventoryDatabaseDocument, + itemsArray: (MissionInventoryUpdateItem | MissionInventoryUpdateCard)[] | undefined, + categoryName: "RawUpgrades" | "MiscItems" +) => { + const category = inventory[categoryName]; + + itemsArray?.forEach(({ ItemCount, ItemType }) => { + const itemIndex = category.findIndex(i => i.ItemType === ItemType); + + if (itemIndex !== -1) { + category[itemIndex].ItemCount += ItemCount; + inventory.markModified(`${categoryName}.${itemIndex}.ItemCount`); + } else { + category.push({ ItemCount, ItemType }); + } + }); +}; + export const missionInventoryUpdate = async (data: MissionInventoryUpdate, accountId: string): Promise => { const { RawUpgrades, MiscItems, Suits, Pistols, LongGuns, Melee, RegularCredits } = data; const inventory = await getInventory(accountId); // TODO - multipliers logic - const addGearExpByCategory = ( - gearArray: MissionInventoryUpdateGear[] | undefined, - categoryName: "Pistols" | "LongGuns" | "Melee" | "Suits" - ) => { - const category = inventory[categoryName]; - - gearArray?.forEach(({ ItemId, XP }) => { - const itemIndex = category.findIndex(i => i._id?.equals(ItemId.$oid)); - const item = category[itemIndex]; - - if (itemIndex !== -1 && item.XP != undefined) { - item.XP += XP; - inventory.markModified(`${categoryName}.${itemIndex}.XP`); - } - }); - }; - - const addItemsByCategory = ( - itemsArray: (MissionInventoryUpdateItem | MissionInventoryUpdateCard)[] | undefined, - categoryName: "RawUpgrades" | "MiscItems" - ) => { - const category = inventory[categoryName]; - - itemsArray?.forEach(({ ItemCount, ItemType }) => { - const itemIndex = category.findIndex(i => i.ItemType === ItemType); - - if (itemIndex !== -1) { - category[itemIndex].ItemCount += ItemCount; - inventory.markModified(`${categoryName}.${itemIndex}.ItemCount`); - } else { - category.push({ ItemCount, ItemType }); - } - }); - }; - inventory.RegularCredits += RegularCredits || 0; - addGearExpByCategory(Pistols, "Pistols"); - addGearExpByCategory(LongGuns, "LongGuns"); - addGearExpByCategory(Melee, "Melee"); - addGearExpByCategory(Suits, "Suits"); - addItemsByCategory(RawUpgrades, "RawUpgrades"); // TODO - check mods fusion level - addItemsByCategory(MiscItems, "MiscItems"); + addGearExpByCategory(inventory, Pistols, "Pistols"); + addGearExpByCategory(inventory, LongGuns, "LongGuns"); + addGearExpByCategory(inventory, Melee, "Melee"); + addGearExpByCategory(inventory, Suits, "Suits"); + addItemsByCategory(inventory, RawUpgrades, "RawUpgrades"); // TODO - check mods fusion level + addItemsByCategory(inventory, MiscItems, "MiscItems"); // TODO - save ChallengeProgress (idk where to save) diff --git a/src/types/inventoryTypes/SuitTypes.ts b/src/types/inventoryTypes/SuitTypes.ts index 187d5ab1..7dc8ca5f 100644 --- a/src/types/inventoryTypes/SuitTypes.ts +++ b/src/types/inventoryTypes/SuitTypes.ts @@ -1,8 +1,11 @@ import { Oid } from "@/src/types/commonTypes"; import { AbilityOverride, Color, Polarity } from "@/src/types/inventoryTypes/commonInventoryTypes"; -import { Document } from "mongoose"; +import { Document, Types } from "mongoose"; -export interface ISuitDocument extends ISuitResponse, Document {} +// export interface ISuitDocument extends ISuitResponse, Document {} +export interface ISuitDocument extends Document, ISuitResponse { + _id: Types.ObjectId; +} export interface ISuitResponse extends ISuitDatabase { ItemId: Oid; @@ -20,6 +23,7 @@ export interface ISuitDatabase { ModSlotPurchases?: number; FocusLens?: string; UnlockLevel?: number; + _id: Types.ObjectId; } export interface SuitConfig { diff --git a/src/types/inventoryTypes/inventoryTypes.ts b/src/types/inventoryTypes/inventoryTypes.ts index 0e095a88..b175dc64 100644 --- a/src/types/inventoryTypes/inventoryTypes.ts +++ b/src/types/inventoryTypes/inventoryTypes.ts @@ -929,7 +929,7 @@ export interface Progress { export interface RawUpgrade { ItemCount: number; - LastAdded: Oid; + LastAdded?: Oid; ItemType: string; } diff --git a/src/types/inventoryTypes/weaponTypes.ts b/src/types/inventoryTypes/weaponTypes.ts index 3c561e2d..7fe6bf40 100644 --- a/src/types/inventoryTypes/weaponTypes.ts +++ b/src/types/inventoryTypes/weaponTypes.ts @@ -1,5 +1,6 @@ import { Oid } from "@/src/types/commonTypes"; import { Color, Polarity } from "@/src/types/inventoryTypes/commonInventoryTypes"; +import { Types } from "mongoose"; export interface IWeaponResponse extends IWeaponDatabase { ItemId: Oid; @@ -20,6 +21,7 @@ export interface IWeaponDatabase { ItemName?: string; ModularParts?: string[]; UnlockLevel?: number; + _id?: Types.ObjectId; } export interface WeaponConfig {