trueskill/src/SkillCalculator.php

73 lines
2.4 KiB
PHP
Raw Normal View History

2022-07-05 13:55:47 +00:00
<?php
namespace DNW\Skills;
use Exception;
/**
* Base class for all skill calculator implementations.
*/
abstract class SkillCalculator
{
2023-08-01 13:35:44 +00:00
protected function __construct(
private $supportedOptions,
private readonly TeamsRange $totalTeamsAllowed,
private readonly PlayersRange $playersPerTeamAllowed
2023-08-01 13:35:44 +00:00
) {
}
/**
* Calculates new ratings based on the prior ratings and team ranks.
*
2023-08-01 13:53:19 +00:00
* @param GameInfo $gameInfo Parameters for the game.
* @param array $teamsOfPlayerToRatings A mapping of team players and their ratings.
* @param array $teamRanks The ranks of the teams where 1 is first place. For a tie, repeat the number (e.g. 1, 2, 2).
* @return All the players and their new ratings.
*/
2023-08-01 13:35:44 +00:00
abstract public function calculateNewRatings(
GameInfo $gameInfo,
array $teamsOfPlayerToRatings,
2023-08-01 13:53:19 +00:00
array $teamRanks
);
/**
* Calculates the match quality as the likelihood of all teams drawing.
*
2023-08-01 13:53:19 +00:00
* @param GameInfo $gameInfo Parameters for the game.
* @param array $teamsOfPlayerToRatings A mapping of team players and their ratings.
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).
*/
2023-08-01 12:43:58 +00:00
abstract public function calculateMatchQuality(GameInfo $gameInfo, array $teamsOfPlayerToRatings): float;
2023-08-01 12:43:58 +00:00
public function isSupported($option): bool
{
return ($this->supportedOptions & $option) == $option;
}
protected function validateTeamCountAndPlayersCountPerTeam(array $teamsOfPlayerToRatings)
{
self::validateTeamCountAndPlayersCountPerTeamWithRanges($teamsOfPlayerToRatings, $this->totalTeamsAllowed, $this->playersPerTeamAllowed);
}
2022-07-05 14:03:06 +00:00
/**
2023-08-01 13:53:19 +00:00
* @param array<\DNW\Skills\Team> $teams
2022-07-05 14:32:18 +00:00
*
2022-07-05 14:03:06 +00:00
* @throws \Exception
*/
2023-08-01 12:43:58 +00:00
private static function validateTeamCountAndPlayersCountPerTeamWithRanges(array $teams, TeamsRange $totalTeams, PlayersRange $playersPerTeam): void
{
$countOfTeams = 0;
foreach ($teams as $currentTeam) {
2022-07-05 14:03:06 +00:00
if (! $playersPerTeam->isInRange($currentTeam->count())) {
2022-07-05 13:55:47 +00:00
throw new Exception('Player count is not in range');
}
$countOfTeams++;
}
2022-07-05 13:55:47 +00:00
if (! $totalTeams->isInRange($countOfTeams)) {
throw new Exception('Team range is not in range');
}
}
}