From c48a17720ea9b082d9b1db4cbf47e63f8172d437 Mon Sep 17 00:00:00 2001 From: holmityd Date: Sun, 3 Sep 2023 12:22:27 +0400 Subject: [PATCH] chore: Mission reward - take 3 --- .../api/missionInventoryUpdateController.ts | 124 +++++++++++++----- src/types/missionInventoryUpdateType.ts | 13 +- 2 files changed, 95 insertions(+), 42 deletions(-) diff --git a/src/controllers/api/missionInventoryUpdateController.ts b/src/controllers/api/missionInventoryUpdateController.ts index 3c4d4a3f..ad06363b 100644 --- a/src/controllers/api/missionInventoryUpdateController.ts +++ b/src/controllers/api/missionInventoryUpdateController.ts @@ -12,6 +12,7 @@ import resourceNames from "@/static/json/resource-names.json"; import gearNames from "@/static/json/gear-names.json"; import arcaneNames from "@/static/json/arcane-names.json"; import craftNames from "@/static/json/craft-names.json"; +import { RawUpgrade } from "@/src/types/inventoryTypes/inventoryTypes"; /* **** INPUT **** @@ -63,25 +64,43 @@ const missionInventoryUpdateController: RequestHandler = async (req, res) => { try { const parsedData = JSON.parse(data) as MissionInventoryUpdate; if (typeof parsedData !== "object" || parsedData === null) throw new Error("Invalid data format"); - // eslint-disable-next-line @typescript-eslint/no-unused-vars + + const { InventoryChanges, MissionRewards } = getRewards(parsedData.RewardInfo); const missionCredits = parsedData.RegularCredits || 0; - const creditsBonus = 0; + const creditsBonus = InventoryChanges.RegularCredits || 1000; const totalCredits = missionCredits + creditsBonus; - const MissionCredits = [missionCredits, missionCredits]; // collected credits - const CreditsBonus = [creditsBonus, creditsBonus]; // mission reward + const MissionCredits = [missionCredits, missionCredits]; + const CreditsBonus = [creditsBonus, creditsBonus]; const TotalCredits = [totalCredits, totalCredits]; + const FusionPoints = + parsedData.FusionPoints || InventoryChanges.FusionPoints + ? (parsedData.FusionPoints || 0) + (InventoryChanges.FusionPoints || 0) + : undefined; - console.log(getRewards(parsedData.RewardInfo)); + // combine reward and loot + parsedData.RegularCredits = totalCredits; + if (FusionPoints) parsedData.FusionPoints = FusionPoints; + if (InventoryChanges.RawUpgrades && !parsedData.RawUpgrades) parsedData.RawUpgrades = []; + InventoryChanges.RawUpgrades?.forEach(i => parsedData.RawUpgrades!.push(i)); + if (InventoryChanges.MiscItems && !parsedData.MiscItems) parsedData.RawUpgrades = []; + InventoryChanges.MiscItems?.forEach(i => parsedData.MiscItems!.push(i)); + const Inventory = await missionInventoryUpdate(parsedData, id); + InventoryChanges.RawUpgrades?.forEach( + (i: RawUpgrade) => (i.LastAdded = Inventory.RawUpgrades.find(j => j.ItemType === i.ItemType)?.LastAdded) + ); // eslint-disable-next-line @typescript-eslint/no-unused-vars - const InventoryJson = JSON.stringify(await missionInventoryUpdate(parsedData, id)); + const InventoryJson = JSON.stringify(Inventory); res.json({ // InventoryJson, // this part will reset game data and missions will be locked TotalCredits, CreditsBonus, - MissionCredits + MissionCredits, + MissionRewards, + InventoryChanges, + ...(FusionPoints !== undefined && { FusionPoints }) }); } catch (err) { console.error("Error parsing JSON data:", err); @@ -91,26 +110,44 @@ const missionInventoryUpdateController: RequestHandler = async (req, res) => { /* **** OUTPUT **** - [x] InventoryJson -- [ ] MissionRewards +- [x] MissionRewards - [x] TotalCredits - [x] CreditsBonus - [x] MissionCredits -- [ ] InventoryChanges -- [ ] FusionPoints int +- [x] InventoryChanges +- [x] FusionPoints */ interface StringDictionary { [key: string]: string; } -const getRewards = (rewardInfo: MissionInventoryUpdateRewardInfo | undefined) => { - if (!rewardInfo) return; +const getRewards = ( + rewardInfo: MissionInventoryUpdateRewardInfo | undefined +): { InventoryChanges: MissionInventoryUpdate; MissionRewards: MissionRewardResponse[] } => { + if (!rewardInfo) return { InventoryChanges: {}, MissionRewards: [] }; + + // TODO - add Rotation logic + // no data for rotation, need reverse engineer rewardSeed, otherwise ingame displayed rotation loot will be different than added to db + + // "RewardInfo": { + // "node": "SolNode39", + // "rewardTier": 1, + // "nightmareMode": false, + // "useVaultManifest": false, + // "EnemyCachesFound": 0, + // "toxinOk": true, + // "lostTargetWave": 0, + // "defenseTargetCount": 1, + // "EOM_AFK": 0, + // "rewardQualifications": "11", + // "PurgatoryRewardQualifications": "", + // "rewardSeed": -5604904486637266000 + // }, const missionName = (missionNames as StringDictionary)[rewardInfo.node]; const rewards = missionsDropTable.find(i => i.mission === missionName)?.rewards; - if (!rewards) return []; - - // TODO - add Rotation logic + if (!rewards) return { InventoryChanges: {}, MissionRewards: [] }; // Separate guaranteed and chance drops const guaranteedDrops: Reward[] = []; @@ -123,6 +160,8 @@ const getRewards = (rewardInfo: MissionInventoryUpdateRewardInfo | undefined) => const randomDrop = getRandomRewardByChance(chanceDrops); if (randomDrop) guaranteedDrops.push(randomDrop); + console.log("Mission rewards:", guaranteedDrops); + return formatRewardsToInventoryType(guaranteedDrops); }; @@ -148,8 +187,20 @@ const getRandomRewardByChance = (data: Reward[] | undefined): Reward | undefined return; }; -const formatRewardsToInventoryType = (rewards: Reward[]) => { - return rewards.map(i => { +interface MissionRewardResponse { + StoreItem?: string; + TypeName: string; + UpgradeLevel: number; + ItemCount: number; + TweetText: string; + ProductCategory: string; +} +const formatRewardsToInventoryType = ( + rewards: Reward[] +): { InventoryChanges: MissionInventoryUpdate; MissionRewards: MissionRewardResponse[] } => { + const InventoryChanges: MissionInventoryUpdate = {}; + const MissionRewards: MissionRewardResponse[] = []; + rewards.forEach(i => { const mod = (modNames as StringDictionary)[i.name]; const skin = (skinNames as StringDictionary)[i.name]; const gear = (gearNames as StringDictionary)[i.name]; @@ -164,32 +215,39 @@ const formatRewardsToInventoryType = (rewards: Reward[]) => { (relicNames as StringDictionary)[i.name.replace("Relic", "Exceptional")] || (relicNames as StringDictionary)[i.name.replace("Relic (Radiant)", "Radiant")]; - let ItemType: string = mod; - const ItemCount = 1; - if (mod) { - ItemType = mod; + if (!InventoryChanges.RawUpgrades) InventoryChanges.RawUpgrades = []; + InventoryChanges.RawUpgrades.push({ ItemType: mod, ItemCount: 1 }); + MissionRewards.push({ + StoreItem: mod.replace("/Lotus/", "/Lotus/StoreItems/"), + TypeName: mod, + UpgradeLevel: 0, + ItemCount: 1, + TweetText: `${i.name} (Mod)`, + ProductCategory: "Upgrades" + }); } else if (skin) { - ItemType = skin; + /* skin */ } else if (gear) { - ItemType = gear; + /* gear */ } else if (arcane) { - ItemType = arcane; + /* arcane */ } else if (craft) { - ItemType = craft; - } else if (misc) { - ItemType = misc; - } else if (resource) { - ItemType = resource; + /* craft */ + } else if (misc || resource) { + if (!InventoryChanges.MiscItems) InventoryChanges.MiscItems = []; + const ItemType = misc || resource; + const ItemCount = parseInt(i.name) || 1; + InventoryChanges.MiscItems.push({ ItemType, ItemCount }); } else if (relic) { - ItemType = relic; + /* relic */ } else if (i.name.includes(" Endo")) { - /* endo */ + InventoryChanges.FusionPoints = parseInt(i.name); } else if (i.name.includes(" Credits Cache") || i.name.includes("Return: ")) { - /* credits */ + InventoryChanges.RegularCredits = parseInt(i.name.replace(/ Credits Cache|Return: |,/g, "")); } - return { ItemType, ItemCount }; }); + return { InventoryChanges, MissionRewards }; }; export { missionInventoryUpdateController }; diff --git a/src/types/missionInventoryUpdateType.ts b/src/types/missionInventoryUpdateType.ts index 5b23cd41..48e06ab5 100644 --- a/src/types/missionInventoryUpdateType.ts +++ b/src/types/missionInventoryUpdateType.ts @@ -1,3 +1,4 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ interface MongooseId { $oid: string; } @@ -37,13 +38,6 @@ export interface MissionInventoryUpdateItem { ItemType: string; } -export interface MissionInventoryUpdateCard extends MissionInventoryUpdateItem { - ItemId: MongooseId; - UpgradeFingerprint: string; - PendingRerollFingerprint: string; - LastAdded: MongooseId; -} - interface MissionInventoryUpdateChallange { Name: string; Progress: number; @@ -72,9 +66,10 @@ export interface MissionInventoryUpdate { Pistols?: MissionInventoryUpdateGear[]; Suits?: MissionInventoryUpdateGear[]; Melee?: MissionInventoryUpdateGear[]; - RawUpgrades?: MissionInventoryUpdateCard[]; + RawUpgrades?: MissionInventoryUpdateItem[]; MiscItems?: MissionInventoryUpdateItem[]; RegularCredits?: number; ChallengeProgress?: MissionInventoryUpdateChallange[]; RewardInfo?: MissionInventoryUpdateRewardInfo; -} \ No newline at end of file + FusionPoints?: number; +}