fix not using weighted RNG
This commit is contained in:
parent
6a81a0a3d1
commit
89309aae19
@ -2,7 +2,7 @@ import { toMongoDate, toOid } from "@/src/helpers/inventoryHelpers";
|
||||
import { config } from "@/src/services/configService";
|
||||
import { addMiscItems, getInventory } from "@/src/services/inventoryService";
|
||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||
import { getRandomElement, getRandomInt } from "@/src/services/rngService";
|
||||
import { getRandomInt, getRandomWeightedReward3 } from "@/src/services/rngService";
|
||||
import { IMongoDate, IOid } from "@/src/types/commonTypes";
|
||||
import { IDroneClient } from "@/src/types/inventoryTypes/inventoryTypes";
|
||||
import { IInventoryChanges } from "@/src/types/purchaseTypes";
|
||||
@ -55,7 +55,8 @@ export const dronesController: RequestHandler = async (req, res) => {
|
||||
: new Date(Date.now() + getRandomInt(3, 4) * 3600_000);
|
||||
drone.PendingDamage = getRandomInt(system.droneDamage.minValue, system.droneDamage.maxValue);
|
||||
}
|
||||
const resource = getRandomElement(system.resources);
|
||||
const resource = getRandomWeightedReward3(system.resources, droneMeta.probabilities)!;
|
||||
//logger.debug(`drone rolled`, resource);
|
||||
drone.ResourceType = "/Lotus/" + resource.StoreItem.substring(18);
|
||||
const resourceMeta = ExportResources[drone.ResourceType];
|
||||
if (resourceMeta.pickupQuantity) {
|
||||
|
@ -18,7 +18,7 @@ export const getRandomInt = (min: number, max: number): number => {
|
||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||
};
|
||||
|
||||
export const getRandomReward = (pool: IRngResult[]): IRngResult | undefined => {
|
||||
export const getRandomReward = <T extends { probability: number }>(pool: T[]): T | undefined => {
|
||||
if (pool.length == 0) return;
|
||||
|
||||
const totalChance = pool.reduce((accum, item) => accum + item.probability, 0);
|
||||
@ -71,3 +71,21 @@ export const getRandomWeightedReward2 = (
|
||||
}
|
||||
return getRandomReward(resultPool);
|
||||
};
|
||||
|
||||
export const getRandomWeightedReward3 = <T extends { Rarity: TRarity }>(
|
||||
pool: T[],
|
||||
weights: Record<TRarity, number>
|
||||
): (T & { probability: number }) | undefined => {
|
||||
const resultPool: (T & { probability: number })[] = [];
|
||||
const rarityCounts: Record<TRarity, number> = { COMMON: 0, UNCOMMON: 0, RARE: 0, LEGENDARY: 0 };
|
||||
for (const entry of pool) {
|
||||
++rarityCounts[entry.Rarity];
|
||||
}
|
||||
for (const entry of pool) {
|
||||
resultPool.push({
|
||||
...entry,
|
||||
probability: weights[entry.Rarity] / rarityCounts[entry.Rarity]
|
||||
});
|
||||
}
|
||||
return getRandomReward(resultPool);
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user