diff --git a/README.md b/README.md index 6a1884e..8c46c1d 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,17 @@ das schon unterstützt. Zusätzlich gibt es die neue Berechtigung `raids.manage` `raids.view` nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden muss, falls dein Account vor diesem Update installiert wurde. +### Premades + +Bildet die Premade-Verwaltung des Discord-Bots (`/premade ...`) auf der Website nach: Gründen, +Umbenennen, Mitglieder einladen (per Discord-PN mit Annehmen/Ablehnen, genau wie im Bot) bzw. +entfernen, Leitung übertragen, Auflösen. Nutzt ausschließlich bereits vorhandene Endpunkte der +Bot-API (`/api/premades*`), es sind keine Änderungen an der Bot-API nötig. + +Neue Berechtigungen `premades.view` (Premades einsehen, eigene verwalten) und `premades.manage` +(alle Premades verwalten, nicht nur die eigene) müssen bei einem vor diesem Update installierten +Account nachträglich unter Berechtigungsgruppen angelegt/zugewiesen werden. + ## Berechtigungskonzept - **Berechtigungen** sind einzelne Schlüssel wie `users.view`, `roles.manage`, `dashboard.view`. diff --git a/assets/css/style.css b/assets/css/style.css index 6a22f29..a987757 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -748,6 +748,7 @@ input:focus, select:focus, textarea:focus { .raid-premade-row { display: flex; + flex-direction: row; flex-wrap: wrap; gap: 0.5rem; align-items: center; @@ -755,6 +756,46 @@ input:focus, select:focus, textarea:focus { .raid-premade-row select { width: auto; } +.premade-member-list { + list-style: none; + margin: 0.75rem 0 0; + padding: 0; +} + +.premade-member-list li { + display: flex; + align-items: center; + justify-content: space-between; + gap: 0.5rem; + padding: 0.4rem 0; + border-bottom: 1px solid var(--border); +} + +.premade-member-list li:last-child { border-bottom: none; } + +.premade-member-pending { color: var(--text-muted); } + +.premade-invite-row { + display: flex; + flex-wrap: wrap; + align-items: center; + justify-content: space-between; + gap: 0.75rem; + padding: 0.6rem 0; + border-bottom: 1px solid var(--border); +} + +.premade-invite-row:last-child { border-bottom: none; } + +.premade-manage { + display: flex; + flex-direction: column; + gap: 0.75rem; + margin-top: 1rem; + padding-top: 1rem; + border-top: 1px dashed var(--border); +} + .raid-empty-hint { margin: 0; } /* Drag & Drop nur optisch aktiv, wenn die Karte im Editor-Modus ist */ diff --git a/includes/header.php b/includes/header.php index b99838a..bcd3cb7 100644 --- a/includes/header.php +++ b/includes/header.php @@ -4,12 +4,14 @@ $currentUser = current_user(); $canViewUsers = $currentUser && user_has_permission('users.view'); $canManageRoles = $currentUser && user_has_permission('roles.manage'); $canViewRaids = $currentUser && user_has_permission('raids.view'); +$canViewPremades = $currentUser && user_has_permission('premades.view'); $showAdminMenu = $canViewUsers || $canManageRoles; $currentScript = basename((string) ($_SERVER['SCRIPT_NAME'] ?? '')); $isDashboardActive = $currentScript === 'dashboard.php'; $isProfileActive = $currentScript === 'profile.php'; $isRaidsActive = $currentScript === 'raids.php'; +$isPremadesActive = $currentScript === 'premades.php'; $isUsersActive = in_array($currentScript, ['users.php', 'user_form.php', 'user_delete.php'], true); $isRolesActive = in_array($currentScript, ['roles.php', 'role_form.php', 'role_delete.php'], true); @@ -18,6 +20,7 @@ $pageBackgrounds = [ 'dashboard.php' => 'bg-dashboard', 'profile.php' => 'bg-profile', 'raids.php' => 'bg-raids', + 'premades.php' => 'bg-raids', 'users.php' => 'bg-admin', 'user_form.php' => 'bg-admin', 'user_delete.php' => 'bg-admin', @@ -53,6 +56,11 @@ $pageBackgroundClass = $pageBackgrounds[$currentScript] ?? ''; Raids + + + Premades + + diff --git a/includes/permissions.php b/includes/permissions.php index 050eaca..fbbc068 100644 --- a/includes/permissions.php +++ b/includes/permissions.php @@ -14,6 +14,8 @@ const CORE_PERMISSIONS = [ ['key' => 'roles.manage', 'description' => 'Berechtigungsgruppen verwalten'], ['key' => 'raids.view', 'description' => 'Raids einsehen'], ['key' => 'raids.manage', 'description' => 'Raids archivieren/wiederherstellen'], + ['key' => 'premades.view', 'description' => 'Premade-Gruppen einsehen'], + ['key' => 'premades.manage', 'description' => 'Alle Premade-Gruppen verwalten (nicht nur die eigene)'], ]; function user_has_permission(string $key): bool diff --git a/includes/raids_api.php b/includes/raids_api.php index de04e7c..d88d420 100644 --- a/includes/raids_api.php +++ b/includes/raids_api.php @@ -182,49 +182,177 @@ function invite_premade(int $raidId, int $premadeId, int $group): int return (int) ($data['invited_count'] ?? 0); } +/** + * Fuehrt einen Request gegen die Premades-API aus (liegt unter derselben Basis-URL + * wie die Raid-API, eine Ebene ueber "/raids") und liefert die dekodierte + * JSON-Antwort zurueck. Analog zu raids_api_request(), nur mit eigener Basis-URL. + * + * @param array|null $jsonBody + * @return array + * @throws RuntimeException + */ +function premades_api_request(string $method, string $path = '', ?array $jsonBody = null): array +{ + if (!function_exists('curl_init')) { + throw new RuntimeException('Die PHP-cURL-Erweiterung ist auf diesem Server nicht aktiviert.'); + } + + $config = raids_api_config(); + // /api/premades liegt eine Ebene ueber /api/raids. + $base = preg_replace('#/raids$#', '', $config['url']); + $url = $base . '/premades' . $path; + + $headers = ['X-API-Key: ' . $config['key']]; + + $ch = curl_init($url); + $options = [ + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_TIMEOUT => 10, + CURLOPT_SSL_VERIFYPEER => true, + ]; + + if ($jsonBody !== null) { + $headers[] = 'Content-Type: application/json'; + $options[CURLOPT_POSTFIELDS] = json_encode($jsonBody, JSON_UNESCAPED_UNICODE); + } + + $options[CURLOPT_HTTPHEADER] = $headers; + curl_setopt_array($ch, $options); + + $response = curl_exec($ch); + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); + $curlError = curl_error($ch); + curl_close($ch); + + if ($response === false) { + throw new RuntimeException('Verbindung zur Premades-API fehlgeschlagen: ' . $curlError); + } + + $data = json_decode($response, true); + + if ($httpCode < 200 || $httpCode >= 300) { + $detail = is_array($data) ? ($data['detail'] ?? '') : ''; + $message = 'Premades-API antwortete mit Statuscode ' . $httpCode . '.'; + if ($detail !== '') { + $message .= ' (' . $detail . ')'; + } + throw new RuntimeException($message); + } + + if (!is_array($data)) { + throw new RuntimeException('Unerwartetes Antwortformat der Premades-API.'); + } + + return $data; +} + /** * @return array> * @throws RuntimeException */ function fetch_premades(): array { - $configPath = __DIR__ . '/../config.php'; - if (!file_exists($configPath)) { - throw new RuntimeException('Konfigurationsdatei fehlt.'); - } - $config = require $configPath; - $apiUrl = $config['raids_api']['url'] ?? ''; - $apiKey = $config['raids_api']['key'] ?? ''; - if ($apiUrl === '' || $apiKey === '') { - throw new RuntimeException('Raid-API ist nicht konfiguriert.'); - } + $data = premades_api_request('GET'); - // /api/premades liegt eine Ebene ueber /api/raids. - $base = preg_replace('#/raids$#', '', rtrim($apiUrl, '/')); - - $ch = curl_init($base . '/premades'); - curl_setopt_array($ch, [ - CURLOPT_RETURNTRANSFER => true, - CURLOPT_HTTPHEADER => ['X-API-Key: ' . $apiKey], - CURLOPT_TIMEOUT => 10, - CURLOPT_SSL_VERIFYPEER => true, - ]); - $response = curl_exec($ch); - $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); - curl_close($ch); - - if ($response === false || $httpCode !== 200) { - throw new RuntimeException('Premades konnten nicht geladen werden.'); - } - - $data = json_decode($response, true); - if (!is_array($data) || ($data['status'] ?? '') !== 'success' || !is_array($data['premades'] ?? null)) { + if (($data['status'] ?? '') !== 'success' || !is_array($data['premades'] ?? null)) { throw new RuntimeException('Unerwartetes Antwortformat der Premades-API.'); } return $data['premades']; } +/** + * @return array{id: int, name: string, owner_id: int} + * @throws RuntimeException + */ +function create_premade(string $name, int $ownerId): array +{ + $data = premades_api_request('POST', '', ['name' => $name, 'owner_id' => $ownerId]); + + return [ + 'id' => (int) ($data['premade_id'] ?? 0), + 'name' => (string) ($data['name'] ?? $name), + 'owner_id' => (int) ($data['owner_id'] ?? $ownerId), + ]; +} + +/** @throws RuntimeException */ +function rename_premade(int $premadeId, string $name): void +{ + premades_api_request('PATCH', '/' . $premadeId, ['name' => $name]); +} + +/** @throws RuntimeException */ +function delete_premade(int $premadeId): void +{ + premades_api_request('DELETE', '/' . $premadeId); +} + +/** + * @param string $status 'accepted' fuegt direkt hinzu, 'pending' verschickt eine + * Beitritts-PN mit Annehmen-/Ablehnen-Buttons ueber den Bot. + * @throws RuntimeException + */ +function add_premade_member(int $premadeId, int $userId, string $status = 'accepted'): void +{ + premades_api_request('POST', '/' . $premadeId . '/members', ['user_id' => $userId, 'status' => $status]); +} + +/** @throws RuntimeException */ +function remove_premade_member(int $premadeId, int $userId): void +{ + premades_api_request('DELETE', '/' . $premadeId . '/members/' . $userId); +} + +/** @throws RuntimeException */ +function transfer_premade_owner(int $premadeId, int $newOwnerId): void +{ + premades_api_request('POST', '/' . $premadeId . '/transfer', ['new_owner_id' => $newOwnerId]); +} + +/** + * Ermittelt die Beziehung eines Discord-Nutzers zu einer Liste von Premades: 'owner' + * (Lead) oder 'accepted' (Mitglied). Laut Bot-Logik kann ein Nutzer nie mehr als einer + * Premade gleichzeitig als Owner/Mitglied angehoeren (siehe is_user_in_any_premade im Bot). + * + * @param array> $premades + * @return array{premade: array, status: string}|null + */ +function find_my_premade(array $premades, int $discordId): ?array +{ + foreach ($premades as $premade) { + if ((int) ($premade['owner_id'] ?? 0) === $discordId) { + return ['premade' => $premade, 'status' => 'owner']; + } + foreach ($premade['members'] ?? [] as $member) { + if ((int) ($member['user_id'] ?? 0) === $discordId && ($member['status'] ?? '') === 'accepted') { + return ['premade' => $premade, 'status' => 'accepted']; + } + } + } + return null; +} + +/** + * @param array> $premades + * @return array> Alle Premades, bei denen der Nutzer eine offene + * (noch nicht angenommene) Einladung hat. + */ +function find_my_pending_invites(array $premades, int $discordId): array +{ + $result = []; + foreach ($premades as $premade) { + foreach ($premade['members'] ?? [] as $member) { + if ((int) ($member['user_id'] ?? 0) === $discordId && ($member['status'] ?? '') === 'pending') { + $result[] = $premade; + break; + } + } + } + return $result; +} + /** * Sucht einen einzelnen Raid (inkl. aktueller Signups) anhand seiner ID. * diff --git a/install.php b/install.php index 4152211..c01587f 100644 --- a/install.php +++ b/install.php @@ -53,6 +53,8 @@ function seed_defaults(PDO $pdo): array ->execute([$memberRoleId, $permissionIds['dashboard.view']]); $pdo->prepare('INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)') ->execute([$memberRoleId, $permissionIds['raids.view']]); + $pdo->prepare('INSERT IGNORE INTO role_permissions (role_id, permission_id) VALUES (?, ?)') + ->execute([$memberRoleId, $permissionIds['premades.view']]); return ['admin_role_id' => $adminRoleId]; } diff --git a/premade_action.php b/premade_action.php new file mode 100644 index 0000000..9d1ffff --- /dev/null +++ b/premade_action.php @@ -0,0 +1,133 @@ +getMessage()); +} + +redirect('/premades.php'); diff --git a/premades.php b/premades.php new file mode 100644 index 0000000..8fc8795 --- /dev/null +++ b/premades.php @@ -0,0 +1,238 @@ +getMessage(); +} + +// Alle vorkommenden Discord-IDs (Owner + Mitglieder) auf Website-Accounts abbilden, +// damit echte Anzeigenamen statt roher Discord-IDs gezeigt werden koennen. +$allDiscordIds = []; +foreach ($premades as $p) { + $allDiscordIds[] = (int) ($p['owner_id'] ?? 0); + foreach ($p['members'] ?? [] as $m) { + $allDiscordIds[] = (int) ($m['user_id'] ?? 0); + } +} +$allDiscordIds = array_values(array_unique(array_filter($allDiscordIds))); + +$namesByDiscordId = []; +if (!empty($allDiscordIds)) { + $placeholders = implode(',', array_fill(0, count($allDiscordIds), '?')); + $stmt = db()->prepare("SELECT discord_id, discord_username, display_name FROM users WHERE discord_id IN ($placeholders)"); + $stmt->execute($allDiscordIds); + foreach ($stmt->fetchAll() as $row) { + $name = $row['display_name'] !== '' ? $row['display_name'] : $row['discord_username']; + $namesByDiscordId[(int) $row['discord_id']] = $name; + } +} + +function premade_name_for(int $discordId, array $namesByDiscordId): string +{ + return $namesByDiscordId[$discordId] ?? ('Discord-Nutzer ' . $discordId); +} + +$myRelation = $discordId ? find_my_premade($premades, $discordId) : null; +$myPendingInvites = $discordId ? find_my_pending_invites($premades, $discordId) : []; + +// Kandidaten fuer "Mitglied einladen": verknuepfte Website-Nutzer, die aktuell weder +// Lead noch akzeptiertes Mitglied einer Premade sind (identisch zur Regel im Bot). +$busyDiscordIds = []; +foreach ($premades as $p) { + $busyDiscordIds[] = (int) ($p['owner_id'] ?? 0); + foreach ($p['members'] ?? [] as $m) { + if (($m['status'] ?? '') === 'accepted') { + $busyDiscordIds[] = (int) ($m['user_id'] ?? 0); + } + } +} +$inviteCandidates = []; +foreach (db()->query("SELECT discord_id, discord_username, display_name FROM users WHERE discord_id IS NOT NULL AND status = 'active' ORDER BY display_name, username")->fetchAll() as $row) { + if (in_array((int) $row['discord_id'], $busyDiscordIds, true)) { + continue; + } + $inviteCandidates[] = $row; +} + +$pageTitle = 'Premades'; +include __DIR__ . '/includes/header.php'; +?> +
+

Premades

+

Feste Gruppen für Raids. Jeder kann höchstens einer Premade gleichzeitig angehören.

+
+ + +
+
Premades konnten nicht geladen werden:
+
+ + + +
+

Meine Einladungen

+ +
+
+ Einladung zu + von +
+
+
+ + + + +
+
+ + + + +
+
+
+ +
+ + + +
+

Neue Premade gründen

+
+ + + + +
+
+ +
+

Verknüpfe deinen Discord-Account, um eine Premade zu gründen oder beizutreten.

+
+ + + +
+

Aktuell gibt es keine Premade-Gruppen.

+
+ + + ($m['status'] ?? '') === 'accepted' && (int) ($m['user_id'] ?? 0) !== $ownerId); + $pending = array_filter($members, fn ($m) => ($m['status'] ?? '') === 'pending'); + $canManageThis = $canManage || ($discordId !== null && $ownerId === $discordId); + ?> +
+
+

⚔️

+
+ 👑 + Mitglieder +
+
+ +
    + +
  • + + +
    + + + + + +
    + +
  • + + +
  • + ⏳ (Einladung offen) + +
    + + + + + +
    + +
  • + + +
  • Noch keine weiteren Mitglieder.
  • + +
+ + +
+
+ + + + + +
+ + +
+ + + + + +
+ + + +
+ + + + + +
+ + +
+ + + + +
+
+ +
+ + + + +