LuminaWebsite/includes/raids_api.php
Maaxxs 8c703f0e1f Add Discord account linking and raid signup/management from the website
- Class name on raid signups now shows only as an icon tooltip instead
  of trailing text (falls back to text when no icon matches).
- Discord OAuth2 linking (discord_login.php/discord_callback.php,
  includes/discord.php): members connect their Discord account from
  the dashboard, storing discord_id/discord_username so raid signups
  can be attributed to their real Discord identity. Needs a Discord
  application's client_id/client_secret in config.php.
- raids.php gets self-service signup/withdraw controls (class + group
  picker, upsert semantics) for members with a linked Discord account,
  plus archive/restore controls gated behind a new raids.manage
  permission. New action scripts raid_signup.php, raid_unsignup.php,
  raid_archive.php, raid_restore.php call the corresponding
  includes/raids_api.php functions.
- Archive/restore already work today (verified live against the bot's
  API - archived and restored the one active raid, signups intact).
  Signup/withdraw are fully wired on the website side but need two new
  bot-side endpoints (POST .../signup, DELETE .../signup/{user_id})
  that don't exist yet - contract documented in the README.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-18 13:25:41 +02:00

140 lines
4 KiB
PHP

<?php
declare(strict_types=1);
const RAID_STATUSES = ['active', 'archived', 'all'];
/**
* @return array<string, mixed>
* @throws RuntimeException
*/
function raids_api_config(): 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 (config.php: raids_api.url / raids_api.key fehlt).');
}
return ['url' => rtrim($apiUrl, '/'), 'key' => $apiKey];
}
/**
* Fuehrt einen Request gegen die Raid-API aus und liefert die dekodierte
* JSON-Antwort zurueck.
*
* @param string $path Wird an die Basis-URL angehaengt, z.B. "/123/signup".
* @param array<string, mixed>|null $jsonBody Wird bei POST/DELETE als JSON-Body mitgeschickt.
* @return array<string, mixed>
* @throws RuntimeException
*/
function raids_api_request(string $method, string $path = '', array $queryParams = [], ?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();
$url = $config['url'] . $path;
if (!empty($queryParams)) {
$url .= '?' . http_build_query($queryParams);
}
$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 Raid-API fehlgeschlagen: ' . $curlError);
}
$data = json_decode($response, true);
if ($httpCode < 200 || $httpCode >= 300) {
$detail = is_array($data) ? ($data['detail'] ?? '') : '';
$message = 'Raid-API antwortete mit Statuscode ' . $httpCode . '.';
if ($detail !== '') {
$message .= ' (' . $detail . ')';
}
throw new RuntimeException($message);
}
if (!is_array($data)) {
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
}
return $data;
}
/**
* @return array<int, array<string, mixed>>
* @throws RuntimeException
*/
function fetch_raids(string $status = 'active'): array
{
if (!in_array($status, RAID_STATUSES, true)) {
$status = 'active';
}
$data = raids_api_request('GET', '', ['status' => $status]);
if (($data['status'] ?? '') !== 'success' || !isset($data['raids']) || !is_array($data['raids'])) {
throw new RuntimeException('Unerwartetes Antwortformat der Raid-API.');
}
return $data['raids'];
}
/** @throws RuntimeException */
function archive_raid(int $raidId): void
{
raids_api_request('DELETE', '/' . $raidId);
}
/** @throws RuntimeException */
function restore_raid(int $raidId): void
{
raids_api_request('POST', '/' . $raidId . '/restore');
}
/** @throws RuntimeException */
function raid_signup(int $raidId, int $discordId, string $discordName, string $className, int $group): void
{
raids_api_request('POST', '/' . $raidId . '/signup', [], [
'user_id' => $discordId,
'name' => $discordName,
'class' => $className,
'group' => $group,
]);
}
/** @throws RuntimeException */
function raid_unsignup(int $raidId, int $discordId): void
{
raids_api_request('DELETE', '/' . $raidId . '/signup/' . $discordId);
}