refactor: add rngService

This commit is contained in:
Sainan 2024-07-02 15:43:31 +02:00
parent 76bff65d92
commit 12cf1cdfdb
2 changed files with 26 additions and 17 deletions

View File

@ -11,6 +11,7 @@ import {
} from "warframe-public-export-plus"; } from "warframe-public-export-plus";
import { IMissionInventoryUpdateRequest } from "../types/requestTypes"; import { IMissionInventoryUpdateRequest } from "../types/requestTypes";
import { logger } from "@/src/utils/logger"; 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 // need reverse engineer rewardSeed, otherwise ingame displayed rotation reward will be different than added to db or displayed on mission end
const getRewards = ({ const getRewards = ({
@ -23,7 +24,7 @@ const getRewards = ({
return { InventoryChanges: {}, MissionRewards: [] }; return { InventoryChanges: {}, MissionRewards: [] };
} }
const drops: IReward[] = []; const drops: IRngResult[] = [];
if (RewardInfo.node in ExportRegions) { if (RewardInfo.node in ExportRegions) {
const region = ExportRegions[RewardInfo.node]; const region = ExportRegions[RewardInfo.node];
const rewardManifests = region.rewardManifests ?? []; const rewardManifests = region.rewardManifests ?? [];
@ -117,21 +118,8 @@ const getRotations = (rotationCount: number): number[] => {
return rotatedValues; return rotatedValues;
}; };
const getRandomRewardByChance = (data: IReward[]): IReward | undefined => { const getRandomRewardByChance = (pool: IReward[]): IRngResult | undefined => {
if (data.length == 0) return; return getRandomReward(pool as IRngResult[]); // As of TS 5.5, we will be able to handle this with .filter instead of 'as'.
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 creditBundles: Record<string, number> = { const creditBundles: Record<string, number> = {
@ -157,7 +145,7 @@ const fusionBundles: Record<string, number> = {
}; };
const formatRewardsToInventoryType = ( const formatRewardsToInventoryType = (
rewards: IReward[] rewards: IRngResult[]
): { InventoryChanges: IMissionInventoryUpdateRequest; MissionRewards: IMissionRewardResponse[] } => { ): { InventoryChanges: IMissionInventoryUpdateRequest; MissionRewards: IMissionRewardResponse[] } => {
const InventoryChanges: IMissionInventoryUpdateRequest = {}; const InventoryChanges: IMissionInventoryUpdateRequest = {};
const MissionRewards: IMissionRewardResponse[] = []; const MissionRewards: IMissionRewardResponse[] = [];

View File

@ -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?");
};