Charaktere: Geschlecht (Default maennlich) plus weibliche Platzhalterportraets

Spalte characters.gender mit Migration, Auswahl im Formular, Anzeige auf Bogen und im PDF-Export. 12 neue Platzhalterbilder assets/portraits/<klasse>-weiblich.webp (fal.ai nano-banana-pro/edit) werden von portrait_placeholder_rel() nach Geschlecht gewaehlt.
This commit is contained in:
torsten 2026-11-22 12:45:52 +01:00
parent 0cbaeeafcd
commit 9388d04e98
23 changed files with 62 additions and 13 deletions

View file

@ -61,16 +61,17 @@ function character_create(int $userId, array $data): int
$data = array_merge($data, saving_throws_for((string) ($data['class_name'] ?? 'Kämpfer'), (int) $data['level']));
$stmt = db()->prepare(
'INSERT INTO characters (user_id, name, title, class_name, level, xp, hp_max, hp_current, ac, attack_bonus,
'INSERT INTO characters (user_id, name, title, gender, class_name, level, xp, hp_max, hp_current, ac, attack_bonus,
attr_str, attr_dex, attr_con, attr_int, attr_wis, attr_cha,
save_death, save_wand, save_paralysis, save_breath, save_spell,
alignment, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)'
);
$stmt->execute([
$userId,
$data['name'],
$data['title'] ?? '',
character_gender($data['gender'] ?? null),
$data['class_name'] ?? 'Kämpfer',
$data['level'] ?? 1,
$data['xp'] ?? 0,
@ -205,7 +206,7 @@ function character_update(int $id, array $d): void
}
$stmt = db()->prepare(
'UPDATE characters SET name = ?, title = ?, class_name = ?, level = ?, xp = ?, hp_max = ?, hp_current = ?,
'UPDATE characters SET name = ?, title = ?, gender = ?, class_name = ?, level = ?, xp = ?, hp_max = ?, hp_current = ?,
ac = ?, attack_bonus = ?, attr_str = ?, attr_dex = ?, attr_con = ?, attr_int = ?, attr_wis = ?, attr_cha = ?,
save_death = ?, save_wand = ?, save_paralysis = ?, save_breath = ?, save_spell = ?,
alignment = ?, deity = ?, age = ?, height = ?, weight = ?, gold_coins = ?,
@ -215,6 +216,7 @@ function character_update(int $id, array $d): void
$stmt->execute([
$d['name'],
$d['title'] ?? '',
character_gender($d['gender'] ?? null),
$d['class_name'] ?? 'Kämpfer',
$d['level'] ?? 1,
$d['xp'] ?? 0,
@ -274,6 +276,13 @@ function character_ac_value(mixed $value): ?int
return $int > 0 ? $int : null;
}
/** Geschlecht auf einen gültigen Wert aus WS_GENDERS begrenzen. */
function character_gender(mixed $value): string
{
$gender = is_string($value) ? trim($value) : '';
return in_array($gender, WS_GENDERS, true) ? $gender : WS_DEFAULT_GENDER;
}
function character_delete(int $id): void
{
db()->prepare('DELETE FROM characters WHERE id = ?')->execute([$id]);

View file

@ -42,6 +42,10 @@ const WS_MIGRATIONS = [
KEY idx_item_catalog_filter (kind, availability)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci",
],
// 4: Geschlecht je Charakter; steuert das Platzhalterporträt.
4 => [
"ALTER TABLE characters ADD COLUMN IF NOT EXISTS gender VARCHAR(20) NOT NULL DEFAULT 'männlich' AFTER title",
],
];
function ws_migrations_apply(PDO $pdo): void

View file

@ -531,6 +531,7 @@ function character_form_values(array $source): array
}
$values['name'] = post_string('name', (string) ($source['name'] ?? ''));
$values['title'] = post_string('title', (string) ($source['title'] ?? ''));
$values['gender'] = character_gender(post_string('gender', (string) ($source['gender'] ?? WS_DEFAULT_GENDER)));
$values['class_name'] = post_string('class_name', (string) ($source['class_name'] ?? 'Kämpfer'));
$values['xp'] = max(0, post_int('xp', (int) ($source['xp'] ?? 0)));
$values['hp_max'] = max(1, post_int('hp_max', (int) ($source['hp_max'] ?? 1)));

View file

@ -28,11 +28,22 @@ function portrait_dir(): string
return WS_ROOT . '/' . WS_PORTRAIT_REL_DIR;
}
function portrait_placeholder_rel(string $className): string
function portrait_placeholder_rel(string $className, string $gender = WS_DEFAULT_GENDER): string
{
$slug = portrait_class_slug($className);
$rel = 'assets/portraits/' . $slug . '.webp';
return is_file(WS_ROOT . '/' . $rel) ? $rel : '';
// Weibliche Charaktere bekommen …-weiblich.webp; fehlt die Variante,
// greift weiterhin das neutrale Platzhalterbild der Klasse.
$candidates = [];
if (character_gender($gender) === 'weiblich') {
$candidates[] = 'assets/portraits/' . $slug . '-weiblich.webp';
}
$candidates[] = 'assets/portraits/' . $slug . '.webp';
foreach ($candidates as $rel) {
if (is_file(WS_ROOT . '/' . $rel)) {
return $rel;
}
}
return '';
}
/** Anzeige-URL: eigenes Bild, sonst Platzhalter der Klasse, sonst leer. */
@ -42,7 +53,7 @@ function portrait_url(array $character): string
if ($stored !== '' && is_file(WS_ROOT . '/' . ltrim($stored, '/'))) {
return url('/' . ltrim($stored, '/'));
}
$placeholder = portrait_placeholder_rel((string) ($character['class_name'] ?? ''));
$placeholder = portrait_placeholder_rel((string) ($character['class_name'] ?? ''), (string) ($character['gender'] ?? ''));
return $placeholder === '' ? '' : url('/' . $placeholder);
}
@ -161,7 +172,7 @@ function portrait_jpeg_bytes(array $character): string
}
}
$placeholder = portrait_placeholder_rel((string) ($character['class_name'] ?? ''));
$placeholder = portrait_placeholder_rel((string) ($character['class_name'] ?? ''), (string) ($character['gender'] ?? ''));
if ($placeholder === '' || !function_exists('imagecreatefromwebp')) {
return '';
}

View file

@ -28,6 +28,13 @@ const WS_SAVES = [
const WS_ALIGNMENTS = ['Rechtschaffen', 'Neutral', 'Chaotisch', 'Gut', 'Böse', '–'];
/**
* Geschlecht: bestimmt das Platzhalterporträt (…-weiblich.webp). Der erste
* Eintrag ist der Standardwert für neue und bestehende Charaktere.
*/
const WS_DEFAULT_GENDER = 'männlich';
const WS_GENDERS = ['männlich', 'weiblich'];
const WS_ITEM_CATEGORIES = ['Waffe', 'Rüstung', 'Schild', 'Ausrüstung', 'Verbrauch', 'Schatz', 'Sonstiges'];
/**

View file

@ -441,6 +441,9 @@ function character_pdf(array $c, array $extra): string
/* ------------------------------------------------------ Person & Notizen */
$facts = [];
if ((string) ($c['gender'] ?? '') !== '') {
$facts[] = 'Geschlecht: ' . $c['gender'];
}
if ((string) $c['deity'] !== '') {
$facts[] = 'Gottheit: ' . $c['deity'];
}