Selektor auch für Waffen und Rüstung, fest auf die jeweilige Art vorgefiltert

This commit is contained in:
torsten 2026-11-21 15:04:59 +01:00
parent a121b8c202
commit c0756971cf
5 changed files with 247 additions and 40 deletions

View file

@ -504,7 +504,9 @@ function item_catalog_sync(bool $force = false): int
/**
* Katalog durchsuchen.
*
* @param array{kind?:string,availability?:string,needle?:string,limit?:int} $filter
* `kinds` schränkt auf mehrere Arten zugleich ein (z. B. Rüstung + Schild).
*
* @param array{kind?:string,kinds?:list<string>,availability?:string,needle?:string,limit?:int} $filter
* @return list<array<string,mixed>>
*/
function item_catalog_search(array $filter = []): array
@ -518,6 +520,12 @@ function item_catalog_search(array $filter = []): array
$args[] = $kind;
}
$kinds = item_catalog_filter_kinds($filter);
if ($kinds !== []) {
$where[] = 'kind IN (' . implode(', ', array_fill(0, count($kinds), '?')) . ')';
$args = array_merge($args, $kinds);
}
$availability = (string) ($filter['availability'] ?? '');
if ($availability === 'methaven') {
$where[] = "availability = 'methaven'";
@ -533,11 +541,13 @@ function item_catalog_search(array $filter = []): array
}
$limit = max(1, min(400, (int) ($filter['limit'] ?? 150)));
// Gruppen in Katalogreihenfolge (WS_ITEM_CATEGORIES), innerhalb einer Gruppe alphabetisch.
$kindOrder = implode(', ', array_fill(0, count(WS_ITEM_CATEGORIES), '?'));
$sql = 'SELECT * FROM item_catalog'
. ($where === [] ? '' : ' WHERE ' . implode(' AND ', $where))
. ' ORDER BY sort_order, name LIMIT ' . $limit;
. ' ORDER BY FIELD(kind, ' . $kindOrder . '), name, id LIMIT ' . $limit;
$stmt = db()->prepare($sql);
$stmt->execute($args);
$stmt->execute(array_merge($args, WS_ITEM_CATEGORIES));
return $stmt->fetchAll();
}
@ -551,6 +561,11 @@ function item_catalog_count(array $filter = []): int
$where[] = 'kind = ?';
$args[] = $kind;
}
$kinds = item_catalog_filter_kinds($filter);
if ($kinds !== []) {
$where[] = 'kind IN (' . implode(', ', array_fill(0, count($kinds), '?')) . ')';
$args = array_merge($args, $kinds);
}
$availability = (string) ($filter['availability'] ?? '');
if ($availability === 'methaven') {
$where[] = "availability = 'methaven'";
@ -577,6 +592,104 @@ function item_catalog_find(int $id): ?array
return $stmt->fetch() ?: null;
}
/**
* Feste Artenliste eines Katalogfilters (nur gültige Kategorien, ohne Dubletten).
*
* @param array<string,mixed> $filter
* @return list<string>
*/
function item_catalog_filter_kinds(array $filter): array
{
$kinds = [];
foreach ((array) ($filter['kinds'] ?? []) as $kind) {
$kind = (string) $kind;
if (in_array($kind, WS_ITEM_CATEGORIES, true) && !in_array($kind, $kinds, true)) {
$kinds[] = $kind;
}
}
return $kinds;
}
/**
* Waffenprofil eines Katalogeintrags (Schaden, Kategorie).
*
* Die OSE-Werte stehen im Katalog nur als Notiz („OSE: 1W6, Nachladen“), deshalb
* wird der erste Schadenswürfel gelesen und die Kategorie aus Name und Notiz
* abgeleitet. Beides lässt sich im Bogen anschließend korrigieren.
*
* @param array<string,mixed> $entry
* @return array{damage:string,category:string}
*/
function item_catalog_weapon_profile(array $entry): array
{
$note = (string) ($entry['note'] ?? '');
$name = (string) ($entry['name'] ?? '');
$damage = '';
if (preg_match('/(\d+)\s*W\s*(\d+)(?:\s*([+-])\s*(\d+))?/u', $note, $m) === 1) {
$damage = $m[1] . 'W' . $m[2];
if (($m[3] ?? '') !== '' && ($m[4] ?? '') !== '') {
$damage .= $m[3] . $m[4];
}
}
$ranged = preg_match('/(bogen|armbrust|schleuder|blasrohr|wurf|lasso|netz|bolas)/iu', $name) === 1
|| preg_match('/fernkampf/iu', $note) === 1;
return ['damage' => $damage, 'category' => $ranged ? 'Fernkampf' : 'Nahkampf'];
}
/**
* Rüstungsbonus eines Katalogeintrags.
*
* OSE nennt in den Notizen die aufsteigende RK in eckigen Klammern („RK 7 [12]“);
* daraus wird der Bonus gegenüber der ungerüsteten RK 10. Schilde tragen ihren
* Bonus als „+1 RK“. Alles andere (Helm, Kleidung) zählt 0.
*
* @param array<string,mixed> $entry
*/
function item_catalog_armor_bonus(array $entry): int
{
$note = (string) ($entry['note'] ?? '');
if (preg_match('/\[\s*(\d+)\s*\]/u', $note, $m) === 1) {
return max(-5, min(15, (int) $m[1] - 10));
}
if (preg_match('/\+\s*(\d+)\s*(?:RK|Rüstung)/iu', $note, $m) === 1) {
return max(-5, min(15, (int) $m[1]));
}
return 0;
}
/**
* Konfiguration für einen Ausrüstungs-Selektor im Charakterbogen.
*
* `$key` ist zugleich Präfix der Dialog-ID und der GET-Parameter
* (`<key>_kind`, `<key>_avail`, `<key>_q`), damit sich mehrere Selektor-Schirme
* auf derselben Seite nicht gegenseitig die Filter überschreiben.
*
* @param list<string>|null $kinds Feste Arten (z. B. ['Waffe']); null = freie Wahl
* @return array{key:string,title:string,action:string,anchor:string,kinds:list<string>|null,filters:array<string,mixed>,catalog:list<array<string,mixed>>,count:int,open:bool}
*/
function item_selector_config(string $key, string $title, string $action, string $anchor, ?array $kinds = null): array
{
$filters = [
'kind' => $kinds === null ? query_string($key . '_kind') : '',
'availability' => query_string($key . '_avail'),
'needle' => query_string($key . '_q'),
];
if ($kinds !== null) {
$filters['kinds'] = $kinds;
}
return [
'key' => $key,
'title' => $title,
'action' => $action,
'anchor' => $anchor,
'kinds' => $kinds,
'filters' => $filters,
'catalog' => item_catalog_search($filters),
'count' => item_catalog_count($filters),
'open' => $filters['kind'] !== '' || $filters['availability'] !== '' || $filters['needle'] !== '',
];
}
/** Preis eines Katalogeintrags als Text (Silbertaler, ohne Nachkommastellen wenn glatt). */
function item_cost_label(mixed $cost): string
{
@ -615,6 +728,48 @@ function character_item_add_from_catalog(int $characterId, int $catalogId): bool
return true;
}
/**
* Waffe aus dem Ausrüstungskatalog in die Bewaffnung übernehmen.
*/
function character_weapon_add_from_catalog(int $characterId, int $catalogId): bool
{
$entry = item_catalog_find($catalogId);
if ($entry === null || (string) $entry['kind'] !== 'Waffe') {
return false;
}
$profile = item_catalog_weapon_profile($entry);
db()->prepare(
'INSERT INTO character_weapons (character_id, weapon_id, name, damage, attack_bonus, category, note, equipped)
VALUES (?, NULL, ?, ?, 0, ?, ?, 0)'
)->execute([
$characterId,
shorten((string) $entry['name'], 80),
shorten($profile['damage'], 20),
$profile['category'],
shorten((string) $entry['note'], 255),
]);
return true;
}
/**
* Rüstung oder Schild aus dem Ausrüstungskatalog übernehmen.
*/
function character_armor_add_from_catalog(int $characterId, int $catalogId): bool
{
$entry = item_catalog_find($catalogId);
$kind = (string) ($entry['kind'] ?? '');
if ($entry === null || !in_array($kind, ['Rüstung', 'Schild'], true)) {
return false;
}
character_armor_add($characterId, 0, [
'name' => (string) $entry['name'],
'ac_bonus' => item_catalog_armor_bonus($entry),
'kind' => $kind,
'note' => (string) $entry['note'],
]);
return true;
}
function character_item_delete(int $characterId, int $id): void
{
db()->prepare('DELETE FROM character_items WHERE id = ? AND character_id = ?')->execute([$id, $characterId]);