game-village-politics-backend/app/Game/Combat/Battle.php

33 lines
910 B
PHP

<?php
namespace App\Game\Combat;
/** The outcome of one battle, from CombatResolver. */
final readonly class Battle
{
/**
* @param float $attackLoss share of every attacking unit that died, 0..1
* @param float $defenseLoss share of every defending unit that died, 0..1
*/
public function __construct(
public bool $attackerWon,
public float $attackLoss,
public float $defenseLoss,
public int $wallBefore,
public int $wallAfter,
public float $attackPower,
public float $defensePower,
) {}
/**
* Dead units of a group at a loss share, rounded per unit type.
*
* @param array<string, int> $units
* @return array<string, int>
*/
public static function dead(array $units, float $loss): array
{
return array_map(fn (int $n) => min($n, (int) round($n * $loss)), $units);
}
}