From 8b836020bf9344f45c7c64d156b37fcf8619e0c4 Mon Sep 17 00:00:00 2001 From: Sainan Date: Fri, 24 Jan 2025 14:27:10 +0100 Subject: [PATCH 1/2] chore: turn getJSONfromString into a template/generic function (#836) --- src/controllers/api/activateRandomModController.ts | 2 +- src/controllers/api/addFriendImageController.ts | 2 +- src/controllers/api/arcaneCommonController.ts | 2 +- src/controllers/api/artifactsController.ts | 2 +- .../api/claimCompletedRecipeController.ts | 2 +- src/controllers/api/createGuildController.ts | 2 +- src/controllers/api/endlessXpController.ts | 2 +- src/controllers/api/evolveWeaponController.ts | 2 +- src/controllers/api/fishmongerController.ts | 2 +- src/controllers/api/genericUpdateController.ts | 2 +- .../api/getVoidProjectionRewardsController.ts | 2 +- src/controllers/api/gildWeaponController.ts | 2 +- .../api/giveKeyChainTriggeredItemsController.ts | 4 ++-- src/controllers/api/infestedFoundryController.ts | 14 +++++++------- .../api/missionInventoryUpdateController.ts | 2 +- .../api/modularWeaponCraftingController.ts | 2 +- src/controllers/api/nameWeaponController.ts | 2 +- src/controllers/api/playerSkillsController.ts | 2 +- src/controllers/api/rerollRandomModController.ts | 2 +- .../api/setEquippedInstrumentController.ts | 2 +- .../api/setWeaponSkillTreeController.ts | 2 +- src/controllers/api/startRecipeController.ts | 2 +- .../api/syndicateSacrificeController.ts | 2 +- src/controllers/api/trainingResultController.ts | 2 +- .../api/updateChallengeProgressController.ts | 2 +- src/controllers/api/updateQuestController.ts | 2 +- src/controllers/api/updateThemeController.ts | 2 +- src/helpers/stringHelpers.ts | 4 ++-- 28 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/controllers/api/activateRandomModController.ts b/src/controllers/api/activateRandomModController.ts index a0ef4745..cb84feba 100644 --- a/src/controllers/api/activateRandomModController.ts +++ b/src/controllers/api/activateRandomModController.ts @@ -9,7 +9,7 @@ import { ExportUpgrades } from "warframe-public-export-plus"; export const activateRandomModController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const request = getJSONfromString(String(req.body)) as IActiveRandomModRequest; + const request = getJSONfromString(String(req.body)); addMods(inventory, [ { ItemType: request.ItemType, diff --git a/src/controllers/api/addFriendImageController.ts b/src/controllers/api/addFriendImageController.ts index 3242234c..3772f7ef 100644 --- a/src/controllers/api/addFriendImageController.ts +++ b/src/controllers/api/addFriendImageController.ts @@ -6,7 +6,7 @@ import { getInventory } from "@/src/services/inventoryService"; const addFriendImageController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const json = getJSONfromString(String(req.body)) as IUpdateGlyphRequest; + const json = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); inventory.ActiveAvatarImageType = json.AvatarImageType; await inventory.save(); diff --git a/src/controllers/api/arcaneCommonController.ts b/src/controllers/api/arcaneCommonController.ts index 1028fc3f..2448140b 100644 --- a/src/controllers/api/arcaneCommonController.ts +++ b/src/controllers/api/arcaneCommonController.ts @@ -6,7 +6,7 @@ import { IOid } from "@/src/types/commonTypes"; export const arcaneCommonController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const json = getJSONfromString(String(req.body)) as IArcaneCommonRequest; + const json = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); const upgrade = inventory.Upgrades.id(json.arcane.ItemId.$oid); if (json.newRank == -1) { diff --git a/src/controllers/api/artifactsController.ts b/src/controllers/api/artifactsController.ts index 564d222a..e9f8d582 100644 --- a/src/controllers/api/artifactsController.ts +++ b/src/controllers/api/artifactsController.ts @@ -7,7 +7,7 @@ import { config } from "@/src/services/configService"; export const artifactsController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const artifactsData = getJSONfromString(String(req.body)) as IArtifactsRequest; + const artifactsData = getJSONfromString(String(req.body)); const { Upgrade, LevelDiff, Cost, FusionPointCost } = artifactsData; diff --git a/src/controllers/api/claimCompletedRecipeController.ts b/src/controllers/api/claimCompletedRecipeController.ts index 21beb6c0..779f4587 100644 --- a/src/controllers/api/claimCompletedRecipeController.ts +++ b/src/controllers/api/claimCompletedRecipeController.ts @@ -21,7 +21,7 @@ export interface IClaimCompletedRecipeRequest { } export const claimCompletedRecipeController: RequestHandler = async (req, res) => { - const claimCompletedRecipeRequest = getJSONfromString(String(req.body)) as IClaimCompletedRecipeRequest; + const claimCompletedRecipeRequest = getJSONfromString(String(req.body)); const accountId = await getAccountIdForRequest(req); if (!accountId) throw new Error("no account id"); diff --git a/src/controllers/api/createGuildController.ts b/src/controllers/api/createGuildController.ts index d5ad31e9..8bc34408 100644 --- a/src/controllers/api/createGuildController.ts +++ b/src/controllers/api/createGuildController.ts @@ -6,7 +6,7 @@ import { Guild } from "@/src/models/guildModel"; export const createGuildController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const payload = getJSONfromString(String(req.body)) as ICreateGuildRequest; + const payload = getJSONfromString(String(req.body)); // Create guild on database const guild = new Guild({ diff --git a/src/controllers/api/endlessXpController.ts b/src/controllers/api/endlessXpController.ts index 855c5f22..656ebb05 100644 --- a/src/controllers/api/endlessXpController.ts +++ b/src/controllers/api/endlessXpController.ts @@ -7,7 +7,7 @@ import { TEndlessXpCategory } from "@/src/types/inventoryTypes/inventoryTypes"; export const endlessXpController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const payload = getJSONfromString(String(req.body)) as IEndlessXpRequest; + const payload = getJSONfromString(String(req.body)); inventory.EndlessXP ??= []; const entry = inventory.EndlessXP.find(x => x.Category == payload.Category); diff --git a/src/controllers/api/evolveWeaponController.ts b/src/controllers/api/evolveWeaponController.ts index 3e70922c..3b2550bb 100644 --- a/src/controllers/api/evolveWeaponController.ts +++ b/src/controllers/api/evolveWeaponController.ts @@ -8,7 +8,7 @@ import { EquipmentFeatures } from "@/src/types/inventoryTypes/commonInventoryTyp export const evolveWeaponController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const payload = getJSONfromString(String(req.body)) as IEvolveWeaponRequest; + const payload = getJSONfromString(String(req.body)); const recipe = getRecipe(payload.Recipe)!; if (payload.Action == "EWA_INSTALL") { diff --git a/src/controllers/api/fishmongerController.ts b/src/controllers/api/fishmongerController.ts index 37951333..898f5e4c 100644 --- a/src/controllers/api/fishmongerController.ts +++ b/src/controllers/api/fishmongerController.ts @@ -9,7 +9,7 @@ import { ExportResources, ExportSyndicates } from "warframe-public-export-plus"; export const fishmongerController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const body = getJSONfromString(String(req.body)) as IFishmongerRequest; + const body = getJSONfromString(String(req.body)); const miscItemChanges: IMiscItem[] = []; let syndicateTag: string | undefined; let gainedStanding = 0; diff --git a/src/controllers/api/genericUpdateController.ts b/src/controllers/api/genericUpdateController.ts index a8060981..79b6ba44 100644 --- a/src/controllers/api/genericUpdateController.ts +++ b/src/controllers/api/genericUpdateController.ts @@ -9,7 +9,7 @@ import { IGenericUpdate } from "@/src/types/genericUpdate"; const genericUpdateController: RequestHandler = async (request, response) => { const accountId = await getAccountIdForRequest(request); - const update = getJSONfromString(String(request.body)) as IGenericUpdate; + const update = getJSONfromString(String(request.body)); await updateGeneric(update, accountId); response.json(update); }; diff --git a/src/controllers/api/getVoidProjectionRewardsController.ts b/src/controllers/api/getVoidProjectionRewardsController.ts index 9a956a20..eecbdc51 100644 --- a/src/controllers/api/getVoidProjectionRewardsController.ts +++ b/src/controllers/api/getVoidProjectionRewardsController.ts @@ -10,7 +10,7 @@ import { ExportRelics, ExportRewards, TRarity } from "warframe-public-export-plu export const getVoidProjectionRewardsController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const data = getJSONfromString(String(req.body)) as IVoidProjectionRewardRequest; + const data = getJSONfromString(String(req.body)); const response: IVoidProjectionRewardResponse = { CurrentWave: data.CurrentWave, ParticipantInfo: data.ParticipantInfo, diff --git a/src/controllers/api/gildWeaponController.ts b/src/controllers/api/gildWeaponController.ts index 5a89415f..ea4f1194 100644 --- a/src/controllers/api/gildWeaponController.ts +++ b/src/controllers/api/gildWeaponController.ts @@ -26,7 +26,7 @@ interface IGildWeaponRequest { export const gildWeaponController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const data = getJSONfromString(String(req.body)) as IGildWeaponRequest; + const data = getJSONfromString(String(req.body)); data.ItemId = String(req.query.ItemId); if (!modularWeaponCategory.includes(req.query.Category as WeaponTypeInternal | "Hoverboards")) { throw new Error(`Unknown modular weapon Category: ${String(req.query.Category)}`); diff --git a/src/controllers/api/giveKeyChainTriggeredItemsController.ts b/src/controllers/api/giveKeyChainTriggeredItemsController.ts index 9437e75a..e8f0dda6 100644 --- a/src/controllers/api/giveKeyChainTriggeredItemsController.ts +++ b/src/controllers/api/giveKeyChainTriggeredItemsController.ts @@ -5,9 +5,9 @@ import { addKeyChainItems, getInventory } from "@/src/services/inventoryService" export const giveKeyChainTriggeredItemsController: RequestHandler = async (req, res) => { const accountId = parseString(req.query.accountId); - const keyChainTriggeredItemsRequest = getJSONfromString( + const keyChainTriggeredItemsRequest = getJSONfromString( (req.body as string).toString() - ) as IGiveKeyChainTriggeredItemsRequest; + ); const inventory = await getInventory(accountId); const inventoryChanges = await addKeyChainItems(inventory, keyChainTriggeredItemsRequest); diff --git a/src/controllers/api/infestedFoundryController.ts b/src/controllers/api/infestedFoundryController.ts index 61a74982..5e27f1c9 100644 --- a/src/controllers/api/infestedFoundryController.ts +++ b/src/controllers/api/infestedFoundryController.ts @@ -22,7 +22,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { switch (req.query.mode) { case "s": { // shard installation - const request = getJSONfromString(String(req.body)) as IShardInstallRequest; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; if (!suit.ArchonCrystalUpgrades || suit.ArchonCrystalUpgrades.length != 5) { @@ -50,7 +50,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { case "x": { // shard removal - const request = getJSONfromString(String(req.body)) as IShardUninstallRequest; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); const suit = inventory.Suits.find(suit => suit._id.toString() == request.SuitId.$oid)!; @@ -88,7 +88,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { case "n": { // name the beast - const request = getJSONfromString(String(req.body)) as IHelminthNameRequest; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); inventory.InfestedFoundry ??= {}; inventory.InfestedFoundry.Name = request.newName; @@ -105,7 +105,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { case "c": { // consume items - const request = getJSONfromString(String(req.body)) as IHelminthFeedRequest; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); inventory.InfestedFoundry ??= {}; inventory.InfestedFoundry.Resources ??= []; @@ -201,7 +201,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { case "o": { // offerings update - const request = getJSONfromString(String(req.body)) as IHelminthOfferingsUpdate; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); inventory.InfestedFoundry ??= {}; inventory.InfestedFoundry.InvigorationIndex = request.OfferingsIndex; @@ -220,7 +220,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { case "a": { // subsume warframe - const request = getJSONfromString(String(req.body)) as IHelminthSubsumeRequest; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); const recipe = getRecipe(request.Recipe)!; for (const ingredient of recipe.secretIngredients!) { @@ -283,7 +283,7 @@ export const infestedFoundryController: RequestHandler = async (req, res) => { } case "u": { - const request = getJSONfromString(String(req.body)) as IHelminthInvigorationRequest; + const request = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); const suit = inventory.Suits.id(request.SuitId.$oid)!; const upgradesExpiry = new Date(new Date().getTime() + 7 * 24 * 60 * 60 * 1000); diff --git a/src/controllers/api/missionInventoryUpdateController.ts b/src/controllers/api/missionInventoryUpdateController.ts index 49203095..f24172c4 100644 --- a/src/controllers/api/missionInventoryUpdateController.ts +++ b/src/controllers/api/missionInventoryUpdateController.ts @@ -52,7 +52,7 @@ import { getInventory } from "@/src/services/inventoryService"; export const missionInventoryUpdateController: RequestHandler = async (req, res): Promise => { const accountId = await getAccountIdForRequest(req); - const missionReport = getJSONfromString((req.body as string).toString()) as IMissionInventoryUpdateRequest; + const missionReport = getJSONfromString((req.body as string).toString()); if (missionReport.MissionStatus !== "GS_SUCCESS") { console.log(`Mission failed: ${missionReport.RewardInfo?.node}`); diff --git a/src/controllers/api/modularWeaponCraftingController.ts b/src/controllers/api/modularWeaponCraftingController.ts index 3eb27310..d70f48ca 100644 --- a/src/controllers/api/modularWeaponCraftingController.ts +++ b/src/controllers/api/modularWeaponCraftingController.ts @@ -29,7 +29,7 @@ interface IModularCraftRequest { export const modularWeaponCraftingController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const data = getJSONfromString(String(req.body)) as IModularCraftRequest; + const data = getJSONfromString(String(req.body)); if (!(data.WeaponType in modularWeaponTypes)) { throw new Error(`unknown modular weapon type: ${data.WeaponType}`); } diff --git a/src/controllers/api/nameWeaponController.ts b/src/controllers/api/nameWeaponController.ts index 2a3f4236..843feeb9 100644 --- a/src/controllers/api/nameWeaponController.ts +++ b/src/controllers/api/nameWeaponController.ts @@ -11,7 +11,7 @@ interface INameWeaponRequest { export const nameWeaponController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const body = getJSONfromString(String(req.body)) as INameWeaponRequest; + 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) )!; diff --git a/src/controllers/api/playerSkillsController.ts b/src/controllers/api/playerSkillsController.ts index 2610183f..de7ab4c8 100644 --- a/src/controllers/api/playerSkillsController.ts +++ b/src/controllers/api/playerSkillsController.ts @@ -7,7 +7,7 @@ import { RequestHandler } from "express"; export const playerSkillsController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const request = getJSONfromString(String(req.body)) as IPlayerSkillsRequest; + const request = getJSONfromString(String(req.body)); const oldRank: number = inventory.PlayerSkills[request.Skill as keyof IPlayerSkills]; const cost = (request.Pool == "LPP_DRIFTER" ? drifterCosts[oldRank] : 1 << oldRank) * 1000; diff --git a/src/controllers/api/rerollRandomModController.ts b/src/controllers/api/rerollRandomModController.ts index ba72dc43..71bc8f1c 100644 --- a/src/controllers/api/rerollRandomModController.ts +++ b/src/controllers/api/rerollRandomModController.ts @@ -7,7 +7,7 @@ import { getRandomElement } from "@/src/services/rngService"; export const rerollRandomModController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const request = getJSONfromString(String(req.body)) as RerollRandomModRequest; + const request = getJSONfromString(String(req.body)); if ("ItemIds" in request) { const inventory = await getInventory(accountId, "Upgrades MiscItems"); const upgrade = inventory.Upgrades.id(request.ItemIds[0])!; diff --git a/src/controllers/api/setEquippedInstrumentController.ts b/src/controllers/api/setEquippedInstrumentController.ts index 0781825a..5b4b9800 100644 --- a/src/controllers/api/setEquippedInstrumentController.ts +++ b/src/controllers/api/setEquippedInstrumentController.ts @@ -6,7 +6,7 @@ import { getJSONfromString } from "@/src/helpers/stringHelpers"; export const setEquippedInstrumentController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const body = getJSONfromString(String(req.body)) as ISetEquippedInstrumentRequest; + const body = getJSONfromString(String(req.body)); inventory.EquippedInstrument = body.Instrument; await inventory.save(); res.end(); diff --git a/src/controllers/api/setWeaponSkillTreeController.ts b/src/controllers/api/setWeaponSkillTreeController.ts index d4557f45..98fe7652 100644 --- a/src/controllers/api/setWeaponSkillTreeController.ts +++ b/src/controllers/api/setWeaponSkillTreeController.ts @@ -7,7 +7,7 @@ import { WeaponTypeInternal } from "@/src/services/itemDataService"; export const setWeaponSkillTreeController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); const inventory = await getInventory(accountId); - const payload = getJSONfromString(String(req.body)) as ISetWeaponSkillTreeRequest; + const payload = getJSONfromString(String(req.body)); const item = inventory[req.query.Category as WeaponTypeInternal].find( item => item._id.toString() == (req.query.ItemId as string) diff --git a/src/controllers/api/startRecipeController.ts b/src/controllers/api/startRecipeController.ts index 06ce6d90..642d0cfc 100644 --- a/src/controllers/api/startRecipeController.ts +++ b/src/controllers/api/startRecipeController.ts @@ -14,7 +14,7 @@ interface IStartRecipeRequest { } export const startRecipeController: RequestHandler = async (req, res) => { - const startRecipeRequest = getJSONfromString(String(req.body)) as IStartRecipeRequest; + const startRecipeRequest = getJSONfromString(String(req.body)); logger.debug("StartRecipe Request", { startRecipeRequest }); const accountId = await getAccountIdForRequest(req); diff --git a/src/controllers/api/syndicateSacrificeController.ts b/src/controllers/api/syndicateSacrificeController.ts index e1be3510..9d7ece71 100644 --- a/src/controllers/api/syndicateSacrificeController.ts +++ b/src/controllers/api/syndicateSacrificeController.ts @@ -9,7 +9,7 @@ import { IInventoryChanges } from "@/src/types/purchaseTypes"; export const syndicateSacrificeController: RequestHandler = async (request, response) => { const accountId = await getAccountIdForRequest(request); const inventory = await getInventory(accountId); - const data = getJSONfromString(String(request.body)) as ISyndicateSacrificeRequest; + const data = getJSONfromString(String(request.body)); let syndicate = inventory.Affiliations.find(x => x.Tag == data.AffiliationTag); if (!syndicate) { diff --git a/src/controllers/api/trainingResultController.ts b/src/controllers/api/trainingResultController.ts index eeab95b5..022d6c10 100644 --- a/src/controllers/api/trainingResultController.ts +++ b/src/controllers/api/trainingResultController.ts @@ -19,7 +19,7 @@ interface ITrainingResultsResponse { const trainingResultController: RequestHandler = async (req, res): Promise => { const accountId = await getAccountIdForRequest(req); - const trainingResults = getJSONfromString(String(req.body)) as ITrainingResultsRequest; + const trainingResults = getJSONfromString(String(req.body)); const inventory = await getInventory(accountId); diff --git a/src/controllers/api/updateChallengeProgressController.ts b/src/controllers/api/updateChallengeProgressController.ts index d665a6fb..2889a333 100644 --- a/src/controllers/api/updateChallengeProgressController.ts +++ b/src/controllers/api/updateChallengeProgressController.ts @@ -5,7 +5,7 @@ import { updateChallengeProgress } from "@/src/services/inventoryService"; import { IUpdateChallengeProgressRequest } from "@/src/types/requestTypes"; const updateChallengeProgressController: RequestHandler = async (req, res) => { - const payload = getJSONfromString(String(req.body)) as IUpdateChallengeProgressRequest; + const payload = getJSONfromString(String(req.body)); const accountId = await getAccountIdForRequest(req); await updateChallengeProgress(payload, accountId); diff --git a/src/controllers/api/updateQuestController.ts b/src/controllers/api/updateQuestController.ts index 2b30a3d1..dbfdb499 100644 --- a/src/controllers/api/updateQuestController.ts +++ b/src/controllers/api/updateQuestController.ts @@ -9,7 +9,7 @@ import { addItem, combineInventoryChanges, getInventory } from "@/src/services/i // eslint-disable-next-line @typescript-eslint/no-misused-promises export const updateQuestController: RequestHandler = async (req, res) => { const accountId = parseString(req.query.accountId); - const updateQuestRequest = getJSONfromString((req.body as string).toString()) as IUpdateQuestRequest; + const updateQuestRequest = getJSONfromString((req.body as string).toString()); // updates should be made only to one quest key per request if (updateQuestRequest.QuestKeys.length > 1) { diff --git a/src/controllers/api/updateThemeController.ts b/src/controllers/api/updateThemeController.ts index 17730064..ccb4ab57 100644 --- a/src/controllers/api/updateThemeController.ts +++ b/src/controllers/api/updateThemeController.ts @@ -9,7 +9,7 @@ const updateThemeController: RequestHandler = async (request, response) => { const body = String(request.body); try { - const json = getJSONfromString(body) as IThemeUpdateRequest; + const json = getJSONfromString(body); if (typeof json !== "object") { throw new Error("Invalid data format"); } diff --git a/src/helpers/stringHelpers.ts b/src/helpers/stringHelpers.ts index 89a3ae9d..6ab13851 100644 --- a/src/helpers/stringHelpers.ts +++ b/src/helpers/stringHelpers.ts @@ -1,6 +1,6 @@ -export const getJSONfromString = (str: string): any => { +export const getJSONfromString = (str: string): T => { const jsonSubstring = str.substring(0, str.lastIndexOf("}") + 1); - return JSON.parse(jsonSubstring); + return JSON.parse(jsonSubstring) as T; }; export const getSubstringFromKeyword = (str: string, keyword: string): string => { -- 2.47.2 From 98d0e6d58504d19e80dda059231e7a1f77f7956a Mon Sep 17 00:00:00 2001 From: Sainan Date: Fri, 24 Jan 2025 14:29:47 +0100 Subject: [PATCH 2/2] chore: switch purchaseService to take inventory document --- .../api/getVoidProjectionRewardsController.ts | 7 +- src/controllers/api/purchaseController.ts | 5 +- .../api/syndicateSacrificeController.ts | 6 +- src/services/inventoryService.ts | 5 +- src/services/purchaseService.ts | 80 +++++++------------ 5 files changed, 42 insertions(+), 61 deletions(-) diff --git a/src/controllers/api/getVoidProjectionRewardsController.ts b/src/controllers/api/getVoidProjectionRewardsController.ts index 9a956a20..c31a6345 100644 --- a/src/controllers/api/getVoidProjectionRewardsController.ts +++ b/src/controllers/api/getVoidProjectionRewardsController.ts @@ -27,18 +27,17 @@ export const getVoidProjectionRewardsController: RequestHandler = async (req, re logger.debug(`relic rolled`, reward); response.ParticipantInfo.Reward = reward.type; - // Remove relic const inventory = await getInventory(accountId); + // Remove relic addMiscItems(inventory, [ { ItemType: data.ParticipantInfo.VoidProjection, ItemCount: -1 } ]); - await inventory.save(); - // Give reward - await handleStoreItemAcquisition(reward.type, accountId, reward.itemCount); + await handleStoreItemAcquisition(reward.type, inventory, reward.itemCount); + await inventory.save(); } res.json(response); }; diff --git a/src/controllers/api/purchaseController.ts b/src/controllers/api/purchaseController.ts index 6c12446f..4b35e0c5 100644 --- a/src/controllers/api/purchaseController.ts +++ b/src/controllers/api/purchaseController.ts @@ -2,10 +2,13 @@ import { RequestHandler } from "express"; import { getAccountIdForRequest } from "@/src/services/loginService"; import { IPurchaseRequest } from "@/src/types/purchaseTypes"; import { handlePurchase } from "@/src/services/purchaseService"; +import { getInventory } from "@/src/services/inventoryService"; export const purchaseController: RequestHandler = async (req, res) => { const purchaseRequest = JSON.parse(String(req.body)) as IPurchaseRequest; const accountId = await getAccountIdForRequest(req); - const response = await handlePurchase(purchaseRequest, accountId); + const inventory = await getInventory(accountId); + const response = await handlePurchase(purchaseRequest, inventory); + await inventory.save(); res.json(response); }; diff --git a/src/controllers/api/syndicateSacrificeController.ts b/src/controllers/api/syndicateSacrificeController.ts index e1be3510..d84edbd8 100644 --- a/src/controllers/api/syndicateSacrificeController.ts +++ b/src/controllers/api/syndicateSacrificeController.ts @@ -57,15 +57,15 @@ export const syndicateSacrificeController: RequestHandler = async (request, resp } } - await inventory.save(); - if (reward) { combineInventoryChanges( res.InventoryChanges, - (await handleStoreItemAcquisition(reward, accountId)).InventoryChanges + (await handleStoreItemAcquisition(reward, inventory)).InventoryChanges ); } + await inventory.save(); + response.json(res); }; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index e602f944..0043ef35 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -949,10 +949,9 @@ export const addMissionComplete = (inventory: TInventoryDatabaseDocument, { Tag, } }; -export const addBooster = async (ItemType: string, time: number, accountId: string): Promise => { +export const addBooster = (ItemType: string, time: number, inventory: TInventoryDatabaseDocument): void => { const currentTime = Math.floor(Date.now() / 1000) - 129600; // Value is wrong without 129600. Figure out why, please. :) - const inventory = await getInventory(accountId); const { Boosters } = inventory; const itemIndex = Boosters.findIndex(booster => booster.ItemType === ItemType); @@ -964,8 +963,6 @@ export const addBooster = async (ItemType: string, time: number, accountId: stri } else { Boosters.push({ ItemType, ExpiryDate: currentTime + time }); } - - await inventory.save(); }; export const updateSyndicate = ( diff --git a/src/services/purchaseService.ts b/src/services/purchaseService.ts index fab6c411..bc2ef924 100644 --- a/src/services/purchaseService.ts +++ b/src/services/purchaseService.ts @@ -5,8 +5,7 @@ import { addItem, addMiscItems, combineInventoryChanges, - getInventory, - updateCurrencyByAccountId, + updateCurrency, updateSlots } from "@/src/services/inventoryService"; import { getRandomWeightedReward } from "@/src/services/rngService"; @@ -25,6 +24,7 @@ import { TRarity } from "warframe-public-export-plus"; import { config } from "./configService"; +import { TInventoryDatabaseDocument } from "../models/inventoryModels/inventoryModel"; export const getStoreItemCategory = (storeItem: string): string => { const storeItemString = getSubstringFromKeyword(storeItem, "StoreItems/"); @@ -43,7 +43,7 @@ export const getStoreItemTypesCategory = (typesItem: string): string => { export const handlePurchase = async ( purchaseRequest: IPurchaseRequest, - accountId: string + inventory: TInventoryDatabaseDocument ): Promise => { logger.debug("purchase request", purchaseRequest); @@ -58,8 +58,8 @@ export const handlePurchase = async ( throw new Error(`unknown vendor offer: ${ItemId}`); } if (offer.ItemPrices) { - await handleItemPrices( - accountId, + handleItemPrices( + inventory, offer.ItemPrices, purchaseRequest.PurchaseParams.Quantity, inventoryChanges @@ -73,17 +73,17 @@ export const handlePurchase = async ( const purchaseResponse = await handleStoreItemAcquisition( purchaseRequest.PurchaseParams.StoreItem, - accountId, + inventory, purchaseRequest.PurchaseParams.Quantity ); combineInventoryChanges(purchaseResponse.InventoryChanges, inventoryChanges); if (!purchaseResponse) throw new Error("purchase response was undefined"); - const currencyChanges = await updateCurrencyByAccountId( + const currencyChanges = updateCurrency( + inventory, purchaseRequest.PurchaseParams.ExpectedPrice, - purchaseRequest.PurchaseParams.UsePremium, - accountId + purchaseRequest.PurchaseParams.UsePremium ); purchaseResponse.InventoryChanges = { ...currencyChanges, @@ -95,7 +95,6 @@ export const handlePurchase = async ( { const syndicateTag = purchaseRequest.PurchaseParams.SyndicateTag!; if (purchaseRequest.PurchaseParams.UseFreeFavor!) { - const inventory = await getInventory(accountId); const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag)!; affiliation.FreeFavorsUsed ??= []; const lastTitle = affiliation.FreeFavorsEarned![affiliation.FreeFavorsUsed.length]; @@ -106,7 +105,6 @@ export const handlePurchase = async ( Title: lastTitle } ]; - await inventory.save(); } else { const syndicate = ExportSyndicates[syndicateTag]; if (syndicate) { @@ -114,7 +112,6 @@ export const handlePurchase = async ( x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem ); if (favour) { - const inventory = await getInventory(accountId); const affiliation = inventory.Affiliations.find(x => x.Tag == syndicateTag); if (affiliation) { purchaseResponse.Standing = [ @@ -124,7 +121,6 @@ export const handlePurchase = async ( } ]; affiliation.Standing -= favour.standingCost; - await inventory.save(); } } } @@ -136,8 +132,8 @@ export const handlePurchase = async ( const vendor = ExportVendors[purchaseRequest.PurchaseParams.SourceId!]; const offer = vendor.items.find(x => x.storeItem == purchaseRequest.PurchaseParams.StoreItem); if (offer && offer.itemPrices) { - await handleItemPrices( - accountId, + handleItemPrices( + inventory, offer.itemPrices, purchaseRequest.PurchaseParams.Quantity, purchaseResponse.InventoryChanges @@ -157,7 +153,6 @@ export const handlePurchase = async ( x => x.ItemType == purchaseRequest.PurchaseParams.StoreItem ); if (offer) { - const inventory = await getInventory(accountId); if (offer.RegularPrice) { const invItem: IMiscItem = { ItemType: "/Lotus/Types/Items/MiscItems/SchismKey", @@ -171,7 +166,6 @@ export const handlePurchase = async ( } else if (!config.infiniteRegalAya) { inventory.PrimeTokens -= offer.PrimePrice! * purchaseRequest.PurchaseParams.Quantity; } - await inventory.save(); } break; } @@ -180,13 +174,12 @@ export const handlePurchase = async ( return purchaseResponse; }; -const handleItemPrices = async ( - accountId: string, +const handleItemPrices = ( + inventory: TInventoryDatabaseDocument, itemPrices: IMiscItem[], purchaseQuantity: number, inventoryChanges: IInventoryChanges -): Promise => { - const inventory = await getInventory(accountId); +): void => { for (const item of itemPrices) { const invItem: IMiscItem = { ItemType: item.ItemType, @@ -203,12 +196,11 @@ const handleItemPrices = async ( (inventoryChanges.MiscItems as IMiscItem[]).push(invItem); } } - await inventory.save(); }; export const handleStoreItemAcquisition = async ( storeItemName: string, - accountId: string, + inventory: TInventoryDatabaseDocument, quantity: number = 1, durability: TRarity = "COMMON", ignorePurchaseQuantity: boolean = false @@ -226,7 +218,7 @@ export const handleStoreItemAcquisition = async ( ( await handleStoreItemAcquisition( component.typeName, - accountId, + inventory, component.purchaseQuantity * quantity, component.durability, true @@ -247,16 +239,14 @@ export const handleStoreItemAcquisition = async ( } switch (storeCategory) { default: { - const inventory = await getInventory(accountId); purchaseResponse = await addItem(inventory, internalName, quantity); - await inventory.save(); break; } case "Types": - purchaseResponse = await handleTypesPurchase(internalName, accountId, quantity); + purchaseResponse = await handleTypesPurchase(internalName, inventory, quantity); break; case "Boosters": - purchaseResponse = await handleBoostersPurchase(internalName, accountId, durability); + purchaseResponse = handleBoostersPurchase(internalName, inventory, durability); break; } } @@ -280,11 +270,11 @@ export const slotPurchaseNameToSlotName: SlotPurchase = { // // new slot above base = extra + 1 and slots +1 // // new frame = slots -1 // // number of frames = extra - slots + 2 -const handleSlotPurchase = async ( +const handleSlotPurchase = ( slotPurchaseNameFull: string, - accountId: string, + inventory: TInventoryDatabaseDocument, quantity: number -): Promise => { +): IPurchaseResponse => { logger.debug(`slot name ${slotPurchaseNameFull}`); const slotPurchaseName = parseSlotPurchaseName( slotPurchaseNameFull.substring(slotPurchaseNameFull.lastIndexOf("/") + 1) @@ -294,9 +284,7 @@ const handleSlotPurchase = async ( const slotName = slotPurchaseNameToSlotName[slotPurchaseName].name; const slotsPurchased = slotPurchaseNameToSlotName[slotPurchaseName].slotsPerPurchase * quantity; - const inventory = await getInventory(accountId); updateSlots(inventory, slotName, slotsPurchased, slotsPurchased); - await inventory.save(); logger.debug(`added ${slotsPurchased} slot ${slotName}`); @@ -314,7 +302,7 @@ const handleSlotPurchase = async ( const handleBoosterPackPurchase = async ( typeName: string, - accountId: string, + inventory: TInventoryDatabaseDocument, quantity: number ): Promise => { const pack = ExportBoosterPacks[typeName]; @@ -325,7 +313,6 @@ const handleBoosterPackPurchase = async ( BoosterPackItems: "", InventoryChanges: {} }; - const inventory = await getInventory(accountId); for (let i = 0; i != quantity; ++i) { for (const weights of pack.rarityWeightsPerRoll) { const result = getRandomWeightedReward(pack.components, weights); @@ -340,29 +327,24 @@ const handleBoosterPackPurchase = async ( } } } - await inventory.save(); return purchaseResponse; }; //TODO: change to getInventory, apply changes then save at the end const handleTypesPurchase = async ( typesName: string, - accountId: string, + inventory: TInventoryDatabaseDocument, quantity: number ): Promise => { const typeCategory = getStoreItemTypesCategory(typesName); logger.debug(`type category ${typeCategory}`); switch (typeCategory) { - default: { - const inventory = await getInventory(accountId); - const resp = await addItem(inventory, typesName, quantity); - await inventory.save(); - return resp; - } + default: + return await addItem(inventory, typesName, quantity); case "BoosterPacks": - return await handleBoosterPackPurchase(typesName, accountId, quantity); + return handleBoosterPackPurchase(typesName, inventory, quantity); case "SlotItems": - return await handleSlotPurchase(typesName, accountId, quantity); + return handleSlotPurchase(typesName, inventory, quantity); } }; @@ -380,11 +362,11 @@ const boosterDuration: Record = { LEGENDARY: 90 * 86400 }; -const handleBoostersPurchase = async ( +const handleBoostersPurchase = ( boosterStoreName: string, - accountId: string, + inventory: TInventoryDatabaseDocument, durability: TRarity -): Promise<{ InventoryChanges: IInventoryChanges }> => { +): { InventoryChanges: IInventoryChanges } => { const ItemType = boosterStoreName.replace("StoreItem", ""); if (!boosterCollection.find(x => x == ItemType)) { logger.error(`unknown booster type: ${ItemType}`); @@ -393,7 +375,7 @@ const handleBoostersPurchase = async ( const ExpiryDate = boosterDuration[durability]; - await addBooster(ItemType, ExpiryDate, accountId); + addBooster(ItemType, ExpiryDate, inventory); return { InventoryChanges: { -- 2.47.2