Refine RNG simulator layout and simulation stats
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
This commit is contained in:
parent
055fb78d60
commit
f8231c748f
11 changed files with 825 additions and 394 deletions
|
|
@ -3,6 +3,7 @@ export const RNG_SIMULATOR_HISTORY_LIMIT = 20;
|
|||
export const RNG_SIMULATOR_DEFAULT_RATE = "2.500";
|
||||
export const RNG_SIMULATOR_LIMITS = {
|
||||
rateMax: 100,
|
||||
costPerDrawMax: 1000000000000,
|
||||
multiplierMin: -1000000,
|
||||
multiplierMax: 1000000,
|
||||
simulationDrawsMin: 1,
|
||||
|
|
@ -11,6 +12,8 @@ export const RNG_SIMULATOR_LIMITS = {
|
|||
occurrenceRateMax: 1000000000
|
||||
};
|
||||
|
||||
export const RNG_SIMULATOR_OCCURRENCE_UNITS = ["perHour", "perDay", "perMonth"];
|
||||
|
||||
export function clampInteger(value, min, max, fallback = min) {
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
if (!Number.isFinite(parsed)) return fallback;
|
||||
|
|
@ -40,6 +43,21 @@ export function normalizeRateString(value, fallback = RNG_SIMULATOR_DEFAULT_RATE
|
|||
return cleanInteger;
|
||||
}
|
||||
|
||||
export function normalizeCostString(value, fallback = "") {
|
||||
const raw = String(value ?? "").trim().replace(",", ".");
|
||||
if (!raw) return fallback;
|
||||
if (!/^\d*(?:\.\d+)?$/.test(raw) || raw === ".") return fallback;
|
||||
|
||||
const parsed = Number(raw);
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) return fallback;
|
||||
if (parsed > RNG_SIMULATOR_LIMITS.costPerDrawMax) return String(RNG_SIMULATOR_LIMITS.costPerDrawMax);
|
||||
|
||||
const [integerPart = "0", decimalPart] = raw.split(".");
|
||||
const cleanInteger = String(Number.parseInt(integerPart || "0", 10));
|
||||
if (decimalPart !== undefined) return `${cleanInteger}.${decimalPart}`;
|
||||
return cleanInteger;
|
||||
}
|
||||
|
||||
export function isValidRateString(value) {
|
||||
const raw = String(value ?? "").trim().replace(",", ".").replace(/%$/, "").trim();
|
||||
if (!raw || !/^\d*(?:\.\d+)?$/.test(raw) || raw === ".") return false;
|
||||
|
|
@ -72,21 +90,20 @@ export function calculateDrawsForChance(rate, targetChance, multiplier = 0) {
|
|||
return Math.ceil(Math.log1p(-target) / Math.log1p(-probability));
|
||||
}
|
||||
|
||||
export function calculateRngSummary({ rate, multiplier = 0, simulationDraws, perHour, perDay, perMonth }) {
|
||||
export function normalizeOccurrenceUnit(value) {
|
||||
return RNG_SIMULATOR_OCCURRENCE_UNITS.includes(value) ? value : "perHour";
|
||||
}
|
||||
|
||||
export function calculateRngSummary({ rate, multiplier = 0, simulationDraws, occurrenceValue = 0, occurrenceUnit = "perHour" }) {
|
||||
const effectiveRate = getEffectiveRate(rate, multiplier);
|
||||
const probability = effectiveRate / 100;
|
||||
const averageDraws = probability > 0 ? 1 / probability : 0;
|
||||
const cleanPerHour = clampDecimal(perHour, RNG_SIMULATOR_LIMITS.occurrenceRateMin, RNG_SIMULATOR_LIMITS.occurrenceRateMax, 0);
|
||||
const cleanPerDay = clampDecimal(perDay, RNG_SIMULATOR_LIMITS.occurrenceRateMin, RNG_SIMULATOR_LIMITS.occurrenceRateMax, 0);
|
||||
const cleanPerMonth = clampDecimal(perMonth, RNG_SIMULATOR_LIMITS.occurrenceRateMin, RNG_SIMULATOR_LIMITS.occurrenceRateMax, 0);
|
||||
const cleanOccurrenceValue = clampDecimal(occurrenceValue, RNG_SIMULATOR_LIMITS.occurrenceRateMin, RNG_SIMULATOR_LIMITS.occurrenceRateMax, 0);
|
||||
const cleanOccurrenceUnit = normalizeOccurrenceUnit(occurrenceUnit);
|
||||
let averageHours = 0;
|
||||
if (cleanPerHour > 0) averageHours = averageDraws / cleanPerHour;
|
||||
else if (cleanPerDay > 0) averageHours = (averageDraws / cleanPerDay) * 24;
|
||||
else if (cleanPerMonth > 0) averageHours = (averageDraws / cleanPerMonth) * 24 * 30;
|
||||
|
||||
const explicitHourTime = cleanPerHour > 0 && averageDraws > 0 ? averageDraws / cleanPerHour : 0;
|
||||
const explicitDayTime = cleanPerDay > 0 && averageDraws > 0 ? averageDraws / cleanPerDay : 0;
|
||||
const explicitMonthTime = cleanPerMonth > 0 && averageDraws > 0 ? averageDraws / cleanPerMonth : 0;
|
||||
if (cleanOccurrenceValue > 0 && cleanOccurrenceUnit === "perHour") averageHours = averageDraws / cleanOccurrenceValue;
|
||||
else if (cleanOccurrenceValue > 0 && cleanOccurrenceUnit === "perDay") averageHours = (averageDraws / cleanOccurrenceValue) * 24;
|
||||
else if (cleanOccurrenceValue > 0 && cleanOccurrenceUnit === "perMonth") averageHours = (averageDraws / cleanOccurrenceValue) * 24 * 30;
|
||||
|
||||
return {
|
||||
probability,
|
||||
|
|
@ -94,9 +111,9 @@ export function calculateRngSummary({ rate, multiplier = 0, simulationDraws, per
|
|||
averageDraws,
|
||||
chanceForSimulationDraws: calculateAtLeastOneProbability(rate, simulationDraws, multiplier),
|
||||
timeByFrequency: {
|
||||
hour: explicitHourTime || averageHours,
|
||||
day: explicitDayTime || (averageHours > 0 ? averageHours / 24 : 0),
|
||||
month: explicitMonthTime || (averageHours > 0 ? averageHours / 24 / 30 : 0)
|
||||
hour: averageHours,
|
||||
day: averageHours > 0 ? averageHours / 24 : 0,
|
||||
month: averageHours > 0 ? averageHours / 24 / 30 : 0
|
||||
},
|
||||
drawsForChance: {
|
||||
50: calculateDrawsForChance(rate, 0.5, multiplier),
|
||||
|
|
@ -107,19 +124,45 @@ export function calculateRngSummary({ rate, multiplier = 0, simulationDraws, per
|
|||
};
|
||||
}
|
||||
|
||||
export function calculateRngCostSummary({ costPerDraw, averageDraws, drawsForChance, simulationDraws, simulationSuccesses, firstSuccessDraw = 0 }) {
|
||||
const cleanCost = Number(normalizeCostString(costPerDraw));
|
||||
if (!Number.isFinite(cleanCost) || cleanCost <= 0) return null;
|
||||
const cleanSimulationDraws = clampInteger(simulationDraws, RNG_SIMULATOR_LIMITS.simulationDrawsMin, RNG_SIMULATOR_LIMITS.simulationDrawsMax, 0);
|
||||
const cleanSuccesses = clampInteger(simulationSuccesses, 0, cleanSimulationDraws, 0);
|
||||
const cleanFirstSuccessDraw = clampInteger(firstSuccessDraw, 0, cleanSimulationDraws, 0);
|
||||
const targetCosts = {};
|
||||
Object.entries(drawsForChance || {}).forEach(([target, draws]) => {
|
||||
targetCosts[target] = Number.isFinite(draws) ? draws * cleanCost : Number.POSITIVE_INFINITY;
|
||||
});
|
||||
return {
|
||||
costPerDraw: cleanCost,
|
||||
averageSuccessCost: Number.isFinite(averageDraws) ? averageDraws * cleanCost : 0,
|
||||
targetCosts,
|
||||
simulationCostPerSuccess: cleanSuccesses > 0 ? (cleanSimulationDraws * cleanCost) / cleanSuccesses : 0,
|
||||
simulationFirstSuccessCost: cleanFirstSuccessDraw > 0 ? cleanFirstSuccessDraw * cleanCost : 0
|
||||
};
|
||||
}
|
||||
|
||||
export function simulateRngDraws(rate, draws, random = Math.random, multiplier = 0) {
|
||||
const probability = getEffectiveRate(rate, multiplier) / 100;
|
||||
const cleanDraws = clampInteger(draws, RNG_SIMULATOR_LIMITS.simulationDrawsMin, RNG_SIMULATOR_LIMITS.simulationDrawsMax, 1);
|
||||
let successes = 0;
|
||||
let currentFailStreak = 0;
|
||||
let currentSuccessStreak = 0;
|
||||
let firstSuccessDraw = 0;
|
||||
let longestFailStreak = 0;
|
||||
let longestSuccessStreak = 0;
|
||||
|
||||
for (let index = 0; index < cleanDraws; index += 1) {
|
||||
if (random() < probability) {
|
||||
successes += 1;
|
||||
if (!firstSuccessDraw) firstSuccessDraw = index + 1;
|
||||
currentSuccessStreak += 1;
|
||||
longestSuccessStreak = Math.max(longestSuccessStreak, currentSuccessStreak);
|
||||
currentFailStreak = 0;
|
||||
} else {
|
||||
currentFailStreak += 1;
|
||||
currentSuccessStreak = 0;
|
||||
longestFailStreak = Math.max(longestFailStreak, currentFailStreak);
|
||||
}
|
||||
}
|
||||
|
|
@ -129,7 +172,9 @@ export function simulateRngDraws(rate, draws, random = Math.random, multiplier =
|
|||
successes,
|
||||
fails: cleanDraws - successes,
|
||||
observedRate: cleanDraws > 0 ? (successes / cleanDraws) * 100 : 0,
|
||||
longestFailStreak
|
||||
longestFailStreak,
|
||||
longestSuccessStreak,
|
||||
firstSuccessDraw
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue