LuminaWebsite/raid_signup_action.php
Maaxxs 73b670a305 Block joining locked groups and fall back to the bench when none are open
The Wunschgruppe picker let members select a locked group, and the
signup endpoint didn't check locked_groups at all, so the wish could
still go through. Locked groups are now passed to the frontend and
rendered disabled with a lock icon, and the server rejects a locked
group the same way it already rejects a role-capped one. When every
group is locked (or the raid has none), the signup is now assigned
straight to the Ersatzbank instead of relying on the bot's own
auto-assign to skip locked groups.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-19 06:57:45 +02:00

92 lines
3.8 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raids_api.php';
require_permission('raids.view');
$input = read_json_body();
verify_csrf_token($input['csrf_token'] ?? '');
$user = current_user();
if (empty($user['discord_id'])) {
json_response(false, 'Bitte verknüpfe zuerst deinen Discord-Account in "Mein Profil".');
}
$raidId = (int) ($input['raid_id'] ?? 0);
$class = trim((string) ($input['class'] ?? ''));
$groupRaw = $input['group'] ?? null; // null/"auto" = automatische Zuteilung, sonst gewuenschte Gruppe (0 = Bank)
if ($raidId <= 0 || $class === '') {
json_response(false, 'Bitte eine Klasse auswählen.');
}
try {
$raid = fetch_raid_by_id($raidId);
if (!$raid) {
json_response(false, 'Raid wurde nicht gefunden.');
}
if (!empty($raid['archived'])) {
json_response(false, 'Dieser Raid ist archiviert. Anmeldungen sind geschlossen.');
}
$maxGroups = count(array_filter(array_map('trim', explode(',', (string) ($raid['groups'] ?? '')))));
$signups = is_array($raid['signups'] ?? null) ? $raid['signups'] : [];
$discordId = (int) $user['discord_id'];
$lockedGroups = is_array($raid['locked_groups'] ?? null) ? array_map('intval', $raid['locked_groups']) : [];
$openGroups = $maxGroups >= 1 ? array_values(array_diff(range(1, $maxGroups), $lockedGroups)) : [];
$wantsAuto = $groupRaw === null || $groupRaw === '' || $groupRaw === 'auto';
$matchedWish = true;
$wishRejectReason = null; // 'locked' | 'full'
$requestGroup = null;
if (!$wantsAuto) {
$desiredGroup = (int) $groupRaw;
if ($desiredGroup < 0 || $desiredGroup > $maxGroups) {
json_response(false, 'Ungültige Gruppe ausgewählt.');
}
if ($desiredGroup > 0 && in_array($desiredGroup, $lockedGroups, true)) {
$requestGroup = null; // Gesperrte Gruppe: wie eine nicht erfuellbare Wunschgruppe behandeln
$matchedWish = false;
$wishRejectReason = 'locked';
} elseif (raid_wish_is_sensible($signups, $discordId, $class, $desiredGroup)) {
$requestGroup = $desiredGroup;
} else {
$requestGroup = null; // Bot automatisch zuteilen lassen
$matchedWish = false;
$wishRejectReason = 'full';
}
}
// Ist keine einzige Gruppe offen (alle gesperrt oder der Raid hat gar keine
// Gruppen), landet man deterministisch auf der Ersatzbank, statt auf eine
// automatische Zuteilung in eine gesperrte Gruppe zu hoffen.
if ($requestGroup === null && empty($openGroups)) {
$requestGroup = 0;
}
$assigned = raid_signup($raidId, $discordId, (string) $user['discord_username'], $class, $requestGroup);
$assignedGroup = (int) ($assigned['group'] ?? 0);
$groupNames = array_values(array_filter(array_map('trim', explode(',', (string) ($raid['groups'] ?? '')))));
$assignedGroupLabel = $assignedGroup === 0 ? 'Ersatzbank' : ($groupNames[$assignedGroup - 1] ?? ('Gruppe ' . $assignedGroup));
if ($matchedWish) {
json_response(true, 'Du bist angemeldet: ' . $class . ' in ' . $assignedGroupLabel . '.', [
'group' => $assignedGroup,
'matched_wish' => true,
]);
} else {
$reasonText = $wishRejectReason === 'locked'
? 'Deine Wunschgruppe ist gesperrt'
: 'Deine Wunschgruppe war schon voll besetzt für diese Rolle';
json_response(true, $reasonText . ', daher wurdest du automatisch zugeteilt: '
. $assignedGroupLabel . '. Melde dich gerne beim Raid-Lead, falls du eine Anpassung möchtest.', [
'group' => $assignedGroup,
'matched_wish' => false,
]);
}
} catch (Throwable $e) {
json_response(false, 'Anmeldung fehlgeschlagen: ' . $e->getMessage());
}