From 929aa52c732ed2d4c91b5843e530ed399289d4b7 Mon Sep 17 00:00:00 2001 From: torsten Date: Sat, 21 Nov 2026 14:05:51 +0100 Subject: [PATCH] =?UTF-8?q?Charakterverwalter:=20Fertigkeiten-Attribut=20f?= =?UTF-8?q?=C3=BCr=20eigene=20Eintr=C3=A4ge,=20Stufe=20ohne=20Vorzeichen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- westreiche_website/app/character.php | 50 +++++++++++-------- westreiche_website/app/migrations.php | 4 ++ westreiche_website/app/pages.php | 11 +++- westreiche_website/app/rules.php | 16 +++++- westreiche_website/app/sheet.php | 2 +- westreiche_website/assets/app.css | 3 ++ westreiche_website/sql/schema.sql | 1 + westreiche_website/views/_selector_skills.php | 12 ++++- westreiche_website/views/character_edit.php | 15 +++++- westreiche_website/views/character_sheet.php | 2 +- 10 files changed, 86 insertions(+), 30 deletions(-) diff --git a/westreiche_website/app/character.php b/westreiche_website/app/character.php index 7026279..378aed6 100644 --- a/westreiche_website/app/character.php +++ b/westreiche_website/app/character.php @@ -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, + ]); } /** diff --git a/westreiche_website/app/migrations.php b/westreiche_website/app/migrations.php index 0b34a34..e153e96 100644 --- a/westreiche_website/app/migrations.php +++ b/westreiche_website/app/migrations.php @@ -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 diff --git a/westreiche_website/app/pages.php b/westreiche_website/app/pages.php index d885c35..fff7903 100644 --- a/westreiche_website/app/pages.php +++ b/westreiche_website/app/pages.php @@ -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'); } diff --git a/westreiche_website/app/rules.php b/westreiche_website/app/rules.php index 2cbd71e..9749da8 100644 --- a/westreiche_website/app/rules.php +++ b/westreiche_website/app/rules.php @@ -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 === '') { diff --git a/westreiche_website/app/sheet.php b/westreiche_website/app/sheet.php index 1014e8d..5730c75 100644 --- a/westreiche_website/app/sheet.php +++ b/westreiche_website/app/sheet.php @@ -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; diff --git a/westreiche_website/assets/app.css b/westreiche_website/assets/app.css index 8fcaf10..a10380c 100644 --- a/westreiche_website/assets/app.css +++ b/westreiche_website/assets/app.css @@ -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; } diff --git a/westreiche_website/sql/schema.sql b/westreiche_website/sql/schema.sql index 8e4cc87..70639b7 100644 --- a/westreiche_website/sql/schema.sql +++ b/westreiche_website/sql/schema.sql @@ -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), diff --git a/westreiche_website/views/_selector_skills.php b/westreiche_website/views/_selector_skills.php index 828710d..929e34d 100644 --- a/westreiche_website/views/_selector_skills.php +++ b/westreiche_website/views/_selector_skills.php @@ -69,9 +69,17 @@ uksort($grouped, static function (string $a, string $b) use ($order): int { Name +