feat: add alternating team turns

This commit is contained in:
Keith Solomon
2026-07-04 16:02:49 -05:00
parent 7588b304e1
commit acfbbf4a91
2 changed files with 174 additions and 0 deletions
+33
View File
@@ -76,6 +76,39 @@ final class CombatEngine
return $next->withWinner($attacker->teamId);
}
public function endTurn(MatchState $match): MatchState
{
$this->assertMatchActive($match);
$teamIds = [];
foreach ($match->units as $unit) {
$teamIds[$unit->teamId] = true;
}
$teamIds = array_keys($teamIds);
sort($teamIds);
if ($teamIds !== ['alpha', 'bravo']) {
throw new CombatException('Matches require alpha and bravo teams.');
}
$endingTeamId = $match->activeTeamId;
$nextTeamId = $endingTeamId === 'alpha' ? 'bravo' : 'alpha';
$units = [];
foreach ($match->units as $unit) {
$units[] = $unit->teamId === $nextTeamId ? $unit->startTurn() : $unit;
}
return $match->copy(
units: $units,
activeTeamId: $nextTeamId,
round: $match->round + ($nextTeamId === 'alpha' ? 1 : 0),
actionLog: [...$match->actionLog, "{$endingTeamId} ended turn"],
);
}
private function unitOrFail(MatchState $match, string $unitId): UnitState
{
try {