From 04d39ed9731af998ef37dfb0c99c058b2aa74f52 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Mon, 31 Mar 2025 04:15:32 -0700 Subject: [PATCH] chore: use SubdocumentArray.id in some more places (#1400) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1400 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- src/controllers/api/evolveWeaponController.ts | 4 ++-- src/controllers/api/focusController.ts | 20 +++++++++---------- .../api/infestedFoundryController.ts | 4 ++-- src/controllers/api/nameWeaponController.ts | 4 +--- .../api/setWeaponSkillTreeController.ts | 6 ++---- .../popArchonCrystalUpgradeController.ts | 2 +- .../pushArchonCrystalUpgradeController.ts | 2 +- src/services/saveLoadoutService.ts | 4 +--- src/services/shipCustomizationsService.ts | 12 +++++------ 9 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/controllers/api/evolveWeaponController.ts b/src/controllers/api/evolveWeaponController.ts index 3b2550bb..395b3340 100644 --- a/src/controllers/api/evolveWeaponController.ts +++ b/src/controllers/api/evolveWeaponController.ts @@ -17,7 +17,7 @@ export const evolveWeaponController: RequestHandler = async (req, res) => { recipe.ingredients.map(x => ({ ItemType: x.ItemType, ItemCount: x.ItemCount * -1 })) ); - const item = inventory[payload.Category].find(item => item._id.toString() == (req.query.ItemId as string))!; + const item = inventory[payload.Category].id(req.query.ItemId as string)!; item.Features ??= 0; item.Features |= EquipmentFeatures.INCARNON_GENESIS; @@ -39,7 +39,7 @@ export const evolveWeaponController: RequestHandler = async (req, res) => { } ]); - const item = inventory[payload.Category].find(item => item._id.toString() == (req.query.ItemId as string))!; + const item = inventory[payload.Category].id(req.query.ItemId as string)!; item.Features! &= ~EquipmentFeatures.INCARNON_GENESIS; } else { throw new Error(`unexpected evolve weapon action: ${payload.Action}`); diff --git a/src/controllers/api/focusController.ts b/src/controllers/api/focusController.ts index a6c1c59c..0d2b3f65 100644 --- a/src/controllers/api/focusController.ts +++ b/src/controllers/api/focusController.ts @@ -18,17 +18,15 @@ export const focusController: RequestHandler = async (req, res) => { case FocusOperation.InstallLens: { const request = JSON.parse(String(req.body)) as ILensInstallRequest; const inventory = await getInventory(accountId); - for (const item of inventory[request.Category]) { - if (item._id.toString() == request.WeaponId) { - item.FocusLens = request.LensType; - addMiscItems(inventory, [ - { - ItemType: request.LensType, - ItemCount: -1 - } satisfies IMiscItem - ]); - break; - } + const item = inventory[request.Category].id(request.WeaponId); + if (item) { + item.FocusLens = request.LensType; + addMiscItems(inventory, [ + { + ItemType: request.LensType, + ItemCount: -1 + } satisfies IMiscItem + ]); } await inventory.save(); res.json({ diff --git a/src/controllers/api/infestedFoundryController.ts b/src/controllers/api/infestedFoundryController.ts index 0e3b4ed7..d63c2203 100644 --- a/src/controllers/api/infestedFoundryController.ts +++ b/src/controllers/api/infestedFoundryController.ts @@ -28,7 +28,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { // shard installation const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); - const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; + const suit = inventory.Suits.id(request.SuitId.$oid)!; if (!suit.ArchonCrystalUpgrades || suit.ArchonCrystalUpgrades.length != 5) { suit.ArchonCrystalUpgrades = [{}, {}, {}, {}, {}]; } @@ -56,7 +56,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { // shard removal const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); - const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; + const suit = inventory.Suits.id(request.SuitId.$oid)!; const miscItemChanges: IMiscItem[] = []; if (suit.ArchonCrystalUpgrades![request.Slot].Color) { diff --git a/src/controllers/api/nameWeaponController.ts b/src/controllers/api/nameWeaponController.ts index 843feeb9..f4fc88a7 100644 --- a/src/controllers/api/nameWeaponController.ts +++ b/src/controllers/api/nameWeaponController.ts @@ -12,9 +12,7 @@ export const nameWeaponController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); const body = getJSONfromString(String(req.body)); - const item = inventory[req.query.Category as string as TEquipmentKey].find( - item => item._id.toString() == (req.query.ItemId as string) - )!; + const item = inventory[req.query.Category as string as TEquipmentKey].id(req.query.ItemId as string)!; if (body.ItemName != "") { item.ItemName = body.ItemName; } else { diff --git a/src/controllers/api/setWeaponSkillTreeController.ts b/src/controllers/api/setWeaponSkillTreeController.ts index 98fe7652..31323adc 100644 --- a/src/controllers/api/setWeaponSkillTreeController.ts +++ b/src/controllers/api/setWeaponSkillTreeController.ts @@ -6,12 +6,10 @@ import { WeaponTypeInternal } from "@/src/services/itemDataService"; export const setWeaponSkillTreeController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const inventory = await getInventory(accountId); + const inventory = await getInventory(accountId, req.query.Category as string); const payload = getJSONfromString(String(req.body)); - const item = inventory[req.query.Category as WeaponTypeInternal].find( - item => item._id.toString() == (req.query.ItemId as string) - )!; + const item = inventory[req.query.Category as WeaponTypeInternal].id(req.query.ItemId as string)!; item.SkillTree = payload.SkillTree; await inventory.save(); diff --git a/src/controllers/custom/popArchonCrystalUpgradeController.ts b/src/controllers/custom/popArchonCrystalUpgradeController.ts index c9c84b85..34e87ec6 100644 --- a/src/controllers/custom/popArchonCrystalUpgradeController.ts +++ b/src/controllers/custom/popArchonCrystalUpgradeController.ts @@ -5,7 +5,7 @@ import { getInventory } from "@/src/services/inventoryService"; export const popArchonCrystalUpgradeController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const suit = inventory.Suits.find(suit => suit._id.toString() == (req.query.oid as string)); + const suit = inventory.Suits.id(req.query.oid as string); if (suit && suit.ArchonCrystalUpgrades) { suit.ArchonCrystalUpgrades = suit.ArchonCrystalUpgrades.filter( x => x.UpgradeType != (req.query.type as string) diff --git a/src/controllers/custom/pushArchonCrystalUpgradeController.ts b/src/controllers/custom/pushArchonCrystalUpgradeController.ts index 093b0678..3a9286ee 100644 --- a/src/controllers/custom/pushArchonCrystalUpgradeController.ts +++ b/src/controllers/custom/pushArchonCrystalUpgradeController.ts @@ -5,7 +5,7 @@ import { getInventory } from "@/src/services/inventoryService"; export const pushArchonCrystalUpgradeController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const suit = inventory.Suits.find(suit => suit._id.toString() == (req.query.oid as string)); + const suit = inventory.Suits.id(req.query.oid as string); if (suit) { suit.ArchonCrystalUpgrades ??= []; const count = (req.query.count as number | undefined) ?? 1; diff --git a/src/services/saveLoadoutService.ts b/src/services/saveLoadoutService.ts index 038f0d23..2f8711c6 100644 --- a/src/services/saveLoadoutService.ts +++ b/src/services/saveLoadoutService.ts @@ -84,9 +84,7 @@ export const handleInventoryItemConfigChange = async ( continue; } - const oldLoadoutConfig = loadout[loadoutSlot].find( - loadout => loadout._id.toString() === loadoutId - ); + const oldLoadoutConfig = loadout[loadoutSlot].id(loadoutId); const { ItemId, ...loadoutConfigItemIdRemoved } = loadoutConfig; const loadoutConfigDatabase: ILoadoutConfigDatabase = { diff --git a/src/services/shipCustomizationsService.ts b/src/services/shipCustomizationsService.ts index bc7ca5fb..20c3d4ca 100644 --- a/src/services/shipCustomizationsService.ts +++ b/src/services/shipCustomizationsService.ts @@ -61,19 +61,17 @@ export const handleSetShipDecorations = async ( if (placedDecoration.MoveId) { //moved within the same room if (placedDecoration.OldRoom === placedDecoration.Room) { - const existingDecorationIndex = roomToPlaceIn.PlacedDecos.findIndex( - deco => deco._id.toString() === placedDecoration.MoveId - ); + const existingDecoration = roomToPlaceIn.PlacedDecos.id(placedDecoration.MoveId); - if (existingDecorationIndex === -1) { + if (!existingDecoration) { throw new Error("decoration to be moved not found"); } - roomToPlaceIn.PlacedDecos[existingDecorationIndex].Pos = placedDecoration.Pos; - roomToPlaceIn.PlacedDecos[existingDecorationIndex].Rot = placedDecoration.Rot; + existingDecoration.Pos = placedDecoration.Pos; + existingDecoration.Rot = placedDecoration.Rot; if (placedDecoration.Scale) { - roomToPlaceIn.PlacedDecos[existingDecorationIndex].Scale = placedDecoration.Scale; + existingDecoration.Scale = placedDecoration.Scale; } await personalRooms.save();