LuminaWebsite/premades.php
Maaxxs 1ca3f2e339 Premades: own group first, inline invite from empty slots, optional class
- premades.php sorts the list so the premade you're a lead/member of
  always renders first, instead of arbitrary API order.
- Empty roster slots are no longer static placeholders for managers:
  if there are invite-eligible linked members left, the slot becomes
  an inline "+ Mitglied einladen" select that submits on change,
  replacing the old separate invite dropdown in the manage panel
  (redundant now that every empty slot doubles as one).
- Members can optionally self-declare which class they play; shown
  under their name. Editable inline (auto-submit select) only on your
  own slot, read-only text for everyone else's. New premade_action.php
  action "set_class" re-validates that the caller actually belongs to
  that premade before writing (never trusts the posted premade_id
  alone). Extracted the class list shared with raids.php into
  AION_CLASSES in functions.php instead of duplicating it.
- Requires the matching Aion2_RaidPlaner bot/API change (separate repo,
  not tracked here): premade_members.class column, GET /api/premades
  returning class per member, and a new PATCH .../members/{user_id}
  endpoint - already implemented and saved to disk, bot needs a
  restart to pick it up. The bot's own premade overview embed and
  admin select menus were updated to show class too, so the Discord
  side stays visually consistent with the website.

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

306 lines
17 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raids_api.php';
require_permission('premades.view');
$user = current_user();
$discordId = $user['discord_id'] ? (int) $user['discord_id'] : null;
$canManage = user_has_permission('premades.manage');
$premades = [];
$apiError = null;
try {
$premades = fetch_premades();
} catch (Throwable $e) {
$apiError = $e->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 = [];
$linkedUsersByDiscordId = [];
if (!empty($allDiscordIds)) {
$placeholders = implode(',', array_fill(0, count($allDiscordIds), '?'));
$stmt = db()->prepare("SELECT id, discord_id, discord_username, display_name, avatar_filename 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;
$linkedUsersByDiscordId[(int) $row['discord_id']] = $row;
}
}
// Bevorzugt den von der Bot-API aufgeloesten Discord-Anzeigenamen (die API kennt Discord direkt,
// die Website nicht); faellt auf den verknuepften Website-Account und zuletzt auf die rohe
// Discord-ID zurueck, falls beides fehlt (z.B. Bot offline beim API-Aufruf).
function premade_name_for(int $discordId, array $namesByDiscordId, ?string $apiName = null): string
{
if ($apiName !== null && $apiName !== '') {
return $apiName;
}
return $namesByDiscordId[$discordId] ?? ('Discord-Nutzer ' . $discordId);
}
// Baut ein fuer avatar_html() nutzbares Nutzer-Array: das echte, hochgeladene Profilbild,
// falls die Discord-ID zu einem verknuepften Website-Account gehoert, sonst ein Platzhalter
// mit dem Anfangsbuchstaben des (von der API aufgeloesten) Namens.
function premade_avatar_user(int $discordId, array $linkedUsersByDiscordId, string $displayName): array
{
if (isset($linkedUsersByDiscordId[$discordId])) {
return $linkedUsersByDiscordId[$discordId];
}
return ['id' => $discordId, 'display_name' => $displayName, 'username' => $displayName, 'avatar_filename' => null];
}
$myRelation = $discordId ? find_my_premade($premades, $discordId) : null;
$myPendingInvites = $discordId ? find_my_pending_invites($premades, $discordId) : [];
// Die eigene Premade (als Lead oder Mitglied) steht immer ganz oben in der Liste.
if ($myRelation !== null) {
$myPremadeId = (int) ($myRelation['premade']['id'] ?? 0);
usort($premades, function (array $a, array $b) use ($myPremadeId): int {
$aMine = (int) ($a['id'] ?? 0) === $myPremadeId;
$bMine = (int) ($b['id'] ?? 0) === $myPremadeId;
if ($aMine === $bMine) {
return 0;
}
return $aMine ? -1 : 1;
});
}
// 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';
?>
<div class="panel">
<h1>Premades</h1>
<p class="text-muted">Feste Gruppen für Raids. Jeder kann höchstens einer Premade gleichzeitig angehören.</p>
</div>
<?php if ($apiError): ?>
<div class="panel">
<div class="flash flash-error">Premades konnten nicht geladen werden: <?= e($apiError) ?></div>
</div>
<?php else: ?>
<?php if (!empty($myPendingInvites)): ?>
<div class="panel">
<h2>Meine Einladungen</h2>
<?php foreach ($myPendingInvites as $invite): ?>
<div class="premade-invite-row">
<div>
Einladung zu <strong><?= e((string) ($invite['name'] ?? '')) ?></strong>
von <?= e(premade_name_for((int) ($invite['owner_id'] ?? 0), $namesByDiscordId, $invite['owner_name'] ?? null)) ?>
</div>
<div class="actions">
<form method="post" action="/premade_action.php" class="inline-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="accept">
<input type="hidden" name="premade_id" value="<?= (int) ($invite['id'] ?? 0) ?>">
<button type="submit">Annehmen</button>
</form>
<form method="post" action="/premade_action.php" class="inline-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="decline">
<input type="hidden" name="premade_id" value="<?= (int) ($invite['id'] ?? 0) ?>">
<button type="submit" class="button-secondary">Ablehnen</button>
</form>
</div>
</div>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php if ($discordId && !$myRelation): ?>
<div class="panel">
<h2>Neue Premade gründen</h2>
<form method="post" action="/premade_action.php" class="raid-premade-row">
<?= csrf_field() ?>
<input type="hidden" name="action" value="create">
<input type="text" name="name" placeholder="Name der Premade-Gruppe" maxlength="50" required style="width:auto;flex:1;min-width:200px;">
<button type="submit">Gründen</button>
</form>
</div>
<?php elseif (!$discordId): ?>
<div class="panel">
<p class="text-muted">Verknüpfe deinen <a href="/profile.php">Discord-Account</a>, um eine Premade zu gründen oder beizutreten.</p>
</div>
<?php endif; ?>
<?php if (empty($premades)): ?>
<div class="panel">
<p class="text-muted">Aktuell gibt es keine Premade-Gruppen.</p>
</div>
<?php else: ?>
<?php foreach ($premades as $premade): ?>
<?php
$premadeId = (int) ($premade['id'] ?? 0);
$premadeName = (string) ($premade['name'] ?? '');
$ownerId = (int) ($premade['owner_id'] ?? 0);
$members = is_array($premade['members'] ?? null) ? $premade['members'] : [];
$accepted = array_filter($members, fn ($m) => ($m['status'] ?? '') === 'accepted' && (int) ($m['user_id'] ?? 0) !== $ownerId);
$pending = array_filter($members, fn ($m) => ($m['status'] ?? '') === 'pending');
$canManageThis = $canManage || ($discordId !== null && $ownerId === $discordId);
?>
<div class="panel">
<div class="raid-header">
<h2>⚔️ <?= e($premadeName) ?></h2>
<div class="badge-group">
<span class="badge"><?= count($accepted) + 1 ?> Mitglieder</span>
</div>
</div>
<?php
// Premades bilden eine volle 5er-Gruppe ab (Lead + bis zu 4 weitere) - freie
// Plaetze werden mit ausgegrauten Platzhaltern aufgefuellt.
$ownerName = premade_name_for($ownerId, $namesByDiscordId, $premade['owner_name'] ?? null);
$slots = [['type' => 'lead', 'id' => $ownerId, 'name' => $ownerName, 'class' => $premade['owner_class'] ?? null]];
foreach ($accepted as $member) {
$mid = (int) ($member['user_id'] ?? 0);
$slots[] = ['type' => 'accepted', 'id' => $mid, 'name' => premade_name_for($mid, $namesByDiscordId, $member['name'] ?? null), 'class' => $member['class'] ?? null];
}
foreach ($pending as $member) {
$mid = (int) ($member['user_id'] ?? 0);
$slots[] = ['type' => 'pending', 'id' => $mid, 'name' => premade_name_for($mid, $namesByDiscordId, $member['name'] ?? null), 'class' => $member['class'] ?? null];
}
while (count($slots) < 5) {
$slots[] = ['type' => 'empty'];
}
?>
<div class="premade-roster">
<?php foreach ($slots as $slot): ?>
<?php if ($slot['type'] === 'empty'): ?>
<?php $isInvitable = $canManageThis && !empty($inviteCandidates); ?>
<div class="premade-slot premade-slot-empty<?= $isInvitable ? ' premade-slot-invitable' : '' ?>">
<div class="premade-slot-inner">
<?php if ($isInvitable): ?>
<form method="post" action="/premade_action.php" class="premade-invite-inline">
<?= csrf_field() ?>
<input type="hidden" name="action" value="invite">
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
<select name="user_id" onchange="this.form.submit()">
<option value="">+ Mitglied einladen</option>
<?php foreach ($inviteCandidates as $candidate): ?>
<option value="<?= (int) $candidate['discord_id'] ?>">
<?= e($candidate['display_name'] !== '' ? $candidate['display_name'] : $candidate['discord_username']) ?>
</option>
<?php endforeach; ?>
</select>
</form>
<?php else: ?>
<span class="premade-slot-placeholder">+</span>
<?php endif; ?>
</div>
</div>
<?php else: ?>
<?php
$slotUser = premade_avatar_user((int) $slot['id'], $linkedUsersByDiscordId, $slot['name']);
$isMe = $discordId !== null && (int) $slot['id'] === $discordId;
?>
<div class="premade-slot premade-slot-<?= e($slot['type']) ?>">
<div class="premade-slot-inner">
<?= avatar_html($slotUser, 40) ?>
<div class="premade-slot-name">
<strong><?= $slot['type'] === 'pending' ? '⏳ ' : '' ?><?= e($slot['name']) ?></strong>
<?php if ($slot['type'] === 'pending'): ?>
<span class="text-muted">Einladung offen</span>
<?php elseif ($isMe): ?>
<form method="post" action="/premade_action.php">
<?= csrf_field() ?>
<input type="hidden" name="action" value="set_class">
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
<select name="class" class="premade-class-select" onchange="this.form.submit()">
<option value="">Klasse wählen…</option>
<?php foreach (AION_CLASSES as $classOption): ?>
<option value="<?= e($classOption) ?>" <?= $slot['class'] === $classOption ? 'selected' : '' ?>><?= e($classOption) ?></option>
<?php endforeach; ?>
</select>
</form>
<?php elseif (!empty($slot['class'])): ?>
<span class="text-muted"><?= e($slot['class']) ?></span>
<?php endif; ?>
</div>
<?php if ($slot['type'] === 'lead'): ?>
<span class="premade-crown" title="Lead der Premade"><?= icon('crown') ?></span>
<?php elseif ($canManageThis): ?>
<form method="post" action="/premade_action.php" class="inline-form">
<?= csrf_field() ?>
<input type="hidden" name="action" value="remove_member">
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
<input type="hidden" name="user_id" value="<?= (int) $slot['id'] ?>">
<button type="submit" class="icon-button icon-button-danger" title="<?= $slot['type'] === 'pending' ? 'Einladung zurückziehen' : 'Aus der Premade entfernen' ?>"><?= icon('x') ?></button>
</form>
<?php endif; ?>
</div>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>
<?php if ($canManageThis): ?>
<div class="premade-manage">
<form method="post" action="/premade_action.php" class="raid-premade-row">
<?= csrf_field() ?>
<input type="hidden" name="action" value="rename">
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
<input type="text" name="name" value="<?= e($premadeName) ?>" style="width:auto;flex:1;min-width:160px;">
<button type="submit" class="button-secondary">Umbenennen</button>
</form>
<?php if (!empty($accepted)): ?>
<form method="post" action="/premade_action.php" class="raid-premade-row">
<?= csrf_field() ?>
<input type="hidden" name="action" value="transfer">
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
<select name="new_owner_id">
<?php foreach ($accepted as $candidate): ?>
<option value="<?= (int) ($candidate['user_id'] ?? 0) ?>">
<?= e(premade_name_for((int) ($candidate['user_id'] ?? 0), $namesByDiscordId, $candidate['name'] ?? null)) ?>
</option>
<?php endforeach; ?>
</select>
<button type="submit" class="button-secondary">Leitung übertragen</button>
</form>
<?php endif; ?>
<form method="post" action="/premade_action.php" class="inline-form" onsubmit="return confirm('Diese Premade wirklich auflösen?');">
<?= csrf_field() ?>
<input type="hidden" name="action" value="delete">
<input type="hidden" name="premade_id" value="<?= $premadeId ?>">
<button type="submit" class="button-danger">Gruppe auflösen</button>
</form>
</div>
<?php endif; ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php endif; ?>
<?php include __DIR__ . '/includes/footer.php'; ?>