LuminaWebsite/includes/professions.php
Maaxxs ee7a3a2e8e Professions: members enter their characters' professions, a new page shows who can craft what
Asked for in Discord: profession management within the legion for a good balance. Aion 2 has five
crafting professions (Blacksmithing, Armorsmithing, Handicrafting, Alchemy, Cooking) with the stages
Beginner, Expert and Master; gathering is one shared skill for six raw materials that players
specialize in with points. Professions belong to characters, so they are entered per character.

- includes/professions.php: PROFESSIONS (German name, client name, icon, products, weapon classes),
  PROFESSION_STAGES, GATHERING_MATERIALS, loading/saving, counts per member, targets and "wanted".
  German names are not confirmed by the German client yet; only keys are stored.
- New table character_professions (stage, optional level, main profession - at most one per
  character) and member_characters.gathering (schema version 57, also in sql/schema.sql).
- Profile editor: per character a professions fieldset (stage, level, main-profession tick - a
  single profession becomes the main one) and the gathering specialization; saved with the
  character (member_action.php), invalid levels are rejected.
- Profile page shows them per character; the member list shows icons on the cards (main
  profession with a gold ring), gets a "Beruf" filter and finds professions in the search.
- New page professions.php ("Berufe" in the menu, members.view): per profession how many members
  have it as main profession and at all, characters per stage, which classes' weapons it makes and
  how many members play them, everyone with stage; below the gathering specializations.
- New permission professions.manage (initially for groups with roles.manage): targets per
  profession (professions_action.php, setting profession_targets, audit log). Professions below
  their target show as "Gerade gesucht" on the page and in the profile editor.
- Hammer icon; live updates for the new page, the member list and profile pages; the data export
  includes professions per character; privacy policy (as of 26.09.), patch notes and README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-26 00:01:11 +02:00

285 lines
13 KiB
PHP

<?php
declare(strict_types=1);
/**
* Berufe der Legion: welche Handwerksberufe die Charaktere der Mitglieder ausueben (Stufe, Hauptberuf) und worauf
* sie sich beim Sammeln spezialisiert haben. Eingetragen unter "Meine Profilseite" → Charaktere (member_edit.php,
* gespeichert von member_action.php), zu sehen auf der Profilseite, in der Mitgliederliste (Filter "Beruf") und in
* der Uebersicht professions.php - dort legt die Leitung (professions.manage) Zielzahlen je Beruf fest, und was
* darunter liegt, erscheint als "Gerade gesucht".
*
* Aion 2 (Global-Client, Stand September 2026): fuenf Handwerksberufe, je mit den Stufen Anfaenger, Fortgeschritten
* und Meister. Sammeln ist kein eigener Beruf, sondern eine gemeinsame Fertigkeit (Essence Extraction) fuer sechs
* Rohstoffe; mit ihren Punkten spezialisiert man sich auf einzelne (fuer alle reichen sie nicht). Die deutschen
* Namen sind noch nicht aus dem Client bestaetigt - gespeichert werden nur die Schluessel, Umbenennen hier genuegt.
*/
// Schluessel => Name, Name im englischen Client, Symbol, was der Beruf herstellt, fuer welche Klassen er die Waffe baut
const PROFESSIONS = [
'blacksmithing' => [
'label' => 'Waffenschmieden', 'en' => 'Blacksmithing', 'icon' => '⚔️',
'makes' => 'Schwerter, Zweihänder, Dolche, Streitkolben und Schilde',
'classes' => ['Gladiator', 'Templer', 'Assassine', 'Kleriker'],
],
'armorsmithing' => [
'label' => 'Rüstungsschmieden', 'en' => 'Armorsmithing', 'icon' => '🛡️',
'makes' => 'Rüstungsteile und Umhänge für alle Klassen',
'classes' => [],
],
'handicrafting' => [
'label' => 'Handwerkskunst', 'en' => 'Handicrafting', 'icon' => '💍',
'makes' => 'Bögen, Stäbe und Schmuck (Ringe, Ohrringe, Halsketten)',
'classes' => ['Jäger', 'Kantor'],
],
'alchemy' => [
'label' => 'Alchemie', 'en' => 'Alchemy', 'icon' => '⚗️',
'makes' => 'Zauberbücher, Orbs, Tränke, Manasteine und Schriftrollen',
'classes' => ['Magier', 'Beschwörer'],
],
'cooking' => [
'label' => 'Kochen', 'en' => 'Cooking', 'icon' => '🍲',
'makes' => 'Essen und Getränke mit Buffs',
'classes' => [],
],
];
const PROFESSION_STAGES = [1 => 'Anfänger', 2 => 'Fortgeschritten', 3 => 'Meister'];
// Level innerhalb einer Stufe (optional): Anfaenger geht bis 50, Fortgeschritten in Korea inzwischen bis 70
const PROFESSION_LEVEL_MAX = 99;
// Zielzahl je Beruf hoechstens
const PROFESSION_TARGET_MAX = 99;
// Rohstoffe der Sammel-Fertigkeit (Essence Extraction) => Name, Symbol
const GATHERING_MATERIALS = [
'ore' => ['label' => 'Erz', 'icon' => '⛏️'],
'gem' => ['label' => 'Edelsteine', 'icon' => '💎'],
'wood' => ['label' => 'Holz', 'icon' => '🪵'],
'herb' => ['label' => 'Kräuter', 'icon' => '🌿'],
'ingredient' => ['label' => 'Kochzutaten', 'icon' => '🥕'],
'odyle' => ['label' => 'Odyle (im Flug)', 'icon' => '✨'],
];
/** Stufe als Text: "Meister" bzw. "Fortgeschritten 32" */
function profession_stage_text(int $stage, ?int $level): string
{
return (PROFESSION_STAGES[$stage] ?? PROFESSION_STAGES[1]) . ($level !== null ? ' ' . $level : '');
}
/**
* Berufe von Charakteren: [Charakter-ID => [Beruf => ['stage' => 1-3, 'level' => ?int, 'main' => bool]]] -
* Hauptberuf zuerst, dann nach Stufe.
*
* @param array<int, int|string> $characterIds
*/
function professions_for_characters(array $characterIds): array
{
$characterIds = array_values(array_unique(array_filter(array_map('intval', $characterIds))));
if (!$characterIds) {
return [];
}
try {
$stmt = db()->prepare(
'SELECT character_id, profession, stage, level, is_main FROM character_professions
WHERE character_id IN (' . implode(',', array_fill(0, count($characterIds), '?')) . ')
ORDER BY is_main DESC, stage DESC, level DESC'
);
$stmt->execute($characterIds);
} catch (PDOException $e) {
return []; // Tabelle entsteht erst beim naechsten Schema-Abgleich
}
$out = [];
foreach ($stmt->fetchAll() as $row) {
if (isset(PROFESSIONS[$row['profession']])) {
$out[(int) $row['character_id']][$row['profession']] = [
'stage' => max(1, min(3, (int) $row['stage'])),
'level' => $row['level'] !== null ? (int) $row['level'] : null,
'main' => (bool) $row['is_main'],
];
}
}
return $out;
}
/** Sammel-Spezialisierung aus member_characters.gathering: die gueltigen Rohstoff-Schluessel. */
function gathering_parse(?string $value): array
{
return array_values(array_filter(
explode(',', (string) $value),
static fn (string $key): bool => isset(GATHERING_MATERIALS[$key])
));
}
/**
* Ein Beruf als Abzeichen: Symbol, Name und Stufe (der Hauptberuf leuchtet). $compact: nur das Symbol, der Rest
* steht im Tooltip (Mitgliederliste).
*/
function profession_badge_html(string $key, array $entry, bool $compact = false): string
{
$profession = PROFESSIONS[$key];
$stage = profession_stage_text($entry['stage'], $entry['level']);
// Mitgliederliste: Stufe des besten Charakters (character), Hauptberuf ggf. eines anderen (main_character)
$character = (string) ($entry['character'] ?? '');
$mainCharacter = (string) ($entry['main_character'] ?? '');
$title = $profession['label'] . ' · ' . $stage . ($character !== '' ? ' (' . $character . ')' : '')
. ($entry['main'] ? ' · Hauptberuf' . ($mainCharacter !== '' && $mainCharacter !== $character ? ' von ' . $mainCharacter : '') : '');
return '<span class="profession-badge profession-stage-' . (int) $entry['stage'] . ($entry['main'] ? ' is-main' : '') . ($compact ? ' is-compact' : '') . '"'
. ' title="' . e($title) . '"' . ($compact ? ' role="img" aria-label="' . e($title) . '"' : '') . '>'
. '<span class="profession-icon" aria-hidden="true">' . $profession['icon'] . '</span>'
. ($compact ? '' : '<span class="profession-name">' . e($profession['label']) . '</span><span class="profession-stage">' . e($stage) . '</span>')
. '</span>';
}
/** Sammel-Spezialisierung als kleine Abzeichen. */
function gathering_badges_html(array $materials): string
{
$html = '';
foreach ($materials as $key) {
$html .= '<span class="gathering-badge" title="Sammeln: ' . e(GATHERING_MATERIALS[$key]['label']) . '">'
. '<span aria-hidden="true">' . GATHERING_MATERIALS[$key]['icon'] . '</span>' . e(GATHERING_MATERIALS[$key]['label']) . '</span>';
}
return $html;
}
/**
* Berufe aus dem Formular eines Charakters (member_edit.php): profession_stage[Beruf] = 0-3, profession_level[Beruf],
* main_profession[] = Beruf (Haken - mit JavaScript immer nur einer, sonst zaehlt der erste), gathering[] = Rohstoffe.
* Ohne Hauptberuf ist ein einzelner Beruf der Hauptberuf.
*
* @return array{0: array<string, array{stage: int, level: ?int, main: bool}>, 1: ?string, 2: string[]} Berufe, Sammeln, Fehler
*/
function professions_from_input(array $input): array
{
$stages = is_array($input['profession_stage'] ?? null) ? $input['profession_stage'] : [];
$levels = is_array($input['profession_level'] ?? null) ? $input['profession_level'] : [];
$mainKeys = is_array($input['main_profession'] ?? null) ? array_values(array_filter($input['main_profession'], 'is_string')) : [];
$professions = [];
$errors = [];
foreach (PROFESSIONS as $key => $profession) {
$stage = (int) ($stages[$key] ?? 0);
if (!isset(PROFESSION_STAGES[$stage])) {
continue;
}
$levelInput = is_string($levels[$key] ?? null) ? trim($levels[$key]) : '';
$level = filter_var($levelInput, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => PROFESSION_LEVEL_MAX]]);
if ($levelInput !== '' && $level === false) {
$errors[] = $profession['label'] . ': das Level muss zwischen 1 und ' . PROFESSION_LEVEL_MAX . ' liegen.';
}
$professions[$key] = ['stage' => $stage, 'level' => $level !== false ? $level : null, 'main' => false];
}
$main = null;
foreach ($mainKeys as $key) {
if (isset($professions[$key])) {
$main = $key;
break;
}
}
if ($main === null && count($professions) === 1) {
$main = array_key_first($professions);
}
if ($main !== null) {
$professions[$main]['main'] = true;
}
$gathering = array_values(array_intersect(array_keys(GATHERING_MATERIALS), is_array($input['gathering'] ?? null) ? $input['gathering'] : []));
return [$professions, $gathering ? implode(',', $gathering) : null, $errors];
}
/** Berufe und Sammel-Spezialisierung eines Charakters speichern (ersetzt die bisherigen). */
function professions_store(int $characterId, array $professions, ?string $gathering): void
{
$pdo = db();
$pdo->prepare('DELETE FROM character_professions WHERE character_id = ?')->execute([$characterId]);
$insert = $pdo->prepare('INSERT INTO character_professions (character_id, profession, stage, level, is_main) VALUES (?, ?, ?, ?, ?)');
foreach ($professions as $key => $entry) {
$insert->execute([$characterId, $key, $entry['stage'], $entry['level'], $entry['main'] ? 1 : 0]);
}
$pdo->prepare('UPDATE member_characters SET gathering = ? WHERE id = ?')->execute([$gathering, $characterId]);
}
/** Zielzahlen der Leitung: [Beruf => Anzahl Mitglieder mit diesem Hauptberuf] (0 = kein Ziel). */
function profession_targets(): array
{
$saved = json_decode(setting_get('profession_targets'), true);
$targets = [];
foreach (array_keys(PROFESSIONS) as $key) {
$targets[$key] = max(0, min(PROFESSION_TARGET_MAX, (int) (is_array($saved) ? ($saved[$key] ?? 0) : 0)));
}
return $targets;
}
/**
* Alle Berufe der aktiven Mitglieder mit Charakter und Mitglied - Grundlage der Uebersicht und der Zaehler.
*
* @return array<int, array<string, mixed>>
*/
function professions_all_rows(): array
{
try {
return db()->query(
"SELECT cp.profession, cp.stage, cp.level, cp.is_main, c.id AS character_id, c.name AS character_name,
c.class_name AS character_class, u.id AS user_id, u.username, u.display_name, u.avatar_filename
FROM character_professions cp
JOIN member_characters c ON c.id = cp.character_id
JOIN users u ON u.id = c.user_id
WHERE u.status = 'active'
ORDER BY cp.stage DESC, cp.level IS NULL, cp.level DESC, COALESCE(NULLIF(u.display_name, ''), u.username)"
)->fetchAll();
} catch (PDOException $e) {
return [];
}
}
/**
* Wie viele Mitglieder einen Beruf haben: ['main' => als Hauptberuf, 'any' => ueberhaupt] je Beruf - jeweils Mitglieder,
* nicht Charaktere (zwei Charaktere desselben Mitglieds zaehlen einmal).
*
* @param array<int, array<string, mixed>>|null $rows aus professions_all_rows()
* @return array<string, array{main: int, any: int}>
*/
function profession_member_counts(?array $rows = null): array
{
$main = [];
$any = [];
foreach ($rows ?? professions_all_rows() as $row) {
$any[$row['profession']][(int) $row['user_id']] = true;
if ((int) $row['is_main'] === 1) {
$main[$row['profession']][(int) $row['user_id']] = true;
}
}
$counts = [];
foreach (array_keys(PROFESSIONS) as $key) {
$counts[$key] = ['main' => count($main[$key] ?? []), 'any' => count($any[$key] ?? [])];
}
return $counts;
}
/**
* Berufe, die die Legion gerade sucht: Hauptberufe unter der Zielzahl der Leitung - der groesste Mangel zuerst.
*
* @return array<string, array{have: int, target: int}>
*/
function professions_wanted(?array $counts = null): array
{
$counts ??= profession_member_counts();
$wanted = [];
foreach (profession_targets() as $key => $target) {
if ($target > 0 && $counts[$key]['main'] < $target) {
$wanted[$key] = ['have' => $counts[$key]['main'], 'target' => $target];
}
}
uasort($wanted, static fn (array $a, array $b): int => ($b['target'] - $b['have']) <=> ($a['target'] - $a['have']));
return $wanted;
}
/** "Gerade gesucht: ⚗️ Alchemie (1 von 3), …" - leer, wenn nichts fehlt. */
function professions_wanted_html(array $wanted): string
{
if (!$wanted) {
return '';
}
$parts = [];
foreach ($wanted as $key => $numbers) {
$parts[] = '<span class="profession-wanted-item">' . PROFESSIONS[$key]['icon'] . ' ' . e(PROFESSIONS[$key]['label'])
. ' <span class="text-muted">(' . $numbers['have'] . ' von ' . $numbers['target'] . ')</span></span>';
}
return '<p class="profession-wanted"><strong>Gerade gesucht:</strong> ' . implode(' ', $parts) . '</p>';
}