From a149f5e8bc31c7b865dead9c9b343bac6e588b7f Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Mon, 11 Feb 2019 22:41:44 +0100 Subject: [PATCH] Added conversion of pairings into rounds --- src/Enums/Gameresult.php | 24 +++++++ src/Models/Game.php | 63 +++++++++++++++++ src/Models/Pairing.php | 4 +- src/Models/Round.php | 20 ++++++ src/Models/Tournament.php | 26 ++++++- src/Round.php | 8 +++ src/Sws.php | 5 +- src/Tournament.php | 143 +++++++++++++++++++++++++++++++++++++- tests/ReadSws_test.php | 4 +- 9 files changed, 285 insertions(+), 12 deletions(-) create mode 100644 src/Enums/Gameresult.php diff --git a/src/Enums/Gameresult.php b/src/Enums/Gameresult.php new file mode 100644 index 0000000..dc90132 --- /dev/null +++ b/src/Enums/Gameresult.php @@ -0,0 +1,24 @@ +white; + } + + /** + * @param Player $white + */ + public function setWhite(Player $white): void + { + $this->white = $white; + } + + /** + * @return Player + */ + public function getBlack() + { + return $this->black; + } + + /** + * @param Player $black + */ + public function setBlack(Player $black): void + { + $this->black = $black; + } + + /** + * @return GameResult + */ + public function getResult() + { + return $this->result; + } + + /** + * @param GameResult $result + */ + public function setResult(GameResult $result): void + { + $this->result = $result; + } + + + + } diff --git a/src/Models/Pairing.php b/src/Models/Pairing.php index e592d60..0374634 100644 --- a/src/Models/Pairing.php +++ b/src/Models/Pairing.php @@ -23,7 +23,7 @@ class Pairing /** * @return Player */ - public function getPlayer(): Player + public function getPlayer() { return $this->Player; } @@ -39,7 +39,7 @@ class Pairing /** * @return Player */ - public function getOpponent(): Player + public function getOpponent() { return $this->Opponent; } diff --git a/src/Models/Round.php b/src/Models/Round.php index 5f3789e..37f4b55 100644 --- a/src/Models/Round.php +++ b/src/Models/Round.php @@ -15,6 +15,9 @@ class Round private $date; private $games; + /** @var int */ + private $roundNo; + /** * @return DateTime * @@ -47,4 +50,21 @@ class Round { $this->games = $games; } + + /** + * @return int + */ + public function getRoundNo(): int + { + return $this->roundNo; + } + + /** + * @param int $roundNo + */ + public function setRoundNo(int $roundNo): void + { + $this->roundNo = $roundNo; + } + } diff --git a/src/Models/Tournament.php b/src/Models/Tournament.php index 736f17f..8a81e28 100644 --- a/src/Models/Tournament.php +++ b/src/Models/Tournament.php @@ -47,7 +47,11 @@ class Tournament private $StartDate; private $EndDate; private $Arbiter; + private $NoOfRounds; + + /** @var Round[] */ private $Rounds; + private $Participants; private $Tempo; private $NonRatedElo; @@ -222,15 +226,31 @@ class Tournament /** * @return int */ - public function getRounds(): int + public function getNoOfRounds(): int + { + return $this->NoOfRounds; + } + + /** + * @param int $NoOfRounds + */ + public function setNoOfRounds(int $NoOfRounds): void + { + $this->NoOfRounds = $NoOfRounds; + } + + /** + * @return Round[] + */ + public function getRounds() { return $this->Rounds; } /** - * @param int $Rounds + * @param Round[] $Rounds */ - public function setRounds(int $Rounds): void + public function setRounds(array $Rounds): void { $this->Rounds = $Rounds; } diff --git a/src/Round.php b/src/Round.php index abfdcb4..a93c55a 100644 --- a/src/Round.php +++ b/src/Round.php @@ -12,4 +12,12 @@ use JeroenED\Libpairtwo\Models\Round as RoundModel; class Round extends RoundModel { + + public function addGame(Game $game) + { + $newarray = $this->getGames(); + $newarray[] = $game; + $this->setGames($newarray); + + } } diff --git a/src/Sws.php b/src/Sws.php index deee09e..1a585f3 100644 --- a/src/Sws.php +++ b/src/Sws.php @@ -385,7 +385,7 @@ class Sws extends SwsModel // Rounds $length = 4; - $sws->getTournament()->setRounds(self::ReadData('Int', substr($swscontents, $offset, $length))); + $sws->getTournament()->setNoOfRounds(self::ReadData('Int', substr($swscontents, $offset, $length))); $offset += $length; // Participants @@ -469,7 +469,7 @@ class Sws extends SwsModel $offset += $length; // Round dates - for ($i = 1; $i < $sws->getTournament()->getRounds(); $i++) { + for ($i = 1; $i < $sws->getTournament()->getNoOfRounds(); $i++) { $length = 4; $sws->setBinaryData('Round_' . $i . '_date', self::ReadData('Date', substr($swscontents, $offset, $length))); $offset += $length; @@ -505,6 +505,7 @@ class Sws extends SwsModel } } + $sws->getTournament()->pairingsToRounds(); return $sws; } diff --git a/src/Tournament.php b/src/Tournament.php index eb56808..45fec88 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -8,8 +8,11 @@ namespace JeroenED\Libpairtwo; +use JeroenED\Libpairtwo\Enums\Result; use JeroenED\Libpairtwo\Models\Tournament as TournamentModel; -use JeroenED\LibPairtwo\Player; +use JeroenED\Libpairtwo\Player; +use JeroenED\Libpairtwo\Enums\Color; +use JeroenED\Libpairtwo\Enums\Gameresult; class Tournament extends TournamentModel { @@ -48,11 +51,20 @@ class Tournament extends TournamentModel */ public function addRound(Round $round) { - $newArray = $this->GetRounds(); - $newArray[] = $round; + $newArray = $this->getRounds(); + $newArray[$round->getRoundNo()] = $round; $this->setRounds($newArray); } + /** + * @param int $roundNo + * @return Round + */ + public function getRoundByNo(int $roundNo): Round + { + return $this->getRounds()[$roundNo]; + } + /** * @param Pairing $pairing */ @@ -63,6 +75,131 @@ class Tournament extends TournamentModel $this->setPairings($newArray); } + /** + * This function converts the array of pairings into rounds + */ + public function pairingsToRounds(): void + { + $pairings = $this->getPairings(); + foreach ($pairings as $pairing) { + $round = $pairing->getRound(); + $player = $pairing->getPlayer(); + $opponent = $pairing->getOpponent(); + $color = $pairing->getColor(); + $result = $pairing->getResult(); + + $game = new Game(); + if ($color->getValue() == Color::white || $color->getValue() == Color::white3) { + if(! is_null($player)) $game->setWhite($player); + if(! is_null($opponent)) $game->setBlack($opponent); + switch ($result->getValue()) { + case Result::won: + case Result::wonbye: + $game->setResult(new Gameresult("1-0")); break; + case Result::wonforfait: + $game->setResult(new Gameresult("1-0FF")); break; + case Result::wonadjourned: + $game->setResult(new Gameresult("1-0A")); break; + case Result::lost: + case Result::bye: + $game->setResult(new Gameresult("0-1")); break; + case Result::absent: + $game->setResult(new Gameresult("0-1FF")); break; + case Result::adjourn: + $game->setResult(new Gameresult("0-1A")); break; + case Result::draw: + $game->setResult(new Gameresult("0.5-0.5")); break; + case Result::drawadjourned: + $game->setResult(new Gameresult("0.5-0.5A")); break; + case Result::none: + default: + $game->setResult(new Gameresult('-')); break; + } + } elseif ($color->getValue() == Color::black || $color->getValue() == Color::black3) { + if(! is_null($player)) $game->setBlack($player); + if(! is_null($opponent)) $game->setWhite($opponent); + switch ($result->getValue()) { + case Result::won: + case Result::wonbye: + $game->setResult(new Gameresult("0-1")); break; + case Result::wonforfait: + $game->setResult(new Gameresult("0-1FF")); break; + case Result::wonadjourned: + $game->setResult(new Gameresult("0-1A")); break; + case Result::lost: + case Result::bye: + $game->setResult(new Gameresult("1-0")); break; + case Result::absent: + $game->setResult(new Gameresult("1-0FF")); break; + case Result::adjourn: + $game->setResult(new Gameresult("1-0A")); break; + case Result::draw: + $game->setResult(new Gameresult("0.5-0.5")); break; + case Result::drawadjourned: + $game->setResult(new Gameresult("0.5-0.5A")); break; + case Result::none: + default: + $game->setResult(new Gameresult('-')); break; + } + } + + // Check if game already exists + if (!$this->GameExists($game, $round)) { + $this->AddGame($game, $round); + } + } + } + + /** + * Checks if a game already is already registered + * + * @param Game $game + * @param int $round + * @return bool + */ + public function GameExists(Game $game, int $round = -1): bool + { + $search = [ $round ]; + if ($round == -1) { + $search = []; + for ($i = 0; $i < $this->getNoOfRounds(); $i++) { + $search[] = $i; + } + } + + foreach ($search as $round) { + if (!isset($this->getRounds()[$round])) { + return false; + } + $games = $this->getRounds()[$round]->getGames(); + foreach ($games as $roundgame) { + if ($roundgame->getWhite() == $game->getWhite() && + $roundgame->getBlack() == $game->getBlack() && + $roundgame->getResult() == $game->getResult() + ) { + return true; + } + } + } + + return false; + } + + /** + * @param Game $game + * @param int $round + */ + public function addGame(Game $game, int $round) + { + if (!isset($this->getRounds()[$round])) { + $roundObj = new Round(); + $roundObj->setRoundNo($round); + $this->addRound($roundObj); + } + + $this->getRoundByNo($round)->addGame($game); + } + /** * @param bool $americansort * @return Player[] diff --git a/tests/ReadSws_test.php b/tests/ReadSws_test.php index 12ed499..84c29ad 100644 --- a/tests/ReadSws_test.php +++ b/tests/ReadSws_test.php @@ -34,8 +34,8 @@ echo "Organiser: " . $sws->getTournament()->getOrganiser(). PHP_EOL; echo "Tempo: " . $sws->getTournament()->getTempo() . PHP_EOL; echo "Country: " . $sws->getTournament()->getOrganiserCountry() . PHP_EOL; echo "Arbiter: " . $sws->getTournament()->getArbiter() . PHP_EOL; -echo "Rounds: " . $sws->getTournament()->getRounds() . PHP_EOL; -echo "Participants: " . $sws->getTournament()->getRounds() . PHP_EOL; +echo "Rounds: " . $sws->getTournament()->getNoOfRounds() . PHP_EOL; +echo "Participants: " . $sws->getTournament()->getNoOfRounds() . PHP_EOL; echo "Fidehomol: " . $sws->getTournament()->getFideHomol() . PHP_EOL; echo "Start-Date: " . $sws->getTournament()->getStartDate()->format('d/m/Y') . PHP_EOL; echo "End-Date: " . $sws->getTournament()->getEndDate()->format('d/m/Y') . PHP_EOL;