diff --git a/src/services/missionInventoryUpdateService.ts b/src/services/missionInventoryUpdateService.ts index b01fbe45..fc5de537 100644 --- a/src/services/missionInventoryUpdateService.ts +++ b/src/services/missionInventoryUpdateService.ts @@ -11,6 +11,7 @@ import { } from "warframe-public-export-plus"; import { IMissionInventoryUpdateRequest } from "../types/requestTypes"; import { logger } from "@/src/utils/logger"; +import { IRngResult, getRandomReward } from "@/src/services/rngService"; // need reverse engineer rewardSeed, otherwise ingame displayed rotation reward will be different than added to db or displayed on mission end const getRewards = ({ @@ -23,7 +24,7 @@ const getRewards = ({ return { InventoryChanges: {}, MissionRewards: [] }; } - const drops: IReward[] = []; + const drops: IRngResult[] = []; if (RewardInfo.node in ExportRegions) { const region = ExportRegions[RewardInfo.node]; const rewardManifests = region.rewardManifests ?? []; @@ -117,21 +118,8 @@ const getRotations = (rotationCount: number): number[] => { return rotatedValues; }; -const getRandomRewardByChance = (data: IReward[]): IReward | undefined => { - if (data.length == 0) return; - - const totalChance = data.reduce((sum, item) => sum + item.probability!, 0); - const randomValue = Math.random() * totalChance; - - let cumulativeChance = 0; - for (const item of data) { - cumulativeChance += item.probability!; - if (randomValue <= cumulativeChance) { - return item; - } - } - - return; +const getRandomRewardByChance = (pool: IReward[]): IRngResult | undefined => { + return getRandomReward(pool as IRngResult[]); // As of TS 5.5, we will be able to handle this with .filter instead of 'as'. }; const creditBundles: Record = { @@ -157,7 +145,7 @@ const fusionBundles: Record = { }; const formatRewardsToInventoryType = ( - rewards: IReward[] + rewards: IRngResult[] ): { InventoryChanges: IMissionInventoryUpdateRequest; MissionRewards: IMissionRewardResponse[] } => { const InventoryChanges: IMissionInventoryUpdateRequest = {}; const MissionRewards: IMissionRewardResponse[] = []; diff --git a/src/services/rngService.ts b/src/services/rngService.ts new file mode 100644 index 00000000..9619d7eb --- /dev/null +++ b/src/services/rngService.ts @@ -0,0 +1,21 @@ +export interface IRngResult { + type: string; + itemCount: number; + probability: number; +} + +export const getRandomReward = (pool: IRngResult[]): IRngResult | undefined => { + if (pool.length == 0) return; + + const totalChance = pool.reduce((accum, item) => accum + item.probability, 0); + const randomValue = Math.random() * totalChance; + + let cumulativeChance = 0; + for (const item of pool) { + cumulativeChance += item.probability; + if (randomValue <= cumulativeChance) { + return item; + } + } + throw new Error("What the fuck?"); +};