rpg-westreiche/westreiche_website/app/sheet.php
torsten 929aa52c73 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
2026-11-21 14:05:51 +01:00

373 lines
14 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
declare(strict_types=1);
/**
* Westreiche – Zeichnet den Charakterbogen als PDF (A4, Pergament-Optik mit
* Gold-Rahmen). Quelle sind die Datensätze aus characters/character_*.
*/
/** @return array{0:float,1:float,2:float} */
function pdf_color(string $hex): array
{
$hex = ltrim($hex, '#');
return [
hexdec(substr($hex, 0, 2)) / 255,
hexdec(substr($hex, 2, 2)) / 255,
hexdec(substr($hex, 4, 2)) / 255,
];
}
/**
* Baut den kompletten Charakterbogen.
*
* @param array $c Charakterdatensatz
* @param array $extra ['skills' => [...], 'spells' => ['arkan'=>[], 'klerikal'=>[]], 'items' => [...]]
*/
function character_pdf(array $c, array $extra): string
{
$ink = pdf_color('#1a1712');
$soft = pdf_color('#4a4336');
$gold = pdf_color('#a8862a');
$goldLight = pdf_color('#d9be6a');
$parchment = pdf_color('#faf5e6');
$parchment2 = pdf_color('#f2e9d2');
$pdf = new WsPdf();
$W = WsPdf::width();
$H = WsPdf::height();
$M = 34.0;
$inner = $W - 2 * $M;
$pageNo = 0;
$y = 0.0;
$frame = static function (WsPdf $pdf, int $pageNo) use ($W, $H, $M, $gold, $goldLight, $parchment, $parchment2, $ink, $soft): void {
$pdf->rect(0, 0, $W, $H, $parchment);
$pdf->rect(0, 0, $W, 16, $parchment2);
$pdf->rect(0, $H - 16, $W, 16, $parchment2);
$pdf->rect($M - 12, 18, $W - 2 * ($M - 12), $H - 36, null, $gold, 1.2);
$pdf->rect($M - 8, 22, $W - 2 * ($M - 8), $H - 44, null, $goldLight, 0.4);
// Ecken-Ornamente
foreach ([[$M - 12, 18], [$W - $M + 12 - 14, 18], [$M - 12, $H - 32], [$W - $M + 12 - 14, $H - 32]] as [$cx, $cy]) {
$pdf->rect($cx, $cy, 14, 14, $gold);
}
$pdf->textCenter($W / 2, $H - 22, 7.5, 'F1', $soft, 'Westreiche · Chronik der Zauberturm-Runde · zauberturm.de');
$pdf->textRight($W - $M, $H - 40, 7.5, 'F1', $soft, 'Seite ' . $pageNo);
};
$newPage = static function () use (&$pageNo, &$y, $pdf, $frame): void {
if ($pageNo > 0) {
$pdf->addPage();
}
$pageNo++;
$frame($pdf, $pageNo);
$y = 52.0;
};
$newPage();
$ensure = static function (float $needed) use (&$y, $newPage, $H): void {
if ($y + $needed > $H - 70) {
$newPage();
}
};
$sectionTitle = static function (string $label) use (&$y, $pdf, $gold, $ink, $ensure, $M, $inner): void {
$ensure(34);
$y += 12;
$pdf->rect($M, $y, 3.2, 10, $gold);
$pdf->text($M + 8, $y + 9, 11.5, 'F4', $ink, mb_strtoupper($label, 'UTF-8'));
$pdf->line($M, $y + 14, $M + $inner, $y + 14, $gold, 0.4);
$y += 22;
};
$paragraph = static function (string $text, float $size = 8.6) use (&$y, $pdf, $ink, $ensure, $M, $inner): void {
if (trim($text) === '') {
return;
}
foreach (preg_split('/\R{2,}/', trim($text)) ?: [] as $block) {
foreach (WsPdf::wrap(str_replace("\n", ' ', $block), $size, 'F3', $inner) as $line) {
$ensure(12);
$pdf->text($M, $y + 9, $size, 'F3', $ink, $line);
$y += 11.4;
}
$y += 3;
}
};
/* ---------------------------------------------------------------- Kopf */
$portraitW = 74.0;
$portraitH = 94.0;
$pX = $W - $M - $portraitW;
$textRight = $pX - 12;
$pdf->text($M, $y, 7.5, 'F2', $gold, 'CHARAKTERBOGEN');
$nameSize = 24.0;
$nameText = (string) $c['name'];
while ($nameText !== '' && WsPdf::textWidth($nameText, $nameSize, 'F4') > $textRight - $M) {
$nameText = mb_substr($nameText, 0, -1, 'UTF-8');
}
if ($nameText !== (string) $c['name']) {
$nameText = rtrim($nameText) . '…';
}
$pdf->text($M, $y + 30, $nameSize, 'F4', $ink, $nameText);
$titleLine = [];
if ((string) $c['title'] !== '') {
$titleLine[] = (string) $c['title'];
}
$titleLine[] = (string) $c['class_name'];
$titleLine[] = 'Stufe ' . (int) $c['level'];
if ((string) $c['alignment'] !== '' && (string) $c['alignment'] !== '–') {
$titleLine[] = 'Gesinnung: ' . (string) $c['alignment'];
}
$titleLine[] = character_status_label((string) $c['status']);
$titleText = implode(' · ', $titleLine);
while ($titleText !== '' && WsPdf::textWidth($titleText, 9.6, 'F5') > $textRight - $M) {
$titleText = mb_substr($titleText, 0, -1, 'UTF-8');
}
$pdf->text($M, $y + 46, 9.6, 'F5', $soft, $titleText);
// Porträt rechts (sonst Monogramm)
$pH = $y + 2;
$portraitBytes = portrait_jpeg_bytes($c);
if ($portraitBytes === '' || !$pdf->imageJpeg($portraitBytes, $pX, $pH, $portraitW, $portraitH, true)) {
$cgX = $W - $M - 26;
$cgY = $y + 26;
$pdf->rect($cgX - 26, $cgY - 26, 52, 52, null, $gold, 1.0);
$pdf->rect($cgX - 22, $cgY - 22, 44, 44, null, $goldLight, 0.4);
$pdf->textCenter($cgX, $cgY + 8, 20, 'F4', $gold, mb_strtoupper(mb_substr((string) $c['name'], 0, 1), 'UTF-8'));
}
$pdf->rect($pX - 2.5, $pH - 2.5, $portraitW + 5, $portraitH + 5, null, $gold, 1.0);
$y += 78;
/* ---------------------------------------------------------- Attribute */
$sectionTitle('Attribute');
$boxW = 60.0;
$boxH = 54.0;
$gap = 6.0;
$x = $M;
foreach (WS_ATTRIBUTES as $field => [$short, $label]) {
$value = (int) ($c[$field] ?? 10);
$pdf->rect($x, $y, $boxW, $boxH, $parchment2, $gold, 0.5);
$pdf->textCenter($x + $boxW / 2, $y + 13, 7.2, 'F2', $soft, $short);
$pdf->textCenter($x + $boxW / 2, $y + 34, 17, 'F4', $ink, (string) $value);
$pdf->textCenter($x + $boxW / 2, $y + 46, 8, 'F2', $gold, signed(attribute_bonus($value)));
$x += $boxW + $gap;
}
// Kampfblock rechts
$combatX = $x + 4;
$combatW = $W - $M - $combatX;
$pdf->rect($combatX, $y, $combatW, $boxH, null, $gold, 0.6);
$combatRows = [
['Trefferpunkte', (int) $c['hp_current'] . ' / ' . (int) $c['hp_max']],
['Rüstungsklasse', (string) (int) ($extra['ac']['total'] ?? 10)],
['Angriff', signed((int) $c['attack_bonus'])],
['Gold', (int) $c['gold_coins'] . ' GM'],
];
$ry = $y + 12;
$rowStep = 12.0;
foreach ($combatRows as $i => [$label, $value]) {
$pdf->text($combatX + 7, $ry, 7.0, 'F1', $soft, $label);
$pdf->textRight($combatX + $combatW - 7, $ry, 9, 'F2', $ink, $value);
$ry += $rowStep;
}
$y += $boxH + 6;
/* ----------------------------------------------------- Rettungswürfe */
$saves = [];
foreach (WS_SAVES as $field => $label) {
$saves[$label] = save_display($c[$field] ?? null);
}
$sectionTitle('Rettungswürfe');
$sw = ($inner - 4 * 5) / 5;
$sx = $M;
foreach ($saves as $label => $value) {
$pdf->rect($sx, $y, $sw, 32, $parchment2, $gold, 0.4);
$pdf->textCenter($sx + $sw / 2, $y + 12, 6.8, 'F1', $soft, $label);
$pdf->textCenter($sx + $sw / 2, $y + 27, 13, 'F4', $ink, $value);
$sx += $sw + 5;
}
$y += 38;
/* ------------------------------------------------- Bewaffnung / Rüstung */
$weapons = $extra['weapons'] ?? [];
if ($weapons !== []) {
$sectionTitle('Bewaffnung');
$ensure(14);
$pdf->text($M, $y + 8, 6.8, 'F2', $soft, 'WAFFE');
$pdf->text($M + 200, $y + 8, 6.8, 'F2', $soft, 'ART');
$pdf->textRight($M + $inner - 74, $y + 8, 6.8, 'F2', $soft, 'TREFFER');
$pdf->textRight($M + $inner, $y + 8, 6.8, 'F2', $soft, 'SCHADEN');
$y += 12;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 4;
foreach ($weapons as $weapon) {
$ensure(13);
$pdf->text($M, $y + 8.5, 8.6, 'F3', $ink, shorten((string) $weapon['name'], 40));
$pdf->text($M + 200, $y + 8.5, 7.6, 'F1', $soft, (string) $weapon['category']);
$pdf->textRight($M + $inner - 74, $y + 8.5, 8.6, 'F2', $ink, signed(weapon_attack_total($c, $weapon)));
$pdf->textRight($M + $inner, $y + 8.5, 8.6, 'F3', $ink, weapon_damage_total($c, $weapon));
$pdf->line($M, $y + 10.8, $M + $inner, $y + 10.8, $goldLight, 0.22);
$y += 13;
}
$y += 4;
}
$armors = $extra['armors'] ?? [];
if ($armors !== []) {
$sectionTitle('Rüstung');
$ensure(14);
$pdf->text($M, $y + 8, 6.8, 'F2', $soft, 'TEIL');
$pdf->text($M + 200, $y + 8, 6.8, 'F2', $soft, 'ART');
$pdf->textRight($M + $inner - 60, $y + 8, 6.8, 'F2', $soft, 'RK-BONUS');
$pdf->textRight($M + $inner, $y + 8, 6.8, 'F2', $soft, 'STATUS');
$y += 12;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 4;
foreach ($armors as $armor) {
$ensure(13);
$pdf->text($M, $y + 8.5, 8.6, 'F3', $ink, shorten((string) $armor['name'], 40));
$pdf->text($M + 200, $y + 8.5, 7.6, 'F1', $soft, (string) $armor['kind']);
$pdf->textRight($M + $inner - 60, $y + 8.5, 8.6, 'F2', $ink, signed((int) $armor['ac_bonus']));
$pdf->textRight($M + $inner, $y + 8.5, 7.6, 'F1', $soft, (int) $armor['equipped'] === 1 ? 'angelegt' : 'abgelegt');
$pdf->line($M, $y + 10.8, $M + $inner, $y + 10.8, $goldLight, 0.22);
$y += 13;
}
$acInfo = $extra['ac'] ?? null;
if (is_array($acInfo)) {
$ensure(14);
$pdf->text($M, $y + 9, 8.2, 'F5', $soft, sprintf(
'Rüstungsklasse gesamt: 10 %s Geschick %s Rüstung = %d%s',
signed((int) $acInfo['dexterity']),
signed((int) $acInfo['armor']),
(int) $acInfo['total'],
$acInfo['manual_active'] ? ' (manuell überschrieben)' : ''
));
$y += 14;
}
$y += 2;
}
/* -------------------------------------------------------- Fertigkeiten */
$skills = $extra['skills'] ?? [];
if ($skills !== []) {
$sectionTitle('Fertigkeiten');
foreach (skills_grouped($skills) as $group => $rows) {
$ensure(26);
$pdf->text($M, $y + 9, 7.0, 'F2', $gold, mb_strtoupper($group, 'UTF-8'));
$pdf->line($M, $y + 12.5, $M + $inner, $y + 12.5, $goldLight, 0.3);
$y += 15;
$colW = ($inner - 16) / 2;
$perColumn = (int) ceil(count($rows) / 2);
for ($i = 0; $i < $perColumn; $i++) {
$ensure(13);
foreach ([0, 1] as $col) {
$index = $i + $col * $perColumn;
if (!isset($rows[$index])) {
continue;
}
$skill = $rows[$index];
$cx = $M + $col * ($colW + 16);
$mod = skill_attribute_modifier($c, $skill);
$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, (string) max(0, (int) $skill['bonus']));
$pdf->line($cx, $y + 10.4, $cx + $colW, $y + 10.4, $goldLight, 0.22);
}
$y += 12.6;
}
$y += 4;
}
}
/* ------------------------------------------------- Zauber & Liturgien */
$spells = $extra['spells'] ?? ['arkan' => [], 'klerikal' => []];
$spellBlocks = [
'arkan' => ['Arkane Zauber', $spells['arkan'] ?? []],
'klerikal' => ['Göttliche Liturgien', $spells['klerikal'] ?? []],
];
foreach ($spellBlocks as $key => [$label, $list]) {
if ($list === []) {
continue;
}
$sectionTitle($label);
$byGrade = [];
foreach ($list as $spell) {
$byGrade[(int) $spell['grade']][] = $spell;
}
ksort($byGrade);
foreach ($byGrade as $grade => $entries) {
$names = [];
foreach ($entries as $entry) {
$names[] = (string) $entry['name'];
}
foreach (WsPdf::wrap('Grad ' . $grade . ': ' . implode(', ', $names), 8.6, 'F3', $inner - 6) as $i => $line) {
$ensure(12);
if ($i === 0) {
$pdf->text($M, $y + 8.5, 8.6, 'F2', $gold, '');
}
$pdf->text($M + 2, $y + 8.5, 8.6, 'F3', $ink, $line);
$y += 11.6;
}
$y += 2;
}
$y += 2;
}
/* -------------------------------------------------------------- Inventar */
$items = $extra['items'] ?? [];
if ($items !== []) {
$sectionTitle('Inventar');
$pdf->text($M, $y + 8, 6.8, 'F2', $soft, 'ANZAHL');
$pdf->text($M + 52, $y + 8, 6.8, 'F2', $soft, 'GEGENSTAND');
$pdf->text($M + $inner - 78, $y + 8, 6.8, 'F2', $soft, 'ART');
$pdf->textRight($M + $inner, $y + 8, 6.8, 'F2', $soft, 'WERT');
$y += 12;
$pdf->line($M, $y, $M + $inner, $y, $gold, 0.4);
$y += 4;
foreach ($items as $item) {
$ensure(12);
$pdf->text($M, $y + 8, 8.4, 'F1', $ink, ((int) $item['qty']) . '×');
$pdf->text($M + 52, $y + 8, 8.6, 'F3', $ink, shorten((string) $item['name'], 56));
$pdf->text($M + $inner - 78, $y + 8, 7.4, 'F1', $soft, (string) $item['category']);
$pdf->textRight($M + $inner, $y + 8, 8.2, 'F3', $ink, (string) ($item['value'] !== '' ? $item['value'] : '–'));
$pdf->line($M, $y + 10.4, $M + $inner, $y + 10.4, $goldLight, 0.22);
$y += 12.6;
}
$y += 4;
}
/* ------------------------------------------------------ Person & Notizen */
$facts = [];
if ((string) $c['deity'] !== '') {
$facts[] = 'Gottheit: ' . $c['deity'];
}
if ((string) $c['age'] !== '') {
$facts[] = 'Alter: ' . $c['age'];
}
if ((string) $c['height'] !== '') {
$facts[] = 'Größe: ' . $c['height'];
}
if ((string) $c['weight'] !== '') {
$facts[] = 'Gewicht: ' . $c['weight'];
}
if ($facts !== []) {
$sectionTitle('Person');
$paragraph(implode(' · ', $facts), 9);
}
foreach (['appearance' => 'Erscheinung', 'background' => 'Hintergrund', 'notes' => 'Notizen'] as $field => $label) {
if (trim((string) $c[$field]) === '') {
continue;
}
$sectionTitle($label);
$paragraph((string) $c[$field]);
}
return $pdf->output();
}