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

90 lines
4.0 KiB
PHP

<?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 by points, 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', '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::where('user_id', $user->id)->where('world_id', $worldId)->value('id');
$rows = match ($kind) {
'players' => $this->players($worldId),
'villages' => $this->villages($worldId),
default => $this->defeated($worldId, $kind),
};
$ranked = $rows->values()->map(fn (array $row, int $i) => ['rank' => $i + 1, ...$row, 'own' => $me !== null && $row['player']['id'] === $me]);
$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]);
}
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]);
}
}