diff --git a/README.md b/README.md index 8c46c1d..e369d00 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,30 @@ 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. +### Raids über die Website erstellen + +Mitglieder mit `raids.manage` können über den Button "+ Neuer Raid" auf der Raids-Seite direkt +einen neuen Raid anlegen (Titel, Datum, Anforderungen, Gruppen, Gearscore, Max. Spieler) - ganz +ohne eine Discord-Kanal-ID zu kennen. Die API postet die Raid-Nachricht automatisch in den +Standard-Raid-Kanal des Servers, den du einmalig im Bot mit `/set_raidchannel` festlegst (oder +über `PATCH /api/settings/{guild_id}` mit `{"raid_channel": ...}`). + +Dafür in `config.php` unter `raids_api` die Discord-**Server**-ID (nicht Kanal-ID) ergänzen: +```php +'raids_api' => [ + 'url' => 'https://api.maaxxs-coding.de/api/raids', + 'key' => 'dein-api-key', + 'guild_id' => 123456789012345678, +], +``` +Ohne `/set_raidchannel` im Bot bzw. `guild_id` in `config.php` schlägt das Erstellen mit einer +verständlichen Fehlermeldung fehl - alle anderen Funktionen der Raids-Seite sind davon unabhängig. + +Beim Bearbeiten eines Raids postet die API die Discord-Nachricht außerdem automatisch neu, falls +sie zwischenzeitlich manuell gelöscht wurde (die `raid_id` entspricht der Discord-Message-ID und +ändert sich in diesem Fall) - die Website zeigt das nach dem Speichern im Editor-Modus an und lädt +danach neu, sodass immer der aktuelle Stand sichtbar ist. + ### Premades Bildet die Premade-Verwaltung des Discord-Bots (`/premade ...`) auf der Website nach: Gründen, diff --git a/assets/css/style.css b/assets/css/style.css index a987757..15735f6 100644 --- a/assets/css/style.css +++ b/assets/css/style.css @@ -400,6 +400,7 @@ input:focus, select:focus, textarea:focus { .flash-error { background: rgba(217, 83, 79, 0.15); border: 1px solid var(--danger); color: var(--danger); } .text-muted { color: var(--text-muted); } +.text-muted.error { color: var(--danger); } .hero { text-align: center; diff --git a/assets/js/raids.js b/assets/js/raids.js index 308fa4c..10f38f5 100644 --- a/assets/js/raids.js +++ b/assets/js/raids.js @@ -192,23 +192,28 @@ }); // ---- Gruppen-Editor: Zeilen hinzufuegen/entfernen ---- - function createGroupEditorRow(name) { + // withLock=false blendet den Sperren-Toggle aus (z.B. beim Erstellen eines neuen + // Raids, wo "Gruppen sperren" von der API noch nicht unterstuetzt wird). + function createGroupEditorRow(name, withLock) { var row = document.createElement('div'); row.className = 'group-editor-row'; - row.innerHTML = - '' + - '' + ICON_LOCK + '' + - '' + ICON_X + ''; + var html = ''; + if (withLock) { + html += '' + ICON_LOCK + ''; + } + html += '' + ICON_X + ''; + row.innerHTML = html; row.querySelector('.group-editor-name').value = name || ''; return row; } document.querySelectorAll('.group-editor').forEach(function (editor) { + var withLock = editor.getAttribute('data-locks') !== '0'; var rows = editor.querySelector('.group-editor-rows'); var addBtn = editor.querySelector('.group-editor-add'); addBtn.addEventListener('click', function () { - var row = createGroupEditorRow(''); + var row = createGroupEditorRow('', withLock); rows.appendChild(row); row.querySelector('.group-editor-name').focus(); }); @@ -226,6 +231,52 @@ }); }); + // ---- Neuen Raid erstellen ---- + var raidCreateToggle = document.getElementById('raid-create-toggle'); + var raidCreatePanel = document.getElementById('raid-create-panel'); + if (raidCreateToggle && raidCreatePanel) { + raidCreateToggle.addEventListener('click', function () { + raidCreatePanel.style.display = raidCreatePanel.style.display === 'none' ? 'block' : 'none'; + }); + } + + var raidCreateForm = document.getElementById('raid-create-form'); + if (raidCreateForm) { + raidCreateForm.addEventListener('submit', function (e) { + e.preventDefault(); + var feedback = raidCreateForm.querySelector('.raid-create-feedback'); + + var groupNames = []; + raidCreateForm.querySelectorAll('.group-editor-row').forEach(function (row) { + var name = row.querySelector('.group-editor-name').value.trim(); + if (name !== '') groupNames.push(name); + }); + if (groupNames.length === 0) { + feedback.textContent = 'Bitte mindestens eine Gruppe angeben.'; + feedback.className = 'raid-create-feedback text-muted error'; + return; + } + + feedback.textContent = 'Wird erstellt …'; + feedback.className = 'raid-create-feedback text-muted'; + postJson('/raid_create_action.php', { + title: raidCreateForm.title.value, + date_raw: raidCreateForm.date_raw.value, + requirements: raidCreateForm.requirements.value, + gearscore: raidCreateForm.gearscore.value, + group_names: groupNames.join(', '), + max_slots: raidCreateForm.max_slots.value + }).then(function (result) { + feedback.textContent = result.message || ''; + if (result.status === 'success') { + setTimeout(function () { window.location.reload(); }, 900); + } + }).catch(function () { + feedback.textContent = 'Verbindung fehlgeschlagen.'; + }); + }); + } + // ---- Premade einladen ---- document.querySelectorAll('.raid-premade-form').forEach(function (form) { form.addEventListener('submit', function (e) { diff --git a/config.example.php b/config.example.php index 091db47..2cdf9e2 100644 --- a/config.example.php +++ b/config.example.php @@ -16,6 +16,10 @@ return [ 'raids_api' => [ 'url' => 'https://api.maaxxs-coding.de/api/raids', 'key' => 'dein-api-key', + // Noetig, damit die Website selbst Raids erstellen kann: die API postet sie + // automatisch in den per /set_raidchannel im Bot konfigurierten Standard-Raid-Kanal + // dieses Servers. Ohne raid_channel-Konfiguration im Bot schlaegt das Erstellen fehl. + 'guild_id' => 'deine-discord-server-id', ], 'discord' => [ 'client_id' => 'deine-discord-client-id', diff --git a/includes/raids_api.php b/includes/raids_api.php index d88d420..23f90b5 100644 --- a/includes/raids_api.php +++ b/includes/raids_api.php @@ -167,11 +167,42 @@ function move_signup(int $raidId, int $discordId, int $group): void * @param array $fields Nur die zu aendernden Felder, z.B. * ['title' => ..., 'date_raw' => ..., 'requirements' => ..., 'gearscore' => ..., * 'group_names' => ..., 'max_slots' => ..., 'locked_groups' => [1, 3]] + * @return array Antwort der API. Enthaelt die aktuell gueltige raid_id - + * die API postet die Discord-Nachricht automatisch neu, falls sie zwischenzeitlich + * manuell geloescht wurde, wobei sich die raid_id (= Discord-Message-ID) aendert. * @throws RuntimeException */ -function update_raid(int $raidId, array $fields): void +function update_raid(int $raidId, array $fields): array { - raids_api_request('PATCH', '/' . $raidId, [], $fields); + return raids_api_request('PATCH', '/' . $raidId, [], $fields); +} + +/** + * Erstellt einen neuen Raid ueber den in config.php (raids_api.guild_id) hinterlegten + * Discord-Server. Die API postet ihn automatisch in den per /set_raidchannel im Bot + * konfigurierten Standard-Raid-Kanal dieses Servers - die Website muss dafuer keine + * Kanal-ID kennen/verwalten. + * + * @param array $fields title, date_raw, requirements, group_names + * (Pflicht), max_slots, gearscore (optional) + * @return array Antwort der API inkl. raid_id + * @throws RuntimeException + */ +function create_raid(array $fields): array +{ + $configPath = __DIR__ . '/../config.php'; + if (!file_exists($configPath)) { + throw new RuntimeException('Konfigurationsdatei fehlt.'); + } + $config = require $configPath; + $guildId = $config['raids_api']['guild_id'] ?? null; + if (!$guildId) { + throw new RuntimeException('Raid-Erstellung ist nicht konfiguriert (config.php: raids_api.guild_id fehlt).'); + } + + $fields['guild_id'] = (int) $guildId; + + return raids_api_request('POST', '', [], $fields); } /** @throws RuntimeException */ diff --git a/raid_create_action.php b/raid_create_action.php new file mode 100644 index 0000000..062e3c4 --- /dev/null +++ b/raid_create_action.php @@ -0,0 +1,32 @@ + $title, + 'date_raw' => $dateRaw, + 'requirements' => $requirements, + 'group_names' => $groupNames, + 'gearscore' => $gearscore, + 'max_slots' => $maxSlots, + ]); + json_response(true, 'Raid wurde erstellt.', ['raid_id' => $result['raid_id'] ?? null]); +} catch (Throwable $e) { + json_response(false, 'Erstellen fehlgeschlagen: ' . $e->getMessage()); +} diff --git a/raid_edit_action.php b/raid_edit_action.php index e402b3d..47455ad 100644 --- a/raid_edit_action.php +++ b/raid_edit_action.php @@ -29,8 +29,16 @@ if (empty($fields)) { } try { - update_raid($raidId, $fields); - json_response(true, 'Raid wurde aktualisiert.'); + $result = update_raid($raidId, $fields); + $newRaidId = (int) ($result['raid_id'] ?? $raidId); + + // Die API postet die Discord-Nachricht automatisch neu, falls sie zwischenzeitlich + // manuell geloescht wurde - die raid_id (= Discord-Message-ID) aendert sich dann. + if ($newRaidId !== $raidId) { + json_response(true, 'Raid wurde aktualisiert. Die Discord-Nachricht war nicht mehr auffindbar und wurde neu gepostet.', ['raid_id' => $newRaidId]); + } else { + json_response(true, 'Raid wurde aktualisiert.', ['raid_id' => $newRaidId]); + } } catch (Throwable $e) { json_response(false, 'Speichern fehlgeschlagen: ' . $e->getMessage()); } diff --git a/raids.php b/raids.php index 46f844b..000cb64 100644 --- a/raids.php +++ b/raids.php @@ -77,7 +77,12 @@ $pageTitle = 'Raids'; include __DIR__ . '/includes/header.php'; ?> - Raids + + Raids + + + Neuer Raid + + Raid-Termine deiner Gilde. $label): ?> @@ -86,6 +91,50 @@ include __DIR__ . '/includes/header.php'; + + + Neuen Raid erstellen + + + + Titel + + + + Datum & Uhrzeit + + + + Max. Spieler + + + + Gearscore + + + + + Anforderungen + + + + Gruppen + + + + = icon('x') ?> + + + + Gruppe hinzufügen + + + Raid erstellen + + + + + + Raid-Daten konnten nicht geladen werden: = e($apiError) ?>
Raid-Termine deiner Gilde.