diff --git a/app/Draft/Commands/GenerateDraft.php b/app/Draft/Commands/GenerateDraft.php index ce62b75..d8ba797 100644 --- a/app/Draft/Commands/GenerateDraft.php +++ b/app/Draft/Commands/GenerateDraft.php @@ -65,6 +65,17 @@ public function generatePlayerData(): array $playerNames = [...$this->settings->playerNames]; + // Preset teams are defined by the order players are entered in the form + // (adjacent pairs form a team). Capture that mapping from the original + // order *before* the draft-order shuffle below can scramble it. + $presetTeams = []; + if ($this->settings->allianceMode && $this->settings->allianceTeamMode == AllianceTeamMode::PRESET) { + $teamNames = $this->generateTeamNames(); + foreach (array_values($playerNames) as $i => $name) { + $presetTeams[$name] = $teamNames[(int) floor($i / 2)]; + } + } + if (! $this->settings->presetDraftOrder) { shuffle($playerNames); } @@ -75,16 +86,24 @@ public function generatePlayerData(): array } if ($this->settings->allianceMode) { - $teamNames = $this->generateTeamNames(); $teamPlayers = []; - if ($this->settings->allianceTeamMode == AllianceTeamMode::RANDOM) { - shuffle($players); - } - - foreach(array_values($players) as $i => $player) { - $teamName = $teamNames[(int) floor($i / 2)]; - $teamPlayers[$player->id->value] = $player->putInTeam($teamName); + if ($this->settings->allianceTeamMode == AllianceTeamMode::PRESET) { + // Teams are fixed: keep each player's preset team regardless of + // the (possibly randomised) draft order. + foreach ($players as $id => $player) { + $teamPlayers[$id] = $player->putInTeam($presetTeams[$player->name]); + } + } else { + // Random teams: pair players up by a fresh shuffle. + $teamNames = $this->generateTeamNames(); + $shuffled = array_values($players); + shuffle($shuffled); + + foreach ($shuffled as $i => $player) { + $teamName = $teamNames[(int) floor($i / 2)]; + $teamPlayers[$player->id->value] = $player->putInTeam($teamName); + } } $players = $teamPlayers; diff --git a/app/Draft/Commands/GenerateDraftTest.php b/app/Draft/Commands/GenerateDraftTest.php index 30ca36c..86d4098 100644 --- a/app/Draft/Commands/GenerateDraftTest.php +++ b/app/Draft/Commands/GenerateDraftTest.php @@ -112,4 +112,36 @@ public function itCanGeneratePlayerDataForAlliances(): void $this->assertSame('Frank', $players[5]->name); $this->assertSame('C', $players[5]->team); } + + #[Test] + public function itKeepsPresetTeamsWhenDraftOrderIsRandomised(): void + { + // Preset teams must stay fixed to the pairs entered in the form, even + // when the draft order is randomised (presetDraftOrder = false). + $originalPlayerNames = ['Alice', 'Bob', 'Christine', 'David', 'Elliot', 'Frank']; + $settings = DraftSettingsFactory::make([ + 'playerNames' => $originalPlayerNames, + 'allianceMode' => true, + 'allianceTeamMode' => AllianceTeamMode::PRESET, + 'presetDraftOrder' => false, + ]); + + // Run a few times because the draft order is shuffled; the team a player + // belongs to must never change. + for ($run = 0; $run < 20; $run++) { + $draft = (new GenerateDraft($settings))->handle(); + + $teamByName = []; + foreach ($draft->players as $player) { + $teamByName[$player->name] = $player->team; + } + + $this->assertSame('A', $teamByName['Alice']); + $this->assertSame('A', $teamByName['Bob']); + $this->assertSame('B', $teamByName['Christine']); + $this->assertSame('B', $teamByName['David']); + $this->assertSame('C', $teamByName['Elliot']); + $this->assertSame('C', $teamByName['Frank']); + } + } } \ No newline at end of file