libpairtwo/src/Tiebreaks.php

149 lines
4.1 KiB
PHP
Raw Normal View History

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-28 18:07:43 +02:00
private const Won = [ Result::won, Result::wonforfait, Result::wonbye, Result::wonadjourned ];
private const Draw = [ Result::draw, Result::drawadjourned];
private const Lost = [ Result::absent, Result::bye, Result::lost, Result::adjourned ];
private const Black = [ Color::black ];
private const White = [ Color::white ];
2019-05-28 17:04:53 +02:00
/**
2019-05-28 18:07:43 +02:00
* @param Player $player
* @return float|null
2019-05-28 17:04:53 +02:00
*/
protected function calculateKeizer(Player $player): ?float
2019-05-28 17:04:53 +02:00
{
return $player->getBinaryData('ScoreAmerican');
2019-05-28 18:07:43 +02:00
}
2019-05-28 18:07:43 +02:00
/**
* @param Player $player
* @return float|null
2019-05-28 18:07:43 +02:00
*/
protected function calculateAmerican(Player $player): ?float
2019-05-28 18:07:43 +02:00
{
return $player->getBinaryData('ScoreAmerican');
2019-05-28 18:07:43 +02:00
}
/**
* @param Player $player
* @return float|null
2019-05-28 18:07:43 +02:00
*/
protected function calculatePoints(Player $player): ?float
2019-05-28 18:07:43 +02:00
{
$points = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getResult(), self::Won) !== false) {
$points = $points + 1;
} elseif (array_search($pairing->getResult(), self::Draw) !== false) {
$points = $points + 0.5;
}
}
return $points;
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
* @return float|null
2019-05-28 17:04:53 +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) {
if (array_search($pairing->getResult(), self::Won) !== false) {
2019-05-28 18:07:43 +02:00
$totalwins++;
}
2019-05-28 17:04:53 +02:00
}
return $totalwins;
2019-05-28 17:04:53 +02:00
}
2019-05-28 17:04:53 +02:00
/**
2019-05-28 18:07:43 +02:00
* @param Player $player
* @return float|null
2019-05-28 18:07:43 +02:00
*/
protected function calculateBlackPlayed(Player $player): ?float
2019-05-28 18:07:43 +02:00
{
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getColor(), self::Black) !== false) {
2019-05-28 18:07:43 +02:00
$totalwins++;
}
}
return $totalwins;
2019-05-28 18:07:43 +02:00
}
2019-05-28 18:07:43 +02:00
/**
* @param Player $player
* @return float|null
2019-05-28 17:04:53 +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) {
if (array_search($pairing->getColor(), self::Black) !== false && array_search($pairing->getResult(), Self::Won) !== false) {
2019-05-28 18:07:43 +02:00
$totalwins++;
}
2019-05-28 17:04:53 +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) {
if (array_search($pairing->getResult(), self::Won) !== false) {
$points = $points + 1;
} elseif (array_search($pairing->getResult(), self::Draw) !== false) {
$points = $points + 0.5;
}
$totalmatches++;
}
}
if ($totalmatches != count($interestingplayers)) {
$points = null;
}
return $points;
}
protected function calculateAverageRating(Player $player) {
$pairings = $player->getPairings();
$totalrating = 0;
$opponents;
foreach ($pairings as $pairing) {
if ($pairing->getOpponent()->getElos['national'])
}
2019-05-28 17:04:53 +02:00
}
}