From c6d1afd171baa048647f12fda9e72fd200e59e98 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Wed, 20 Mar 2019 12:46:46 +0100 Subject: [PATCH] Reimplemented creation of results --- src/Enums/Gameresult.php | 23 +++++----- src/Game.php | 28 +++++++++++++ src/Models/Game.php | 17 ++++---- src/Tournament.php | 90 ++++++++++++++-------------------------- 4 files changed, 80 insertions(+), 78 deletions(-) diff --git a/src/Enums/Gameresult.php b/src/Enums/Gameresult.php index dc90132..b5c5900 100644 --- a/src/Enums/Gameresult.php +++ b/src/Enums/Gameresult.php @@ -12,13 +12,16 @@ use MyCLabs\Enum\Enum; class Gameresult extends Enum { - const WhiteWins = "1-0"; - const WhiteWinsForfait = "1-0FF"; - const WhiteWinsAdjourned = "1-0A"; - const BlackWins = "0-1"; - const BlackWinsForfait = "0-1FF"; - const BlackWinsAdjourned = "0-1A"; - const Draw = "0.5-0.5"; - const DrawAdjourned = "0.5-0.5A"; - const None = "-"; -} \ No newline at end of file + const WhiteWins = '1-0'; + const Draw = '0.5-0.5'; + const BlackWins = '0-1'; + const WhiteWinsForfait = '1-0 FF'; + const BlackWinsForfait = '0-1 FF'; + const BothLoseForfait = '0-0 FF'; + const BothWinAdjourned = '1-1 A'; + const WhiteWinsBlackDrawsAdjourned = '1-0.5 A'; + const WhiteDrawsBlackWinsAdjourned = '0.5-1 A'; + const DrawAdjourned = '0.5-0.5 A'; + const WhiteLoseBlackDrawsAdjourned = '0-0.5'; + const WhiteDrawsBlackLoseAdjourned = '0.5-0'; +} diff --git a/src/Game.php b/src/Game.php index f7ac565..f1eea88 100644 --- a/src/Game.php +++ b/src/Game.php @@ -8,8 +8,36 @@ namespace JeroenED\Libpairtwo; +use JeroenED\Libpairtwo\Enums\Gameresult; use JeroenED\Libpairtwo\Models\Game as GameModel; class Game extends GameModel { + /** + * This function gets the result from the game + */ + public function getResult(): Gameresult + { + if (!is_null(parent::getResult())) { + return parent::getResult(); + } + + $whiteResult = $this->getWhite()->getResult(); + $blackResult = $this->getBlack()->getResult(); + + $whitesplit = explode(" ", $whiteResult); + $blacksplit = explode(" ", $blackResult); + + $special=''; + if (isset($whitesplit[1]) && $whitesplit[1] != 'Bye') { + $special = ' ' . $whitesplit[1]; + } + if (isset($blacksplit[1]) && $blacksplit[1] != 'Bye') { + $special = ' ' . $blacksplit[1]; + } + $result = $whitesplit[0] . '-' . $blacksplit[0] . $special; + $this->setResult(new Gameresult($result)); + + return new Gameresult($result); + } } diff --git a/src/Models/Game.php b/src/Models/Game.php index 1ed31c1..eb27d99 100644 --- a/src/Models/Game.php +++ b/src/Models/Game.php @@ -10,20 +10,21 @@ namespace JeroenED\Libpairtwo\Models; use JeroenED\Libpairtwo\Enums\Gameresult; use JeroenED\LibPairtwo\Player; +use JeroenED\LibPairtwo\Pairing; class Game { - /** @var Player */ + /** @var Pairing */ private $white; - /** @var Player */ + /** @var Pairing */ private $black; /** @var GameResult */ private $result; /** - * @return Player + * @return Pairing */ public function getWhite() { @@ -31,15 +32,15 @@ class Game } /** - * @param Player $white + * @param Pairing $white */ - public function setWhite(Player $white): void + public function setWhite($white): void { $this->white = $white; } /** - * @return Player + * @return Pairing */ public function getBlack() { @@ -47,9 +48,9 @@ class Game } /** - * @param Player $black + * @param Pairing $black */ - public function setBlack(Player $black): void + public function setBlack($black): void { $this->black = $black; } diff --git a/src/Tournament.php b/src/Tournament.php index 41ea551..21a6572 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -81,71 +81,39 @@ class Tournament extends TournamentModel public function pairingsToRounds(): void { $pairings = $this->getPairings(); + + /** @var Pairing[] */ + $cache = array(); + 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) { - 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::adjourned: - $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) { - 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::adjourned: - $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; + $opponent = null; + foreach($cache as $key=>$cached) { + if (!is_null($cached)) { + if ($cached->getOpponent() == $pairing->getPlayer() && ($cached->getRound() == $pairing->getRound())) { + $opponent = $cached; + $cache[$key] == null; + break; + } } } + $game = new Game(); + if ($color->getValue() == Color::white) { + $game->setWhite($pairing); + $game->setBlack($opponent); + } elseif ($color->getValue() == Color::black) { + $game->setWhite($opponent); + $game->setBlack($pairing); + } - // Check if game already exists - if (!$this->GameExists($game, $round)) { - $this->AddGame($game, $round); + if (is_null($game->getWhite()) || is_null($game->getBlack())) { + $cache[] = $pairing; + } else { + // Check if game already exists + if (!$this->GameExists($game, $round)) { + $this->AddGame($game, $round); + } } } } @@ -172,7 +140,9 @@ class Tournament extends TournamentModel return false; } $games = $this->getRounds()[$round]->getGames(); - if(is_null($games)) return false; + if (is_null($games)) { + return false; + } foreach ($games as $roundgame) { if ($roundgame->getWhite() == $game->getWhite() && $roundgame->getBlack() == $game->getBlack() &&