From 4f15eb0c9fbefd4599976e9f86278d97f38bc584 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Tue, 28 May 2019 15:30:55 +0200 Subject: [PATCH 1/4] Removed some fields that are not interesting enough --- src/Enums/Tiebreak.php | 5 +- src/Models/Player.php | 275 ++++++++------------------------------- src/Readers/Pairtwo6.php | 51 ++++---- tests/ReadSws_test.php | 18 +-- 4 files changed, 95 insertions(+), 254 deletions(-) diff --git a/src/Enums/Tiebreak.php b/src/Enums/Tiebreak.php index c552efe..d5ea99a 100644 --- a/src/Enums/Tiebreak.php +++ b/src/Enums/Tiebreak.php @@ -8,6 +8,9 @@ use MyCLabs\Enum\Enum; class Tiebreak extends Enum { const None = ""; + const Keizer = "Keizer"; + const Points = "Points"; + const American = "American"; const Buchholz = "Buchholz"; const BuchholzMed = "Buchholz Median"; const BuchholzCut = "Buchholz Cut"; @@ -16,7 +19,7 @@ class Tiebreak extends Enum const Cumulative = "Cumulative"; const Between = "Mutual Result"; const Koya = "Koya"; - const Baumbach = "Baumbach"; + const Baumbach = "Most wins"; // Ref: https://en.wikipedia.org/wiki/Tie-breaking_in_Swiss-system_tournaments#Most_wins_(Baumbach) Please tell me why? const Performance = "Performance"; const Aro = "Average Rating"; const AroCut = "Average Rating Cut"; diff --git a/src/Models/Player.php b/src/Models/Player.php index 161c3f7..f64a097 100644 --- a/src/Models/Player.php +++ b/src/Models/Player.php @@ -18,42 +18,22 @@ abstract class Player /** @var string */ private $Name; - /** @var int */ - private $Rank; + /** @var int[] */ + private $Ids; - /** @var int */ - private $FideId; - - /** @var int */ - private $ExtraPts; - - /** @var int */ - private $KbsbElo; + /** @var int[] */ + private $Elos; /** @var DateTime */ - private $dateofbirth; + private $DateOfBirth; - /** @var int */ - private $KbsbID; - - /** @var float */ - private $Points; - - /** @var int */ - private $ClubNr; - - /** @var float */ - private $ScoreBucholtz; - - /** @var float */ - private $ScoreAmerican; - - /** @var int */ - private $FideElo; + /** @var float[] */ + private $Tiebreaks; /** @var string */ private $Nation; + // TODO: Implement categories /** @var string */ private $Category; @@ -63,15 +43,12 @@ abstract class Player /** @var Gender */ private $Gender; - /** @var int */ - private $NumberOfTies; - - /** @var bool */ - private $Absent; - /** @var Pairing[] */ private $Pairings = []; + /** @var bool|DateTime|int|string[] */ + private $BinaryData; + /** * @return string */ @@ -91,200 +68,74 @@ abstract class Player } /** - * @return int + * @return int[] */ - public function getRank(): int + public function getIds(): array { - return $this->Rank; + return $this->Ids; } /** - * @param int $Rank + * @param int[] $Ids * @return Player */ - public function setRank(int $Rank): Player + public function setIds(array $Ids): Player { - $this->Rank = $Rank; + $this->Ids = $Ids; return $this; } /** - * @return int + * @return int[] */ - public function getFideId(): int + public function getElos(): array { - return $this->FideId; + return $this->Elos; } /** - * @param int $FideId + * @param int[] $Elos * @return Player */ - public function setFideId(int $FideId): Player + public function setElos(array $Elos): Player { - $this->FideId = $FideId; + $this->Elos = $Elos; return $this; } /** - * @return int + * @return \DateTime */ - public function getExtraPts(): int + public function getDateOfBirth(): \DateTime { - return $this->ExtraPts; + return $this->DateOfBirth; } /** - * @param int $ExtraPts + * @param \DateTime $DateOfBirth * @return Player */ - public function setExtraPts(int $ExtraPts): Player + public function setDateOfBirth(\DateTime $DateOfBirth): Player { - $this->ExtraPts = $ExtraPts; + $this->DateOfBirth = $DateOfBirth; return $this; } /** - * @return int + * @return float[] */ - public function getKbsbElo(): int + public function getTiebreaks(): array { - return $this->KbsbElo; + return $this->Tiebreaks; } /** - * @param int $KbsbElo + * @param float[] $Tiebreaks * @return Player */ - public function setKbsbElo(int $KbsbElo): Player + public function setTiebreaks(array $Tiebreaks): Player { - $this->KbsbElo = $KbsbElo; - return $this; - } - - /** - * @return DateTime - */ - public function getDateofbirth(): DateTime - { - return $this->dateofbirth; - } - - /** - * @param DateTime $dateofbirth - * @return Player - */ - public function setDateofbirth(DateTime $dateofbirth): Player - { - $this->dateofbirth = $dateofbirth; - return $this; - } - - /** - * @return int - */ - public function getKbsbID(): int - { - return $this->KbsbID; - } - - /** - * @param int $KbsbID - * @return Player - */ - public function setKbsbID(int $KbsbID): Player - { - $this->KbsbID = $KbsbID; - return $this; - } - - /** - * @return float - */ - public function getPoints(): float - { - return $this->Points; - } - - /** - * @param float $Points - * @return Player - */ - public function setPoints(float $Points): Player - { - $this->Points = $Points; - return $this; - } - - /** - * @return int - */ - public function getClubNr(): int - { - return $this->ClubNr; - } - - /** - * @param int $ClubNr - * @return Player - */ - public function setClubNr(int $ClubNr): Player - { - $this->ClubNr = $ClubNr; - return $this; - } - - /** - * @return float - */ - public function getScoreBucholtz(): float - { - return $this->ScoreBucholtz; - } - - /** - * @param float $ScoreBucholtz - * @return Player - */ - public function setScoreBucholtz(float $ScoreBucholtz): Player - { - $this->ScoreBucholtz = $ScoreBucholtz; - return $this; - } - - /** - * @return float - */ - public function getScoreAmerican(): float - { - return $this->ScoreAmerican; - } - - /** - * @param float $ScoreAmerican - * @return Player - */ - public function setScoreAmerican(float $ScoreAmerican): Player - { - $this->ScoreAmerican = $ScoreAmerican; - return $this; - } - - /** - * @return int - */ - public function getFideElo(): int - { - return $this->FideElo; - } - - /** - * @param int $FideElo - * @return Player - */ - public function setFideElo(int $FideElo): Player - { - $this->FideElo = $FideElo; + $this->Tiebreaks = $Tiebreaks; return $this; } @@ -364,42 +215,6 @@ abstract class Player return $this; } - /** - * @return int - */ - public function getNumberOfTies(): int - { - return $this->NumberOfTies; - } - - /** - * @param int $NumberOfTies - * @return Player - */ - public function setNumberOfTies(int $NumberOfTies): Player - { - $this->NumberOfTies = $NumberOfTies; - return $this; - } - - /** - * @return bool - */ - public function isAbsent(): bool - { - return $this->Absent; - } - - /** - * @param bool $Absent - * @return Player - */ - public function setAbsent(bool $Absent): Player - { - $this->Absent = $Absent; - return $this; - } - /** * @return Pairing[] */ @@ -417,4 +232,24 @@ abstract class Player $this->Pairings = $Pairings; return $this; } + + /** + * @param string $Key + * @return bool|DateTime|int|string + */ + public function getBinaryData(string $Key) + { + return $this->BinaryData[$Key]; + } + + /** + * @param string $Key + * @param bool|int|DateTime|string $Value + * @return Player + */ + public function setBinaryData(string $Key, $Value): Player + { + $this->BinaryData[$Key] = $Value; + return $this; + } } diff --git a/src/Readers/Pairtwo6.php b/src/Readers/Pairtwo6.php index cefbfc0..e07d636 100644 --- a/src/Readers/Pairtwo6.php +++ b/src/Readers/Pairtwo6.php @@ -296,24 +296,25 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface for ($i = 0; $i < $this->getBinaryData("NewPlayer"); $i++) { $player = new Player(); + // Rank (Unused value) $length = 4; - $player->SetRank($this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("Rank", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 4; - $this->setBinaryData("Players($i)_NamePos", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("NamePos", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 4; - $player->SetFideId($this->readData('Int', substr($swscontents, $offset, $length))); + $ids['fide'] = $this->readData('Int', substr($swscontents, $offset, $length)); $offset += $length; $length = 4; - $player->SetExtraPts($this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("ExtraPts", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 4; - $player->SetKbsbElo($this->readData('Int', substr($swscontents, $offset, $length))); + $elos['kbsb'] = $this->readData('Int', substr($swscontents, $offset, $length)); $offset += $length; $length = 4; @@ -321,35 +322,35 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface $offset += $length; $length = 4; - $player->setKbsbID($this->readData('Int', substr($swscontents, $offset, $length))); + $ids['kbsb'] = $this->readData('Int', substr($swscontents, $offset, $length)); $offset += $length; $length = 4; - $player->setPoints($this->readData('Int', substr($swscontents, $offset, $length)) / 2); + $player->setBinaryData("Points", $this->readData('Int', substr($swscontents, $offset, $length)) / 2); $offset += $length; $length = 4; - $player->setClubNr($this->readData('Int', substr($swscontents, $offset, $length))); + $ids['club'] = $this->readData('Int', substr($swscontents, $offset, $length)); $offset += $length; $length = 4; - $player->setScoreBucholtz($this->readData('Int', substr($swscontents, $offset, $length)) / 2); + $player->setBinaryData("ScoreBuchholz", $this->readData('Int', substr($swscontents, $offset, $length)) / 2); $offset += $length; $length = 4; - $player->setScoreAmerican($this->readData('Int', substr($swscontents, $offset, $length)) / 2); + $player->setBinaryData("ScoreAmerican", $this->readData('Int', substr($swscontents, $offset, $length)) / 2); $offset += $length; $length = 4; - $this->setBinaryData("Players($i)_HelpValue", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("HelpValue", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 4; - $player->setFideElo($this->readData('Int', substr($swscontents, $offset, $length))); + $elos['fide'] = $this->readData('Int', substr($swscontents, $offset, $length)); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_NameLength", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("NameLength", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 3; @@ -422,41 +423,43 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface $offset += $length; $length = 1; - $player->setNumberOfTies($this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData('NumberOfTies', $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $player->setAbsent($this->readData('Bool', substr($swscontents, $offset, $length))); + $player->setBinaryData('Absent', $this->readData('Bool', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_ColorDiff", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("ColorDiff", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_ColorPref", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("ColorPref", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_Paired", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("Paired", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_Float", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("Float", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_FloatPrev", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("FloatPrev", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_FloatBefore", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("FloatBefore", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; $length = 1; - $this->setBinaryData("Players($i)_TieMatch", $this->readData('Int', substr($swscontents, $offset, $length))); + $player->setBinaryData("TieMatch", $this->readData('Int', substr($swscontents, $offset, $length))); $offset += $length; + $player->setElos($elos); + $player->setIds($ids); $this->getTournament()->addPlayer($player); } // PlayerNames @@ -465,9 +468,9 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface $offset += $length; for ($i = 0; $i < $this->getBinaryData("NewPlayer"); $i++) { - $namelength = $this->getBinaryData("Players($i)_NameLength"); - $nameoffset = $this->getBinaryData("Players($i)_NamePos"); $player = $this->getTournament()->getPlayerById($i); + $namelength = $player->getBinaryData("NameLength"); + $nameoffset = $player->getBinaryData("NamePos"); $player->setName($this->readData("String", substr($this->getBinaryData("PlayerNames"), $nameoffset, $namelength))); $this->getTournament()->updatePlayer($i, $player); diff --git a/tests/ReadSws_test.php b/tests/ReadSws_test.php index fff4e85..ba0d038 100644 --- a/tests/ReadSws_test.php +++ b/tests/ReadSws_test.php @@ -46,21 +46,21 @@ echo "Place: " . $sws->getTournament()->getOrganiserPlace() . PHP_EOL; echo "Unrated-Elo: " . $sws->getTournament()->getNonRatedElo() . PHP_EOL; echo "Federation: " . $sws->getTournament()->getFederation() . PHP_EOL; echo "Organiser: " . $sws->getTournament()->getOrganiserClubNo() . PHP_EOL; -echo "Fide Elo P1: " . $sws->getTournament()->getPlayerById(0)->getFideElo() . PHP_EOL; -echo "Fide Elo P2: " . $sws->getTournament()->getPlayerById(1)->getFideElo() . PHP_EOL; -echo "Fide Elo P3: " . $sws->getTournament()->getPlayerById(2)->getFideElo() . PHP_EOL; -echo "KBSB Elo P1: " . $sws->getTournament()->getPlayerById(0)->getKbsbElo() . PHP_EOL; -echo "KBSB Elo P2: " . $sws->getTournament()->getPlayerById(1)->getKbsbElo() . PHP_EOL; -echo "KBSB Elo P3: " . $sws->getTournament()->getPlayerById(2)->getKbsbElo() . PHP_EOL; +echo "Fide Elo P1: " . $sws->getTournament()->getPlayerById(0)->getElos()['fide'] . PHP_EOL; +echo "Fide Elo P2: " . $sws->getTournament()->getPlayerById(1)->getElos()['fide'] . PHP_EOL; +echo "Fide Elo P3: " . $sws->getTournament()->getPlayerById(2)->getElos()['fide'] . PHP_EOL; +echo "KBSB Elo P1: " . $sws->getTournament()->getPlayerById(0)->getElos()['kbsb'] . PHP_EOL; +echo "KBSB Elo P2: " . $sws->getTournament()->getPlayerById(1)->getElos()['kbsb'] . PHP_EOL; +echo "KBSB Elo P3: " . $sws->getTournament()->getPlayerById(2)->getElos()['kbsb'] . PHP_EOL; echo "Name P1: " . $sws->getTournament()->getPlayerById(0)->getName() . PHP_EOL; echo "Name P2: " . $sws->getTournament()->getPlayerById(1)->getName() . PHP_EOL; echo "Name P3: " . $sws->getTournament()->getPlayerById(2)->getName() . PHP_EOL; echo "Gender P1: " . $sws->getTournament()->getPlayerById(0)->getGender()->getKey() . PHP_EOL; echo "Gender P2: " . $sws->getTournament()->getPlayerById(1)->getGender()->getKey() . PHP_EOL; echo "Gender P3: " . $sws->getTournament()->getPlayerById(2)->getGender()->getKey() . PHP_EOL; -echo "Absent P1: " . $sws->getTournament()->getPlayerById(0)->isAbsent() . PHP_EOL; -echo "Absent P2: " . $sws->getTournament()->getPlayerById(1)->isAbsent() . PHP_EOL; -echo "Absent P3: " . $sws->getTournament()->getPlayerById(2)->isAbsent() . PHP_EOL; +echo "Absent P1: " . $sws->getTournament()->getPlayerById(0)->getBinaryData("absent") . PHP_EOL; +echo "Absent P2: " . $sws->getTournament()->getPlayerById(1)->getBinaryData("absent") . PHP_EOL; +echo "Absent P3: " . $sws->getTournament()->getPlayerById(2)->getBinaryData("absent") . PHP_EOL; echo "Date Round 1: " . $sws->getTournament()->getRoundByNo(0)->getDate()->format('d/m/Y') . PHP_EOL; echo "Date Round 2: " . $sws->getTournament()->getRoundByNo(1)->getDate()->format('d/m/Y') . PHP_EOL; echo "Date Round 3: " . $sws->getTournament()->getRoundByNo(2)->getDate()->format('d/m/Y') . PHP_EOL; From 74688ac8b8088b5963ec20e913c3f8be0a59574f Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Tue, 28 May 2019 16:26:03 +0200 Subject: [PATCH 2/4] Implemented first tiebreaks --- src/Enums/TournamentSystem.php | 2 +- src/Interfaces/ReaderInterface.php | 2 +- src/Models/Game.php | 2 - src/Models/Player.php | 2 +- src/Models/Tournament.php | 1 - src/Readers/Models/Pairtwo6.php | 2 - src/Readers/Pairtwo6.php | 29 ++++++++++++- src/Tournament.php | 68 +++++++++++++++++++++++++----- tests/ReadSws_test.php | 3 ++ 9 files changed, 91 insertions(+), 20 deletions(-) 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; +} From 865893b30f623aa86caef811064449918a6f45c4 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Tue, 28 May 2019 17:04:53 +0200 Subject: [PATCH 3/4] Moved tiebreaks to own class --- src/Tiebreaks.php | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/Tournament.php | 39 +-------------------------------------- 2 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 src/Tiebreaks.php diff --git a/src/Tiebreaks.php b/src/Tiebreaks.php new file mode 100644 index 0000000..4b5cb81 --- /dev/null +++ b/src/Tiebreaks.php @@ -0,0 +1,45 @@ +getPlayers() as $player) { + $currentTiebreaks = $player->getTiebreaks(); + $currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican'); + $player->setTiebreaks($currentTiebreaks); + } + } + + /** + * @param int $key + */ + protected function calculateAmerican(int $key) + { + foreach ($this->getPlayers() as $player) { + $currentTiebreaks = $player->getTiebreaks(); + $currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican'); + $player->setTiebreaks($currentTiebreaks); + } + } + + /** + * @param int $key + */ + protected function calculatePoints(int $key) + { + foreach ($this->getPlayers() as $player) { + $currentTiebreaks = $player->getTiebreaks(); + $currentTiebreaks[$key] = $player->getBinaryData('Points'); + $player->setTiebreaks($currentTiebreaks); + } + } +} diff --git a/src/Tournament.php b/src/Tournament.php index 125329b..b071d21 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -9,10 +9,9 @@ namespace JeroenED\Libpairtwo; use JeroenED\Libpairtwo\Enums\Tiebreak; -use JeroenED\Libpairtwo\Models\Tournament as TournamentModel; use JeroenED\Libpairtwo\Enums\Color; -class Tournament extends TournamentModel +class Tournament extends Tiebreaks { /** * Gets a player by its ID @@ -246,40 +245,4 @@ class Tournament extends TournamentModel } return $this; } - - /** - * @param int $key - */ - private function calculateKeizer(int $key) - { - 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); - } - } } From 6d61d2a650dbe093b17ec5921cd3a471be87dae0 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Tue, 28 May 2019 18:07:43 +0200 Subject: [PATCH 4/4] 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; }