Add chance simulator toolbox and refine checklist interactions
All checks were successful
Deploy Sokko G / deploy (push) Successful in 17s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 17s
This commit is contained in:
parent
35fe877d58
commit
6b85e58765
25 changed files with 1518 additions and 18 deletions
138
website/src/features/toolboxes/modules/rngSimulatorUtils.js
Normal file
138
website/src/features/toolboxes/modules/rngSimulatorUtils.js
Normal file
|
|
@ -0,0 +1,138 @@
|
|||
// Rôle : fournit les calculs probabilistes et simulations pour l'outil simulateur de chance.
|
||||
export const RNG_SIMULATOR_HISTORY_LIMIT = 20;
|
||||
export const RNG_SIMULATOR_DEFAULT_RATE = "2.500";
|
||||
export const RNG_SIMULATOR_LIMITS = {
|
||||
rateMax: 100,
|
||||
multiplierMin: -1000000,
|
||||
multiplierMax: 1000000,
|
||||
simulationDrawsMin: 1,
|
||||
simulationDrawsMax: 1000000,
|
||||
occurrenceRateMin: 0,
|
||||
occurrenceRateMax: 1000000000
|
||||
};
|
||||
|
||||
export function clampInteger(value, min, max, fallback = min) {
|
||||
const parsed = Number.parseInt(value, 10);
|
||||
if (!Number.isFinite(parsed)) return fallback;
|
||||
return Math.min(max, Math.max(min, parsed));
|
||||
}
|
||||
|
||||
export function clampDecimal(value, min, max, fallback = min) {
|
||||
const raw = String(value ?? "").trim();
|
||||
if (!raw) return fallback;
|
||||
const parsed = Number(raw.replace(",", "."));
|
||||
if (!Number.isFinite(parsed)) return fallback;
|
||||
return Math.min(max, Math.max(min, parsed));
|
||||
}
|
||||
|
||||
export function normalizeRateString(value, fallback = RNG_SIMULATOR_DEFAULT_RATE) {
|
||||
const raw = String(value ?? "").trim().replace(",", ".").replace(/%$/, "").trim();
|
||||
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.rateMax) return "100";
|
||||
|
||||
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;
|
||||
const parsed = Number(raw);
|
||||
return Number.isFinite(parsed) && parsed > 0 && parsed <= RNG_SIMULATOR_LIMITS.rateMax;
|
||||
}
|
||||
|
||||
export function getEffectiveRate(rate, multiplier = 0) {
|
||||
const baseRate = Number(normalizeRateString(rate));
|
||||
const cleanMultiplier = clampDecimal(multiplier, RNG_SIMULATOR_LIMITS.multiplierMin, RNG_SIMULATOR_LIMITS.multiplierMax, 0);
|
||||
if (!Number.isFinite(baseRate) || baseRate <= 0) return 0;
|
||||
const factor = Math.max(0, 1 + cleanMultiplier / 100);
|
||||
return Math.min(RNG_SIMULATOR_LIMITS.rateMax, baseRate * factor);
|
||||
}
|
||||
|
||||
export function calculateAtLeastOneProbability(rate, draws, multiplier = 0) {
|
||||
const probability = getEffectiveRate(rate, multiplier) / 100;
|
||||
const cleanDraws = clampInteger(draws, 0, Number.MAX_SAFE_INTEGER, 0);
|
||||
if (probability <= 0 || cleanDraws <= 0) return 0;
|
||||
if (probability >= 1) return 1;
|
||||
return -Math.expm1(cleanDraws * Math.log1p(-probability));
|
||||
}
|
||||
|
||||
export function calculateDrawsForChance(rate, targetChance, multiplier = 0) {
|
||||
const probability = getEffectiveRate(rate, multiplier) / 100;
|
||||
const target = clampDecimal(targetChance, 0, 1, 0);
|
||||
if (probability <= 0 || target <= 0) return 0;
|
||||
if (probability >= 1) return 1;
|
||||
if (target >= 1) return Number.POSITIVE_INFINITY;
|
||||
return Math.ceil(Math.log1p(-target) / Math.log1p(-probability));
|
||||
}
|
||||
|
||||
export function calculateRngSummary({ rate, multiplier = 0, simulationDraws, perHour, perDay, perMonth }) {
|
||||
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);
|
||||
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;
|
||||
|
||||
return {
|
||||
probability,
|
||||
effectiveRate,
|
||||
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)
|
||||
},
|
||||
drawsForChance: {
|
||||
50: calculateDrawsForChance(rate, 0.5, multiplier),
|
||||
90: calculateDrawsForChance(rate, 0.9, multiplier),
|
||||
95: calculateDrawsForChance(rate, 0.95, multiplier),
|
||||
99: calculateDrawsForChance(rate, 0.99, multiplier)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
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 longestFailStreak = 0;
|
||||
|
||||
for (let index = 0; index < cleanDraws; index += 1) {
|
||||
if (random() < probability) {
|
||||
successes += 1;
|
||||
currentFailStreak = 0;
|
||||
} else {
|
||||
currentFailStreak += 1;
|
||||
longestFailStreak = Math.max(longestFailStreak, currentFailStreak);
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
draws: cleanDraws,
|
||||
successes,
|
||||
fails: cleanDraws - successes,
|
||||
observedRate: cleanDraws > 0 ? (successes / cleanDraws) * 100 : 0,
|
||||
longestFailStreak
|
||||
};
|
||||
}
|
||||
|
||||
export function addRngSimulationHistory(history, entry) {
|
||||
return [entry, ...(Array.isArray(history) ? history : [])].slice(0, RNG_SIMULATOR_HISTORY_LIMIT);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue