Charakterverwalter: Fertigkeiten-Attribut für eigene Einträge, Stufe ohne Vorzeichen

- eigenedefinierte Fertigkeiten bekommen ein Bezugsattribut (STR…CHA) im
  Selektor und lassen es im Bearbeiten-Abschnitt korrigieren
- neue Spalte character_skills.attribute (Migration 2, schema.sql angepasst);
  character_skills() nutzt COALESCE aus Zeilen- und Katalogattribut
- Fertigkeitsstufe startet bei max(0, Attributsbonus) und wird in Formular,
  Update und Anzeige (Web-Bogen, PDF) nie negativ bzw. nie mit Vorzeichen gezeigt
- MOD- und Stufe-Spalte im Bogen auf gemeinsame Grundlinie gezogen
This commit is contained in:
torsten 2026-11-21 14:05:51 +01:00
parent b4d4c53dd4
commit 929aa52c73
10 changed files with 86 additions and 30 deletions

View file

@ -152,7 +152,7 @@ function character_seed_skills(int $characterId): int
$count = 0;
foreach (skills_catalog() as $skill) {
$attribute = trim((string) ($skill['attribute'] ?? ''));
$bonus = $attribute === '' ? 0 : attribute_bonus((int) ($character['attr_' . strtolower($attribute)] ?? 10));
$bonus = $attribute === '' ? 0 : max(0, attribute_bonus((int) ($character['attr_' . strtolower($attribute)] ?? 10)));
$insert->execute([$characterId, (int) $skill['id'], (string) $skill['name'], $bonus]);
$count++;
}
@ -170,23 +170,15 @@ function character_reset_skills(int $characterId, bool $force = false): int
if ($character === null) {
return 0;
}
$catalog = [];
foreach (skills_catalog() as $skill) {
$catalog[(int) $skill['id']] = $skill;
}
$changed = 0;
$update = db()->prepare('UPDATE character_skills SET bonus = ? WHERE id = ?');
foreach (character_skills($characterId) as $row) {
$skill = $catalog[(int) ($row['skill_id'] ?? 0)] ?? null;
if ($skill === null) {
continue;
}
$attribute = trim((string) ($skill['attribute'] ?? ''));
$attribute = trim((string) ($row['attribute'] ?? ''));
if ($attribute === '') {
continue;
}
$target = attribute_bonus((int) ($character['attr_' . strtolower($attribute)] ?? 10));
if ($force || (int) $row['bonus'] === $target) {
$target = max(0, attribute_bonus((int) ($character['attr_' . strtolower($attribute)] ?? 10)));
if ($force || (int) $row['bonus'] === $target || (int) $row['bonus'] < 0) {
$update->execute([$target, (int) $row['id']]);
$changed++;
}
@ -326,15 +318,18 @@ function skills_catalog_delete(int $id): void
function character_skills(int $characterId): array
{
$stmt = db()->prepare(
'SELECT cs.*, s.attribute, s.category FROM character_skills cs
"SELECT cs.id, cs.character_id, cs.skill_id, cs.name, cs.bonus, cs.note,
COALESCE(NULLIF(cs.attribute, ''), s.attribute) AS attribute,
s.category
FROM character_skills cs
LEFT JOIN skills s ON s.id = cs.skill_id
WHERE cs.character_id = ? ORDER BY cs.name'
WHERE cs.character_id = ? ORDER BY cs.name"
);
$stmt->execute([$characterId]);
return $stmt->fetchAll();
}
function character_skill_add(int $characterId, ?int $skillId, string $name, int $bonus, string $note): void
function character_skill_add(int $characterId, ?int $skillId, string $name, int $bonus, string $note, string $attribute = ''): void
{
if ($skillId !== null && $name === '') {
$stmt = db()->prepare('SELECT name FROM skills WHERE id = ?');
@ -345,9 +340,16 @@ function character_skill_add(int $characterId, ?int $skillId, string $name, int
return;
}
db()->prepare(
'INSERT INTO character_skills (character_id, skill_id, name, bonus, note) VALUES (?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE bonus = VALUES(bonus), note = VALUES(note), skill_id = VALUES(skill_id)'
)->execute([$characterId, $skillId, shorten($name, 80), $bonus, shorten($note, 255)]);
'INSERT INTO character_skills (character_id, skill_id, name, attribute, bonus, note) VALUES (?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE attribute = VALUES(attribute), bonus = VALUES(bonus), note = VALUES(note), skill_id = VALUES(skill_id)'
)->execute([
$characterId,
$skillId,
shorten($name, 80),
skill_attribute_code($attribute),
max(0, min(30, $bonus)),
shorten($note, 255),
]);
}
function character_skill_delete(int $characterId, int $id): void
@ -647,10 +649,16 @@ function character_armor_toggle(int $characterId, int $id): void
->execute([$id, $characterId]);
}
function character_skill_update(int $characterId, int $id, int $bonus, string $note): void
function character_skill_update(int $characterId, int $id, int $bonus, string $note, ?string $attribute = null): void
{
db()->prepare('UPDATE character_skills SET bonus = ?, note = ? WHERE id = ? AND character_id = ?')
->execute([max(-10, min(30, $bonus)), shorten($note, 255), $id, $characterId]);
db()->prepare('UPDATE character_skills SET attribute = COALESCE(?, attribute), bonus = ?, note = ? WHERE id = ? AND character_id = ?')
->execute([
$attribute === null ? null : skill_attribute_code($attribute),
max(0, min(30, $bonus)),
shorten($note, 255),
$id,
$characterId,
]);
}
/**

View file

@ -21,6 +21,10 @@ const WS_MIGRATIONS = [
1 => [
'ALTER TABLE spells ADD COLUMN IF NOT EXISTS body LONGTEXT NULL AFTER summary',
],
// 2: Eigene Fertigkeiten tragen ihr Bezugsattribut direkt am Charakterbogen.
2 => [
"ALTER TABLE character_skills ADD COLUMN IF NOT EXISTS attribute VARCHAR(3) NOT NULL DEFAULT '' AFTER name",
],
];
function ws_migrations_apply(PDO $pdo): void

View file

@ -142,7 +142,8 @@ function page_handle(string $method, string $path): void
$skillId > 0 ? $skillId : null,
post_string('name'),
post_int('bonus', 0),
post_string('note')
post_string('note'),
post_string('attribute')
);
flash('success', 'Fertigkeit eingetragen.');
} else {
@ -161,7 +162,13 @@ function page_handle(string $method, string $path): void
if (preg_match('#^/characters/(\d+)/skills/(\d+)$#', $path, $m) === 1) {
require_post();
$character = character_require((int) $m[1], $user);
character_skill_update((int) $character['id'], (int) $m[2], post_int('bonus', 0), post_string('note'));
character_skill_update(
(int) $character['id'],
(int) $m[2],
post_int('bonus', 0),
post_string('note'),
array_key_exists('attribute', $_POST) ? post_string('attribute') : null
);
flash('success', 'Fertigkeit gespeichert.');
redirect('/characters/' . $character['id'] . '/edit#fertigkeiten');
}

View file

@ -117,7 +117,21 @@ function bonus_class(int $value): string
return $value > 0 ? 'bonus-pos' : ($value < 0 ? 'bonus-neg' : 'bonus-zero');
}
/** Rettungswurf-Feld formatieren; leere Werte bleiben ein Gedankenstrich. */
/** Attributkürzel (STR, DEX, …) prüfen; unbekannte Werte werden zu ''. */
function skill_attribute_code(string $value): string
{
$code = strtoupper(trim($value));
foreach (WS_ATTRIBUTES as $attribute) {
if ($attribute[0] === $code) {
return $code;
}
}
return '';
}
/**
* Rettungswurf-Feld formatieren; leere Werte bleiben ein Gedankenstrich.
*/
function save_display(mixed $value): string
{
if ($value === null || $value === '') {

View file

@ -276,7 +276,7 @@ function character_pdf(array $c, array $extra): string
$pdf->text($cx, $y + 8.5, 8.4, 'F3', $ink, shorten((string) $skill['name'], 34));
$pdf->text($cx + $colW - 64, $y + 8.2, 6.6, 'F1', $soft, trim((string) ($skill['attribute'] ?? '')));
$pdf->textRight($cx + $colW - 28, $y + 8.2, 6.8, 'F1', $soft, $mod === null ? '–' : signed($mod));
$pdf->textRight($cx + $colW, $y + 8.5, 8.4, 'F2', $gold, signed((int) $skill['bonus']));
$pdf->textRight($cx + $colW, $y + 8.5, 8.4, 'F2', $gold, (string) max(0, (int) $skill['bonus']));
$pdf->line($cx, $y + 10.4, $cx + $colW, $y + 10.4, $goldLight, 0.22);
}
$y += 12.6;

View file

@ -795,6 +795,9 @@ input[type="file"] {
.skill-group:first-of-type { margin-top: 0.6rem; }
.skill-table td.skill-name { min-width: 12rem; white-space: nowrap; }
.skill-table th.num, .skill-table td.num { width: 4.5rem; }
/* Zahlenzellen einer Zeile auf eine gemeinsame Grundlinie ziehen, damit
MOD (regulär) und STUFE (fett) exakt auf gleicher Höhe stehen. */
.skill-table td.num { vertical-align: baseline; }
/* --- Boni: positiv grün, null grau, negativ rot ------------------------- */
.bonus-pos { color: #79d29a; }

View file

@ -139,6 +139,7 @@ CREATE TABLE IF NOT EXISTS character_skills (
character_id BIGINT UNSIGNED NOT NULL,
skill_id BIGINT UNSIGNED NULL,
name VARCHAR(80) NOT NULL,
attribute VARCHAR(3) NOT NULL DEFAULT '',
bonus SMALLINT NOT NULL DEFAULT 0,
note VARCHAR(255) NOT NULL DEFAULT '',
PRIMARY KEY (id),

View file

@ -69,9 +69,17 @@ uksort($grouped, static function (string $a, string $b) use ($order): int {
<span>Name</span>
<input type="text" name="name" maxlength="80" placeholder="z. B. Kochen" required>
</label>
<label class="field" style="max-width:8rem">
<span>Attribut</span>
<select name="attribute">
<?php foreach (WS_ATTRIBUTES as [$short, $label]): ?>
<option value="<?= e($short) ?>"><?= e($short) ?> · <?= e($label) ?></option>
<?php endforeach; ?>
</select>
</label>
<label class="field" style="max-width:7rem">
<span>Bonus</span>
<input type="number" name="bonus" value="0" min="-10" max="20">
<span>Stufe</span>
<input type="number" name="bonus" value="0" min="0" max="20">
</label>
<label class="field grow">
<span>Notiz</span>

View file

@ -87,7 +87,8 @@ $base = url('/characters/' . $cid);
<section class="panel" id="fertigkeiten">
<h2>Fertigkeiten</h2>
<p class="muted">
Startwert jeder Fertigkeit ist der Attributsbonus (Fertigkeitsstufe, Hausregel V1.1);
Startwert jeder Fertigkeit ist der Attributsbonus (Fertigkeitsstufe, Hausregel V1.1),
mindestens jedoch 0 – ein negativer Attributsmodifikator drückt die Stufe nie unter 0.
Steigerungen trägst du rechts ein. Eigene Fertigkeiten sind zusätzlich erlaubt.
</p>
<form method="post" action="<?= e($base . '/skills/reset') ?>" class="inline-form" style="margin-bottom:.8rem">
@ -124,9 +125,19 @@ $base = url('/characters/' . $cid);
<div class="inline-form">
<form method="post" action="<?= e($base . '/skills/' . (int) $skill['id']) ?>" class="inline-form">
<?= csrf_field() ?>
<?php if ((int) ($skill['skill_id'] ?? 0) === 0): ?>
<label class="field" style="max-width:8rem">
<span class="visually-quiet">Attribut</span>
<select name="attribute">
<?php foreach (WS_ATTRIBUTES as [$short, $label]): ?>
<option value="<?= e($short) ?>"<?= strtoupper(trim((string) ($skill['attribute'] ?? ''))) === $short ? ' selected' : '' ?>><?= e($short) ?> · <?= e($label) ?></option>
<?php endforeach; ?>
</select>
</label>
<?php endif; ?>
<label class="field" style="max-width:6rem">
<span class="visually-quiet">Stufe</span>
<input type="number" name="bonus" value="<?= (int) $skill['bonus'] ?>" min="-10" max="30">
<input type="number" name="bonus" value="<?= (int) $skill['bonus'] ?>" min="0" max="30">
</label>
<label class="field grow">
<span class="visually-quiet">Notiz</span>

View file

@ -169,7 +169,7 @@ $equipped = array_values(array_filter($items, static fn ($item) => (int) $item['
<td class="skill-name"><?= e((string) $skill['name']) ?></td>
<td class="muted"><?= e((string) ($skill['attribute'] ?? '')) ?></td>
<td class="num <?= $mod === null ? 'muted' : bonus_class($mod) ?>"><?= $mod === null ? '–' : e(signed($mod)) ?></td>
<td class="num <?= bonus_class((int) $skill['bonus']) ?>"><b><?= e(signed((int) $skill['bonus'])) ?></b></td>
<td class="num <?= bonus_class((int) $skill['bonus']) ?>"><b><?= max(0, (int) $skill['bonus']) ?></b></td>
<td class="muted"><?= e((string) $skill['note']) ?></td>
</tr>
<?php endforeach; ?>