game-village-politics-backend/app/Actions/Ranking.php

126 lines
5.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Actions;
use App\Game\GameRuleViolation;
use App\Models\Player;
use App\Models\User;
use App\Models\World;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
/**
* The world's ranking, like Die Stämme's: players, tribes and villages by points, and
* opponents defeated in attack and in defence (the `defeated.*` counters Rally bumps).
* Ties go to the older player or village. Without a page it opens on the user's own row.
*/
final class Ranking
{
public const PER_PAGE = 25;
public const KINDS = ['players', 'tribes', 'villages', 'attack', 'defense'];
/** @return array{kind: string, page: int, pages: int, rows: list<array<string, mixed>>, own: array{rank: int, page: int}|null} */
public function list(User $user, int $worldId, string $kind, ?int $page = null): array
{
if (! World::whereKey($worldId)->exists() || ! in_array($kind, self::KINDS, true)) {
throw new GameRuleViolation('not_found', 'No such ranking.');
}
$me = Player::with('membership')->where('user_id', $user->id)->where('world_id', $worldId)->first();
$rows = match ($kind) {
'players' => $this->players($worldId),
'tribes' => $this->tribes($worldId),
'villages' => $this->villages($worldId),
default => $this->defeated($worldId, $kind),
};
$own = fn (array $row) => $kind === 'tribes'
? $me?->tribeId() !== null && $row['tribe']['id'] === $me->tribeId()
: $me !== null && $row['player']['id'] === $me->id;
$tags = $this->tags($worldId);
$ranked = $rows->values()->map(fn (array $row, int $i) => [
'rank' => $i + 1,
...$row,
...(isset($row['player']) ? ['player' => [...$row['player'], 'tribe' => $tags[$row['player']['id']] ?? null]] : []),
'own' => $own($row),
]);
$mine = $ranked->firstWhere('own', true);
$own = $mine ? ['rank' => $mine['rank'], 'page' => (int) ceil($mine['rank'] / self::PER_PAGE)] : null;
$pages = max(1, (int) ceil($ranked->count() / self::PER_PAGE));
$page = max(1, min($pages, $page ?? $own['page'] ?? 1));
return [
'kind' => $kind,
'page' => $page,
'pages' => $pages,
'rows' => $ranked->forPage($page, self::PER_PAGE)->values()->all(),
'own' => $own,
];
}
/** Players with villages, by the sum of their villages' points. */
private function players(int $worldId): Collection
{
return DB::table('players')
->join('villages', 'villages.player_id', '=', 'players.id')
->where('players.world_id', $worldId)
->groupBy('players.id', 'players.name')
->selectRaw('players.id, players.name, SUM(villages.points) as points, COUNT(villages.id) as villages')
->orderByDesc('points')->orderBy('players.id')
->get()
->map(fn ($r) => ['player' => ['id' => (int) $r->id, 'name' => $r->name], 'value' => (int) $r->points, 'villages' => (int) $r->villages]);
}
/** Tribes by the points of all their members' villages. */
private function tribes(int $worldId): Collection
{
return DB::table('tribes')
->join('tribe_members', 'tribe_members.tribe_id', '=', 'tribes.id')
->leftJoin('villages', 'villages.player_id', '=', 'tribe_members.player_id')
->where('tribes.world_id', $worldId)
->groupBy('tribes.id', 'tribes.name', 'tribes.tag')
->selectRaw('tribes.id, tribes.name, tribes.tag, COALESCE(SUM(villages.points), 0) as points, COUNT(DISTINCT tribe_members.player_id) as members')
->orderByDesc('points')->orderBy('tribes.id')
->get()
->map(fn ($r) => ['tribe' => ['id' => (int) $r->id, 'name' => $r->name, 'tag' => $r->tag], 'value' => (int) $r->points, 'members' => (int) $r->members]);
}
/**
* Tribe tags of the world's players, for the rows.
*
* @return array<int, array{id: int, tag: string}>
*/
private function tags(int $worldId): array
{
return DB::table('tribe_members')->join('tribes', 'tribes.id', '=', 'tribe_members.tribe_id')->where('tribes.world_id', $worldId)
->get(['tribe_members.player_id', 'tribes.id', 'tribes.tag'])
->mapWithKeys(fn ($r) => [(int) $r->player_id => ['id' => (int) $r->id, 'tag' => $r->tag]])->all();
}
private function villages(int $worldId): Collection
{
return DB::table('villages')
->join('players', 'players.id', '=', 'villages.player_id')
->where('villages.world_id', $worldId)
->orderByDesc('villages.points')->orderBy('villages.id')
->get(['villages.id', 'villages.name', 'villages.x', 'villages.y', 'villages.points', 'players.id as player_id', 'players.name as player_name'])
->map(fn ($r) => [
'player' => ['id' => (int) $r->player_id, 'name' => $r->player_name],
'village' => ['id' => (int) $r->id, 'name' => $r->name, 'x' => (int) $r->x, 'y' => (int) $r->y],
'value' => (int) $r->points,
]);
}
/** Players by opponents defeated in attack or in defence. */
private function defeated(int $worldId, string $kind): Collection
{
return DB::table('player_counters')
->join('players', 'players.id', '=', 'player_counters.player_id')
->where('players.world_id', $worldId)
->where('player_counters.key', "defeated.$kind")
->where('player_counters.value', '>', 0)
->orderByDesc('player_counters.value')->orderBy('players.id')
->get(['players.id', 'players.name', 'player_counters.value'])
->map(fn ($r) => ['player' => ['id' => (int) $r->id, 'name' => $r->name], 'value' => (int) $r->value]);
}
}