feat: report.index filters by kind: attacks, defence, support

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Fabian @ Blax Software 2026-10-29 00:57:09 +01:00
parent fc7e7e1b23
commit dfe5f50ac6
3 changed files with 38 additions and 4 deletions

View File

@ -13,10 +13,18 @@ final class Reports
{
public const PER_PAGE = 25;
/** @return array{reports: list<array<string, mixed>>, page: int, pages: int, unread: int} */
public function list(User $user, int $worldId, int $page = 1): array
/**
* A page of reports, newest first; `type` keeps one kind (attack, defense, support).
* The unread count is over every kind.
*
* @return array{reports: list<array<string, mixed>>, page: int, pages: int, unread: int}
*/
public function list(User $user, int $worldId, int $page = 1, ?string $type = null): array
{
$query = $this->of($user, $worldId)->orderByDesc('happened_at')->orderByDesc('id');
if (in_array($type, [Report::ATTACK, Report::DEFENSE, Report::SUPPORT], true)) {
$query->where('type', $type);
}
$pages = max(1, (int) ceil($query->count() / self::PER_PAGE));
$page = max(1, min($pages, $page));

View File

@ -5,12 +5,17 @@ namespace App\Websocket\Controllers;
use App\Actions\Reports;
use App\Websocket\GameController;
/** report.index {world_id, page}, report.show {id}, report.delete {world_id, ids}, report.readAll {world_id} */
/** report.index {world_id, page, type?}, report.show {id}, report.delete {world_id, ids}, report.readAll {world_id} */
class ReportController extends GameController
{
public function index()
{
return $this->play(fn () => app(Reports::class)->list(auth()->user(), (int) request('world_id'), (int) (request('page') ?: 1)));
return $this->play(fn () => app(Reports::class)->list(
auth()->user(),
(int) request('world_id'),
(int) (request('page') ?: 1),
request('type') ? (string) request('type') : null,
));
}
public function show()

View File

@ -0,0 +1,21 @@
<?php
use App\Actions\Reports;
use App\Models\Report;
use Carbon\CarbonImmutable;
it('filters reports by kind and counts every unread one', function () {
$village = startVillage();
$at = CarbonImmutable::parse('2026-10-29 12:00:00');
foreach (['attack', 'defense', 'support', 'attack'] as $i => $type) {
Report::write($village->player_id, $type, ['n' => $i], $at->addMinutes($i));
}
$attacks = app(Reports::class)->list(owner($village), $village->world_id, 1, 'attack');
$all = app(Reports::class)->list(owner($village), $village->world_id);
expect(array_column($attacks['reports'], 'type'))->toBe(['attack', 'attack'])
->and($attacks['unread'])->toBe(4)
->and($all['reports'])->toHaveCount(4)
->and($all['reports'][0]['payload'])->toBe(['n' => 3]);
});