Fixing codestandards.

This commit is contained in:
2023-08-01 13:35:44 +00:00
parent 7792552c9f
commit da8125be94
11 changed files with 153 additions and 57 deletions

View File

@ -7,18 +7,23 @@ namespace DNW\Skills;
*/
class GameInfo
{
final const DEFAULT_BETA = 4.1666666666666666666666666666667; // Default initial mean / 6
private const DEFAULT_BETA = 4.1666666666666666666666666666667; // Default initial mean / 6
final const DEFAULT_DRAW_PROBABILITY = 0.10;
private const DEFAULT_DRAW_PROBABILITY = 0.10;
final const DEFAULT_DYNAMICS_FACTOR = 0.083333333333333333333333333333333; // Default initial mean / 300
private const DEFAULT_DYNAMICS_FACTOR = 0.083333333333333333333333333333333; // Default initial mean / 300
final const DEFAULT_INITIAL_MEAN = 25.0;
private const DEFAULT_INITIAL_MEAN = 25.0;
final const DEFAULT_INITIAL_STANDARD_DEVIATION = 8.3333333333333333333333333333333;
private const DEFAULT_INITIAL_STANDARD_DEVIATION = 8.3333333333333333333333333333333;
public function __construct(private $_initialMean = self::DEFAULT_INITIAL_MEAN, private $_initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION, private $_beta = self::DEFAULT_BETA, private $_dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR, private $_drawProbability = self::DEFAULT_DRAW_PROBABILITY)
{
public function __construct(
private $_initialMean = self::DEFAULT_INITIAL_MEAN,
private $_initialStandardDeviation = self::DEFAULT_INITIAL_STANDARD_DEVIATION,
private $_beta = self::DEFAULT_BETA,
private $_dynamicsFactor = self::DEFAULT_DYNAMICS_FACTOR,
private $_drawProbability = self::DEFAULT_DRAW_PROBABILITY
) {
}
public function getInitialMean()

View File

@ -14,21 +14,21 @@ class Guard
public static function argumentNotNull(mixed $value, string $parameterName): void
{
if ($value == null) {
throw new Exception($parameterName.' can not be null');
throw new Exception($parameterName . ' can not be null');
}
}
public static function argumentIsValidIndex(int $index, int $count, string $parameterName): void
{
if (($index < 0) || ($index >= $count)) {
throw new Exception($parameterName.' is an invalid index');
throw new Exception($parameterName . ' is an invalid index');
}
}
public static function argumentInRangeInclusive(float $value, float $min, float $max, string $parameterName): void
{
if (($value < $min) || ($value > $max)) {
throw new Exception($parameterName.' is not in the valid range ['.$min.', '.$max.']');
throw new Exception($parameterName . ' is not in the valid range [' . $min . ', ' . $max . ']');
}
}
}

View File

@ -7,42 +7,42 @@ namespace DNW\Skills;
*/
class HashMap
{
private array $_hashToValue = [];
private array $hashToValue = [];
private array $_hashToKey = [];
private array $hashToKey = [];
public function getValue(string|object $key): mixed
{
$hash = self::getHash($key);
return $this->_hashToValue[$hash];
return $this->hashToValue[$hash];
}
public function setValue(string|object $key, mixed $value): self
{
$hash = self::getHash($key);
$this->_hashToKey[$hash] = $key;
$this->_hashToValue[$hash] = $value;
$this->hashToKey[$hash] = $key;
$this->hashToValue[$hash] = $value;
return $this;
}
public function getAllKeys(): array
{
return array_values($this->_hashToKey);
return array_values($this->hashToKey);
}
public function getAllValues(): array
{
return array_values($this->_hashToValue);
return array_values($this->hashToValue);
}
public function count(): int
{
return count($this->_hashToKey);
return count($this->hashToKey);
}
private static function getHash(string|Object $key): string
private static function getHash(string|object $key): string
{
if (is_object($key)) {
return spl_object_hash($key);

View File

@ -5,7 +5,8 @@ namespace DNW\Skills;
/**
* Represents a comparison between two players.
*
* @internal The actual values for the enum were chosen so that the also correspond to the multiplier for updates to means.
* @internal The actual values for the enum were chosen so
* that the also correspond to the multiplier for updates to means.
*/
enum PairwiseComparison: int
{

View File

@ -9,21 +9,25 @@ use Exception;
*/
abstract class SkillCalculator
{
protected function __construct(private $_supportedOptions, private readonly TeamsRange $_totalTeamsAllowed, private readonly PlayersRange $_playersPerTeamAllowed)
{
protected function __construct(
private $_supportedOptions,
private readonly TeamsRange $_totalTeamsAllowed,
private readonly PlayersRange $_playersPerTeamAllowed
) {
}
/**
* Calculates new ratings based on the prior ratings and team ranks.
*
* @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).
* @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.
*/
abstract public function calculateNewRatings(GameInfo $gameInfo,
array $teamsOfPlayerToRatings,
array $teamRanks);
abstract public function calculateNewRatings(
GameInfo $gameInfo,
array $teamsOfPlayerToRatings,
array $teamRanks);
/**
* Calculates the match quality as the likelihood of all teams drawing.
@ -66,11 +70,3 @@ abstract class SkillCalculator
}
}
class SkillCalculatorSupportedOptions
{
final const NONE = 0x00;
final const PARTIAL_PLAY = 0x01;
final const PARTIAL_UPDATE = 0x02;
}

View File

@ -0,0 +1,12 @@
<?php
namespace DNW\Skills;
class SkillCalculatorSupportedOptions
{
public const NONE = 0x00;
public const PARTIAL_PLAY = 0x01;
public const PARTIAL_UPDATE = 0x02;
}

View File

@ -11,10 +11,11 @@ use Exception;
// The whole purpose of this is to do a loop on the bottom
class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
{
public function __construct(TrueSkillFactorGraph $parentGraph,
private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $_TeamPerformancesToTeamPerformanceDifferencesLayer,
private readonly TeamDifferencesComparisonLayer $_TeamDifferencesComparisonLayer)
{
public function __construct(
TrueSkillFactorGraph $parentGraph,
private readonly TeamPerformancesToTeamPerformanceDifferencesLayer $_TeamPerformancesToTeamPerformanceDifferencesLayer,
private readonly TeamDifferencesComparisonLayer $_TeamDifferencesComparisonLayer
) {
parent::__construct($parentGraph);
}
@ -142,13 +143,16 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
[
new ScheduleStep(
sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 0', $i),
$differencesFactor, 0),
$differencesFactor, 0
),
new ScheduleStep(
sprintf('greaterThanOrWithinResultFactors[totalTeamDifferences - 1 - %d] @ 0', $i),
$comparisonFactor, 0),
$comparisonFactor, 0
),
new ScheduleStep(
sprintf('teamPerformanceToPerformanceDifferenceFactors[totalTeamDifferences - 1 - %d] @ 1', $i),
$performancesToDifferencesFactor, 1),
$performancesToDifferencesFactor, 1
),
]);
$backwardScheduleList[] = $currentBackwardSchedulePiece;
}
@ -158,13 +162,15 @@ class IteratedTeamDifferencesInnerLayer extends TrueSkillFactorGraphLayer
$forwardBackwardScheduleToLoop =
new ScheduleSequence(
'forward Backward Schedule To Loop',
[$forwardSchedule, $backwardSchedule]);
[$forwardSchedule, $backwardSchedule]
);
$initialMaxDelta = 0.0001;
return new ScheduleLoop(
sprintf('loop with max delta of %f', $initialMaxDelta),
$forwardBackwardScheduleToLoop,
$initialMaxDelta);
$initialMaxDelta
);
}
}

View File

@ -40,9 +40,9 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
);
}
private function createOutputVariable($key)
private function createOutputVariable($key): KeyedVariable
{
return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key."'s performance");
return $this->getParentFactorGraph()->getVariableFactory()->createKeyedVariable($key, $key . "'s performance");
}
public function createPriorSchedule(): ScheduleSequence
@ -52,8 +52,10 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
return $this->scheduleSequence(
array_map(
fn ($likelihood) => new ScheduleStep('Skill to Perf step', $likelihood, 0),
$localFactors),
'All skill to performance sending');
$localFactors
),
'All skill to performance sending'
);
}
public function createPosteriorSchedule(): ScheduleSequence
@ -63,7 +65,9 @@ class PlayerSkillsToPerformancesLayer extends TrueSkillFactorGraphLayer
return $this->scheduleSequence(
array_map(
fn ($likelihood) => new ScheduleStep('name', $likelihood, 1),
$localFactors),
'All skill to performance sending');
$localFactors
),
'All skill to performance sending'
);
}
}

View File

@ -41,17 +41,21 @@ class TwoTeamTrueSkillCalculator extends SkillCalculator
$results = new RatingContainer();
self::updatePlayerRatings($gameInfo,
self::updatePlayerRatings(
$gameInfo,
$results,
$team1,
$team2,
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::WIN);
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::WIN
);
self::updatePlayerRatings($gameInfo,
self::updatePlayerRatings(
$gameInfo,
$results,
$team2,
$team1,
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::LOSE);
$wasDraw ? PairwiseComparison::DRAW : PairwiseComparison::LOSE
);
return $results;
}