2019-05-28 17:04:53 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
namespace JeroenED\Libpairtwo;
|
|
|
|
|
2019-05-28 18:07:43 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Color;
|
2019-05-28 17:04:53 +02:00
|
|
|
use JeroenED\Libpairtwo\Models\Tournament;
|
2019-05-28 18:07:43 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Result;
|
2019-05-28 17:04:53 +02:00
|
|
|
|
|
|
|
abstract class Tiebreaks extends Tournament
|
|
|
|
{
|
2019-05-29 15:48:23 +02:00
|
|
|
|
2019-05-28 17:04:53 +02:00
|
|
|
/**
|
2019-05-28 18:07:43 +02:00
|
|
|
* @param Player $player
|
2019-05-29 15:48:23 +02:00
|
|
|
* @return float|null
|
2019-05-28 17:04:53 +02:00
|
|
|
*/
|
2019-05-29 15:48:23 +02:00
|
|
|
protected function calculateKeizer(Player $player): ?float
|
2019-05-28 17:04:53 +02:00
|
|
|
{
|
2019-05-29 15:48:23 +02:00
|
|
|
return $player->getBinaryData('ScoreAmerican');
|
2019-05-28 18:07:43 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:48:23 +02:00
|
|
|
|
2019-05-28 18:07:43 +02:00
|
|
|
/**
|
|
|
|
* @param Player $player
|
2019-05-29 15:48:23 +02:00
|
|
|
* @return float|null
|
2019-05-28 18:07:43 +02:00
|
|
|
*/
|
2019-05-29 15:48:23 +02:00
|
|
|
protected function calculateAmerican(Player $player): ?float
|
2019-05-28 18:07:43 +02:00
|
|
|
{
|
2019-05-29 15:48:23 +02:00
|
|
|
return $player->getBinaryData('ScoreAmerican');
|
2019-05-28 18:07:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Player $player
|
2019-05-29 15:48:23 +02:00
|
|
|
* @return float|null
|
2019-05-28 18:07:43 +02:00
|
|
|
*/
|
2019-05-29 15:48:23 +02:00
|
|
|
protected function calculatePoints(Player $player): ?float
|
2019-05-28 18:07:43 +02:00
|
|
|
{
|
2019-05-30 21:02:31 +02:00
|
|
|
return $player->getPoints();
|
2019-05-28 17:04:53 +02:00
|
|
|
}
|
|
|
|
|
2019-05-28 18:07:43 +02:00
|
|
|
|
2019-05-28 17:04:53 +02:00
|
|
|
/**
|
2019-05-28 18:07:43 +02:00
|
|
|
* @param Player $player
|
2019-05-29 15:48:23 +02:00
|
|
|
* @return float|null
|
2019-05-28 17:04:53 +02:00
|
|
|
*/
|
2019-05-29 15:48:23 +02:00
|
|
|
protected function calculateBaumbach(Player $player): ?float
|
2019-05-28 17:04:53 +02:00
|
|
|
{
|
2019-05-28 18:07:43 +02:00
|
|
|
$totalwins = 0;
|
|
|
|
foreach ($player->getPairings() as $pairing) {
|
2019-05-30 20:54:53 +02:00
|
|
|
if (array_search($pairing->getResult(), Constants::Won) !== false) {
|
2019-05-28 18:07:43 +02:00
|
|
|
$totalwins++;
|
|
|
|
}
|
2019-05-28 17:04:53 +02:00
|
|
|
}
|
2019-05-29 15:48:23 +02:00
|
|
|
return $totalwins;
|
2019-05-28 17:04:53 +02:00
|
|
|
}
|
|
|
|
|
2019-05-29 15:48:23 +02:00
|
|
|
|
2019-05-28 17:04:53 +02:00
|
|
|
/**
|
2019-05-28 18:07:43 +02:00
|
|
|
* @param Player $player
|
2019-05-29 15:48:23 +02:00
|
|
|
* @return float|null
|
2019-05-28 18:07:43 +02:00
|
|
|
*/
|
2019-05-29 15:48:23 +02:00
|
|
|
protected function calculateBlackPlayed(Player $player): ?float
|
2019-05-28 18:07:43 +02:00
|
|
|
{
|
|
|
|
$totalwins = 0;
|
|
|
|
foreach ($player->getPairings() as $pairing) {
|
2019-05-30 20:54:53 +02:00
|
|
|
if (array_search($pairing->getColor(), Constants::Black) !== false) {
|
2019-05-28 18:07:43 +02:00
|
|
|
$totalwins++;
|
|
|
|
}
|
|
|
|
}
|
2019-05-29 15:48:23 +02:00
|
|
|
return $totalwins;
|
2019-05-28 18:07:43 +02:00
|
|
|
}
|
2019-05-29 15:48:23 +02:00
|
|
|
|
2019-05-28 18:07:43 +02:00
|
|
|
/**
|
|
|
|
* @param Player $player
|
2019-05-29 15:48:23 +02:00
|
|
|
* @return float|null
|
2019-05-28 17:04:53 +02:00
|
|
|
*/
|
2019-05-29 15:48:23 +02:00
|
|
|
protected function calculateBlackWin(Player $player): ?float
|
2019-05-28 17:04:53 +02:00
|
|
|
{
|
2019-05-28 18:07:43 +02:00
|
|
|
$totalwins = 0;
|
|
|
|
foreach ($player->getPairings() as $pairing) {
|
2019-05-30 20:54:53 +02:00
|
|
|
if (array_search($pairing->getColor(), Constants::Black) !== false && array_search($pairing->getResult(), Constants::Won) !== false) {
|
2019-05-28 18:07:43 +02:00
|
|
|
$totalwins++;
|
|
|
|
}
|
2019-05-28 17:04:53 +02:00
|
|
|
}
|
2019-05-29 15:48:23 +02:00
|
|
|
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) {
|
2019-05-30 20:54:53 +02:00
|
|
|
if (array_search($pairing->getResult(), Constants::Won) !== false) {
|
2019-05-29 15:48:23 +02:00
|
|
|
$points = $points + 1;
|
2019-05-30 20:54:53 +02:00
|
|
|
} elseif (array_search($pairing->getResult(), Constants::Draw) !== false) {
|
2019-05-29 15:48:23 +02:00
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
|
|
|
$totalmatches++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($totalmatches != count($interestingplayers)) {
|
|
|
|
$points = null;
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
|
|
|
|
2019-05-29 17:56:30 +02:00
|
|
|
/**
|
|
|
|
* @param Player $player
|
|
|
|
* @return float
|
|
|
|
*/
|
2019-05-30 08:33:52 +02:00
|
|
|
protected function calculateAverageRating(Player $player, int $cut = 0)
|
2019-05-29 17:56:30 +02:00
|
|
|
{
|
2019-05-29 15:48:23 +02:00
|
|
|
$pairings = $player->getPairings();
|
|
|
|
$totalrating = 0;
|
2019-05-29 17:56:30 +02:00
|
|
|
$totalopponents = 0;
|
2019-05-30 08:33:52 +02:00
|
|
|
$allratings = [];
|
2019-05-29 15:48:23 +02:00
|
|
|
foreach ($pairings as $pairing) {
|
2019-05-30 20:54:53 +02:00
|
|
|
if (array_search($pairing->getResult(), Constants::NotPlayed) === false) {
|
2019-05-29 17:56:30 +02:00
|
|
|
$toadd = $pairing->getOpponent()->getElos()['home'];
|
|
|
|
if ($toadd != 0) {
|
2019-05-30 08:33:52 +02:00
|
|
|
$allratings[] = $toadd;
|
2019-05-29 17:56:30 +02:00
|
|
|
}
|
|
|
|
}
|
2019-05-29 15:48:23 +02:00
|
|
|
}
|
2019-05-30 08:33:52 +02:00
|
|
|
sort($allratings);
|
|
|
|
$allratings = array_slice($allratings, $cut);
|
|
|
|
return round(array_sum($allratings) / count($allratings));
|
2019-05-28 17:04:53 +02:00
|
|
|
}
|
|
|
|
}
|