Added some tiebreaks

This commit is contained in:
Jeroen De Meerleer 2019-05-28 18:07:43 +02:00
parent 865893b30f
commit 6d61d2a650
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 114 additions and 34 deletions

View File

@ -3,43 +3,111 @@
namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Color;
use JeroenED\Libpairtwo\Models\Tournament;
use JeroenED\Libpairtwo\Enums\Result;
abstract class Tiebreaks extends Tournament
{
/**
* @param int $key
* @param Player $player
* @return float
*/
protected function calculateKeizer(int $key)
protected function calculateKeizer(int $key, Player $player): float
{
foreach ($this->getPlayers() as $player) {
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican');
$player->setTiebreaks($currentTiebreaks);
}
$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)
protected function calculateAmerican(int $key, Player $player): float
{
foreach ($this->getPlayers() as $player) {
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican');
$player->setTiebreaks($currentTiebreaks);
$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];
}
/**
* @param int $key
* @param Player $player
* @return float
*/
protected function calculateBaumbach(int $key, Player $player): float
{
$wonArray = [Result::won, Result::wonadjourned, Result::wonbye, Result::wonforfait];
$totalwins = 0;
foreach ($player->getPairings() as $pairing) {
if (array_search($pairing->getResult(), $wonArray) !== false) {
$totalwins++;
}
}
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $totalwins;
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
}
/**
* @param int $key
* @param Player $player
* @return float
*/
protected function calculatePoints(int $key)
protected function calculateBlackPlayed(int $key, Player $player): float
{
foreach ($this->getPlayers() as $player) {
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('Points');
$player->setTiebreaks($currentTiebreaks);
$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
*/
protected function calculateBlackWin(int $key, Player $player): float
{
$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++;
}
}
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $totalwins;
$player->setTiebreaks($currentTiebreaks);
return $currentTiebreaks[$key];
}
}

View File

@ -206,7 +206,6 @@ class Tournament extends Tiebreaks
*/
public function getRanking()
{
$this->calculateTiebreaks();
$players = $this->getPlayers();
usort($players, array($this, "SortTiebreak"));
@ -221,28 +220,41 @@ class Tournament extends Tiebreaks
*/
private function sortTiebreak(Player $a, Player $b)
{
return $b->getTiebreaks()[0] - $a->getTiebreaks()[0];
$result = 0;
foreach ($this->getTiebreaks() as $key=>$tiebreak) {
$result = $this->CalculateTiebreak($key, $b, $a) - $this->CalculateTiebreak($key, $a, $b);
if ($result != 0) {
return $result;
}
}
}
/**
* @return Tournament
* @return float
*/
private function calculateTiebreaks(): Tournament
private function calculateTiebreak(int $key, Player $player, Player $opponent): float
{
foreach ($this->getTiebreaks() as $key=>$tiebreak) {
switch ($tiebreak) {
case Tiebreak::Keizer:
$this->calculateKeizer($key);
break;
case Tiebreak::American:
$this->calculateAmerican($key);
break;
case Tiebreak::Points:
$this->calculatePoints($key);
break;
}
$tiebreak = $this->getTiebreaks()[$key];
switch ($tiebreak) {
case Tiebreak::Keizer:
return $this->calculateKeizer($key, $player);
break;
case Tiebreak::American:
return $this->calculateAmerican($key, $player);
break;
case Tiebreak::Points:
return $this->calculatePoints($key, $player);
break;
case Tiebreak::Baumbach:
return $this->calculateBaumbach($key, $player);
break;
case Tiebreak::BlackPlayed:
return $this->calculateBlackPlayed($key, $player);
break;
case Tiebreak::BlackWin:
return $this->calculateBlackWin($key, $player);
break;
}
return $this;
}
}

View File

@ -74,5 +74,5 @@ echo "Tiebreak 1: " . $sws->getTournament()->getTiebreaks()[0]->getValue() . P
echo "Tiebreak 2: " . $sws->getTournament()->getTiebreaks()[1]->getValue() . PHP_EOL;
echo "Tiebreak 3: " . $sws->getTournament()->getTiebreaks()[2]->getValue() . PHP_EOL;
foreach ($sws->getTournament()->getRanking() as $player) {
echo $player->getName() . ' ' . $player->getTiebreaks()[0] . PHP_EOL;
echo $player->getName() . ' ' . implode(' ', $player->getTiebreaks()) . PHP_EOL;
}