2022-07-05 15:55:47 +02:00
|
|
|
<?php
|
|
|
|
|
2024-02-02 14:53:38 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
namespace DNW\Skills;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
use Exception;
|
2010-09-25 10:15:51 -04:00
|
|
|
|
2016-05-24 14:10:39 +02:00
|
|
|
/**
|
2010-08-28 22:05:41 -04:00
|
|
|
* Base class for all skill calculator implementations.
|
|
|
|
*/
|
|
|
|
abstract class SkillCalculator
|
|
|
|
{
|
2024-05-21 12:24:59 +00:00
|
|
|
public const NONE = 0x00;
|
|
|
|
|
|
|
|
public const PARTIAL_PLAY = 0x01;
|
|
|
|
|
|
|
|
public const PARTIAL_UPDATE = 0x02;
|
|
|
|
|
2023-08-01 13:35:44 +00:00
|
|
|
protected function __construct(
|
2024-02-20 14:21:44 +00:00
|
|
|
private readonly int $supportedOptions,
|
2023-08-02 09:15:01 +00:00
|
|
|
private readonly TeamsRange $totalTeamsAllowed,
|
|
|
|
private readonly PlayersRange $playersPerTeamAllowed
|
2024-02-02 14:53:38 +00:00
|
|
|
)
|
|
|
|
{
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Calculates new ratings based on the prior ratings and team ranks.
|
2016-05-24 14:10:39 +02:00
|
|
|
*
|
2024-02-02 14:53:38 +00:00
|
|
|
* @param GameInfo $gameInfo Parameters for the game.
|
|
|
|
* @param Team[] $teams A mapping of team players and their ratings.
|
|
|
|
* @param int[] $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2).
|
|
|
|
*
|
2023-08-02 10:10:57 +00:00
|
|
|
* @return RatingContainer All the players and their new ratings.
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2023-08-01 13:35:44 +00:00
|
|
|
abstract public function calculateNewRatings(
|
|
|
|
GameInfo $gameInfo,
|
2023-08-04 06:56:47 +00:00
|
|
|
array $teams,
|
2023-08-01 13:53:19 +00:00
|
|
|
array $teamRanks
|
2023-08-02 10:10:57 +00:00
|
|
|
): RatingContainer;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2010-10-08 21:44:36 -04:00
|
|
|
/**
|
|
|
|
* Calculates the match quality as the likelihood of all teams drawing.
|
2016-05-24 14:10:39 +02:00
|
|
|
*
|
2024-02-02 15:16:11 +00:00
|
|
|
* @param GameInfo $gameInfo Parameters for the game.
|
|
|
|
* @param Team[] $teams A mapping of team players and their ratings.
|
2024-02-02 14:53:38 +00:00
|
|
|
*
|
2023-08-01 12:43:58 +00:00
|
|
|
* @return float The quality of the match between the teams as a percentage (0% = bad, 100% = well matched).
|
2010-10-08 21:44:36 -04:00
|
|
|
*/
|
2023-08-04 06:56:47 +00:00
|
|
|
abstract public function calculateMatchQuality(GameInfo $gameInfo, array $teams): float;
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2023-08-02 13:19:35 +00:00
|
|
|
public function isSupported(int $option): bool
|
2016-05-24 14:10:39 +02:00
|
|
|
{
|
2024-08-16 07:57:08 +00:00
|
|
|
return ($this->supportedOptions & $option) === $option;
|
2016-05-24 14:10:39 +02:00
|
|
|
}
|
2010-08-28 22:05:41 -04:00
|
|
|
|
2023-08-03 09:13:19 +00:00
|
|
|
/**
|
|
|
|
* @param Team[] $teamsOfPlayerToRatings
|
|
|
|
*/
|
2023-08-02 10:59:15 +00:00
|
|
|
protected function validateTeamCountAndPlayersCountPerTeam(array $teamsOfPlayerToRatings): void
|
2010-08-28 22:05:41 -04:00
|
|
|
{
|
2023-08-02 09:15:01 +00:00
|
|
|
self::validateTeamCountAndPlayersCountPerTeamWithRanges($teamsOfPlayerToRatings, $this->totalTeamsAllowed, $this->playersPerTeamAllowed);
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 16:03:06 +02:00
|
|
|
/**
|
2024-08-16 07:57:08 +00:00
|
|
|
* @param Team[] $teams
|
2022-07-05 16:32:18 +02:00
|
|
|
*
|
2022-07-05 16:03:06 +02:00
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2023-08-01 12:43:58 +00:00
|
|
|
private static function validateTeamCountAndPlayersCountPerTeamWithRanges(array $teams, TeamsRange $totalTeams, PlayersRange $playersPerTeam): void
|
2016-05-24 14:10:39 +02:00
|
|
|
{
|
2010-08-28 22:05:41 -04:00
|
|
|
$countOfTeams = 0;
|
2016-05-24 14:10:39 +02:00
|
|
|
|
|
|
|
foreach ($teams as $currentTeam) {
|
2022-07-05 16:03:06 +02:00
|
|
|
if (! $playersPerTeam->isInRange($currentTeam->count())) {
|
2022-07-05 15:55:47 +02:00
|
|
|
throw new Exception('Player count is not in range');
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
2024-02-21 13:48:37 +00:00
|
|
|
|
|
|
|
++$countOfTeams;
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
|
|
|
|
2022-07-05 15:55:47 +02:00
|
|
|
if (! $totalTeams->isInRange($countOfTeams)) {
|
|
|
|
throw new Exception('Team range is not in range');
|
2010-08-28 22:05:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|