feat: add alternating team turns
This commit is contained in:
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user