Asked for: "füge ein, dass man sich für premaids auch bewerben kann" - every premade with a free place takes
applications unless its lead switches them off, and the bot sends DMs (a new application to the lead, a decline
to the applicant). Later: "man soll sich auch auf reserveplätze bewerben können".
- includes/premade_applications.php: the applications live on the website (premade_applications: premade ID and
name, lead at that time, applicant, class, wished place group/reserve, message, status open/accepted/declined/
withdrawn/closed, reason, who decided when; premade_options.applications_open). Rules: one open application
per premade, at most 3 open in total, 7 days pause after a decline by the same premade, none while invited,
when full or switched off. premade_applications_sync() matches them with the bot: applications of dissolved
premades are closed, whoever is in the premade by now counts as accepted, name and lead stay current,
decisions expire after 30 days.
- premades.php: "Bewerben" under every other premade with a free place. The popup asks for the place (group or
reserve, taken places greyed out; the group is preselected unless it is full), the class (main preselected)
and an optional message. "Meine Bewerbungen" lists the own ones with the answer for 14 days, withdrawing
works there and on the card. Leads and the leadership (premades.manage) see open applications on the card,
also outside the edit mode, with a "Reserve" tag and a confirmation that names the place; their number shows
at the menu item "Premades" (premades without a lead count for the leadership). The edit mode switches
applications off and on.
- premade_action.php: apply (checks the rules and the chosen place again), withdraw, application_accept (the
bot puts the applicant on the wished place, on the other one if it was taken meanwhile; an open invitation
becomes the membership on its place), application_decline (optional reason), applications_toggle. An
invitation to someone with an open application carries its class, also when accepted in Discord.
Dissolving a premade closes its applications, a new lead takes over the open ones.
- includes/raids_api.php: add_premade_member() passes the wished place and returns the place the bot gave;
premade_free_slots().
- Bot (already deployed, with a backup): POST /api/premades/{id}/applications sends the DMs (to the lead with
place, class, message and a link to the card; to the applicant with the reason), POST /members with source
"application" tells the applicant where they ended up and, when the leadership accepted, the lead;
add_premade_member() takes the wished place. If the bot cannot send a DM, the application is stored anyway.
- Live updates of the page and the menu number, schema version 62 (tables also in sql/schema.sql).
- Privacy: privacy policy (what is stored, who sees it, for how long), data export section premade_bewerbungen,
account deletion, data reset area "Premade-Bewerbungen".
- Patch notes and README.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
268 lines
12 KiB
PHP
268 lines
12 KiB
PHP
<?php
|
||
declare(strict_types=1);
|
||
|
||
/**
|
||
* Bewerbungen fuer Premades (premades.php, premade_action.php): Ein Mitglied bewirbt sich bei einer Premade, in der
|
||
* noch ein Platz frei ist - fuer die Gruppe oder die Reserve, mit der Klasse, als die es mitspielen moechte, und auf
|
||
* Wunsch einer kurzen Nachricht. Der Lead (bzw. die Leitung mit premades.manage) nimmt an oder lehnt ab, auf Wunsch
|
||
* mit Begruendung. Leads koennen Bewerbungen fuer ihre Gruppe abschalten.
|
||
*
|
||
* Die Premades liegen beim Discord-Bot, die Bewerbungen hier (premade_applications, premade_options je Premade-ID).
|
||
* Beim Annehmen traegt der Bot den Spieler auf den gewuenschten Platz ein (add_premade_member mit source
|
||
* 'application'), und er verschickt die PNs: an den Lead bei einer neuen Bewerbung, an den Bewerber bei Annahme und
|
||
* Ablehnung (premade_application_notify).
|
||
*/
|
||
|
||
const PREMADE_APPLICATION_MESSAGE_MAX = 500;
|
||
const PREMADE_APPLICATION_REASON_MAX = 500;
|
||
// So viele offene Bewerbungen darf ein Mitglied gleichzeitig haben
|
||
const PREMADE_APPLICATION_OPEN_MAX = 3;
|
||
// Nach einer Absage bei derselben Premade erst nach so vielen Tagen wieder
|
||
const PREMADE_APPLICATION_COOLDOWN_DAYS = 7;
|
||
// "Meine Bewerbungen" zeigt Entscheidungen so lange, danach verfallen sie (steht so in der Datenschutzerklaerung)
|
||
const PREMADE_APPLICATION_SHOW_DAYS = 14;
|
||
const PREMADE_APPLICATION_KEEP_DAYS = 30;
|
||
|
||
/** Offene Bewerbungen aller Premades mit Name und Profilbild der Bewerber: [premade_id => [Zeile, …]], aelteste zuerst. */
|
||
function premade_applications_open(): array
|
||
{
|
||
try {
|
||
$rows = db()->query(
|
||
"SELECT a.*, u.username, u.display_name, u.discord_username, u.avatar_filename
|
||
FROM premade_applications a JOIN users u ON u.id = a.user_id
|
||
WHERE a.status = 'open' ORDER BY a.created_at, a.id"
|
||
)->fetchAll();
|
||
} catch (PDOException $e) {
|
||
return []; // Tabelle kommt erst mit dem naechsten Schema-Abgleich
|
||
}
|
||
$out = [];
|
||
foreach ($rows as $row) {
|
||
$row['name'] = premade_applicant_name($row);
|
||
$out[(int) $row['premade_id']][] = $row;
|
||
}
|
||
return $out;
|
||
}
|
||
|
||
/** Anzeigename eines Bewerbers aus einer Zeile mit display_name, discord_username und username (Tabelle users). */
|
||
function premade_applicant_name(array $row): string
|
||
{
|
||
return (string) ((string) ($row['display_name'] ?? '') !== '' ? $row['display_name'] : (($row['discord_username'] ?? '') ?: ($row['username'] ?? '')));
|
||
}
|
||
|
||
/** Bewerbungen eines Mitglieds: die offenen und die in den letzten PREMADE_APPLICATION_SHOW_DAYS entschiedenen, neueste zuerst. */
|
||
function premade_applications_of(int $userId): array
|
||
{
|
||
try {
|
||
$stmt = db()->prepare(
|
||
"SELECT * FROM premade_applications
|
||
WHERE user_id = ? AND (status = 'open' OR decided_at > ?)
|
||
ORDER BY created_at DESC, id DESC"
|
||
);
|
||
$stmt->execute([$userId, date('Y-m-d H:i:s', time() - PREMADE_APPLICATION_SHOW_DAYS * 86400)]);
|
||
return $stmt->fetchAll();
|
||
} catch (PDOException $e) {
|
||
return [];
|
||
}
|
||
}
|
||
|
||
/** Premades, deren Lead gerade keine Bewerbungen annimmt: [premade_id => true]. */
|
||
function premade_applications_closed(): array
|
||
{
|
||
try {
|
||
$ids = db()->query('SELECT premade_id FROM premade_options WHERE applications_open = 0')->fetchAll(PDO::FETCH_COLUMN);
|
||
} catch (PDOException $e) {
|
||
return [];
|
||
}
|
||
return array_fill_keys(array_map('intval', $ids), true);
|
||
}
|
||
|
||
/** Bewerbungen fuer eine Premade erlauben oder abschalten (offene bleiben zur Entscheidung stehen). */
|
||
function premade_applications_set_open(int $premadeId, bool $open, int $userId): void
|
||
{
|
||
db()->prepare(
|
||
'INSERT INTO premade_options (premade_id, applications_open, updated_by) VALUES (?, ?, ?)
|
||
ON DUPLICATE KEY UPDATE applications_open = VALUES(applications_open), updated_by = VALUES(updated_by)'
|
||
)->execute([$premadeId, $open ? 1 : 0, $userId]);
|
||
}
|
||
|
||
/**
|
||
* Warum sich ein Mitglied gerade nicht bei dieser Premade bewerben kann - null, wenn es geht, sonst [Kennung, Text]:
|
||
* member (schon drin oder eingeladen), pending (Bewerbung offen), full, closed, cooldown (nach einer Absage) oder
|
||
* limit (zu viele offene Bewerbungen). $mine = seine Bewerbungen (premade_applications_of).
|
||
*
|
||
* @return array{0: string, 1: string}|null
|
||
*/
|
||
function premade_application_blocker(array $premade, int $discordId, array $mine, bool $closed): ?array
|
||
{
|
||
$premadeId = (int) ($premade['id'] ?? 0);
|
||
if (premade_has_user($premade, $discordId)) {
|
||
return ['member', 'Du bist schon in dieser Premade oder eingeladen.'];
|
||
}
|
||
foreach ($mine as $row) {
|
||
if ((int) $row['premade_id'] === $premadeId && $row['status'] === 'open') {
|
||
return ['pending', 'Deine Bewerbung wartet noch auf eine Antwort.'];
|
||
}
|
||
}
|
||
if (premade_is_full($premade)) {
|
||
return ['full', 'Die Premade ist voll.'];
|
||
}
|
||
if ($closed) {
|
||
return ['closed', 'Diese Premade nimmt gerade keine Bewerbungen an.'];
|
||
}
|
||
foreach ($mine as $row) {
|
||
if ((int) $row['premade_id'] === $premadeId && $row['status'] === 'declined' && $row['decided_at'] !== null) {
|
||
$until = (int) strtotime((string) $row['decided_at']) + PREMADE_APPLICATION_COOLDOWN_DAYS * 86400;
|
||
if ($until > time()) {
|
||
return ['cooldown', 'Nach der Absage kannst du dich hier ab dem ' . date('d.m.', $until) . ' wieder bewerben.'];
|
||
}
|
||
}
|
||
}
|
||
$open = count(array_filter($mine, static fn (array $row): bool => $row['status'] === 'open'));
|
||
if ($open >= PREMADE_APPLICATION_OPEN_MAX) {
|
||
return ['limit', 'Du hast schon ' . $open . ' offene Bewerbungen – warte erst eine Antwort ab oder zieh eine zurück.'];
|
||
}
|
||
return null;
|
||
}
|
||
|
||
/** Klasse aus der offenen Bewerbung eines Spielers (Discord-ID) bei dieser Premade - null ohne Bewerbung. */
|
||
function premade_application_class(int $premadeId, int $discordId): ?string
|
||
{
|
||
try {
|
||
$stmt = db()->prepare("SELECT class_name FROM premade_applications WHERE premade_id = ? AND discord_id = ? AND status = 'open' ORDER BY id DESC LIMIT 1");
|
||
$stmt->execute([$premadeId, $discordId]);
|
||
$class = (string) ($stmt->fetchColumn() ?: '');
|
||
} catch (PDOException $e) {
|
||
return null;
|
||
}
|
||
return in_array($class, AION_CLASSES, true) ? $class : null;
|
||
}
|
||
|
||
/**
|
||
* Wohin das Annehmen einer Bewerbung fuehrt: true = Reserve, false = Gruppe, null = alles voll. Wer schon eingeladen
|
||
* ist, behaelt den Platz der Einladung. Sonst gilt der Wunsch aus der Bewerbung - ist dieser Platz inzwischen belegt,
|
||
* der andere (tauschen kann der Lead danach per Drag & Drop).
|
||
*/
|
||
function premade_application_target(array $premade, array $application): ?bool
|
||
{
|
||
$applicantId = (int) $application['discord_id'];
|
||
if ((int) ($premade['owner_id'] ?? 0) === $applicantId) {
|
||
return false;
|
||
}
|
||
foreach ($premade['members'] ?? [] as $member) {
|
||
if ((int) ($member['user_id'] ?? 0) === $applicantId && in_array($member['status'] ?? '', ['accepted', 'pending'], true)) {
|
||
return !empty($member['reserve']);
|
||
}
|
||
}
|
||
[$groupFree, $reserveFree] = premade_free_slots($premade);
|
||
if (!empty($application['reserve'])) {
|
||
return $reserveFree ? true : ($groupFree ? false : null);
|
||
}
|
||
return $groupFree ? false : ($reserveFree ? true : null);
|
||
}
|
||
|
||
/**
|
||
* Mit den Premades des Bots abgleichen (nur mit der vollstaendigen Liste aufrufen): Offene Bewerbungen bei aufgeloesten
|
||
* Premades gelten als erledigt (status closed), ihre Einstellung faellt weg. Wer inzwischen in der Premade ist (z.B.
|
||
* eine Einladung angenommen hat), dessen Bewerbung gilt als angenommen. Name und Lead jeder Bewerbung bleiben aktuell
|
||
* (der Lead fuer die Zahl am Menuepunkt), und alte Entscheidungen verfallen.
|
||
*
|
||
* @param array<int, array<string, mixed>> $premades
|
||
*/
|
||
function premade_applications_sync(array $premades): void
|
||
{
|
||
$byId = [];
|
||
foreach ($premades as $premade) {
|
||
$byId[(int) ($premade['id'] ?? 0)] = $premade;
|
||
}
|
||
try {
|
||
$pdo = db();
|
||
$open = $pdo->query("SELECT id, premade_id, premade_name, discord_id, lead_discord_id FROM premade_applications WHERE status = 'open'")->fetchAll();
|
||
$known = $pdo->query("SELECT premade_id FROM premade_applications WHERE status = 'open' UNION SELECT premade_id FROM premade_options")->fetchAll(PDO::FETCH_COLUMN);
|
||
} catch (PDOException $e) {
|
||
return;
|
||
}
|
||
foreach ($known as $premadeId) {
|
||
if (!isset($byId[(int) $premadeId])) {
|
||
premade_applications_dissolved((int) $premadeId);
|
||
}
|
||
}
|
||
$accept = $pdo->prepare("UPDATE premade_applications SET status = 'accepted', decided_at = NOW() WHERE id = ? AND status = 'open'");
|
||
$update = $pdo->prepare('UPDATE premade_applications SET premade_name = ?, lead_discord_id = ? WHERE id = ?');
|
||
foreach ($open as $row) {
|
||
$premade = $byId[(int) $row['premade_id']] ?? null;
|
||
if ($premade === null) {
|
||
continue;
|
||
}
|
||
if (premade_has_user($premade, (int) $row['discord_id'], false)) {
|
||
$accept->execute([$row['id']]);
|
||
continue;
|
||
}
|
||
$leadId = (int) ($premade['owner_id'] ?? 0) > 0 ? (string) (int) $premade['owner_id'] : null;
|
||
$name = mb_substr((string) ($premade['name'] ?? ''), 0, 60);
|
||
if ($name !== (string) $row['premade_name'] || $leadId !== ($row['lead_discord_id'] !== null ? (string) $row['lead_discord_id'] : null)) {
|
||
$update->execute([$name, $leadId, $row['id']]);
|
||
}
|
||
}
|
||
$pdo->prepare("DELETE FROM premade_applications WHERE status <> 'open' AND decided_at < ?")
|
||
->execute([date('Y-m-d H:i:s', time() - PREMADE_APPLICATION_KEEP_DAYS * 86400)]);
|
||
}
|
||
|
||
/** Premade aufgeloest: offene Bewerbungen sind erledigt (die Bewerber sehen es bei "Meine Bewerbungen"), die Einstellung faellt weg. */
|
||
function premade_applications_dissolved(int $premadeId): void
|
||
{
|
||
try {
|
||
db()->prepare("UPDATE premade_applications SET status = 'closed', decided_at = NOW() WHERE premade_id = ? AND status = 'open'")->execute([$premadeId]);
|
||
db()->prepare('DELETE FROM premade_options WHERE premade_id = ?')->execute([$premadeId]);
|
||
} catch (PDOException $e) {
|
||
// Tabellen kommen erst mit dem naechsten Schema-Abgleich - dann gibt es auch nichts aufzuraeumen
|
||
}
|
||
}
|
||
|
||
/**
|
||
* Offene Bewerbungen, ueber die jemand entscheidet: bei Premades, die er leitet - mit premades.manage auch bei Premades
|
||
* ohne Lead (Zahl am Menuepunkt "Premades").
|
||
*/
|
||
function premade_applications_badge(?int $discordId, bool $canManage): int
|
||
{
|
||
$conditions = [];
|
||
$params = [];
|
||
if ($discordId !== null && $discordId > 0) {
|
||
$conditions[] = 'lead_discord_id = ?';
|
||
$params[] = $discordId;
|
||
}
|
||
if ($canManage) {
|
||
$conditions[] = 'lead_discord_id IS NULL';
|
||
}
|
||
if (!$conditions) {
|
||
return 0;
|
||
}
|
||
try {
|
||
$stmt = db()->prepare("SELECT COUNT(*) FROM premade_applications WHERE status = 'open' AND (" . implode(' OR ', $conditions) . ')');
|
||
$stmt->execute($params);
|
||
return (int) $stmt->fetchColumn();
|
||
} catch (PDOException $e) {
|
||
return 0;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* PN ueber den Bot: 'applied' an den Lead, 'declined' an den Bewerber. Ein Bot ohne diese Schnittstelle (noch nicht
|
||
* aktualisiert) oder ohne Verbindung aendert nichts an der Bewerbung - dann sieht man sie eben nur auf der Website.
|
||
*/
|
||
function premade_application_notify(int $premadeId, array $body): bool
|
||
{
|
||
try {
|
||
premades_api_request('POST', '/' . $premadeId . '/applications', $body, 5);
|
||
return true;
|
||
} catch (Throwable $e) {
|
||
return false;
|
||
}
|
||
}
|
||
|
||
/** Freitext aus dem Formular: Zeilenumbrueche bleiben (hoechstens einer am Stueck), sonst einfacher Leerraum. */
|
||
function premade_application_text(mixed $value, int $max): string
|
||
{
|
||
$text = str_replace(["\r\n", "\r"], "\n", is_string($value) ? $value : '');
|
||
$text = (string) preg_replace(['/[^\S\n]+/u', '/\n{2,}/'], [' ', "\n"], $text);
|
||
return mb_substr(trim($text), 0, $max);
|
||
}
|