From 989f8eda4b57ca65eef7398e63beed51872d403d Mon Sep 17 00:00:00 2001 From: Ordis <134585663+OrdisPrime@users.noreply.github.com> Date: Fri, 24 Jan 2025 16:03:44 +0100 Subject: [PATCH] adjust mission update controller --- .../api/missionInventoryUpdateController.ts | 20 ++++++++----------- src/services/missionInventoryUpdateService.ts | 9 ++------- src/utils/ts-utils.ts | 5 +++++ 3 files changed, 15 insertions(+), 19 deletions(-) create mode 100644 src/utils/ts-utils.ts diff --git a/src/controllers/api/missionInventoryUpdateController.ts b/src/controllers/api/missionInventoryUpdateController.ts index 9ae22eb9..1f9b6bbc 100644 --- a/src/controllers/api/missionInventoryUpdateController.ts +++ b/src/controllers/api/missionInventoryUpdateController.ts @@ -55,29 +55,25 @@ export const missionInventoryUpdateController: RequestHandler = async (req, res) const missionReport = getJSONfromString((req.body as string).toString()); - if (missionReport.MissionStatus !== "GS_SUCCESS") { - logger.debug(`Mission failed: ${missionReport.RewardInfo?.node}`); - //todo: return expected response for failed mission - res.json([]); - return; - //duvirisadjob does not provide missionStatus - } - const inventory = await getInventory(accountId); const missionRewardsResults = await addMissionRewards(inventory, missionReport); - if (!missionRewardsResults) { - logger.error("Failed to add mission rewards"); - res.status(500).json({ error: "Failed to add mission rewards" }); + if (missionReport.MissionStatus !== "GS_SUCCESS") { + const InventoryJson = JSON.stringify((await inventory.save()).toJSON()); + + res.json({ InventoryJson, MissionRewards: [] }); return; } + if (!missionRewardsResults) { + throw new Error("Failed to add mission rewards, should not happen"); + } + const { MissionRewards, inventoryChanges, missionCompletionCredits } = missionRewardsResults; const inventoryUpdates = addMissionInventoryUpdates(inventory, missionReport); - //todo ? can go after not awaiting //creditBonus is not correct for mirage mission 3 const credits = calculateFinalCredits(inventory, { missionCompletionCredits, diff --git a/src/services/missionInventoryUpdateService.ts b/src/services/missionInventoryUpdateService.ts index 1d6c5702..dc78d55a 100644 --- a/src/services/missionInventoryUpdateService.ts +++ b/src/services/missionInventoryUpdateService.ts @@ -21,6 +21,7 @@ import { HydratedDocument } from "mongoose"; import { IInventoryChanges } from "@/src/types/purchaseTypes"; import { getLevelKeyRewards, getNode } from "@/src/services/itemDataService"; import { InventoryDocumentProps, TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel"; +import { getEntriesUnsafe } from "@/src/utils/ts-utils"; const getRotations = (rotationCount: number): number[] => { if (rotationCount === 0) return [0]; @@ -64,12 +65,6 @@ export const fusionBundles: Record = { "/Lotus/Upgrades/Mods/FusionBundles/RareFusionBundle": 80 }; -type Entries = (K extends unknown ? [K, T[K]] : never)[]; - -function getEntriesUnsafe(object: T): Entries { - return Object.entries(object) as Entries; -} - //type TMissionInventoryUpdateKeys = keyof IMissionInventoryUpdateRequest; //const ignoredInventoryUpdateKeys = ["FpsAvg", "FpsMax", "FpsMin", "FpsSamples"] satisfies TMissionInventoryUpdateKeys[]; // for keys with no meaning for this server //type TignoredInventoryUpdateKeys = (typeof ignoredInventoryUpdateKeys)[number]; @@ -185,7 +180,7 @@ export const addMissionRewards = async ( { RewardInfo: rewardInfo, LevelKeyName: levelKeyName, Missions: missions }: IMissionInventoryUpdateRequest ) => { if (!rewardInfo) { - logger.error("no reward info provided"); + logger.warn("no reward info provided"); return; } diff --git a/src/utils/ts-utils.ts b/src/utils/ts-utils.ts new file mode 100644 index 00000000..4f456c12 --- /dev/null +++ b/src/utils/ts-utils.ts @@ -0,0 +1,5 @@ +type Entries = (K extends unknown ? [K, T[K]] : never)[]; + +export function getEntriesUnsafe(object: T): Entries { + return Object.entries(object) as Entries; +}