Reader TRN16 finished

This commit is contained in:
Jeroen De Meerleer 2023-01-23 19:51:41 +01:00
parent 99c27eb48a
commit 2003701710
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 112 additions and 59 deletions

View File

@ -9,8 +9,7 @@
092 Standard - Round Robin - Individual 092 Standard - Round Robin - Individual
102 Jeroen De Meerleer 102 Jeroen De Meerleer
112 Jerry The Blackbird 112 Jerry The Blackbird
122 120 min/10 moves + 30 min. avec incr. 30" starting from 40th move 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
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 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 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 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 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 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 = 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

View File

@ -0,0 +1,8 @@
<?php
namespace Blackbirdchess\Service\Import\Exceptions;
class InvalidFileException extends \Blackbirdchess\Service\Results\Exceptions\LibpairtwoException
{
}

View File

@ -2,6 +2,8 @@
namespace Blackbirdchess\Service\Import\Readers; namespace Blackbirdchess\Service\Import\Readers;
use Blackbirdchess\Service\Import\Exceptions\InvalidFileException;
use Blackbirdchess\Service\Import\Interfaces\ReaderInterface;
use Blackbirdchess\Service\Results\Enums\Color; use Blackbirdchess\Service\Results\Enums\Color;
use Blackbirdchess\Service\Results\Enums\Gender; use Blackbirdchess\Service\Results\Enums\Gender;
use Blackbirdchess\Service\Results\Enums\Result; use Blackbirdchess\Service\Results\Enums\Result;
@ -10,8 +12,9 @@ use Blackbirdchess\Service\Results\Pairing;
use Blackbirdchess\Service\Results\Player; use Blackbirdchess\Service\Results\Player;
use Blackbirdchess\Service\Results\Round; use Blackbirdchess\Service\Results\Round;
use Blackbirdchess\Service\Results\Tournament; use Blackbirdchess\Service\Results\Tournament;
use DateTime;
class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface class Trf16 implements ReaderInterface
{ {
/** /**
@ -32,7 +35,9 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface
$this->Tournament = new Tournament(); $this->Tournament = new Tournament();
$this->playerIndexes['0000'] = NULL; $this->playerIndexes['0000'] = NULL;
$this->playerIndexes[' '] = 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); $datatype = substr($line, 0, 3);
switch($datatype) { switch($datatype) {
@ -55,8 +60,17 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface
case '112': case '112':
$this->Tournament->addArbiter(substr($line, 4)); $this->Tournament->addArbiter(substr($line, 4));
break; break;
case '132':
$this->readRoundData($line);
break;
case '001': case '001':
$this->readPlayerData($line); if(!empty($this->Tournament->Rounds)) {
$this->readPlayerData($line);
} elseif($lineno < $linecount) {
$file[] = $line;
} else {
throw new InvalidFileException();
}
break; break;
} }
} }
@ -119,65 +133,67 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface
$player->setId('Fide', (int)trim(substr($line, 57, 10))); $player->setId('Fide', (int)trim(substr($line, 57, 10)));
$player->DateOfBirth = \DateTime::createFromFormat("Y/m/d", substr($line, 69, 10)); $player->DateOfBirth = \DateTime::createFromFormat("Y/m/d", substr($line, 69, 10));
$player->Points = (float)trim(substr($line, 80, 4)); $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 //read pairing round 1
$pairing = new Pairing(); $position = 91;
$round = new Round(); $length = strlen($line);
$round->RoundNo = 1; $roundnum = 1;
$round->Date = \DateTime::createFromFormat("y/m/d", '23/01/14'); do {
$this->Tournament->addRound($round); $pairing = new Pairing();
$pairing->Round = 1; $pairing->Round = $roundnum;
$pairing->Player = $player; $pairing->Player = $player;
$pairing->OpponentIndex = substr($line, 91, 4); $pairing->OpponentIndex = substr($line, $position, 4);
switch (trim(substr($line, 96, 1))) switch (trim(substr($line, $position + 6, 1)))
{ {
case 'w': case 'w':
$pairing->Color = Color::WHITE; $pairing->Color = Color::WHITE;
break; break;
case 'b': case 'b':
$pairing->Color = Color::BLACK; $pairing->Color = Color::BLACK;
break; break;
case '-': case '-':
default: default:
$pairing->Color = Color::NONE; $pairing->Color = Color::NONE;
break; break;
} }
switch (strtoupper(trim(substr($line, 98, 1)))) switch (strtoupper(trim(substr($line, $position + 8, 1))))
{ {
case '-': case '-':
case 'Z': case 'Z':
$pairing->Result = Result::ABSENT; $pairing->Result = Result::ABSENT;
break; break;
case '+': case '+':
$pairing->Result = Result::WON_FORFAIT; $pairing->Result = Result::WON_FORFAIT;
break; break;
case 'W': case 'W':
case '1': case '1':
$pairing->Result = Result::WON; $pairing->Result = Result::WON;
break; break;
case 'D': case 'D':
case '=': case '=':
$pairing->Result = Result::DRAW; $pairing->Result = Result::DRAW;
break; break;
case 'L': case 'L':
case '0': case '0':
$pairing->Result = Result::LOST; $pairing->Result = Result::LOST;
break; break;
case 'H': case 'H':
case 'F': case 'F':
case 'U': case 'U':
$pairing->Result = Result::WON_BYE; $pairing->Result = Result::WON_BYE;
break; break;
default: default:
$pairing->Color = Result::NONE; $pairing->Color = Result::NONE;
break; break;
} }
$this->Tournament->addPairing($pairing); $position += 10;
$roundnum ++;
$this->Tournament->addPairing($pairing);
} while ($length > $position);
$this->playerIndexes[$player->InitialRank] = $player; $this->playerIndexes[$player->InitialRank] = $player;
$this->Tournament->addPlayer($player); $this->Tournament->addPlayer($player);
} }
@ -188,4 +204,19 @@ class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface
$pairing->Opponent = $this->playerIndexes[$pairing->OpponentIndex]; $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);
}
} }

View File

@ -102,8 +102,22 @@ class Trf16Test extends TestCase
{ {
$this->assertEquals(3, $this->testobject->Tournament->Players[0]->Rank); $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 public function testReadPlayerPairing1Opponent(): void
{ {
$this->assertEquals($this->testobject->Tournament->Players[7], $this->testobject->Tournament->Players[0]->Pairings[0]->Opponent); $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);
}
} }