diff --git a/app/Draft/Commands/GenerateSlicePool.php b/app/Draft/Commands/GenerateSlicePool.php index 2df6eab..c0723e6 100644 --- a/app/Draft/Commands/GenerateSlicePool.php +++ b/app/Draft/Commands/GenerateSlicePool.php @@ -208,7 +208,13 @@ 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..b5fad29 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' => [], ];