Fertigkeiten: Praxispunkte (10 Kaestchen) und Wuergel je Fertigkeitsstufe in Bogen und PDF

This commit is contained in:
torsten 2026-11-22 19:31:30 +01:00
parent 3a5d366aba
commit 85fd24a0f0
10 changed files with 185 additions and 7 deletions

View file

@ -344,7 +344,7 @@ function skills_catalog_delete(int $id): void
function character_skills(int $characterId): array
{
$stmt = db()->prepare(
"SELECT cs.id, cs.character_id, cs.skill_id, cs.name, cs.bonus, cs.note,
"SELECT cs.id, cs.character_id, cs.skill_id, cs.name, cs.bonus, cs.note, cs.pp,
COALESCE(NULLIF(cs.attribute, ''), s.attribute) AS attribute,
s.category
FROM character_skills cs
@ -378,6 +378,35 @@ function character_skill_add(int $characterId, ?int $skillId, string $name, int
]);
}
/**
* Praxispunkte einer Fertigkeit setzen (0 … WS_SKILL_PRACTICE_SLOTS).
*
* Klick auf das zuletzt gefüllte Kästchen nimmt den Punkt wieder weg, deshalb
* toggelt die Funktion bei gleichem Wert auf eins weniger.
*
* @return int gespeicherter Wert
*/
function character_skill_set_pp(int $characterId, int $id, int $pp): int
{
$current = character_skill_pp($characterId, $id);
$pp = max(0, min(WS_SKILL_PRACTICE_SLOTS, $pp));
if ($current !== null && $pp === $current) {
$pp = max(0, $pp - 1);
}
db()->prepare('UPDATE character_skills SET pp = ? WHERE id = ? AND character_id = ?')
->execute([$pp, $id, $characterId]);
return $pp;
}
/** Gespeicherte Praxispunkte einer Fertigkeit; null, wenn sie nicht existiert. */
function character_skill_pp(int $characterId, int $id): ?int
{
$stmt = db()->prepare('SELECT pp FROM character_skills WHERE id = ? AND character_id = ? LIMIT 1');
$stmt->execute([$id, $characterId]);
$value = $stmt->fetchColumn();
return $value === false ? null : (int) $value;
}
function character_skill_delete(int $characterId, int $id): void
{
db()->prepare('DELETE FROM character_skills WHERE id = ? AND character_id = ?')->execute([$id, $characterId]);

View file

@ -65,6 +65,10 @@ const WS_MIGRATIONS = [
6 => [
"ALTER TABLE characters ADD COLUMN IF NOT EXISTS hp_rolls VARCHAR(160) NOT NULL DEFAULT '' AFTER hp_max",
],
// 7: Praxispunkte je Fertigkeit (Bogen-Spalte „PP", 0 … 10 ankreuzbar).
7 => [
'ALTER TABLE character_skills ADD COLUMN IF NOT EXISTS pp TINYINT UNSIGNED NOT NULL DEFAULT 0 AFTER note',
],
];
function ws_migrations_apply(PDO $pdo): void

View file

@ -132,6 +132,29 @@ function page_handle(string $method, string $path): void
flash('info', '„' . $character['name'] . '“ wurde gelöscht.');
redirect('/');
}
if (preg_match('#^/characters/(\d+)/skills/(\d+)/pp$#', $path, $m) === 1) {
require_post();
$character = character_require((int) $m[1], $user);
$wantsJson = strtolower((string) ($_SERVER['HTTP_X_REQUESTED_WITH'] ?? '')) === 'fetch';
if (!csrf_valid()) {
if ($wantsJson) {
http_response_code(419);
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok' => false, 'error' => 'Sitzung abgelaufen.'], JSON_UNESCAPED_UNICODE);
return;
}
flash('error', 'Die Sitzung ist abgelaufen. Bitte erneut versuchen.');
redirect('/characters/' . $character['id'] . '#fertigkeiten');
}
$pp = max(0, min(WS_SKILL_PRACTICE_SLOTS, post_int('pp', 0)));
$pp = character_skill_set_pp((int) $character['id'], (int) $m[2], $pp);
if ($wantsJson) {
header('Content-Type: application/json; charset=utf-8');
echo json_encode(['ok' => true, 'pp' => $pp], JSON_UNESCAPED_UNICODE);
return;
}
redirect('/characters/' . $character['id'] . '#fertigkeiten');
}
if (preg_match('#^/characters/(\d+)/skills/(add|\d+/delete)$#', $path, $m) === 1) {
require_post();
$character = character_require((int) $m[1], $user);

View file

@ -111,6 +111,16 @@ const WS_SKILL_CATALOG = [
'Zauberkunde' => ['INT', 'Wissen'],
];
/**
* Fertigkeitsstufen des Charakterbogens (RPG/6-Dokumente/Charakterbogen.pdf):
* Index = Stufe. Stufe 0 = ungelernt, ab Stufe 6 immer 3W6.
*/
const WS_SKILL_DICE = ['W4', 'W6', 'W8', 'W10', 'W12', '2W6', '3W6'];
const WS_SKILL_TIERS = ['ungelernt', 'Anfänger', 'Geselle', 'Könner', 'Profi', 'Meister', 'Großmeister'];
/** Ankreuzbare Praxispunkte je Fertigkeit (Bogen-Spalte „PP"). */
const WS_SKILL_PRACTICE_SLOTS = 10;
const WS_CHARACTER_STATUS = [
'active' => 'im Spiel',
'dead' => 'gefallen',
@ -148,6 +158,26 @@ function bonus_class(int $value): string
return $value > 0 ? 'bonus-pos' : ($value < 0 ? 'bonus-neg' : 'bonus-zero');
}
/**
* Stufe einer Fertigkeit auf die Würfelleiter abbilden (0 … 6).
*/
function skill_tier_index(int $level): int
{
return max(0, min(count(WS_SKILL_DICE) - 1, $level));
}
/** Würfel der Fertigkeitsstufe, z. B. Stufe 3 → „W10". */
function skill_die(int $level): string
{
return WS_SKILL_DICE[skill_tier_index($level)];
}
/** Bezeichnung der Fertigkeitsstufe, z. B. Stufe 0 → „ungelernt". */
function skill_tier(int $level): string
{
return WS_SKILL_TIERS[skill_tier_index($level)];
}
/** Attributkürzel (STR, GES, …) prüfen; unbekannte Werte werden zu ''. */
function skill_attribute_code(string $value): string
{

View file

@ -334,19 +334,37 @@ function character_pdf(array $c, array $extra): string
$skill = $rows[$index];
$cx = $M + $col * ($colW + 16);
$mod = skill_attribute_modifier($c, $skill);
$right = $cx + $colW;
$skillName = shorten((string) $skill['name'], 34);
$pdf->text($cx, $y + 8.5, 8.4, 'F3', $ink, $skillName);
$skillNote = trim((string) ($skill['note'] ?? ''));
if ($skillNote !== '') {
$noteX = $cx + WsPdf::textWidth($skillName, 8.4, 'F3') + 6;
$noteLimit = $cx + $colW - 84;
$noteLimit = $right - 128;
if ($noteLimit - $noteX > 26) {
$pdf->text($noteX, $y + 8.4, 6.8, 'F1', $soft, WsPdf::fit($skillNote, 6.8, 'F1', $noteLimit - $noteX));
}
}
$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, (string) max(0, (int) $skill['bonus']));
$pdf->textRight($right - 112, $y + 8.2, 6.6, 'F1', $soft, trim((string) ($skill['attribute'] ?? '')));
$pdf->textRight($right - 92, $y + 8.2, 6.8, 'F1', $soft, $mod === null ? '–' : signed($mod));
$pdf->textRight($right - 74, $y + 8.5, 8.4, 'F2', $gold, (string) max(0, (int) $skill['bonus']));
// Würfel der Fertigkeitsstufe (W4 ungelernt … 3W6 Großmeister)
$pdf->textRight($right, $y + 8.3, 7.0, 'F2', $ink, skill_die((int) $skill['bonus']));
// Praxispunkte: 10 ankreuzbare Kästchen
$pp = max(0, min(WS_SKILL_PRACTICE_SLOTS, (int) ($skill['pp'] ?? 0)));
$slotW = 4.2;
$slotGap = 0.9;
$slotH = 5.4;
$slotsRight = $right - 22;
$slotsLeft = $slotsRight - (WS_SKILL_PRACTICE_SLOTS * $slotW + (WS_SKILL_PRACTICE_SLOTS - 1) * $slotGap);
for ($slot = 1; $slot <= WS_SKILL_PRACTICE_SLOTS; $slot++) {
$sx = $slotsLeft + ($slot - 1) * ($slotW + $slotGap);
if ($slot <= $pp) {
$pdf->rect($sx, $y + 4.6, $slotW, $slotH, $gold, $gold, 0.2);
} else {
$pdf->rect($sx, $y + 4.6, $slotW, $slotH, null, $goldLight, 0.25);
}
}
$pdf->line($cx, $y + 10.4, $cx + $colW, $y + 10.4, $goldLight, 0.22);
}
$y += 12.6;