diff --git a/res/trf16-testfile.trf b/res/trf16-testfile.trf index 1532708..8935496 100644 --- a/res/trf16-testfile.trf +++ b/res/trf16-testfile.trf @@ -9,8 +9,7 @@ 092 Standard - Round Robin - Individual 102 Jeroen De Meerleer 112 Jerry The Blackbird -122 120 min/10 moves + 30 min. avec incr. 30" starting from 40th move -132 23/01/14 23/01/14 23/01/14 23/01/14 23/01/14 23/01/14 23/01/14 +122 120 min/10 moves + 30 min. avec incr. 30" starting from 40th move 001 1 m Player 3 1850 BEL 0 1900/01/01 7.0 3 8 w 1 3 b 1 2 w 1 6 b 1 5 w 1 7 w 1 4 b 1 001 2 wWIM Player 1 1900 BEL 0 1900/01/01 5.5 2 5 w 1 6 w 1 1 b 0 7 w 1 4 b 1 8 w = 3 b 1 001 3 w Player 7 1650 BEL 0 1900/01/01 4.0 7 6 b 1 1 w 0 7 b 1 4 w 1 8 b 1 5 b 0 2 w 0 @@ -19,3 +18,4 @@ 001 6 m CM Player 2 1950 BEL 0 1900/01/01 2.0 1 3 w 0 2 b 0 5 w = 1 w 0 7 b 1 4 w 0 8 b = 001 7 w Player 4 1800 BEL 0 1900/01/01 1.5 4 4 w 0 8 b 1 3 w 0 2 b 0 6 w 0 1 b 0 5 w = 001 8 m Player 6 1700 BEL 0 1900/01/01 1.5 6 1 b 0 7 w 0 4 b 0 5 b = 3 w 0 2 b = 6 w = +132 23/01/14 23/01/14 23/01/14 23/01/14 23/01/14 23/01/14 23/01/14 \ No newline at end of file diff --git a/src/Exceptions/InvalidFileException.php b/src/Exceptions/InvalidFileException.php new file mode 100644 index 0000000..9725c0c --- /dev/null +++ b/src/Exceptions/InvalidFileException.php @@ -0,0 +1,8 @@ +Tournament = new Tournament(); $this->playerIndexes['0000'] = NULL; $this->playerIndexes[' '] = NULL; - foreach ($file as $line) { + $linecount = count($file); + for ($lineno = 0; $lineno < count($file); $lineno++) { + $line = $file[$lineno]; $datatype = substr($line, 0, 3); switch($datatype) { @@ -55,8 +60,17 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface case '112': $this->Tournament->addArbiter(substr($line, 4)); break; + case '132': + $this->readRoundData($line); + break; case '001': - $this->readPlayerData($line); + if(!empty($this->Tournament->Rounds)) { + $this->readPlayerData($line); + } elseif($lineno < $linecount) { + $file[] = $line; + } else { + throw new InvalidFileException(); + } break; } } @@ -119,65 +133,67 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface $player->setId('Fide', (int)trim(substr($line, 57, 10))); $player->DateOfBirth = \DateTime::createFromFormat("Y/m/d", substr($line, 69, 10)); $player->Points = (float)trim(substr($line, 80, 4)); - $player->Rank = (float)trim(substr($line, 85, 3)); + $player->Rank = (int)trim(substr($line, 85, 4)); - $pairingData = substr($line, 91); //read pairing round 1 - $pairing = new Pairing(); - $round = new Round(); - $round->RoundNo = 1; - $round->Date = \DateTime::createFromFormat("y/m/d", '23/01/14'); - $this->Tournament->addRound($round); - $pairing->Round = 1; - $pairing->Player = $player; - $pairing->OpponentIndex = substr($line, 91, 4); + $position = 91; + $length = strlen($line); + $roundnum = 1; + do { + $pairing = new Pairing(); + $pairing->Round = $roundnum; + $pairing->Player = $player; + $pairing->OpponentIndex = substr($line, $position, 4); - switch (trim(substr($line, 96, 1))) - { - case 'w': - $pairing->Color = Color::WHITE; - break; - case 'b': - $pairing->Color = Color::BLACK; - break; - case '-': - default: - $pairing->Color = Color::NONE; - break; - } + switch (trim(substr($line, $position + 6, 1))) + { + case 'w': + $pairing->Color = Color::WHITE; + break; + case 'b': + $pairing->Color = Color::BLACK; + break; + case '-': + default: + $pairing->Color = Color::NONE; + break; + } - switch (strtoupper(trim(substr($line, 98, 1)))) - { - case '-': - case 'Z': - $pairing->Result = Result::ABSENT; - break; - case '+': - $pairing->Result = Result::WON_FORFAIT; - break; - case 'W': - case '1': - $pairing->Result = Result::WON; - break; - case 'D': - case '=': - $pairing->Result = Result::DRAW; - break; - case 'L': - case '0': - $pairing->Result = Result::LOST; - break; - case 'H': - case 'F': - case 'U': - $pairing->Result = Result::WON_BYE; - break; - default: - $pairing->Color = Result::NONE; - break; - } - $this->Tournament->addPairing($pairing); + switch (strtoupper(trim(substr($line, $position + 8, 1)))) + { + case '-': + case 'Z': + $pairing->Result = Result::ABSENT; + break; + case '+': + $pairing->Result = Result::WON_FORFAIT; + break; + case 'W': + case '1': + $pairing->Result = Result::WON; + break; + case 'D': + case '=': + $pairing->Result = Result::DRAW; + break; + case 'L': + case '0': + $pairing->Result = Result::LOST; + break; + case 'H': + case 'F': + case 'U': + $pairing->Result = Result::WON_BYE; + break; + default: + $pairing->Color = Result::NONE; + break; + } + $position += 10; + $roundnum ++; + $this->Tournament->addPairing($pairing); + } while ($length > $position); $this->playerIndexes[$player->InitialRank] = $player; $this->Tournament->addPlayer($player); } @@ -188,4 +204,19 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface $pairing->Opponent = $this->playerIndexes[$pairing->OpponentIndex]; } } + + private function readRoundData(string $line) + { + $position = 91; + $length = strlen($line); + $roundnum = 1; + do { + $round = new Round(); + $round->Date = DateTime::createFromFormat('y/m/d', substr($line, $position, 8)); + $round->RoundNo = $roundnum; + $this->Tournament->addRound($round); + $position += 10; + $roundnum ++; + } while ($length > $position); + } } diff --git a/tests/Readers/Trf16Test.php b/tests/Readers/Trf16Test.php index cae367c..84463a8 100644 --- a/tests/Readers/Trf16Test.php +++ b/tests/Readers/Trf16Test.php @@ -102,8 +102,22 @@ class Trf16Test extends TestCase { $this->assertEquals(3, $this->testobject->Tournament->Players[0]->Rank); } + public function testReadTournamentRound1Date(): void + { + $expected = DateTime::createFromFormat("Y/m/d", "2023/01/14"); + $this->assertEquals($expected, $this->testobject->Tournament->roundByNo(1)->Date); + } + public function testReadTournamentRound3Date(): void + { + $expected = DateTime::createFromFormat("Y/m/d", "2023/01/14"); + $this->assertEquals($expected, $this->testobject->Tournament->roundByNo(3)->Date); + } public function testReadPlayerPairing1Opponent(): void { $this->assertEquals($this->testobject->Tournament->Players[7], $this->testobject->Tournament->Players[0]->Pairings[0]->Opponent); } + public function testReadPlayerPairing3Opponent(): void + { + $this->assertEquals($this->testobject->Tournament->Players[1], $this->testobject->Tournament->Players[0]->Pairings[2]->Opponent); + } }