From a8cd32054c461c10aa49369f0c27a37cf6cb0fa4 Mon Sep 17 00:00:00 2001 From: Sam Tubbax Date: Tue, 30 Dec 2025 14:04:32 +0100 Subject: [PATCH 1/2] Validate custom slices & trim leading zeroes --- app/Draft/Commands/GenerateSlicePool.php | 9 ++++++++- app/Draft/Commands/GenerateSlicePoolTest.php | 17 +++++++++++++++++ .../InvalidDraftSettingsException.php | 5 +++++ .../HandleGenerateDraftRequest.php | 2 +- .../HandleGenerateDraftRequestTest.php | 4 ++-- 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/app/Draft/Commands/GenerateSlicePool.php b/app/Draft/Commands/GenerateSlicePool.php index 2df6eab..1658b8e 100644 --- a/app/Draft/Commands/GenerateSlicePool.php +++ b/app/Draft/Commands/GenerateSlicePool.php @@ -208,7 +208,14 @@ private function validateTileSelection(array $tileIds): bool private function slicesFromCustomSlices(): array { return array_map(function (array $sliceData) { - $tileData = array_map(fn ($tileId) => $this->tileData[$tileId], $sliceData); + + $tileData = []; + foreach ($sliceData as $tileId) { + if (!isset($this->tileData[$tileId])) { + throw InvalidDraftSettingsException::unknownTileInCustomSlice($tileId); + } + $tileData[] = $this->tileData[$tileId]; + } return new Slice($tileData); }, $this->settings->customSlices); diff --git a/app/Draft/Commands/GenerateSlicePoolTest.php b/app/Draft/Commands/GenerateSlicePoolTest.php index 40eca14..6bbd947 100644 --- a/app/Draft/Commands/GenerateSlicePoolTest.php +++ b/app/Draft/Commands/GenerateSlicePoolTest.php @@ -256,6 +256,23 @@ public function itCanReturnCustomSlices(): void } } + #[Test] + public function itThrowsErrorWhenCustomSliceContainsIncorrectTileNumber(): void + { + $customSlices = [ + ['64', '33AEZD', '42', '67', '59'] + ]; + + $generator = new GenerateSlicePool(DraftSettingsFactory::make([ + 'numberOfSlices' => 4, + 'customSlices' => $customSlices, + ])); + + $this->expectException(InvalidDraftSettingsException::class); + + $slices = $generator->handle(); + } + #[Test] public function itGivesUpIfSettingsAreImpossible(): void { diff --git a/app/Draft/Exceptions/InvalidDraftSettingsException.php b/app/Draft/Exceptions/InvalidDraftSettingsException.php index 6b26754..a94ffd8 100644 --- a/app/Draft/Exceptions/InvalidDraftSettingsException.php +++ b/app/Draft/Exceptions/InvalidDraftSettingsException.php @@ -34,6 +34,11 @@ public static function notEnoughFactionsForPlayers(): self return new self('Cannot have less factions than players'); } + public static function unknownTileInCustomSlice($id): self + { + return new self('Custom slices contain unknown tile number:' . $id); + } + public static function notEnoughTilesForSlices(float $maxSlices): self { return new self(sprintf('This selection of tilesets only supports %d slices', $maxSlices)); diff --git a/app/Http/RequestHandlers/HandleGenerateDraftRequest.php b/app/Http/RequestHandlers/HandleGenerateDraftRequest.php index 0df8c58..828923c 100644 --- a/app/Http/RequestHandlers/HandleGenerateDraftRequest.php +++ b/app/Http/RequestHandlers/HandleGenerateDraftRequest.php @@ -61,7 +61,7 @@ private function settingsFromRequest(): Settings $slice = []; $t = explode(',', $s); foreach ($t as $tile) { - $tile = trim($tile); + $tile = ltrim(trim($tile), '0'); $slice[] = $tile; } $customSlices[] = $slice; diff --git a/app/Http/RequestHandlers/HandleGenerateDraftRequestTest.php b/app/Http/RequestHandlers/HandleGenerateDraftRequestTest.php index e234439..b6e62ac 100644 --- a/app/Http/RequestHandlers/HandleGenerateDraftRequestTest.php +++ b/app/Http/RequestHandlers/HandleGenerateDraftRequestTest.php @@ -76,13 +76,13 @@ public static function settingsPayload() ]; yield 'Custom Slices' => [ 'postData' => [ - 'custom_slices' => "1,2,3,4,5\n6,7,8,9,10\n11,12,13,14,15", + 'custom_slices' => "01,2,03,4,5\n6,7,8,9,10\n11,012,13,014,15A", ], 'field' => 'customSlices', 'expected' => [ ['1', '2', '3', '4', '5'], ['6', '7', '8', '9', '10'], - ['11', '12', '13', '14', '15'], + ['11', '12', '13', '14', '15A'], ], 'expectedWhenNotSet' => [], ]; From 5fcd226512532e411d89371ed6ea864a4e01d6ed Mon Sep 17 00:00:00 2001 From: Sam Tubbax Date: Tue, 30 Dec 2025 14:13:11 +0100 Subject: [PATCH 2/2] cs --- app/Draft/Commands/GenerateSlicePool.php | 3 +-- app/Draft/Commands/GenerateSlicePoolTest.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/Draft/Commands/GenerateSlicePool.php b/app/Draft/Commands/GenerateSlicePool.php index 1658b8e..c0723e6 100644 --- a/app/Draft/Commands/GenerateSlicePool.php +++ b/app/Draft/Commands/GenerateSlicePool.php @@ -208,10 +208,9 @@ private function validateTileSelection(array $tileIds): bool private function slicesFromCustomSlices(): array { return array_map(function (array $sliceData) { - $tileData = []; foreach ($sliceData as $tileId) { - if (!isset($this->tileData[$tileId])) { + if (! isset($this->tileData[$tileId])) { throw InvalidDraftSettingsException::unknownTileInCustomSlice($tileId); } $tileData[] = $this->tileData[$tileId]; diff --git a/app/Draft/Commands/GenerateSlicePoolTest.php b/app/Draft/Commands/GenerateSlicePoolTest.php index 6bbd947..b5fad29 100644 --- a/app/Draft/Commands/GenerateSlicePoolTest.php +++ b/app/Draft/Commands/GenerateSlicePoolTest.php @@ -260,7 +260,7 @@ public function itCanReturnCustomSlices(): void public function itThrowsErrorWhenCustomSliceContainsIncorrectTileNumber(): void { $customSlices = [ - ['64', '33AEZD', '42', '67', '59'] + ['64', '33AEZD', '42', '67', '59'], ]; $generator = new GenerateSlicePool(DraftSettingsFactory::make([