Signup is now a single "Anmelden" button that opens a class-icon
picker + optional group choice. A new raid_wish_is_sensible() mirrors
the bot's exact role-cap algorithm (Tank/Heal 1, DPS 3 per 5-player
group, ROLE_MAPPING copied from teamplaner.py) to decide server-side
whether the requested group is honored or the request falls back to
the bot's own auto-assignment - with a message telling the member to
ping the raid lead if they wanted something specific. Verified the
cap logic with 7 synthetic cases (own-signup exclusion, full group,
role caps, bench always allowed) before wiring it up.
Admins get a per-raid edit-mode toggle (pencil icon) that reveals:
inline raid editing (title/date/requirements/gearscore/groups/slots/
group locks) via PATCH, a premade-invite mini-form via the new
premades endpoint, and native HTML5 drag-and-drop to move a signup
between groups/bench via PATCH .../signup/{user_id}.
New pieces: includes/raids_api.php gained move_signup(), update_raid(),
invite_premade(), fetch_premades(), fetch_raid_by_id() and the wish-
sensibility check; four new JSON action endpoints (raid_signup_action,
raid_move_action, raid_edit_action, raid_premade_invite_action) reusing
the session CSRF token via a new verify_csrf_token()/read_json_body()/
json_response() trio in functions.php; assets/js/raids.js drives the
modal, edit-mode, and drag-and-drop with plain fetch() (no framework).
Read-heavy parts (page render, class icons, signup-wish logic) verified
against real data/live API. Edit, move and premade-invite are NOT
tested against the live API yet since they'd mutate the real raid or
send real Discord DMs to guild members - flagging this for a
supervised live check before considering it fully verified.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
282 lines
8.4 KiB
PHP
282 lines
8.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');
|
|
}
|
|
|
|
/**
|
|
* @param int|null $group Feste Zielgruppe (0 = Ersatzbank) oder null, damit der Bot
|
|
* automatisch nach Rollen-Caps zuteilt.
|
|
* @return array<string, mixed> Die von der API zugewiesene Anmeldung (u.a. tatsaechliche "group").
|
|
* @throws RuntimeException
|
|
*/
|
|
function raid_signup(int $raidId, int $discordId, string $discordName, string $className, ?int $group): array
|
|
{
|
|
$data = raids_api_request('POST', '/' . $raidId . '/signup', [], [
|
|
'user_id' => $discordId,
|
|
'name' => $discordName,
|
|
'class' => $className,
|
|
'group' => $group,
|
|
]);
|
|
|
|
return $data['signup'] ?? [];
|
|
}
|
|
|
|
/** @throws RuntimeException */
|
|
function raid_unsignup(int $raidId, int $discordId): void
|
|
{
|
|
raids_api_request('DELETE', '/' . $raidId . '/signup/' . $discordId);
|
|
}
|
|
|
|
/** @throws RuntimeException */
|
|
function move_signup(int $raidId, int $discordId, int $group): void
|
|
{
|
|
raids_api_request('PATCH', '/' . $raidId . '/signup/' . $discordId, [], ['group' => $group]);
|
|
}
|
|
|
|
/**
|
|
* @param array<string, mixed> $fields Nur die zu aendernden Felder, z.B.
|
|
* ['title' => ..., 'date_raw' => ..., 'requirements' => ..., 'gearscore' => ...,
|
|
* 'group_names' => ..., 'max_slots' => ..., 'locked_groups' => [1, 3]]
|
|
* @throws RuntimeException
|
|
*/
|
|
function update_raid(int $raidId, array $fields): void
|
|
{
|
|
raids_api_request('PATCH', '/' . $raidId, [], $fields);
|
|
}
|
|
|
|
/** @throws RuntimeException */
|
|
function invite_premade(int $raidId, int $premadeId, int $group): int
|
|
{
|
|
$data = raids_api_request('POST', '/' . $raidId . '/premades/' . $premadeId . '/invite', [], ['group' => $group]);
|
|
|
|
return (int) ($data['invited_count'] ?? 0);
|
|
}
|
|
|
|
/**
|
|
* @return array<int, array<string, mixed>>
|
|
* @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.');
|
|
}
|
|
|
|
// /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)) {
|
|
throw new RuntimeException('Unerwartetes Antwortformat der Premades-API.');
|
|
}
|
|
|
|
return $data['premades'];
|
|
}
|
|
|
|
/**
|
|
* Sucht einen einzelnen Raid (inkl. aktueller Signups) anhand seiner ID.
|
|
*
|
|
* @return array<string, mixed>|null
|
|
* @throws RuntimeException
|
|
*/
|
|
function fetch_raid_by_id(int $raidId): ?array
|
|
{
|
|
foreach (fetch_raids('all') as $raid) {
|
|
if ((int) ($raid['raid_id'] ?? 0) === $raidId) {
|
|
return $raid;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
// Rollen-Zuordnung 1:1 aus ROLE_MAPPING in teamplaner.py uebernommen.
|
|
const RAID_ROLE_MAPPING = [
|
|
'Templer' => 'Tank',
|
|
'Kleriker' => 'Heal',
|
|
'Kantor' => 'Heal',
|
|
'Gladiator' => 'DPS',
|
|
'Assassine' => 'DPS',
|
|
'Jäger' => 'DPS',
|
|
'Magier' => 'DPS',
|
|
'Beschwörer' => 'DPS',
|
|
];
|
|
const RAID_GROUP_SIZE = 5;
|
|
const RAID_ROLE_CAPS = ['Tank' => 1, 'Heal' => 1, 'DPS' => 3];
|
|
|
|
/**
|
|
* Prueft, ob eine gewuenschte Gruppe fuer eine Klasse nach den gleichen Rollen-Caps
|
|
* Sinn ergeben wuerde wie der Smart-Sort-Algorithmus des Bots (Tank/Heal je max. 1,
|
|
* DPS max. 3 pro 5er-Gruppe). Die Ersatzbank (Gruppe 0) ist immer erlaubt.
|
|
*
|
|
* @param array<int, array<string, mixed>> $signups Aktuelle Anmeldungen des Raids.
|
|
*/
|
|
function raid_wish_is_sensible(array $signups, int $discordId, string $className, int $desiredGroup): bool
|
|
{
|
|
if ($desiredGroup <= 0) {
|
|
return true;
|
|
}
|
|
|
|
$role = RAID_ROLE_MAPPING[$className] ?? 'DPS';
|
|
$counts = ['Tank' => 0, 'Heal' => 0, 'DPS' => 0];
|
|
$total = 0;
|
|
|
|
foreach ($signups as $signup) {
|
|
if ((int) ($signup['user_id'] ?? 0) === $discordId) {
|
|
continue; // eigene bestehende Anmeldung nicht mitzaehlen
|
|
}
|
|
if ((int) ($signup['group'] ?? 0) !== $desiredGroup) {
|
|
continue;
|
|
}
|
|
$memberRole = RAID_ROLE_MAPPING[$signup['class'] ?? ''] ?? 'DPS';
|
|
$counts[$memberRole]++;
|
|
$total++;
|
|
}
|
|
|
|
if ($total >= RAID_GROUP_SIZE) {
|
|
return false;
|
|
}
|
|
|
|
return $counts[$role] < RAID_ROLE_CAPS[$role];
|
|
}
|