Server fuer Konten, Akten und Squads
PHP 8 ohne Framework unter server/: oeffentliche Einstiege fuer sva.tyrveil.com (Schnittstelle, Konto-Links, Verwaltung, Einrichtung) und die Logik in sva-privat neben dem Dokumentenstamm. - Konten mit E-Mail-Bestaetigung, Anmeldung per Token, Passwort vergessen, Konto loeschen - Akten auf dem Server mit serververgebener Aktennummer und Versionspruefung; Portraets werden neu als PNG gespeichert - Squads mit Einladungen, Fuehrung uebergeben, Aufloesen - Verwaltung fuer info@tyrveil.com: Nutzer, Charaktere, Squads - Bremsen gegen Durchprobieren, Token nur als Hash, CSRF-Schutz werkzeuge/server_test.py prueft alles lokal mit SQLite (91 Faelle). Der Release-Bau legt dist/server und dist/sva-privat an, ohne config.php. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
parent
58a3573fd6
commit
6e4d4afc1d
23 changed files with 2872 additions and 1 deletions
317
server/sva-privat/app/squads.php
Normal file
317
server/sva-privat/app/squads.php
Normal file
|
|
@ -0,0 +1,317 @@
|
|||
<?php
|
||||
/*
|
||||
* Squads: Ein Charakter gruendet ein Squad und fuehrt es. Er laedt andere
|
||||
* Charaktere ein, deren Spieler annehmen muessen. Jeder Charakter ist in
|
||||
* hoechstens einem Squad.
|
||||
*
|
||||
* Alle Aenderungen laufen in Transaktionen mit Zeilensperre, damit etwa zwei
|
||||
* gleichzeitig angenommene Einladungen einen Charakter nicht doppelt
|
||||
* zuordnen.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
final class Squads
|
||||
{
|
||||
public const MAX_OFFENE_EINLADUNGEN = 30;
|
||||
|
||||
private static function eigenerCharakter(array $nutzer, int $charakterId): array
|
||||
{
|
||||
$zeile = DB::eine('SELECT * FROM sm_charakter WHERE id = ? AND nutzer_id = ?' . DB::sperre(),
|
||||
[$charakterId, $nutzer['id']]);
|
||||
if ($zeile === null) {
|
||||
throw new SmFehler('charakter_nicht_gefunden',
|
||||
'Diesen Charakter gibt es nicht oder er gehört dir nicht.', 404);
|
||||
}
|
||||
return $zeile;
|
||||
}
|
||||
|
||||
private static function squadMitSperre(int $squadId): array
|
||||
{
|
||||
$squad = DB::eine('SELECT * FROM sm_squad WHERE id = ?' . DB::sperre(), [$squadId]);
|
||||
if ($squad === null) {
|
||||
throw new SmFehler('squad_nicht_gefunden', 'Dieses Squad gibt es nicht mehr.', 404);
|
||||
}
|
||||
return $squad;
|
||||
}
|
||||
|
||||
private static function alsFuehrer(array $nutzer, int $squadId): array
|
||||
{
|
||||
$squad = self::squadMitSperre($squadId);
|
||||
if (!DB::wert('SELECT 1 FROM sm_charakter WHERE id = ? AND nutzer_id = ?',
|
||||
[$squad['fuehrer_id'], $nutzer['id']])) {
|
||||
throw new SmFehler('kein_fuehrer', 'Das kann nur der Squadführer.', 403);
|
||||
}
|
||||
return $squad;
|
||||
}
|
||||
|
||||
private static function name(string $name): string
|
||||
{
|
||||
$name = trim(preg_replace('/\s+/u', ' ', $name) ?? '');
|
||||
if (mb_strlen($name) < 3 || mb_strlen($name) > 48 || preg_match('/[\x00-\x1F\x7F]/u', $name)) {
|
||||
throw new SmFehler('name_ungueltig', 'Der Squadname braucht 3 bis 48 Zeichen.');
|
||||
}
|
||||
return $name;
|
||||
}
|
||||
|
||||
public static function gruenden(array $nutzer, int $charakterId, string $name,
|
||||
string $beschreibung = ''): array
|
||||
{
|
||||
$name = self::name($name);
|
||||
return DB::transaktion(function () use ($nutzer, $charakterId, $name, $beschreibung) {
|
||||
$charakter = self::eigenerCharakter($nutzer, $charakterId);
|
||||
if ($charakter['squad_id'] !== null) {
|
||||
throw new SmFehler('bereits_im_squad', 'Dieser Charakter ist schon in einem Squad.', 409);
|
||||
}
|
||||
if (DB::wert('SELECT 1 FROM sm_squad WHERE name_klein = ?', [mb_strtolower($name)])) {
|
||||
throw new SmFehler('name_vergeben', 'Diesen Squadnamen gibt es schon.', 409);
|
||||
}
|
||||
$id = DB::einfuegen('sm_squad', [
|
||||
'name' => $name,
|
||||
'name_klein' => mb_strtolower($name),
|
||||
'beschreibung' => mb_substr(trim($beschreibung), 0, 2000),
|
||||
'fuehrer_id' => $charakterId,
|
||||
'erstellt_am' => DB::jetzt(),
|
||||
]);
|
||||
DB::aktualisieren('sm_charakter', ['squad_id' => $id, 'squad_seit' => DB::jetzt()],
|
||||
'id = ?', [$charakterId]);
|
||||
self::hinfaellig($charakterId);
|
||||
return self::details($nutzer, $id);
|
||||
});
|
||||
}
|
||||
|
||||
public static function einladen(array $nutzer, int $squadId, int $charakterId): array
|
||||
{
|
||||
return DB::transaktion(function () use ($nutzer, $squadId, $charakterId) {
|
||||
$squad = self::alsFuehrer($nutzer, $squadId);
|
||||
$ziel = DB::eine('SELECT c.* FROM sm_charakter c JOIN sm_nutzer n ON n.id = c.nutzer_id
|
||||
WHERE c.id = ? AND n.gesperrt = 0' . DB::sperre(), [$charakterId]);
|
||||
if ($ziel === null) {
|
||||
throw new SmFehler('charakter_nicht_gefunden', 'Diesen Charakter gibt es nicht.', 404);
|
||||
}
|
||||
if ($ziel['squad_id'] !== null) {
|
||||
throw new SmFehler('bereits_im_squad', 'Dieser Charakter ist schon in einem Squad.', 409);
|
||||
}
|
||||
if (DB::wert("SELECT 1 FROM sm_einladung WHERE squad_id = ? AND charakter_id = ? AND status = 'offen'",
|
||||
[$squadId, $charakterId])) {
|
||||
throw new SmFehler('schon_eingeladen', 'Dieser Charakter ist schon eingeladen.', 409);
|
||||
}
|
||||
if ((int) DB::wert("SELECT COUNT(*) FROM sm_einladung WHERE squad_id = ? AND status = 'offen'",
|
||||
[$squadId]) >= self::MAX_OFFENE_EINLADUNGEN) {
|
||||
throw new SmFehler('zu_viele_einladungen', 'Zu viele offene Einladungen.', 409);
|
||||
}
|
||||
$id = DB::einfuegen('sm_einladung', [
|
||||
'squad_id' => $squadId,
|
||||
'charakter_id' => $charakterId,
|
||||
'von_charakter_id' => $squad['fuehrer_id'],
|
||||
'status' => 'offen',
|
||||
'erstellt_am' => DB::jetzt(),
|
||||
]);
|
||||
return self::einladungDarstellen(DB::eine('SELECT * FROM sm_einladung WHERE id = ?', [$id]));
|
||||
});
|
||||
}
|
||||
|
||||
private static function charakterKurz(?int $id): ?array
|
||||
{
|
||||
if ($id === null) {
|
||||
return null;
|
||||
}
|
||||
$zeile = DB::eine('SELECT c.id, c.callsign, c.vorname, c.nachname, c.rang, c.status,
|
||||
n.benutzername AS spieler
|
||||
FROM sm_charakter c JOIN sm_nutzer n ON n.id = c.nutzer_id WHERE c.id = ?', [$id]);
|
||||
return $zeile === null ? null : ['id' => (int) $zeile['id']] + $zeile;
|
||||
}
|
||||
|
||||
private static function einladungDarstellen(array $zeile): array
|
||||
{
|
||||
$squad = DB::eine('SELECT id, name FROM sm_squad WHERE id = ?', [$zeile['squad_id']]);
|
||||
return [
|
||||
'id' => (int) $zeile['id'],
|
||||
'status' => $zeile['status'],
|
||||
'erstellt_am' => $zeile['erstellt_am'],
|
||||
'squad' => $squad === null ? null : ['id' => (int) $squad['id'], 'name' => $squad['name']],
|
||||
'charakter' => self::charakterKurz((int) $zeile['charakter_id']),
|
||||
'von' => self::charakterKurz($zeile['von_charakter_id'] === null ? null : (int) $zeile['von_charakter_id']),
|
||||
];
|
||||
}
|
||||
|
||||
/** Offene Einladungen an eigene Charaktere und von eigenen Squads. */
|
||||
public static function einladungen(array $nutzer): array
|
||||
{
|
||||
$eingang = DB::alle("SELECT e.* FROM sm_einladung e JOIN sm_charakter c ON c.id = e.charakter_id
|
||||
WHERE c.nutzer_id = ? AND e.status = 'offen' ORDER BY e.erstellt_am", [$nutzer['id']]);
|
||||
$ausgang = DB::alle("SELECT e.* FROM sm_einladung e
|
||||
JOIN sm_squad s ON s.id = e.squad_id
|
||||
JOIN sm_charakter f ON f.id = s.fuehrer_id
|
||||
WHERE f.nutzer_id = ? AND e.status = 'offen' ORDER BY e.erstellt_am", [$nutzer['id']]);
|
||||
return [
|
||||
'eingang' => array_map([self::class, 'einladungDarstellen'], $eingang),
|
||||
'ausgang' => array_map([self::class, 'einladungDarstellen'], $ausgang),
|
||||
];
|
||||
}
|
||||
|
||||
private static function einladungMitSperre(int $einladungId): array
|
||||
{
|
||||
$einladung = DB::eine('SELECT * FROM sm_einladung WHERE id = ?' . DB::sperre(), [$einladungId]);
|
||||
if ($einladung === null || $einladung['status'] !== 'offen') {
|
||||
throw new SmFehler('einladung_nicht_offen', 'Diese Einladung ist nicht mehr offen.', 409);
|
||||
}
|
||||
return $einladung;
|
||||
}
|
||||
|
||||
public static function beantworten(array $nutzer, int $einladungId, bool $annehmen): void
|
||||
{
|
||||
DB::transaktion(function () use ($nutzer, $einladungId, $annehmen) {
|
||||
$einladung = self::einladungMitSperre($einladungId);
|
||||
$charakter = self::eigenerCharakter($nutzer, (int) $einladung['charakter_id']);
|
||||
if ($annehmen) {
|
||||
if ($charakter['squad_id'] !== null) {
|
||||
throw new SmFehler('bereits_im_squad',
|
||||
'Dieser Charakter ist schon in einem Squad. Verlasse es zuerst.', 409);
|
||||
}
|
||||
self::squadMitSperre((int) $einladung['squad_id']);
|
||||
DB::aktualisieren('sm_charakter',
|
||||
['squad_id' => $einladung['squad_id'], 'squad_seit' => DB::jetzt()],
|
||||
'id = ?', [$charakter['id']]);
|
||||
}
|
||||
DB::aktualisieren('sm_einladung',
|
||||
['status' => $annehmen ? 'angenommen' : 'abgelehnt', 'beantwortet_am' => DB::jetzt()],
|
||||
'id = ?', [$einladungId]);
|
||||
if ($annehmen) {
|
||||
self::hinfaellig((int) $charakter['id']);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/** Weitere offene Einladungen eines Charakters, der gerade einem Squad beigetreten ist. */
|
||||
private static function hinfaellig(int $charakterId): void
|
||||
{
|
||||
DB::ausfuehren("UPDATE sm_einladung SET status = 'hinfaellig', beantwortet_am = ?
|
||||
WHERE charakter_id = ? AND status = 'offen'", [DB::jetzt(), $charakterId]);
|
||||
}
|
||||
|
||||
public static function zurueckziehen(array $nutzer, int $einladungId): void
|
||||
{
|
||||
DB::transaktion(function () use ($nutzer, $einladungId) {
|
||||
$einladung = self::einladungMitSperre($einladungId);
|
||||
self::alsFuehrer($nutzer, (int) $einladung['squad_id']);
|
||||
DB::aktualisieren('sm_einladung', ['status' => 'zurueckgezogen', 'beantwortet_am' => DB::jetzt()],
|
||||
'id = ?', [$einladungId]);
|
||||
});
|
||||
}
|
||||
|
||||
public static function verlassen(array $nutzer, int $charakterId): void
|
||||
{
|
||||
DB::transaktion(function () use ($nutzer, $charakterId) {
|
||||
$charakter = self::eigenerCharakter($nutzer, $charakterId);
|
||||
if ($charakter['squad_id'] === null) {
|
||||
throw new SmFehler('nicht_im_squad', 'Dieser Charakter ist in keinem Squad.', 409);
|
||||
}
|
||||
$squad = self::squadMitSperre((int) $charakter['squad_id']);
|
||||
if ((int) $squad['fuehrer_id'] === $charakterId) {
|
||||
$weitere = (int) DB::wert('SELECT COUNT(*) FROM sm_charakter WHERE squad_id = ? AND id <> ?',
|
||||
[$squad['id'], $charakterId]);
|
||||
if ($weitere > 0) {
|
||||
throw new SmFehler('fuehrer_muss_uebergeben',
|
||||
'Als Squadführer musst du zuerst die Führung übergeben oder das Squad auflösen.', 409);
|
||||
}
|
||||
self::aufloesenIntern((int) $squad['id']);
|
||||
return;
|
||||
}
|
||||
DB::aktualisieren('sm_charakter', ['squad_id' => null, 'squad_seit' => null], 'id = ?', [$charakterId]);
|
||||
});
|
||||
}
|
||||
|
||||
public static function entfernen(array $nutzer, int $squadId, int $charakterId): void
|
||||
{
|
||||
DB::transaktion(function () use ($nutzer, $squadId, $charakterId) {
|
||||
$squad = self::alsFuehrer($nutzer, $squadId);
|
||||
if ((int) $squad['fuehrer_id'] === $charakterId) {
|
||||
throw new SmFehler('fuehrer', 'Der Squadführer kann sich nicht selbst entfernen.', 409);
|
||||
}
|
||||
$anzahl = DB::aktualisieren('sm_charakter', ['squad_id' => null, 'squad_seit' => null],
|
||||
'id = ? AND squad_id = ?', [$charakterId, $squadId]);
|
||||
if ($anzahl === 0) {
|
||||
throw new SmFehler('nicht_im_squad', 'Dieser Charakter ist nicht in deinem Squad.', 409);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static function fuehrungUebergeben(array $nutzer, int $squadId, int $charakterId): void
|
||||
{
|
||||
DB::transaktion(function () use ($nutzer, $squadId, $charakterId) {
|
||||
self::alsFuehrer($nutzer, $squadId);
|
||||
if (!DB::wert('SELECT 1 FROM sm_charakter WHERE id = ? AND squad_id = ?', [$charakterId, $squadId])) {
|
||||
throw new SmFehler('nicht_im_squad', 'Dieser Charakter ist nicht in deinem Squad.', 409);
|
||||
}
|
||||
DB::aktualisieren('sm_squad', ['fuehrer_id' => $charakterId], 'id = ?', [$squadId]);
|
||||
});
|
||||
}
|
||||
|
||||
public static function aufloesen(array $nutzer, int $squadId): void
|
||||
{
|
||||
DB::transaktion(function () use ($nutzer, $squadId) {
|
||||
self::alsFuehrer($nutzer, $squadId);
|
||||
self::aufloesenIntern($squadId);
|
||||
});
|
||||
}
|
||||
|
||||
/** Fuer die Verwaltung. */
|
||||
public static function aufloesenNachId(int $squadId): void
|
||||
{
|
||||
DB::transaktion(fn() => self::aufloesenIntern($squadId));
|
||||
}
|
||||
|
||||
private static function aufloesenIntern(int $squadId): void
|
||||
{
|
||||
DB::ausfuehren('UPDATE sm_charakter SET squad_id = NULL, squad_seit = NULL WHERE squad_id = ?', [$squadId]);
|
||||
DB::ausfuehren('DELETE FROM sm_einladung WHERE squad_id = ?', [$squadId]);
|
||||
DB::ausfuehren('DELETE FROM sm_squad WHERE id = ?', [$squadId]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ein Charakter wird geloescht. Fuehrt er ein Squad, uebernimmt das
|
||||
* dienstaelteste Mitglied; ist er allein, wird das Squad aufgeloest.
|
||||
*/
|
||||
public static function beimLoeschen(array $charakter): void
|
||||
{
|
||||
if ($charakter['squad_id'] === null) {
|
||||
return;
|
||||
}
|
||||
$squad = DB::eine('SELECT * FROM sm_squad WHERE id = ?' . DB::sperre(), [$charakter['squad_id']]);
|
||||
if ($squad === null || (int) $squad['fuehrer_id'] !== (int) $charakter['id']) {
|
||||
return;
|
||||
}
|
||||
$nachfolger = DB::wert('SELECT id FROM sm_charakter WHERE squad_id = ? AND id <> ?
|
||||
ORDER BY squad_seit, id LIMIT 1', [$squad['id'], $charakter['id']]);
|
||||
if ($nachfolger === null) {
|
||||
self::aufloesenIntern((int) $squad['id']);
|
||||
} else {
|
||||
DB::aktualisieren('sm_squad', ['fuehrer_id' => (int) $nachfolger], 'id = ?', [$squad['id']]);
|
||||
}
|
||||
}
|
||||
|
||||
public static function details(array $nutzer, int $squadId): array
|
||||
{
|
||||
$squad = DB::eine('SELECT * FROM sm_squad WHERE id = ?', [$squadId]);
|
||||
if ($squad === null) {
|
||||
throw new SmFehler('squad_nicht_gefunden', 'Dieses Squad gibt es nicht mehr.', 404);
|
||||
}
|
||||
$mitglieder = DB::alle('SELECT c.*, n.benutzername AS spieler
|
||||
FROM sm_charakter c JOIN sm_nutzer n ON n.id = c.nutzer_id
|
||||
WHERE c.squad_id = ? ORDER BY c.squad_seit, c.id', [$squadId]);
|
||||
return [
|
||||
'id' => (int) $squad['id'],
|
||||
'name' => $squad['name'],
|
||||
'beschreibung' => (string) $squad['beschreibung'],
|
||||
'fuehrer_id' => (int) $squad['fuehrer_id'],
|
||||
'erstellt_am' => $squad['erstellt_am'],
|
||||
'mitglieder' => array_map(fn($zeile) => Charaktere::zusammenfassung($zeile, $nutzer), $mitglieder),
|
||||
];
|
||||
}
|
||||
|
||||
public static function liste(array $nutzer): array
|
||||
{
|
||||
$ids = DB::alle('SELECT id FROM sm_squad ORDER BY name');
|
||||
return array_map(fn($zeile) => self::details($nutzer, (int) $zeile['id']), $ids);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue