libpairtwo/src/Tiebreaks.php

164 lines
4.4 KiB
PHP

<?php
namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Color;
use JeroenED\Libpairtwo\Models\Tournament;
use JeroenED\Libpairtwo\Enums\Result;
abstract class Tiebreaks extends Tournament
{
/**
* @param Player $player
* @return float|null
*/
protected function calculateKeizer(Player $player): ?float
{
return $player->getBinaryData('ScoreAmerican');
}
/**
* @param Player $player
* @return float|null
*/
protected function calculateAmerican(Player $player): ?float
{
return $player->getBinaryData('ScoreAmerican');
}
/**
* @param Player $player
* @return float|null
*/
protected function calculatePoints(Player $player): ?float
{
return $player->getPoints();
}
/**
* @param Player $player
* @return float|null
*/
protected function calculateBaumbach(Player $player): ?float
{
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getResult(), Constants::Won) !== false) {
$totalwins++;
}
}
return $totalwins;
}
/**
* @param Player $player
* @return float|null
*/
protected function calculateBlackPlayed(Player $player): ?float
{
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getColor(), Constants::Black) !== false) {
$totalwins++;
}
}
return $totalwins;
}
/**
* @param Player $player
* @return float|null
*/
protected function calculateBlackWin(Player $player): ?float
{
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getColor(), Constants::Black) !== false && array_search($pairing->getResult(), Constants::Won) !== false) {
$totalwins++;
}
}
return $totalwins;
}
/**
* @param Player $player
* @param array $opponents
* @param int $key
* @return float|null
*/
protected function calculateMutualResult(Player $player, array $opponents, int $key): ?float
{
$interestingplayers = $opponents;
if ($key != 0) {
$interestingplayers = [];
foreach ($opponents as $opponent) {
if (($opponent->getTiebreaks()[$key - 1] == $player->getTiebreaks()[$key - 1]) && ($player != $opponent)) {
$interestingplayers[] = $opponent;
}
}
}
$points = 0;
$totalmatches = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getOpponent(), $interestingplayers) !== false) {
if (array_search($pairing->getResult(), Constants::Won) !== false) {
$points = $points + 1;
} elseif (array_search($pairing->getResult(), Constants::Draw) !== false) {
$points = $points + 0.5;
}
$totalmatches++;
}
}
if ($totalmatches != count($interestingplayers)) {
$points = null;
}
return $points;
}
/**
* @param Player $player
* @return float
*/
protected function calculateAverageRating(Player $player, int $cut = 0)
{
$pairings = $player->getPairings();
$totalrating = 0;
$totalopponents = 0;
$allratings = [];
foreach ($pairings as $pairing) {
if (array_search($pairing->getResult(), Constants::NotPlayed) === false) {
$toadd = $pairing->getOpponent()->getElos()['home'];
if ($toadd != 0) {
$allratings[] = $toadd;
}
}
}
sort($allratings);
$allratings = array_slice($allratings, $cut);
return round(array_sum($allratings) / count($allratings));
}
/**
* @param Player $player
* @param int $cut
* @return int
*/
protected function calculateKoya(Player $player, int $cut = 50)
{
$tiebreak = 0;
foreach ($player->getPairings() as $plkey=>$plpairing) {
if (($plpairing->getOpponent()->getNoOfWins() / count($plpairing->getOpponent()->getPairings()) * 100) >= $cut) {
$tiebreak += $plpairing->getOpponent()->getNoOfWins();
}
}
return $tiebreak;
}
}