Trefferwuerfe: Rechenkette anzeigen, Bearbeitung im Overlay mit einem Feld je Wurf
This commit is contained in:
parent
27a267893e
commit
f9126675b2
5 changed files with 205 additions and 67 deletions
|
|
@ -61,61 +61,108 @@
|
|||
update();
|
||||
});
|
||||
|
||||
// Trefferpunkte: Einzelwürfe live zu TP max zusammenrechnen und auswürfeln
|
||||
const hpInput = document.querySelector('[data-hp-rolls]');
|
||||
if (hpInput) {
|
||||
const totalField = document.querySelector('[data-hp-total]');
|
||||
const label = document.querySelector('[data-hp-label]');
|
||||
const die = parseInt(hpInput.getAttribute('data-hp-die'), 10) || 4;
|
||||
const baseDie = parseInt(hpInput.getAttribute('data-hp-base'), 10) || 4;
|
||||
const slots = parseInt(hpInput.getAttribute('data-hp-slots'), 10) || 1;
|
||||
const con = parseInt(hpInput.getAttribute('data-hp-con'), 10) || 0;
|
||||
const signedText = function (value) {
|
||||
return value > 0 ? '+' + value : String(value);
|
||||
};
|
||||
const parseRolls = function () {
|
||||
return (hpInput.value.match(/[0-9]+/g) || []).map(function (token) {
|
||||
// Trefferpunkte: Rechenkette anzeigen, im Overlay je Wurf ein Feld
|
||||
const hpWidget = document.querySelector('[data-hp-widget]');
|
||||
if (hpWidget) {
|
||||
const hpInput = hpWidget.querySelector('[data-hp-input]');
|
||||
const totalField = hpWidget.querySelector('[data-hp-total]');
|
||||
const sumOut = hpWidget.querySelector('[data-hp-sum]');
|
||||
const dialog = document.querySelector('#hp-rolls-dialog');
|
||||
const dialogSum = dialog ? dialog.querySelector('[data-hp-dialog-sum]') : null;
|
||||
const slots = dialog ? Array.from(dialog.querySelectorAll('[data-hp-slot]')) : [];
|
||||
const die = parseInt(hpWidget.getAttribute('data-hp-die'), 10) || 4;
|
||||
const baseDie = parseInt(hpWidget.getAttribute('data-hp-base'), 10) || 4;
|
||||
const con = parseInt(hpWidget.getAttribute('data-hp-con'), 10) || 0;
|
||||
|
||||
const parse = function (text) {
|
||||
return (String(text || '').match(/[0-9]+/g) || []).map(function (token) {
|
||||
return parseInt(token, 10) || 0;
|
||||
}).slice(0, slots);
|
||||
};
|
||||
const update = function () {
|
||||
const rolls = parseRolls();
|
||||
let sum = 0;
|
||||
const levelDice = [];
|
||||
for (let slot = 0; slot < slots; slot++) {
|
||||
const value = rolls[slot] || 0;
|
||||
if (value > 0) {
|
||||
sum += value;
|
||||
}
|
||||
if (slot > 0) {
|
||||
levelDice.push(value > 0 ? String(value) : '–');
|
||||
}
|
||||
}
|
||||
const total = Math.max(1, sum + con);
|
||||
if (totalField) {
|
||||
totalField.value = String(total);
|
||||
}
|
||||
if (label) {
|
||||
const base = rolls[0] > 0 ? String(rolls[0]) : '–';
|
||||
label.textContent = '1W' + baseDie + ' ' + base + ' · W' + die + ' ' + (levelDice.join(' ') || '–')
|
||||
+ ' · KON ' + signedText(con) + ' = ' + total;
|
||||
}
|
||||
};
|
||||
hpInput.addEventListener('input', update);
|
||||
const rollButton = document.querySelector('[data-hp-roll]');
|
||||
if (rollButton) {
|
||||
rollButton.addEventListener('click', function () {
|
||||
const rolls = parseRolls();
|
||||
const next = [];
|
||||
for (let slot = 0; slot < slots; slot++) {
|
||||
const value = rolls[slot] || 0;
|
||||
next.push(value > 0 ? value : 1 + Math.floor(Math.random() * (slot === 0 ? baseDie : die)));
|
||||
}
|
||||
hpInput.value = next.join(' ');
|
||||
update();
|
||||
});
|
||||
};
|
||||
const values = function () {
|
||||
return slots.map(function (input) {
|
||||
const value = parseInt(input.value, 10);
|
||||
return isNaN(value) || value < 1 ? 0 : value;
|
||||
});
|
||||
};
|
||||
const chainText = function (list) {
|
||||
let sum = 0;
|
||||
const parts = list.map(function (value) {
|
||||
sum += value;
|
||||
return value > 0 ? String(value) : '–';
|
||||
});
|
||||
return { text: parts.join(' + ') + ' = ' + Math.max(1, sum + con), sum: sum };
|
||||
};
|
||||
const paint = function (list) {
|
||||
const info = chainText(list);
|
||||
if (sumOut) {
|
||||
sumOut.textContent = info.text;
|
||||
}
|
||||
if (dialogSum) {
|
||||
dialogSum.textContent = info.text;
|
||||
}
|
||||
if (totalField) {
|
||||
totalField.value = String(Math.max(1, info.sum + con));
|
||||
}
|
||||
};
|
||||
// Kette aus dem gespeicherten Feld in die Eingabefelder des Overlays legen
|
||||
const load = function () {
|
||||
const stored = parse(hpInput ? hpInput.value : '');
|
||||
slots.forEach(function (input, index) {
|
||||
const value = stored[index] || 0;
|
||||
input.value = value > 0 ? String(value) : '';
|
||||
});
|
||||
};
|
||||
load();
|
||||
paint(values());
|
||||
|
||||
slots.forEach(function (input) {
|
||||
input.addEventListener('input', function () {
|
||||
paint(values());
|
||||
});
|
||||
});
|
||||
|
||||
const apply = function () {
|
||||
const list = values();
|
||||
while (list.length > 0 && list[list.length - 1] === 0) {
|
||||
list.pop();
|
||||
}
|
||||
if (hpInput) {
|
||||
hpInput.value = list.join(' ');
|
||||
}
|
||||
paint(list.length > 0 ? list : [0]);
|
||||
if (dialog && dialog.open) {
|
||||
dialog.close();
|
||||
}
|
||||
};
|
||||
if (dialog) {
|
||||
const openButton = hpWidget.querySelector('[data-dialog-open]');
|
||||
if (openButton) {
|
||||
openButton.addEventListener('click', load);
|
||||
}
|
||||
const applyButton = dialog.querySelector('[data-hp-apply]');
|
||||
if (applyButton) {
|
||||
applyButton.addEventListener('click', apply);
|
||||
}
|
||||
// Enter im Overlay übernimmt die Würfe, statt den Charakterbogen abzuschicken
|
||||
dialog.addEventListener('keydown', function (event) {
|
||||
if (event.key === 'Enter') {
|
||||
event.preventDefault();
|
||||
apply();
|
||||
}
|
||||
});
|
||||
const rollButton = dialog.querySelector('[data-hp-roll]');
|
||||
if (rollButton) {
|
||||
rollButton.addEventListener('click', function () {
|
||||
slots.forEach(function (input, index) {
|
||||
if (!input.value) {
|
||||
input.value = String(1 + Math.floor(Math.random() * (index === 0 ? baseDie : die)));
|
||||
}
|
||||
});
|
||||
paint(values());
|
||||
});
|
||||
}
|
||||
}
|
||||
update();
|
||||
}
|
||||
|
||||
// Zauberdetails im Bogen: Klick auf den Namen füllt den Detail-Schirm
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue