Charakterverwalter: Ausrüstungskatalog mit Selektor im Inventar

- data/item_catalog.csv als kuratierbare Master-Liste (481 Gegenstände nach
  OSE-Kernlisten plus Hausrat, Handel und Schatz), Preise in Silbertalern
  (OSE-Goldmünzen 1:1), Art aus WS_ITEM_CATEGORIES, Verfügbarkeit gestaffelt
  (Methaven/Garana/selten)
- tools/build_item_catalog.py erzeugt app/item_catalog_data.php und
  sql/seed_items.sql; item_catalog_sync() spiegelt den Katalog in die neue
  Tabelle item_catalog (Migration 3)
- Selektor-Dialog im Inventar filtert serverseitig nach Art, Verfügbarkeit und
  Suche (GET item_kind/item_avail/item_q) und übernimmt die Auswahl als
  Inventarzeile (POST /characters/{id}/items/from-catalog)
- Admin-Ansicht Ausrüstung listet den Katalog und liest die CSV neu ein
This commit is contained in:
torsten 2026-11-21 14:31:29 +01:00
parent b68e92f581
commit 5f01146d4b
17 changed files with 2029 additions and 4 deletions

View file

@ -461,6 +461,160 @@ function character_item_add(int $characterId, array $d): void
]);
}
/**
* Ausrüstungskatalog (Selektor im Inventar).
*
* Reihenfolge der Filter: Art (WS_ITEM_CATEGORIES) und Verfügbarkeit als Stufe –
* „Methaven“ liefert nur Dorfware, „Garana“ zusätzlich städtische Ware, „alle“
* auch das, was sonst nirgends zu kaufen ist.
*/
function item_catalog_sync(bool $force = false): int
{
require_once WS_APP . '/item_catalog_data.php';
$data = item_catalog_data();
$expected = count($data);
if (!$force) {
$current = (int) db()->query('SELECT COUNT(*) FROM item_catalog')->fetchColumn();
if ($current === $expected) {
return 0;
}
}
$stmt = db()->prepare(
'INSERT INTO item_catalog (name, kind, cost, weight, availability, note, sort_order)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE kind = VALUES(kind), cost = VALUES(cost), weight = VALUES(weight),
availability = VALUES(availability), note = VALUES(note), sort_order = VALUES(sort_order)'
);
$order = 0;
foreach ($data as $row) {
$order += 10;
$stmt->execute([
shorten((string) $row['name'], 120),
(string) $row['kind'],
(float) $row['cost'],
shorten((string) $row['weight'], 20),
(string) $row['availability'],
shorten((string) $row['note'], 255),
$order,
]);
}
return $expected;
}
/**
* Katalog durchsuchen.
*
* @param array{kind?:string,availability?:string,needle?:string,limit?:int} $filter
* @return list<array<string,mixed>>
*/
function item_catalog_search(array $filter = []): array
{
$where = [];
$args = [];
$kind = (string) ($filter['kind'] ?? '');
if (in_array($kind, WS_ITEM_CATEGORIES, true)) {
$where[] = 'kind = ?';
$args[] = $kind;
}
$availability = (string) ($filter['availability'] ?? '');
if ($availability === 'methaven') {
$where[] = "availability = 'methaven'";
} elseif ($availability === 'garana') {
$where[] = "availability IN ('methaven', 'garana')";
}
$needle = trim((string) ($filter['needle'] ?? ''));
if ($needle !== '') {
$where[] = '(name LIKE ? OR note LIKE ?)';
$args[] = '%' . $needle . '%';
$args[] = '%' . $needle . '%';
}
$limit = max(1, min(400, (int) ($filter['limit'] ?? 150)));
$sql = 'SELECT * FROM item_catalog'
. ($where === [] ? '' : ' WHERE ' . implode(' AND ', $where))
. ' ORDER BY sort_order, name LIMIT ' . $limit;
$stmt = db()->prepare($sql);
$stmt->execute($args);
return $stmt->fetchAll();
}
/** Anzahl der Treffer einer Katalogsuche (ohne Limit). */
function item_catalog_count(array $filter = []): int
{
$where = [];
$args = [];
$kind = (string) ($filter['kind'] ?? '');
if (in_array($kind, WS_ITEM_CATEGORIES, true)) {
$where[] = 'kind = ?';
$args[] = $kind;
}
$availability = (string) ($filter['availability'] ?? '');
if ($availability === 'methaven') {
$where[] = "availability = 'methaven'";
} elseif ($availability === 'garana') {
$where[] = "availability IN ('methaven', 'garana')";
}
$needle = trim((string) ($filter['needle'] ?? ''));
if ($needle !== '') {
$where[] = '(name LIKE ? OR note LIKE ?)';
$args[] = '%' . $needle . '%';
$args[] = '%' . $needle . '%';
}
$stmt = db()->prepare(
'SELECT COUNT(*) FROM item_catalog' . ($where === [] ? '' : ' WHERE ' . implode(' AND ', $where))
);
$stmt->execute($args);
return (int) $stmt->fetchColumn();
}
function item_catalog_find(int $id): ?array
{
$stmt = db()->prepare('SELECT * FROM item_catalog WHERE id = ? LIMIT 1');
$stmt->execute([$id]);
return $stmt->fetch() ?: null;
}
/** Preis eines Katalogeintrags als Text (Silbertaler, ohne Nachkommastellen wenn glatt). */
function item_cost_label(mixed $cost): string
{
$value = (float) $cost;
if ($value <= 0) {
return '–';
}
$text = number_format($value, 2, ',', '.');
$text = rtrim(rtrim($text, '0'), ',');
return $text . ' SM';
}
/** Gewicht eines Katalogeintrags in deutscher Schreibweise. */
function item_weight_label(mixed $weight): string
{
$text = trim((string) $weight);
return $text === '' ? '' : str_replace('.', ',', $text);
}
/** Katalogeintrag ins Inventar übernehmen (ein Stück, Art = Kategorie). */
function character_item_add_from_catalog(int $characterId, int $catalogId): bool
{
$entry = item_catalog_find($catalogId);
if ($entry === null) {
return false;
}
character_item_add($characterId, [
'name' => (string) $entry['name'],
'qty' => 1,
'category' => (string) $entry['kind'],
'weight' => (string) $entry['weight'],
'value' => item_cost_label($entry['cost']),
'equipped' => 0,
'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]);