Organize app script sections

This commit is contained in:
Jan Bader
2026-06-17 22:31:34 +02:00
parent 929cbc6bdd
commit f763c9794c
+32
View File
@@ -1,3 +1,7 @@
/* ==========================================================================
Data definitions and shared helpers
========================================================================== */
const EFFECTS = [ const EFFECTS = [
{ id: "critChance", label: "Crit chance", cap: 12, group: "offense" }, { id: "critChance", label: "Crit chance", cap: 12, group: "offense" },
{ id: "critDamage", label: "Crit damage", cap: 80, group: "offense" }, { id: "critDamage", label: "Crit damage", cap: 80, group: "offense" },
@@ -55,6 +59,10 @@ const effectById = (id) => EFFECTS.find((effect) => effect.id === id);
let state = loadDraft() || createDefaultState(); let state = loadDraft() || createDefaultState();
/* ==========================================================================
State creation and normalization
========================================================================== */
function createDefaultState() { function createDefaultState() {
return { return {
profile: "lifesteal", profile: "lifesteal",
@@ -166,6 +174,10 @@ function clampEffectValue(type, value) {
return Math.min(effect.cap, Math.max(1, Number.isFinite(value) ? value : 1)); return Math.min(effect.cap, Math.max(1, Number.isFinite(value) ? value : 1));
} }
/* ==========================================================================
Rendering
========================================================================== */
function render() { function render() {
renderControls(); renderControls();
renderGear(); renderGear();
@@ -370,6 +382,10 @@ function updateSwapControls() {
$("#swapStatus").textContent = "Press Swap again to exchange back."; $("#swapStatus").textContent = "Press Swap again to exchange back.";
} }
/* ==========================================================================
Replacement comparison and swap helpers
========================================================================== */
function calculateTotals() { function calculateTotals() {
const totals = { baseDamage: 0, baseHealth: 0, effects: Object.fromEntries(EFFECTS.map((effect) => [effect.id, 0])) }; const totals = { baseDamage: 0, baseHealth: 0, effects: Object.fromEntries(EFFECTS.map((effect) => [effect.id, 0])) };
for (const piece of state.pieces) { for (const piece of state.pieces) {
@@ -490,6 +506,10 @@ function createNewItemFromPiece(piece, target, targetPieceId = null) {
}; };
} }
/* ==========================================================================
Calculations and scoring
========================================================================== */
function formatDelta(value, suffix = "") { function formatDelta(value, suffix = "") {
const sign = value > 0 ? "+" : ""; const sign = value > 0 ? "+" : "";
return `${sign}${fmt(value)}${suffix}`; return `${sign}${fmt(value)}${suffix}`;
@@ -669,6 +689,10 @@ function renderSacrifices(pieceScores) {
`).join(""); `).join("");
} }
/* ==========================================================================
Persistence, save slots, import, and export
========================================================================== */
function saveDraft() { function saveDraft() {
localStorage.setItem(DRAFT_KEY, JSON.stringify(state)); localStorage.setItem(DRAFT_KEY, JSON.stringify(state));
} }
@@ -801,6 +825,10 @@ function importUploadedJson(event) {
reader.readAsText(file); reader.readAsText(file);
} }
/* ==========================================================================
Event handling
========================================================================== */
function bindEvents() { function bindEvents() {
$("#profileSelect").addEventListener("change", (event) => { $("#profileSelect").addEventListener("change", (event) => {
state.profile = event.target.value; state.profile = event.target.value;
@@ -921,5 +949,9 @@ function escapeHtml(value) {
return String(value).replace(/[&<>"]/g, (char) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[char])); return String(value).replace(/[&<>"]/g, (char) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;" }[char]));
} }
/* ==========================================================================
Bootstrap
========================================================================== */
bindEvents(); bindEvents();
render(); render();