libpairtwo/src/Tiebreaks.php

114 lines
3.3 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
2019-05-28 17:04:53 +02:00
/**
* @param int $key
2019-05-28 18:07:43 +02:00
* @param Player $player
* @return float
2019-05-28 17:04:53 +02:00
*/
2019-05-28 18:07:43 +02:00
protected function calculateKeizer(int $key, Player $player): float
2019-05-28 17:04:53 +02:00
{
2019-05-28 18:07:43 +02:00
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican');
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
}
/**
* @param int $key
* @param Player $player
* @return float
*/
protected function calculateAmerican(int $key, Player $player): float
{
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican');
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
}
/**
* @param int $key
* @param Player $player
* @return float
*/
protected function calculatePoints(int $key, Player $player): float
{
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('Points');
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
2019-05-28 17:04:53 +02:00
}
2019-05-28 18:07:43 +02:00
2019-05-28 17:04:53 +02:00
/**
* @param int $key
2019-05-28 18:07:43 +02:00
* @param Player $player
* @return float
2019-05-28 17:04:53 +02:00
*/
2019-05-28 18:07:43 +02:00
protected function calculateBaumbach(int $key, Player $player): float
2019-05-28 17:04:53 +02:00
{
2019-05-28 18:07:43 +02:00
$wonArray = [Result::won, Result::wonadjourned, Result::wonbye, Result::wonforfait];
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getResult(), $wonArray) !== false) {
$totalwins++;
}
2019-05-28 17:04:53 +02:00
}
2019-05-28 18:07:43 +02:00
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $totalwins;
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
2019-05-28 17:04:53 +02:00
}
/**
* @param int $key
2019-05-28 18:07:43 +02:00
* @param Player $player
* @return float
*/
protected function calculateBlackPlayed(int $key, Player $player): float
{
$blackArray = [Color::black];
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getColor(), $blackArray) !== false) {
$totalwins++;
}
}
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $totalwins;
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
}
/**
* @param int $key
* @param Player $player
* @return float
2019-05-28 17:04:53 +02:00
*/
2019-05-28 18:07:43 +02:00
protected function calculateBlackWin(int $key, Player $player): float
2019-05-28 17:04:53 +02:00
{
2019-05-28 18:07:43 +02:00
$wonArray = [Result::won, Result::wonadjourned, Result::wonbye, Result::wonforfait];
$blackArray = [Color::black];
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getColor(), $blackArray) !== false && array_search($pairing->getResult(), $wonArray) !== false) {
$totalwins++;
}
2019-05-28 17:04:53 +02:00
}
2019-05-28 18:07:43 +02:00
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $totalwins;
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
2019-05-28 17:04:53 +02:00
}
}