Trefferwuerfe: Rechenkette anzeigen, Bearbeitung im Overlay mit einem Feld je Wurf

This commit is contained in:
torsten 2026-11-22 19:12:26 +01:00
parent 27a267893e
commit f9126675b2
5 changed files with 205 additions and 67 deletions

View file

@ -64,8 +64,11 @@ zauberturm.de: Nachtblau/Schwarz/Gold, Pergament, Animationen.
W6, Dieb/Magier W4) + Konstitutionsbonus (einmal, Hausregel V1.1). Die Würfe
liegen kompakt in `characters.hp_rolls` („3 7 5 2“), `hp_max` wird beim
Speichern daraus berechnet (leer = Altdaten, dann gilt `hp_max` manuell).
Formular: ein Textfeld + „Würfeln“-Knopf (füllt fehlende Würfe), Live-Summe;
Bogen/PDF zeigen die Kette „1W4 3 · W8 7 5 2 · KON +1“ (`hp_rolls_label()`).
Formular: Rechenkette „3 + 4 + 7 + 8 = 25“ als Knopf (`hp_rolls_sum()`);
Klick öffnet das Overlay `#hp-rolls-dialog` mit einem Feld je Wurf (Stufe 0
= 1W4 …), „Fehlende würfeln“ und „Übernehmen“ schreiben das hidden Feld
`hp_rolls` (`assets/app.js`). Bogen/PDF zeigen die Kette als
„1W4 3 · W8 7 5 2 · KON +1“ (`hp_rolls_label()`).
- **Ableitungen im Bogen:** Trefferwurf = Grundangriff + Attributsbonus
(Nahkampf STR, Fernkampf DEX) + Waffenbonus; Schaden = Würfel + Bonus;
RK = 10 + DEX-Bonus + angelegte Rüstung (Feld `characters.ac` = manueller

View file

@ -497,6 +497,23 @@ function hp_max_for(array $character): int
return hp_breakdown($character)['total'];
}
/**
* Kurze Rechenkette für das Formular: „3 + 4 + 7 + 8 = 25“.
* Fehlende Würfe erscheinen als „–“; der Konstitutionsbonus steckt im Ergebnis.
*/
function hp_rolls_sum(array $character): string
{
$info = hp_breakdown($character);
if ($info['manual']) {
return 'noch keine Würfe erfasst';
}
$parts = [$info['base'] === null ? '–' : (string) $info['base']];
foreach ($info['rolls'] as $value) {
$parts[] = $value === null ? '–' : (string) $value;
}
return implode(' + ', $parts) . ' = ' . (int) $info['total'];
}
/**
* Kompakte Klartextkette der Würfe für Bogen und PDF:
* „1W4 3 · W8 7 5 2 · KON +1“ (fehlende Würfe erscheinen als „–“).

View file

@ -821,6 +821,39 @@ input[type="file"] {
.stat .b.bonus-zero { color: var(--parchment-dim); }
.stat .b.bonus-neg { color: #e0756a; }
/* --- Trefferwürfe: Rechenkette als Feld, Overlay zum Bearbeiten --------- */
.hp-sum {
display: flex;
align-items: baseline;
justify-content: space-between;
gap: 0.6rem;
width: 100%;
padding: 0.55rem 0.7rem;
border: 1px solid rgba(201, 162, 39, 0.35);
border-radius: var(--radius);
background: rgba(6, 9, 16, 0.55);
color: var(--parchment);
font: inherit;
font-variant-numeric: tabular-nums;
letter-spacing: 0.02em;
text-align: left;
cursor: pointer;
}
.hp-sum:hover,
.hp-sum:focus-visible {
border-color: var(--gold);
background: rgba(201, 162, 39, 0.12);
}
.hp-sum small { color: var(--parchment-dim); font-size: 0.72rem; white-space: nowrap; }
.hp-roll-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(6.5rem, 1fr));
gap: 0.6rem;
}
.hp-roll-grid .field { margin: 0; }
.hp-roll-grid input { text-align: center; font-variant-numeric: tabular-nums; }
dialog.hp-rolls-dialog { width: min(34rem, 94vw); }
/* --- Selektor-Schirm für Fertigkeiten und Zauber ------------------------ */
.selector-actions { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; }

View file

@ -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

View file

@ -54,8 +54,15 @@
</label>
</div>
<div class="grid-4">
<?php $hp = hp_breakdown($values); ?>
<?php
// Trefferpunkte: Maximalwert ist aus den Einzelwürfen berechnet. Angezeigt
// wird die Rechenkette („3 + 4 + 7 + 8 = 25“); bearbeitet wird sie in einem
// Overlay mit einem Feld je Wurf (Stufe 0: 1W4, dann je Stufe 1W<Klassenwürfel>).
$hp = hp_breakdown($values);
$hpRolls = hp_rolls_parse($values['hp_rolls'] ?? '');
$hpSlotValue = static fn (int $slot): string => ((int) ($hpRolls[$slot] ?? 0)) > 0 ? (string) (int) $hpRolls[$slot] : '';
?>
<div class="grid-4" data-hp-widget data-hp-die="<?= (int) $hp['die'] ?>" data-hp-base="<?= (int) WS_HP_LEVEL0_DIE ?>" data-hp-con="<?= (int) $hp['con'] ?>">
<label class="field">
<span>TP aktuell</span>
<input type="number" name="hp_current" value="<?= (int) $values['hp_current'] ?>">
@ -64,20 +71,19 @@
<span>TP maximal <small class="muted">(berechnet)</small></span>
<input type="text" data-hp-total value="<?= (int) $hp['total'] ?>" readonly
title="1W<?= (int) WS_HP_LEVEL0_DIE ?> (Stufe 0) + 1W<?= (int) $hp['die'] ?> je Stufe + Konstitutionsbonus">
<small class="muted">
1W<?= (int) WS_HP_LEVEL0_DIE ?> + <?= (int) $hp['level'] ?>× 1W<?= (int) $hp['die'] ?> <?= e(signed((int) $hp['con'])) ?> KON
</small>
<small class="muted">1W<?= (int) WS_HP_LEVEL0_DIE ?> + <?= (int) $hp['level'] ?>× 1W<?= (int) $hp['die'] ?> <?= e(signed((int) $hp['con'])) ?> KON</small>
</label>
<div class="field" style="grid-column: span 2;">
<span>Trefferwürfe <small class="muted">Stufe 0 (1W<?= (int) WS_HP_LEVEL0_DIE ?>), dann Stufe 1 … <?= (int) $hp['level'] ?></small></span>
<div style="display:flex; gap:.4rem; align-items:center;">
<input type="text" name="hp_rolls" data-hp-rolls value="<?= e(hp_rolls_format(hp_rolls_parse($values['hp_rolls']))) ?>"
inputmode="numeric" placeholder="z. B. 3 7 5 2" style="flex:1 1 auto; min-width:0;"
data-hp-die="<?= (int) $hp['die'] ?>" data-hp-base="<?= (int) WS_HP_LEVEL0_DIE ?>"
data-hp-slots="<?= (int) $hp['slots'] ?>" data-hp-con="<?= (int) $hp['con'] ?>">
<button class="btn" type="button" data-hp-roll title="Fehlende Würfe auswürfeln">Würfeln</button>
</div>
<small class="muted" data-hp-label><?= e(hp_rolls_label($values)) ?> = <?= (int) $hp['total'] ?></small>
<input type="hidden" name="hp_rolls" data-hp-input value="<?= e(hp_rolls_format($hpRolls)) ?>">
<button class="hp-sum" type="button" data-dialog-open="#hp-rolls-dialog" title="Einzelne Würfe bearbeiten">
<span data-hp-sum><?= e(hp_rolls_sum($values)) ?></span>
<small>bearbeiten</small>
</button>
<small class="muted">
<?= (int) $hp['con'] === 0 ? 'ohne Konstitutionsbonus' : 'Konstitutionsbonus ' . e(signed((int) $hp['con'])) . ' eingerechnet' ?>
· fehlende Würfe zählen nicht mit
</small>
</div>
<label class="field">
<span>Rüstungsklasse (leer = automatisch)</span>
@ -182,3 +188,35 @@
<textarea name="notes" rows="3" maxlength="4000"><?= e((string) $values['notes']) ?></textarea>
</label>
</div>
<dialog class="selector hp-rolls-dialog" id="hp-rolls-dialog" aria-labelledby="hp-rolls-dialog-title">
<div class="selector-head">
<h2 id="hp-rolls-dialog-title">Trefferwürfe</h2>
<button class="btn btn-small" type="button" data-dialog-close>Schließen</button>
</div>
<div class="selector-body">
<p class="selector-hint muted" style="margin-top:0;">
Stufe 0: 1W<?= (int) WS_HP_LEVEL0_DIE ?> · je Stufe 1W<?= (int) $hp['die'] ?>
· Konstitutionsbonus <?= e(signed((int) $hp['con'])) ?> (einmal, Hausregel V1.1)
</p>
<div class="hp-roll-grid">
<label class="field">
<span>Stufe 0 <small class="muted">1W<?= (int) WS_HP_LEVEL0_DIE ?></small></span>
<input type="number" inputmode="numeric" min="1" max="<?= (int) WS_HP_LEVEL0_DIE ?>"
data-hp-slot="0" value="<?= e($hpSlotValue(0)) ?>">
</label>
<?php for ($slot = 1; $slot <= (int) $hp['level']; $slot++): ?>
<label class="field">
<span>Stufe <?= $slot ?> <small class="muted">1W<?= (int) $hp['die'] ?></small></span>
<input type="number" inputmode="numeric" min="1" max="<?= (int) $hp['die'] ?>"
data-hp-slot="<?= $slot ?>" value="<?= e($hpSlotValue($slot)) ?>">
</label>
<?php endfor; ?>
</div>
<div class="selector-actions" style="margin-top:0.9rem;">
<button class="btn" type="button" data-hp-roll>Fehlende würfeln</button>
<button class="btn btn-primary" type="button" data-hp-apply>Übernehmen</button>
<span class="muted" data-hp-dialog-sum><?= e(hp_rolls_sum($values)) ?></span>
</div>
</div>
</dialog>