tests/file-headers.test.mjs now fails when a source file over 300 lines lacks "Utilisé par :" and @see, when a listed central file lacks "Utilisé par :", or when a file adding methods through Object.assign does not say so. The eleven files below the bar are completed, so no waiting list is needed. tests/toolbox-compactors.test.mjs makes sure every tool type has a storage compactor, the gap found while documenting the toolboxes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
313 lines
14 KiB
JavaScript
313 lines
14 KiB
JavaScript
/**
|
|
* @file Calculs probabilistes et simulations du Simulateur de chance : bornes des réglages, durées proposées,
|
|
* probabilités sur N essais, essais nécessaires pour un seuil, tirages simulés.
|
|
*
|
|
* Utilisé par : RngSimulatorModule.jsx (calculs affichés) et storage/modules/rngSimulator.js (bornes à la
|
|
* normalisation).
|
|
* @see docs/STORAGE_SCHEMA.md
|
|
*/
|
|
|
|
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,
|
|
simulationDrawsMax: 1000000,
|
|
realDrawsMax: 1000000000,
|
|
occurrenceRateMin: 0,
|
|
occurrenceRateMax: 1000000000
|
|
};
|
|
|
|
export const RNG_SIMULATOR_OCCURRENCE_UNITS = ["perHour", "perDay"];
|
|
export const RNG_SIMULATOR_HOURLY_DURATION_PRESETS = [
|
|
{ id: "1h", label: "1h", hours: 1 },
|
|
{ id: "5h", label: "5h", hours: 5 },
|
|
{ id: "10h", label: "10h", hours: 10 },
|
|
{ id: "1j", label: "1j", hours: 24 }
|
|
];
|
|
export const RNG_SIMULATOR_DAILY_DURATION_PRESETS = [
|
|
{ id: "1j", label: "1j", hours: 24 },
|
|
{ id: "3j", label: "3j", hours: 72 },
|
|
{ id: "7j", label: "7j", hours: 168 },
|
|
{ id: "15j", label: "15j", hours: 360 }
|
|
];
|
|
export const RNG_SIMULATOR_MONTHLY_DURATION_PRESETS = [
|
|
{ id: "1j", label: "1j", hours: 24 },
|
|
{ id: "3j", label: "3j", hours: 72 },
|
|
{ id: "7j", label: "7j", hours: 168 },
|
|
{ id: "30j", label: "30j", hours: 720 }
|
|
];
|
|
export const RNG_SIMULATOR_YEARLY_DURATION_PRESETS = [
|
|
{ id: "30j", label: "30j", hours: 720 },
|
|
{ id: "90j", label: "90j", hours: 2160 },
|
|
{ id: "1a", label: "1 an", hours: 8760 },
|
|
{ id: "5a", label: "5 ans", hours: 43800 }
|
|
];
|
|
export const RNG_SIMULATOR_LONG_DURATION_PRESETS = [
|
|
{ id: "1a", label: "1 an", hours: 8760 },
|
|
{ id: "5a", label: "5 ans", hours: 43800 },
|
|
{ id: "10a", label: "10 ans", hours: 87600 },
|
|
{ id: "25a", label: "25 ans", hours: 219000 }
|
|
];
|
|
|
|
export function clampInteger(value, min, max, fallback = min) {
|
|
const parsed = Number.parseInt(String(value ?? "").replace(/[\s\u00a0]/g, ""), 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 normalizeCostString(value, fallback = "") {
|
|
const raw = String(value ?? "").trim().replace(/[\s\u00a0]/g, "").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;
|
|
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);
|
|
return calculateProbabilityForDrawCount(probability, cleanDraws);
|
|
}
|
|
|
|
function calculateProbabilityForDrawCount(probability, draws) {
|
|
const cleanDraws = clampDecimal(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 normalizeOccurrenceUnit(value) {
|
|
if (value === "perMonth") return "perDay";
|
|
return RNG_SIMULATOR_OCCURRENCE_UNITS.includes(value) ? value : "perHour";
|
|
}
|
|
|
|
export function normalizeOccurrenceSettings(value, unit) {
|
|
const cleanValue = clampDecimal(value, RNG_SIMULATOR_LIMITS.occurrenceRateMin, RNG_SIMULATOR_LIMITS.occurrenceRateMax, 0);
|
|
if (unit === "perMonth") {
|
|
return {
|
|
occurrenceValue: cleanValue / 30,
|
|
occurrenceUnit: "perDay"
|
|
};
|
|
}
|
|
return {
|
|
occurrenceValue: cleanValue,
|
|
occurrenceUnit: normalizeOccurrenceUnit(unit)
|
|
};
|
|
}
|
|
|
|
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 { occurrenceValue: cleanOccurrenceValue, occurrenceUnit: cleanOccurrenceUnit } = normalizeOccurrenceSettings(occurrenceValue, occurrenceUnit);
|
|
let averageHours = 0;
|
|
if (cleanOccurrenceValue > 0 && cleanOccurrenceUnit === "perHour") averageHours = averageDraws / cleanOccurrenceValue;
|
|
else if (cleanOccurrenceValue > 0 && cleanOccurrenceUnit === "perDay") averageHours = (averageDraws / cleanOccurrenceValue) * 24;
|
|
|
|
return {
|
|
probability,
|
|
effectiveRate,
|
|
averageDraws,
|
|
chanceForSimulationDraws: calculateAtLeastOneProbability(rate, simulationDraws, multiplier),
|
|
timeByFrequency: {
|
|
hour: averageHours,
|
|
day: averageHours > 0 ? averageHours / 24 : 0
|
|
},
|
|
durationChances: calculateDurationChances(rate, multiplier, cleanOccurrenceValue, cleanOccurrenceUnit),
|
|
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 calculateDurationChances(rate, multiplier = 0, occurrenceValue = 0, occurrenceUnit = "perHour") {
|
|
const { occurrenceValue: cleanOccurrenceValue, occurrenceUnit: cleanOccurrenceUnit } = normalizeOccurrenceSettings(occurrenceValue, occurrenceUnit);
|
|
const drawsPerHour = cleanOccurrenceUnit === "perDay" ? cleanOccurrenceValue / 24 : cleanOccurrenceValue;
|
|
const averageHours = drawsPerHour > 0 ? (1 / (getEffectiveRate(rate, multiplier) / 100)) / drawsPerHour : 0;
|
|
const presets = getDurationPresetsForAverageHours(averageHours, cleanOccurrenceUnit);
|
|
return presets.map((preset) => ({
|
|
...preset,
|
|
draws: drawsPerHour > 0 ? drawsPerHour * preset.hours : 0,
|
|
chance: drawsPerHour > 0 ? calculateProbabilityForDrawCount(getEffectiveRate(rate, multiplier) / 100, drawsPerHour * preset.hours) : 0
|
|
}));
|
|
}
|
|
|
|
export function getDurationPresetsForAverageHours(averageHours, occurrenceUnit = "perHour") {
|
|
if (!Number.isFinite(averageHours) || averageHours <= 0) {
|
|
return occurrenceUnit === "perDay" ? RNG_SIMULATOR_DAILY_DURATION_PRESETS : RNG_SIMULATOR_HOURLY_DURATION_PRESETS;
|
|
}
|
|
if (occurrenceUnit === "perDay" && averageHours <= 24 * 365) return RNG_SIMULATOR_MONTHLY_DURATION_PRESETS;
|
|
if (averageHours <= 24 * 7) return RNG_SIMULATOR_HOURLY_DURATION_PRESETS;
|
|
if (averageHours <= 24 * 365) return RNG_SIMULATOR_MONTHLY_DURATION_PRESETS;
|
|
if (averageHours <= 24 * 365 * 10) return RNG_SIMULATOR_YEARLY_DURATION_PRESETS;
|
|
return RNG_SIMULATOR_LONG_DURATION_PRESETS;
|
|
}
|
|
|
|
export function getAveragePositionStatus(averageRatio) {
|
|
const ratio = Number.isFinite(averageRatio) && averageRatio >= 0 ? averageRatio : 0;
|
|
if (ratio < 0.25) return { id: "start", tone: "positive" };
|
|
if (ratio < 0.5) return { id: "farBelow", tone: "positive" };
|
|
if (ratio < 0.9) return { id: "below", tone: "positive" };
|
|
if (ratio < 1.1) return { id: "average", tone: "" };
|
|
if (ratio < 1.5) return { id: "slightlyAbove", tone: "negative" };
|
|
if (ratio < 3) return { id: "high", tone: "negative" };
|
|
return { id: "catastrophic", tone: "negative" };
|
|
}
|
|
|
|
export function calculateRngRealDrawsSummary({ rate, multiplier = 0, realDraws = 0, averageDraws = 0, drawsForChance = {}, costPerDraw = "" }) {
|
|
const cleanRealDraws = clampInteger(realDraws, 0, RNG_SIMULATOR_LIMITS.realDrawsMax, 0);
|
|
const cumulativeChance = calculateAtLeastOneProbability(rate, cleanRealDraws, multiplier);
|
|
const noSuccessChance = 1 - cumulativeChance;
|
|
const cleanAverageDraws = Number.isFinite(averageDraws) && averageDraws > 0 ? averageDraws : 0;
|
|
const averageRatio = cleanAverageDraws > 0 ? cleanRealDraws / cleanAverageDraws : 0;
|
|
const averagePosition = getAveragePositionStatus(averageRatio);
|
|
const drawsUntilAverage = cleanAverageDraws > cleanRealDraws ? Math.ceil(cleanAverageDraws - cleanRealDraws) : 0;
|
|
const cleanCost = Number(normalizeCostString(costPerDraw));
|
|
const hasCost = Number.isFinite(cleanCost) && cleanCost > 0;
|
|
const averageSuccessCost = hasCost && cleanAverageDraws > 0 ? cleanAverageDraws * cleanCost : 0;
|
|
const spentCost = hasCost ? cleanRealDraws * cleanCost : 0;
|
|
const targets = {};
|
|
Object.entries(drawsForChance || {}).forEach(([target, draws]) => {
|
|
const remainingDraws = Number.isFinite(draws) ? Math.max(0, draws - cleanRealDraws) : Number.POSITIVE_INFINITY;
|
|
targets[target] = {
|
|
draws,
|
|
remainingDraws,
|
|
progress: Number.isFinite(draws) && draws > 0 ? Math.min(1, cleanRealDraws / draws) : 0,
|
|
remainingCost: hasCost && Number.isFinite(remainingDraws) ? remainingDraws * cleanCost : 0
|
|
};
|
|
});
|
|
return {
|
|
realDraws: cleanRealDraws,
|
|
hasCost,
|
|
cumulativeChance,
|
|
noSuccessChance,
|
|
averageRatio,
|
|
averagePosition,
|
|
drawsUntilAverage,
|
|
averageExceeded: cleanAverageDraws > 0 && cleanRealDraws >= cleanAverageDraws,
|
|
spentCost,
|
|
averageSuccessCost,
|
|
averageCostDelta: hasCost && cleanAverageDraws > 0 ? spentCost - averageSuccessCost : 0,
|
|
costUntilAverage: hasCost && drawsUntilAverage > 0 ? drawsUntilAverage * cleanCost : 0,
|
|
targets
|
|
};
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
return {
|
|
draws: cleanDraws,
|
|
successes,
|
|
fails: cleanDraws - successes,
|
|
observedRate: cleanDraws > 0 ? (successes / cleanDraws) * 100 : 0,
|
|
longestFailStreak,
|
|
longestSuccessStreak,
|
|
firstSuccessDraw
|
|
};
|
|
}
|
|
|
|
export function addRngSimulationHistory(history, entry) {
|
|
return [entry, ...(Array.isArray(history) ? history : [])].slice(0, RNG_SIMULATOR_HISTORY_LIMIT);
|
|
}
|