Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion app/Draft/Commands/GenerateSlicePool.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 17 additions & 0 deletions app/Draft/Commands/GenerateSlicePoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
5 changes: 5 additions & 0 deletions app/Draft/Exceptions/InvalidDraftSettingsException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion app/Http/RequestHandlers/HandleGenerateDraftRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions app/Http/RequestHandlers/HandleGenerateDraftRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [],
];
Expand Down