// Export Web/SCORM (voir le plan "port complet du runtime jouable côté // navigateur") — port fidèle de screens/data_actions/compute_operation.py, // utilisé UNIQUEMENT quand window.FORGE_OFFLINE est vrai (paquet exporté, // aucun serveur disponible) — voir apply-actions.js, les deux seuls // endroits qui en ont besoin. Toute divergence avec la version Python doit // être volontaire et commentée (ex. modulo/int()). // int() Python tronque vers zéro — Math.trunc fait exactement ça // (contrairement à Math.floor, qui arrondit vers -l'infini). function forgePyInt(n) { return Math.trunc(n); } // Le signe du résultat de "%" suit le DIVISEUR en Python, le DIVIDENDE en // JS — sans ce correctif, "modulo" donnerait un résultat différent de // compute_operation.py dès qu'une des deux valeurs est négative. function forgePyMod(a, b) { return ((a % b) + b) % b; } function forgeComputeNewValue(operation, current, rawValue, isDecimal) { var round = function (v) { return isDecimal ? v : forgePyInt(v); }; if (operation === "definir_texte") return rawValue || ""; if (operation === "definir_nombre") { var n = parseFloat(rawValue); return isNaN(n) ? 0 : round(n); } if (operation === "definir_bool_vrai") return 1; if (operation === "definir_bool_faux") return 0; if (operation === "basculer_bool") return current ? 0 : 1; if (operation === "incrementer" || operation === "decrementer") { var amount = (rawValue === null || rawValue === undefined || rawValue === "") ? 0 : parseFloat(rawValue); if (isNaN(amount)) amount = 0; if (operation === "decrementer") amount = -amount; return round((current || 0) + amount); } if (operation === "multiplier") { var factor = (rawValue === null || rawValue === undefined || rawValue === "") ? 1 : parseFloat(rawValue); if (isNaN(factor)) factor = 1; return round((current || 0) * factor); } if (operation === "diviser" || operation === "modulo") { var divisor = (rawValue === null || rawValue === undefined || rawValue === "") ? 0 : parseFloat(rawValue); if (isNaN(divisor)) divisor = 0; if (divisor === 0) return current; var result = operation === "diviser" ? (current || 0) / divisor : forgePyMod(current || 0, divisor); return round(result); } if (operation === "minimum" || operation === "maximum") { var bound = parseFloat(rawValue); if (isNaN(bound)) return current; var res = operation === "minimum" ? Math.max(current || 0, bound) : Math.min(current || 0, bound); return round(res); } if (operation === "alea") { var parts = String(rawValue || "").split(","); var lo = parseFloat(parts[0]), hi = parseFloat(parts[1]); if (isNaN(lo)) lo = 0; if (isNaN(hi)) hi = 0; if (lo > hi) { var tmp = lo; lo = hi; hi = tmp; } if (isDecimal) return Math.random() * (hi - lo) + lo; var iLo = Math.trunc(lo), iHi = Math.trunc(hi); return Math.floor(Math.random() * (iHi - iLo + 1)) + iLo; } throw new Error("opération inconnue : " + operation); } // static/js/play/__tests__/ (node:test) — même patron que conditions.js. if (typeof module !== 'undefined' && module.exports) { module.exports = { forgeComputeNewValue, forgePyInt, forgePyMod }; }