From eeba61595c96ec1b001b0ac8aa16f896213aa9f0 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Tue, 28 May 2019 18:07:43 +0200 Subject: [PATCH] Added some tiebreaks --- src/Tiebreaks.php | 100 ++++++++++++++++++++++++++++++++++------- src/Tournament.php | 46 ++++++++++++------- tests/ReadSws_test.php | 2 +- 3 files changed, 114 insertions(+), 34 deletions(-) diff --git a/src/Tiebreaks.php b/src/Tiebreaks.php index 4b5cb81..0c88693 100644 --- a/src/Tiebreaks.php +++ b/src/Tiebreaks.php @@ -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]; } } diff --git a/src/Tournament.php b/src/Tournament.php index b071d21..b0d696d 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -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; } } diff --git a/tests/ReadSws_test.php b/tests/ReadSws_test.php index bb7551c..aff1dde 100644 --- a/tests/ReadSws_test.php +++ b/tests/ReadSws_test.php @@ -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; }