From 39be09581883d0ff795307e9a38c1f0f64858552 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Tue, 8 Apr 2025 03:06:36 -0700 Subject: [PATCH] chore: handle season challenge completion in missionInventoryUpdate (#1511) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1511 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- .../api/updateChallengeProgressController.ts | 44 ++++---------- src/services/inventoryService.ts | 58 ++++++++++++------- src/services/missionInventoryUpdateService.ts | 2 +- 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/src/controllers/api/updateChallengeProgressController.ts b/src/controllers/api/updateChallengeProgressController.ts index 3e056538..93135634 100644 --- a/src/controllers/api/updateChallengeProgressController.ts +++ b/src/controllers/api/updateChallengeProgressController.ts @@ -1,10 +1,8 @@ import { RequestHandler } from "express"; import { getJSONfromString } from "@/src/helpers/stringHelpers"; import { getAccountIdForRequest } from "@/src/services/loginService"; -import { addChallenges, addSeasonalChallengeHistory, getInventory } from "@/src/services/inventoryService"; +import { addChallenges, getInventory } from "@/src/services/inventoryService"; import { IChallengeProgress, ISeasonChallenge } from "@/src/types/inventoryTypes/inventoryTypes"; -import { ExportNightwave } from "warframe-public-export-plus"; -import { logger } from "@/src/utils/logger"; import { IAffiliationMods } from "@/src/types/purchaseTypes"; export const updateChallengeProgressController: RequestHandler = async (req, res) => { @@ -12,41 +10,19 @@ export const updateChallengeProgressController: RequestHandler = async (req, res const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId, "ChallengeProgress SeasonChallengeHistory Affiliations"); + let affiliationMods: IAffiliationMods[] = []; if (challenges.ChallengeProgress) { - addChallenges(inventory, challenges.ChallengeProgress); + affiliationMods = addChallenges(inventory, challenges.ChallengeProgress, challenges.SeasonChallengeCompletions); } if (challenges.SeasonChallengeHistory) { - addSeasonalChallengeHistory(inventory, challenges.SeasonChallengeHistory); - } - const affiliationMods: IAffiliationMods[] = []; - if (challenges.ChallengeProgress && challenges.SeasonChallengeCompletions) { - for (const challenge of challenges.SeasonChallengeCompletions) { - // Ignore challenges that weren't completed just now - if (!challenges.ChallengeProgress.find(x => challenge.challenge.indexOf(x.Name) != -1)) { - continue; + challenges.SeasonChallengeHistory.forEach(({ challenge, id }) => { + const itemIndex = inventory.SeasonChallengeHistory.findIndex(i => i.challenge === challenge); + if (itemIndex !== -1) { + inventory.SeasonChallengeHistory[itemIndex].id = id; + } else { + inventory.SeasonChallengeHistory.push({ challenge, id }); } - - const meta = ExportNightwave.challenges[challenge.challenge]; - logger.debug("Completed challenge", meta); - - let affiliation = inventory.Affiliations.find(x => x.Tag == ExportNightwave.affiliationTag); - if (!affiliation) { - affiliation = - inventory.Affiliations[ - inventory.Affiliations.push({ - Tag: ExportNightwave.affiliationTag, - Standing: 0 - }) - 1 - ]; - } - affiliation.Standing += meta.standing; - - if (affiliationMods.length == 0) { - affiliationMods.push({ Tag: ExportNightwave.affiliationTag }); - } - affiliationMods[0].Standing ??= 0; - affiliationMods[0].Standing += meta.standing; - } + }); } await inventory.save(); diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 92281629..67ca9f9f 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -1,7 +1,7 @@ import { Inventory, TInventoryDatabaseDocument } from "@/src/models/inventoryModels/inventoryModel"; import { config } from "@/src/services/configService"; import { Types } from "mongoose"; -import { SlotNames, IInventoryChanges, IBinChanges, slotNames } from "@/src/types/purchaseTypes"; +import { SlotNames, IInventoryChanges, IBinChanges, slotNames, IAffiliationMods } from "@/src/types/purchaseTypes"; import { IChallengeProgress, IFlavourItem, @@ -45,6 +45,7 @@ import { ExportGear, ExportKeys, ExportMisc, + ExportNightwave, ExportRailjackWeapons, ExportRecipes, ExportResources, @@ -1302,35 +1303,52 @@ export const addFocusXpIncreases = (inventory: TInventoryDatabaseDocument, focus inventory.DailyFocus -= focusXpPlus.reduce((a, b) => a + b, 0); }; -export const addSeasonalChallengeHistory = ( +export const addChallenges = ( inventory: TInventoryDatabaseDocument, - itemsArray: ISeasonChallenge[] -): void => { - const category = inventory.SeasonChallengeHistory; - - itemsArray.forEach(({ challenge, id }) => { - const itemIndex = category.findIndex(i => i.challenge === challenge); + ChallengeProgress: IChallengeProgress[], + SeasonChallengeCompletions: ISeasonChallenge[] | undefined +): IAffiliationMods[] => { + ChallengeProgress.forEach(({ Name, Progress }) => { + const itemIndex = inventory.ChallengeProgress.findIndex(i => i.Name === Name); if (itemIndex !== -1) { - category[itemIndex].id = id; + inventory.ChallengeProgress[itemIndex].Progress = Progress; } else { - category.push({ challenge, id }); + inventory.ChallengeProgress.push({ Name, Progress }); } }); -}; -export const addChallenges = (inventory: TInventoryDatabaseDocument, itemsArray: IChallengeProgress[]): void => { - const category = inventory.ChallengeProgress; + const affiliationMods: IAffiliationMods[] = []; + if (SeasonChallengeCompletions) { + for (const challenge of SeasonChallengeCompletions) { + // Ignore challenges that weren't completed just now + if (!ChallengeProgress.find(x => challenge.challenge.indexOf(x.Name) != -1)) { + continue; + } - itemsArray.forEach(({ Name, Progress }) => { - const itemIndex = category.findIndex(i => i.Name === Name); + const meta = ExportNightwave.challenges[challenge.challenge]; + logger.debug("Completed challenge", meta); - if (itemIndex !== -1) { - category[itemIndex].Progress = Progress; - } else { - category.push({ Name, Progress }); + let affiliation = inventory.Affiliations.find(x => x.Tag == ExportNightwave.affiliationTag); + if (!affiliation) { + affiliation = + inventory.Affiliations[ + inventory.Affiliations.push({ + Tag: ExportNightwave.affiliationTag, + Standing: 0 + }) - 1 + ]; + } + affiliation.Standing += meta.standing; + + if (affiliationMods.length == 0) { + affiliationMods.push({ Tag: ExportNightwave.affiliationTag }); + } + affiliationMods[0].Standing ??= 0; + affiliationMods[0].Standing += meta.standing; } - }); + } + return affiliationMods; }; export const addMissionComplete = (inventory: TInventoryDatabaseDocument, { Tag, Completes }: IMission): void => { diff --git a/src/services/missionInventoryUpdateService.ts b/src/services/missionInventoryUpdateService.ts index 92f219e2..b23adb3c 100644 --- a/src/services/missionInventoryUpdateService.ts +++ b/src/services/missionInventoryUpdateService.ts @@ -195,7 +195,7 @@ export const addMissionInventoryUpdates = async ( addRecipes(inventory, value); break; case "ChallengeProgress": - addChallenges(inventory, value); + addChallenges(inventory, value, inventoryUpdates.SeasonChallengeCompletions); break; case "FusionTreasures": addFusionTreasures(inventory, value);