diff --git a/src/Enums/TournamentSystem.php b/src/Enums/TournamentSystem.php index 98b0f8f..03305ce 100644 --- a/src/Enums/TournamentSystem.php +++ b/src/Enums/TournamentSystem.php @@ -15,5 +15,5 @@ class TournamentSystem extends Enum const Swiss = 'Swiss'; const Closed = 'Closed'; const American = 'American'; - const Imperial = 'Imperial'; + const Keizer = 'Keizer'; } diff --git a/src/Interfaces/ReaderInterface.php b/src/Interfaces/ReaderInterface.php index d0f83b4..68f7f90 100644 --- a/src/Interfaces/ReaderInterface.php +++ b/src/Interfaces/ReaderInterface.php @@ -7,6 +7,6 @@ use JeroenED\Libpairtwo\Tournament; interface ReaderInterface { - public function read($filename); + public function read($filename): ReaderInterface; public function getTournament(): Tournament; } diff --git a/src/Models/Game.php b/src/Models/Game.php index de3eae1..e8922d8 100644 --- a/src/Models/Game.php +++ b/src/Models/Game.php @@ -75,6 +75,4 @@ abstract class Game $this->result = $result; return $this; } - - } diff --git a/src/Models/Player.php b/src/Models/Player.php index f64a097..33e08c5 100644 --- a/src/Models/Player.php +++ b/src/Models/Player.php @@ -28,7 +28,7 @@ abstract class Player private $DateOfBirth; /** @var float[] */ - private $Tiebreaks; + private $Tiebreaks = []; /** @var string */ private $Nation; diff --git a/src/Models/Tournament.php b/src/Models/Tournament.php index 20a4354..150d3f6 100644 --- a/src/Models/Tournament.php +++ b/src/Models/Tournament.php @@ -520,5 +520,4 @@ abstract class Tournament $this->Tiebreaks = $Tiebreaks; return $this; } - } diff --git a/src/Readers/Models/Pairtwo6.php b/src/Readers/Models/Pairtwo6.php index 8494ab8..84ee58c 100644 --- a/src/Readers/Models/Pairtwo6.php +++ b/src/Readers/Models/Pairtwo6.php @@ -72,6 +72,4 @@ abstract class Pairtwo6 $this->BinaryData[$Key] = $Value; return $this; } - - } diff --git a/src/Readers/Pairtwo6.php b/src/Readers/Pairtwo6.php index e07d636..5a1ad96 100644 --- a/src/Readers/Pairtwo6.php +++ b/src/Readers/Pairtwo6.php @@ -60,7 +60,7 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface * @param string $filename * @return Pairtwo6 */ - public function read($filename) + public function read($filename): ReaderInterface { $swshandle = fopen($filename, 'rb'); $swscontents = fread($swshandle, filesize($filename)); @@ -556,7 +556,7 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface $system = TournamentSystem::American; break; case 6: - $system = TournamentSystem::Imperial; + $system = TournamentSystem::Keizer; break; case 0: default: @@ -694,6 +694,8 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface } } + $this->addTiebreaks(); + $this->getTournament()->pairingsToRounds(); return $this; } @@ -799,4 +801,27 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface return DateTime::createFromFormat($format, $concat); } + + + /** + * @return $this + */ + private function addTiebreaks(): Pairtwo6 + { + switch ($this->getTournament()->getSystem()) { + case TournamentSystem::Keizer: + $firstElement = new Tiebreak(Tiebreak::Keizer); + break; + case TournamentSystem::American: + $firstElement = new Tiebreak(Tiebreak::American); + break; + case TournamentSystem::Closed: + case TournamentSystem::Swiss: + $firstElement = new Tiebreak(Tiebreak::Points); + } + $tiebreaks = $this->getTournament()->getTiebreaks(); + array_unshift($tiebreaks, $firstElement); + $this->getTournament()->setTiebreaks($tiebreaks); + return $this; + } } diff --git a/src/Tournament.php b/src/Tournament.php index bbe728c..125329b 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -203,14 +203,14 @@ class Tournament extends TournamentModel /** * Gets the ranking of the tournament * - * @param bool $americansort * @return Player[] */ - public function getRanking(bool $americansort = false) + public function getRanking() { + $this->calculateTiebreaks(); $players = $this->getPlayers(); - $americansort ? usort($players, array($this, "SortAmerican")) : usort($players, array($this, "SortNormal")); + usort($players, array($this, "SortTiebreak")); return $players; } @@ -220,18 +220,66 @@ class Tournament extends TournamentModel * @param Player $b * @return int */ - private function sortNormal(Player $a, Player $b) + private function sortTiebreak(Player $a, Player $b) { - return $b->getPoints() - $a->getPoints(); + return $b->getTiebreaks()[0] - $a->getTiebreaks()[0]; + } + + + /** + * @return Tournament + */ + private function calculateTiebreaks(): Tournament + { + 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; + } + } + return $this; } /** - * @param Player $a - * @param Player $b - * @return int + * @param int $key */ - private function sortAmerican(Player $a, Player $b) + private function calculateKeizer(int $key) { - return $b->getScoreAmerican() - $a->getScoreAmerican(); + foreach ($this->getPlayers() as $player) { + $currentTiebreaks = $player->getTiebreaks(); + $currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican'); + $player->setTiebreaks($currentTiebreaks); + } + } + + /** + * @param int $key + */ + private function calculateAmerican(int $key) + { + foreach ($this->getPlayers() as $player) { + $currentTiebreaks = $player->getTiebreaks(); + $currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican'); + $player->setTiebreaks($currentTiebreaks); + } + } + + /** + * @param int $key + */ + private function calculatePoints(int $key) + { + foreach ($this->getPlayers() as $player) { + $currentTiebreaks = $player->getTiebreaks(); + $currentTiebreaks[$key] = $player->getBinaryData('Points'); + $player->setTiebreaks($currentTiebreaks); + } } } diff --git a/tests/ReadSws_test.php b/tests/ReadSws_test.php index ba0d038..bb7551c 100644 --- a/tests/ReadSws_test.php +++ b/tests/ReadSws_test.php @@ -73,3 +73,6 @@ echo "Player Pairing 3: " . $sws->getTournament()->getPairings()[2]->getPlayer() echo "Tiebreak 1: " . $sws->getTournament()->getTiebreaks()[0]->getValue() . PHP_EOL; 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; +}