diff --git a/app/Actions/Reports.php b/app/Actions/Reports.php index e301d8a..633f934 100644 --- a/app/Actions/Reports.php +++ b/app/Actions/Reports.php @@ -13,10 +13,18 @@ final class Reports { public const PER_PAGE = 25; - /** @return array{reports: list>, 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>, 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)); diff --git a/app/Websocket/Controllers/ReportController.php b/app/Websocket/Controllers/ReportController.php index f9f193f..3a14f61 100644 --- a/app/Websocket/Controllers/ReportController.php +++ b/app/Websocket/Controllers/ReportController.php @@ -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() diff --git a/tests/Feature/ReportsTest.php b/tests/Feature/ReportsTest.php new file mode 100644 index 0000000..e0212e9 --- /dev/null +++ b/tests/Feature/ReportsTest.php @@ -0,0 +1,21 @@ + $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]); +});