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:
parent
b4d4c53dd4
commit
929aa52c73
10 changed files with 86 additions and 30 deletions
|
|
@ -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,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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');
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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 === '') {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue