From e3309f8d967a8f0cbd8811b27d93c08c48285e7f Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Tue, 29 Apr 2025 22:21:20 +0200 Subject: [PATCH] feat: handle client setting InfestationDate on equipment --- src/controllers/custom/addXpController.ts | 4 ++-- src/helpers/inventoryHelpers.ts | 4 ++++ src/services/inventoryService.ts | 18 +++++++++++------- src/services/missionInventoryUpdateService.ts | 5 ++--- 4 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/controllers/custom/addXpController.ts b/src/controllers/custom/addXpController.ts index 7cb284fe..0ca05102 100644 --- a/src/controllers/custom/addXpController.ts +++ b/src/controllers/custom/addXpController.ts @@ -1,4 +1,4 @@ -import { addGearExpByCategory, getInventory } from "@/src/services/inventoryService"; +import { applyClientEquipmentUpdates, getInventory } from "@/src/services/inventoryService"; import { getAccountIdForRequest } from "@/src/services/loginService"; import { IEquipmentClient } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { TEquipmentKey } from "@/src/types/inventoryTypes/inventoryTypes"; @@ -20,7 +20,7 @@ export const addXpController: RequestHandler = async (req, res) => { } } } - addGearExpByCategory(inventory, gear, category as TEquipmentKey); + applyClientEquipmentUpdates(inventory, gear, category as TEquipmentKey); } await inventory.save(); res.end(); diff --git a/src/helpers/inventoryHelpers.ts b/src/helpers/inventoryHelpers.ts index 79466fb3..4dd38c5a 100644 --- a/src/helpers/inventoryHelpers.ts +++ b/src/helpers/inventoryHelpers.ts @@ -10,6 +10,10 @@ export const toMongoDate = (date: Date): IMongoDate => { return { $date: { $numberLong: date.getTime().toString() } }; }; +export const fromMongoData = (date: IMongoDate): Date => { + return new Date(parseInt(date.$date.$numberLong)); +}; + export const kubrowWeights: Record = { COMMON: 6, UNCOMMON: 4, diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index da8b0848..f528ec43 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -69,6 +69,7 @@ import { import { createShip } from "./shipService"; import { catbrowDetails, + fromMongoData, kubrowDetails, kubrowFurPatternsWeights, kubrowWeights, @@ -1471,21 +1472,20 @@ export const addEmailItem = async ( return inventoryChanges; }; -//TODO: wrong id is not erroring -export const addGearExpByCategory = ( +export const applyClientEquipmentUpdates = ( inventory: TInventoryDatabaseDocument, gearArray: IEquipmentClient[], categoryName: TEquipmentKey ): void => { const category = inventory[categoryName]; - gearArray.forEach(({ ItemId, XP }) => { - if (!XP) { - return; + gearArray.forEach(({ ItemId, XP, InfestationDate }) => { + const item = category.id(ItemId.$oid); + if (!item) { + throw new Error(`No item with id ${ItemId.$oid} in ${categoryName}`); } - const item = category.id(ItemId.$oid); - if (item) { + if (XP) { item.XP ??= 0; item.XP += XP; @@ -1500,6 +1500,10 @@ export const addGearExpByCategory = ( }); } } + + if (InfestationDate) { + item.InfestationDate = fromMongoData(InfestationDate); + } }); }; diff --git a/src/services/missionInventoryUpdateService.ts b/src/services/missionInventoryUpdateService.ts index 66328a0d..39d74f66 100644 --- a/src/services/missionInventoryUpdateService.ts +++ b/src/services/missionInventoryUpdateService.ts @@ -21,7 +21,6 @@ import { addFocusXpIncreases, addFusionPoints, addFusionTreasures, - addGearExpByCategory, addItem, addLevelKeys, addLoreFragmentScans, @@ -32,6 +31,7 @@ import { addShipDecorations, addSkin, addStanding, + applyClientEquipmentUpdates, combineInventoryChanges, generateRewardSeed, getCalendarProgress, @@ -670,9 +670,8 @@ export const addMissionInventoryUpdates = async ( } break; default: - // Equipment XP updates if (equipmentKeys.includes(key as TEquipmentKey)) { - addGearExpByCategory(inventory, value as IEquipmentClient[], key as TEquipmentKey); + applyClientEquipmentUpdates(inventory, value as IEquipmentClient[], key as TEquipmentKey); } break; // if (