From 3a681e97d52f32ce9da7d09950c3b06b59edde84 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Sat, 28 Sep 2019 21:49:33 +0200 Subject: [PATCH] NEW FEATURE: `Game::getBoard()` for getting the board number of the game --- CHANGELOG.md | 2 + dist/template.php | 5 ++- src/Constants.php | 14 +++---- src/Enums/Color.php | 6 +-- src/Enums/Result.php | 22 +++++------ src/Game.php | 55 ++++++++++++++++++++-------- src/IOFactory.php | 6 +-- src/Pairing.php | 23 ++++++++++++ src/Readers/Pairtwo6.php | 29 ++++++++------- src/Readers/Swar4.php | 40 ++++++++++---------- src/Round.php | 79 ++++++++++++++++++++++++++++------------ src/Tournament.php | 29 +++++++++++++-- 12 files changed, 207 insertions(+), 103 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e61d30..5f5a58d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # CHANGELOG ## vx.y.z (Release: aa-bbb-cccc) +* NEW FEATURE: `Game::getBoard()` for getting the board number of the game +* ENHANCEMENT: Some fields has been renamed to match coding guideline (Please see [1ab96fa](https://github.com/JeroenED/libpairtwo/commit/1ab96fa04782c1b0f2b6bb9d1bac8397a74ab38e) for more info) ## v1.2 (Release: 28-sep-2019) * NEW READER: `Reader::swar-4` for reading out files created with SWAR version 4. diff --git a/dist/template.php b/dist/template.php index 35d7ec1..330af7e 100644 --- a/dist/template.php +++ b/dist/template.php @@ -40,11 +40,12 @@ foreach ($reader->getTournament()->getRounds() as $round) { echo '' . PHP_EOL; echo '' . PHP_EOL; - echo '' . PHP_EOL; + echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; - foreach ($round->getGames() as $game) { + foreach ($round->getGamesByBoard() as $game) { echo '' . PHP_EOL; + echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; diff --git a/src/Constants.php b/src/Constants.php index 8950dd2..68ec994 100644 --- a/src/Constants.php +++ b/src/Constants.php @@ -27,11 +27,11 @@ use JeroenED\Libpairtwo\Enums\Result; */ class Constants { - const Won = [ Result::won, Result::wonforfait, Result::wonbye, Result::wonadjourned ]; - const Draw = [ Result::draw, Result::drawadjourned ]; - const Lost = [ Result::absent, Result::bye, Result::lost, Result::adjourned ]; - const NotPlayed = [ Result::bye, Result::wonbye, Result::absent ]; - const Played = [ Result::won, Result::wonforfait, Result::wonbye, Result::wonadjourned, Result::draw, Result::drawadjourned, Result::absent, Result::bye, Result::lost, Result::adjourned ]; - const Black = [ Color::black ]; - const White = [ Color::white ]; + const Won = [ Result::Won, Result::WonForfait, Result::WonBye, Result::WonAdjourned ]; + const Draw = [ Result::Draw, Result::DrawAdjourned ]; + const Lost = [ Result::Absent, Result::Bye, Result::Lost, Result::Adjourned ]; + const NotPlayed = [ Result::Bye, Result::WonBye, Result::Absent ]; + const Played = [ Result::Won, Result::WonForfait, Result::WonBye, Result::WonAdjourned, Result::Draw, Result::DrawAdjourned, Result::Absent, Result::Bye, Result::Lost, Result::Adjourned ]; + const Black = [ Color::Black ]; + const White = [ Color::White ]; } diff --git a/src/Enums/Color.php b/src/Enums/Color.php index 9f35580..32d4827 100644 --- a/src/Enums/Color.php +++ b/src/Enums/Color.php @@ -26,7 +26,7 @@ use MyCLabs\Enum\Enum; */ class Color extends Enum { - const black = 'B'; - const white = 'W'; - const none = '*'; + const Black = 'B'; + const White = 'W'; + const None = '*'; } diff --git a/src/Enums/Result.php b/src/Enums/Result.php index a064612..15922a2 100644 --- a/src/Enums/Result.php +++ b/src/Enums/Result.php @@ -26,15 +26,15 @@ use MyCLabs\Enum\Enum; */ class Result extends Enum { - const none = '*'; - const lost = '0'; - const draw = '0.5'; - const won = '1'; - const absent = '0 FF'; - const wonforfait = '1 FF'; - const adjourned = '0 A'; - const drawadjourned = '0.5 A'; - const wonadjourned = '1 A'; - const bye = '0 Bye'; - const wonbye = '1 Bye'; + const None = '*'; + const Lost = '0'; + const Draw = '0.5'; + const Won = '1'; + const Absent = '0 FF'; + const WonForfait = '1 FF'; + const Adjourned = '0 A'; + const DrawAdjourned = '0.5 A'; + const WonAdjourned = '1 A'; + const Bye = '0 Bye'; + const WonBye = '1 Bye'; } diff --git a/src/Game.php b/src/Game.php index 34655b2..0da15af 100644 --- a/src/Game.php +++ b/src/Game.php @@ -29,13 +29,16 @@ use DateTime; class Game { /** @var Pairing | null */ - private $white; + private $White; /** @var Pairing | null */ - private $black; + private $Black; /** @var GameResult | null */ - private $result; + private $Result; + + /** @var int */ + private $Board; /** * Returns the result for the game @@ -44,8 +47,8 @@ class Game */ public function getResult(): Gameresult { - if (!is_null($this->result)) { - return $this->result; + if (!is_null($this->Result)) { + return $this->Result; } $whiteResult = $this->getWhite()->getResult(); @@ -80,18 +83,18 @@ class Game */ public function getWhite(): ?Pairing { - return $this->white; + return $this->White; } /** * Sets pairing for white player * - * @param Pairing | null $white + * @param Pairing | null $White * @return Game */ - public function setWhite(?Pairing $white): Game + public function setWhite(?Pairing $White): Game { - $this->white = $white; + $this->White = $White; return $this; } @@ -102,30 +105,50 @@ class Game */ public function getBlack(): ?Pairing { - return $this->black; + return $this->Black; } /** * Sets pairing for black player * - * @param Pairing | null $black + * @param Pairing | null $Black * @return Game */ - public function setBlack(?Pairing $black): Game + public function setBlack(?Pairing $Black): Game { - $this->black = $black; + $this->Black = $Black; return $this; } /** * Sets result for game * - * @param Gameresult | null $result + * @param Gameresult | null $Result * @return Game */ - public function setResult(?Gameresult $result): Game + public function setResult(?Gameresult $Result): Game { - $this->result = $result; + $this->Result = $Result; return $this; } + + /** + * Sets the board no of the game + * + * @return int + */ + public function getBoard(): int + { + return $this->Board; + } + + /** + * Returns the board no of the game + * + * @param int $Board + */ + public function setBoard(int $Board): void + { + $this->Board = $Board; + } } diff --git a/src/IOFactory.php b/src/IOFactory.php index c5f97c5..9b3e70e 100644 --- a/src/IOFactory.php +++ b/src/IOFactory.php @@ -33,7 +33,7 @@ abstract class IOFactory * * @var array */ - private static $readers = [ + private static $Readers = [ 'Swar-4' => Readers\Swar4::class, 'Pairtwo-6' => Readers\Pairtwo6::class, 'Pairtwo-5' => Readers\Pairtwo6::class // File structure identical @@ -51,12 +51,12 @@ abstract class IOFactory */ public static function createReader(string $type): ReaderInterface { - if (!isset(self::$readers[$type])) { + if (!isset(self::$Readers[$type])) { throw new LibpairtwoException("Cannot read type $type"); } // create reader class - $readerClass = self::$readers[$type]; + $readerClass = self::$Readers[$type]; $reader = new $readerClass; return $reader; diff --git a/src/Pairing.php b/src/Pairing.php index 21c9bd8..8b2d4a6 100644 --- a/src/Pairing.php +++ b/src/Pairing.php @@ -42,6 +42,9 @@ class Pairing /** @var int */ private $Round; + /** @var int */ + private $Board; + /** * Returns the player of the pairing * @@ -151,4 +154,24 @@ class Pairing $this->Round = $Round; return $this; } + + /** + * Sets the board no of the pairing + * + * @return int + */ + public function getBoard(): int + { + return $this->Board; + } + + /** + * Returns the board no of the pairing + * + * @param int $Board + */ + public function setBoard(int $Board): void + { + $this->Board = $Board; + } } diff --git a/src/Readers/Pairtwo6.php b/src/Readers/Pairtwo6.php index 0055b21..86f5aec 100644 --- a/src/Readers/Pairtwo6.php +++ b/src/Readers/Pairtwo6.php @@ -703,15 +703,15 @@ class Pairtwo6 implements ReaderInterface switch ($this->readData('Int', substr($swscontents, $offset, $length))) { case 255: case 253: - $color = Color::black; + $color = Color::Black; break; case 1: case 3: - $color = Color::white; + $color = Color::White; break; case 0: default: - $color = Color::none; + $color = Color::None; break; } $pairing->setColor(new Color($color)); @@ -720,38 +720,38 @@ class Pairtwo6 implements ReaderInterface $length = 1; switch ($this->readData('Int', substr($swscontents, $offset, $length))) { case 1: - $result = Result::lost; + $result = Result::Lost; break; case 2: - $result = Result::absent; + $result = Result::Absent; break; case 3: - $result = Result::adjourned; + $result = Result::Adjourned; break; case 4: - $result = Result::bye; + $result = Result::Bye; break; case 6: - $result = Result::draw; + $result = Result::Draw; break; case 8: - $result = Result::drawadjourned; + $result = Result::DrawAdjourned; break; case 11: - $result = Result::won; + $result = Result::Won; break; case 12: - $result = Result::wonforfait; + $result = Result::WonForfait; break; case 13: - $result = Result::wonadjourned; + $result = Result::WonAdjourned; break; case 14: - $result = Result::wonbye; + $result = Result::WonBye; break; case 0: default: - $result = Result::none; + $result = Result::None; break; } $pairing->setResult(new Result($result)); @@ -760,6 +760,7 @@ class Pairtwo6 implements ReaderInterface $pairing->setRound($x); $offset += 2; + $pairing->setBoard(-1); if ($x < $this->getBinaryData("CurrentRound")) { $this->getTournament()->addPairing($pairing); } diff --git a/src/Readers/Swar4.php b/src/Readers/Swar4.php index 8ad9e54..de91687 100644 --- a/src/Readers/Swar4.php +++ b/src/Readers/Swar4.php @@ -37,10 +37,10 @@ class Swar4 implements ReaderInterface private $tournament; /** @var bool|int|DateTime|string[] */ - private $binaryData; + private $BinaryData; /** @var string */ - private $release; + private $Release; /** @var array */ private const CompatibleVersions = ['v4.']; @@ -405,7 +405,7 @@ class Swar4 implements ReaderInterface for ($j = 0; $j < $player->getBinaryData('AllocatedRounds'); $j++) { $this->getTournament()->setBinaryData('Pairing_' . $pt . '_player', $i); $this->getTournament()->setBinaryData('Pairing_' . $pt . '_round', $this->readData('Int', $swshandle) - 1); - $this->getTournament()->setBinaryData('Pairing_' . $pt . '_table', $this->readData('Int', $swshandle)); + $this->getTournament()->setBinaryData('Pairing_' . $pt . '_table', $this->readData('Int', $swshandle) - 1); $this->getTournament()->setBinaryData('Pairing_' . $pt . '_opponent', $this->readData('Int', $swshandle)); $this->getTournament()->setBinaryData('Pairing_' . $pt . '_result', $this->readData('Hex', $swshandle)); $this->getTournament()->setBinaryData('Pairing_' . $pt . '_color', $this->readData('Int', $swshandle)); @@ -430,49 +430,51 @@ class Swar4 implements ReaderInterface } switch ($this->getTournament()->getBinaryData('Pairing_' . $ptn . '_result')) { case '1000': - $result = Result::lost; + $result = Result::Lost; break; case '01': - $result = Result::absent; + $result = Result::Absent; break; case '0010': - $result = Result::bye; + $result = Result::Bye; break; case '2000': - $result = Result::draw; + $result = Result::Draw; break; case '4000': - $result = Result::won; + $result = Result::Won; break; case '04': - $result = Result::wonforfait; + $result = Result::WonForfait; break; case '40': - $result = Result::wonbye; + $result = Result::WonBye; break; case '00': default: - $result = Result::none; + $result = Result::None; break; } if (array_search($this->getTournament()->getBinaryData('Pairing_' . $ptn . '_table'), [ 16384, 8192 ]) !== false) { - $result = Result::absent; + $result = Result::Absent; } $pairing->setResult(new Result($result)); switch ($this->getTournament()->getBinaryData('Pairing_' . $ptn . '_color')) { case 4294967295: - $color = Color::black; + $color = Color::Black; break; case 1: - $color = Color::white; + $color = Color::White; break; case 0: default: - $color = Color::none; + $color = Color::None; break; } $pairing->setColor(new Color($color)); + + $pairing->setBoard($this->getTournament()->getBinaryData('Pairing_' . $ptn . '_table')); $ptn++; $this->getTournament()->addPairing($pairing); } @@ -571,15 +573,15 @@ class Swar4 implements ReaderInterface */ public function getRelease(): string { - return $this->release; + return $this->Release; } /** - * @param string $release + * @param string $Release */ - public function setRelease(string $release): void + public function setRelease(string $Release): void { - $this->release = $release; + $this->Release = $Release; } /** diff --git a/src/Round.php b/src/Round.php index cc2aa99..ca73537 100644 --- a/src/Round.php +++ b/src/Round.php @@ -32,28 +32,28 @@ class Round * * @var DateTime */ - private $date; + private $Date; /** * Array of all games * * @var Game[] */ - private $games = []; + private $Games = []; /** * Number of the round * * @var int */ - private $roundNo; + private $RoundNo; /** * Array of all pairings for this round * * @var Pairing[] */ - private $pairings = []; + private $Pairings = []; /** * Adds a game to the round @@ -83,7 +83,6 @@ class Round return $this; } - /** * Returns an array of pairings where the player is bye * @@ -94,14 +93,13 @@ class Round $allPairings = $this->getPairings(); $byePairings = []; foreach ($allPairings as $pairing) { - if ($pairing->getResult() == Result::wonbye) { + if ($pairing->getResult() == Result::WonBye) { $byePairings[] = $pairing; } } return $byePairings; } - /** * Returns an array of pairings where the player is absent * @@ -112,13 +110,40 @@ class Round $allPairings = $this->getPairings(); $absentPairings = []; foreach ($allPairings as $pairing) { - if ($pairing->getResult() == Result::absent) { + if ($pairing->getResult() == Result::Absent) { $absentPairings[] = $pairing; } } return $absentPairings; } + /** + * Retuns an array with the games of this round sorted by board + * + * @return Game[] + */ + public function getGamesByBoard(): array + { + $allGames = $this->getGames(); + usort($allGames, array($this, 'sortByBoard')); + return $allGames; + } + + /** + * Sort by board + * + * @param Game $a + * @param Game $b + * @return int + */ + private function sortByBoard(Game $a, Game $b): int + { + if (($a->getBoard() == $b->getBoard()) || ($a->getBoard() === false) || ($b->getBoard() === false)) { + return 0; + } + return ($a->getBoard() > $b->getBoard()) ? +1 : -1; + } + /** * Returns the date of the round * @@ -126,19 +151,20 @@ class Round */ public function getDate(): DateTime { - return $this->date; + return $this->Date; } /** * Sets the date of the round * - * @param DateTime $date + * @param DateTime $Date * @return Round */ - public function setDate(DateTime $date): Round + public function setDate(DateTime $Date): Round { - $this->date = $date; + $this->Date = $Date; return $this; } + /** * Returns an array of all games for the round * @@ -146,19 +172,21 @@ class Round */ public function getGames(): array { - return $this->games; + return $this->Games; } + /** * Sets an array of all games for the round * - * @param Game[] $games + * @param Game[] $Games * @return Round */ - public function setGames(array $games): Round + public function setGames(array $Games): Round { - $this->games = $games; + $this->Games = $Games; return $this; } + /** * Returns the round number of the round * @@ -166,19 +194,21 @@ class Round */ public function getRoundNo(): int { - return $this->roundNo; + return $this->RoundNo; } + /** * Sets the round number of the round * - * @param int $roundNo + * @param int $RoundNo * @return Round */ - public function setRoundNo(int $roundNo): Round + public function setRoundNo(int $RoundNo): Round { - $this->roundNo = $roundNo; + $this->RoundNo = $RoundNo; return $this; } + /** * Returns an array of all pairings for the round * @@ -186,17 +216,18 @@ class Round */ public function getPairings(): array { - return $this->pairings; + return $this->Pairings; } + /** * Sets an array of all pairings for the round * - * @param Pairing[] $pairings + * @param Pairing[] $Pairings * @return Round */ - public function setPairings(array $pairings): Round + public function setPairings(array $Pairings): Round { - $this->pairings = $pairings; + $this->Pairings = $Pairings; return $this; } } diff --git a/src/Tournament.php b/src/Tournament.php index 0a3d8f5..4a5c3be 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -103,7 +103,7 @@ class Tournament private $PriorityId = 'Nation'; /** @var bool|DateTime|int|string[] */ - private $binaryData = []; + private $BinaryData = []; /** * Gets a player by its ID @@ -226,6 +226,10 @@ class Tournament /** @var Pairing[] */ $cache = array(); + /** @var int[] */ + $lastboards; + + /** @var Pairing $pairing */ foreach ($pairings as $pairing) { // Add pairing to player $pairing->getPlayer()->addPairing($pairing); @@ -234,6 +238,11 @@ class Tournament $this->getRoundByNo($round)->addPairing($pairing); $opponent = null; + + /** + * @var int $key + * @var Pairing $cached + */ foreach ($cache as $key=>$cached) { if (!is_null($cached)) { if (($cached->getOpponent() == $pairing->getPlayer()) && ($cached->getRound() == $pairing->getRound())) { @@ -244,10 +253,10 @@ class Tournament } } $game = new Game(); - if ($color->getValue() == Color::white) { + if ($color->getValue() == Color::White) { $game->setWhite($pairing); $game->setBlack($opponent); - } elseif ($color->getValue() == Color::black) { + } elseif ($color->getValue() == Color::Black) { $game->setWhite($opponent); $game->setBlack($pairing); } @@ -257,6 +266,18 @@ class Tournament } else { // Check if game already exists if (!$this->gameExists($game, $round)) { + $game->setBoard($game->getWhite()->getBoard()); + // Add board if inexistent + if ($game->getBoard() == -1) { + if (isset($lastboards[$round])) { + $lastboards[$round] += 1; + } else { + $lastboards[$round] = 0; + } + $game->setBoard($lastboards[$round]); + $game->getWhite()->setBoard($lastboards[$round]); + $game->getBlack()->setBoard($lastboards[$round]); + } $this->AddGame($game, $round); } } @@ -344,7 +365,7 @@ class Tournament $tosortplayers = $sortedplayers; $sortedplayers = []; foreach ($tosortplayers as $groupkey=>$sortedplayerselem) { - usort($tosortplayers[$groupkey], $this->SortTiebreak($tbkey)); + usort($tosortplayers[$groupkey], $this->sortTiebreak($tbkey)); foreach ($tosortplayers[$groupkey] as $playerkey => $player) { if (!is_null($player->getTiebreaks()[$tbkey])) { if ($playerkey != 0) {
WhiteBlackResult
WhiteBlackResult
' . ($game->getBoard() + 1) . '' . $game->getWhite()->getPlayer()->getName() . '' . $game->getBlack()->getPlayer()->getName() . '' . $game->getResult()->getValue() . '