From 9aac3b8564b1d8612b64a6810aa751cf5a05979c Mon Sep 17 00:00:00 2001 From: nrkovacs Date: Fri, 10 Jul 2026 23:03:08 -0700 Subject: [PATCH] Fix preset alliance teams being randomised by draft-order shuffle generatePlayerData() shuffled the player list for random draft order before assigning alliance teams by adjacency, so "Preset teams" only worked when "Use specified player order" was also enabled. With random draft order (the default) the preset pairs were scrambled. Capture the preset team-per-player mapping from the original input order before the draft-order shuffle, and re-apply it afterwards so team composition stays fixed regardless of draft order. Random teams keep their existing pairing behaviour. Fixes #114 --- app/Draft/Commands/GenerateDraft.php | 35 ++++++++++++++++++------ app/Draft/Commands/GenerateDraftTest.php | 32 ++++++++++++++++++++++ 2 files changed, 59 insertions(+), 8 deletions(-) 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