WIP: TRF16Reader

This commit is contained in:
Jeroen De Meerleer 2023-01-21 16:46:41 +01:00
parent f32de68a41
commit 99c27eb48a
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 305 additions and 4 deletions

View File

@ -34,10 +34,11 @@ abstract class IOFactory
* @var array * @var array
*/ */
private static $Readers = [ private static $Readers = [
'Swar-4' => Blackbirdchess\Service\Import\Readers\Swar4::class, 'Swar-4' => Readers\Swar4::class,
'Swar-5' => Blackbirdchess\Service\Import\Readers\Swar5::class, 'Swar-5' => Readers\Swar5::class,
'Pairtwo-6' => Blackbirdchess\Service\Import\Readers\Pairtwo6::class, 'Pairtwo-6' => Readers\Pairtwo6::class,
'Pairtwo-5' => Blackbirdchess\Service\Import\Readers\Pairtwo6::class // File structure identical 'Pairtwo-5' => Readers\Pairtwo6::class, // File structure identical
'TRF-16' => Readers\Trf16::class,
]; ];
/** /**

191
src/Readers/Trf16.php Normal file
View File

@ -0,0 +1,191 @@
<?php
namespace Blackbirdchess\Service\Import\Readers;
use Blackbirdchess\Service\Results\Enums\Color;
use Blackbirdchess\Service\Results\Enums\Gender;
use Blackbirdchess\Service\Results\Enums\Result;
use Blackbirdchess\Service\Results\Enums\Title;
use Blackbirdchess\Service\Results\Pairing;
use Blackbirdchess\Service\Results\Player;
use Blackbirdchess\Service\Results\Round;
use Blackbirdchess\Service\Results\Tournament;
class Trf16 implements \Blackbirdchess\Service\Import\Interfaces\ReaderInterface
{
/**
* The tournament
*
* @var Tournament
*/
public $Tournament;
private $playerIndexes;
/**
* @inheritDoc
*/
public function read(string $filename): void
{
$file = explode("\n", str_replace("\r", "", file_get_contents($filename)));
$this->Tournament = new Tournament();
$this->playerIndexes['0000'] = NULL;
$this->playerIndexes[' '] = NULL;
foreach ($file as $line) {
$datatype = substr($line, 0, 3);
switch($datatype) {
case '012':
$this->Tournament->Name = substr($line, 4);
break;
case '022':
$this->Tournament->OrganiserPlace = substr($line, 4);
break;
case '032':
$this->Tournament->OrganiserCountry = substr($line, 4);
break;
case '042':
$this->Tournament->StartDate = \DateTime::createFromFormat("Y/m/d", substr($line, 4));
break;
case '052':
$this->Tournament->EndDate = \DateTime::createFromFormat("Y/m/d", substr($line, 4));
break;
case '102':
case '112':
$this->Tournament->addArbiter(substr($line, 4));
break;
case '001':
$this->readPlayerData($line);
break;
}
}
$this->setOpponentIndexToPlayerObj();
$this->Tournament->pairingsToRounds();
}
private function readPlayerData(string $line): void
{
$player = new Player();
$player->InitialRank = substr($line, 4, 4);
switch (substr($line, 9, 1))
{
case 'm':
$player->Gender = Gender::MALE;
break;
case 'w':
$player->Gender = Gender::FEMALE;
break;
default:
$player->Gender = NULL;
break;
}
switch (trim(substr($line, 10, 3)))
{
case 'GM':
$player->Title = Title::GM;
break;
case 'IM':
$player->Title = Title::IM;
break;
case 'WGM':
$player->Title = Title::WGM;
break;
case 'FM':
$player->Title = Title::FM;
break;
case 'WIM':
$player->Title = Title::WIM;
break;
case 'CM':
$player->Title = Title::CM;
break;
case 'WFM':
$player->Title = Title::WFM;
break;
case 'WCM':
$player->Title = Title::WCM;
break;
default:
$player->Title = NULL;
break;
}
$player->Name = trim(substr($line, 14, 32));
$player->setElo('Fide', (int)trim(substr($line, 48, 4)));
$player->Nation = trim(substr($line, 53, 3));
$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));
$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);
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 (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);
$this->playerIndexes[$player->InitialRank] = $player;
$this->Tournament->addPlayer($player);
}
private function setOpponentIndexToPlayerObj()
{
foreach($this->Tournament->Pairings as &$pairing) {
$pairing->Opponent = $this->playerIndexes[$pairing->OpponentIndex];
}
}
}

109
tests/Readers/Trf16Test.php Normal file
View File

@ -0,0 +1,109 @@
<?php
namespace Blackbirdchess\Tests\Service\Import\Readers;
use Blackbirdchess\Service\Import\Readers\Trf16;
use Blackbirdchess\Service\Results\Enums\Gender;
use Blackbirdchess\Service\Results\Enums\Title;
use DateTime;
use PHPUnit\Framework\TestCase;
class Trf16Test extends TestCase
{
private string $testfile = '/res/trf16-testfile.trf';
private Trf16 $testobject;
public function setUp(): void
{
$root = NULL;
$directory = dirname(__FILE__);
do {
$directory = dirname($directory);
$composer = $directory . '/composer.json';
if(file_exists($composer)) $root = $directory;
} while(is_null($root) && $directory != '/');
$this->testfile = $root . $this->testfile;
$this->testobject = new Trf16();
$this->testobject->read($this->testfile);
}
public function testReadTournamentName(): void
{
$this->assertEquals('Blackbird Chess Unit Test Tournament', $this->testobject->Tournament->Name);
}
public function testReadTournamentLocation(): void
{
$this->assertEquals('Waregem', $this->testobject->Tournament->OrganiserPlace);
}
public function testReadTournamentFederation(): void
{
$this->assertEquals('BEL', $this->testobject->Tournament->OrganiserCountry);
}
public function testReadTournamentStartDate(): void
{
$expected = DateTime::createFromFormat("Y/m/d", "2023/01/14");
$this->assertEquals($expected, $this->testobject->Tournament->StartDate);
}
public function testReadTournamentEndDate(): void
{
$expected = DateTime::createFromFormat("Y/m/d", "2023/01/14");
$this->assertEquals($expected, $this->testobject->Tournament->EndDate);
}
public function testReadTournamentArbiter(): void
{
$this->assertEquals("Jeroen De Meerleer", $this->testobject->Tournament->Arbiters[0]);
}
public function testReadTournamentDeputyArbiter(): void
{
$this->assertEquals("Jerry The Blackbird", $this->testobject->Tournament->Arbiters[1]);
}
public function testReadPlayerInitialRank(): void
{
$this->assertEquals(1, $this->testobject->Tournament->Players[0]->InitialRank);
}
public function testReadPlayerSex(): void
{
$this->assertEquals(Gender::MALE, $this->testobject->Tournament->Players[0]->Gender);
}
public function testReadPlayerTitle(): void
{
$this->assertEquals(Title::WIM, $this->testobject->Tournament->Players[1]->Title);
}
public function testReadPlayerName(): void
{
$this->assertEquals('Player 3', $this->testobject->Tournament->Players[0]->Name);
}
public function testReadPlayerRating(): void
{
$this->assertEquals(1850, $this->testobject->Tournament->Players[0]->getElo('Fide'));
}
public function testReadPlayerFederation(): void
{
$this->assertEquals('BEL', $this->testobject->Tournament->Players[0]->Nation);
}
public function testReadPlayerID(): void
{
$this->assertEquals('0', $this->testobject->Tournament->Players[0]->GetId('Fide'));
}
public function testReadPlayerBirthday(): void
{
$expected = DateTime::createFromFormat("Y/m/d", "1900/01/01");
$this->assertEquals($expected, $this->testobject->Tournament->Players[0]->DateOfBirth);
}
public function testReadPlayerPoints(): void
{
$this->assertEquals(7, $this->testobject->Tournament->Players[0]->Points);
}
public function testReadPlayerRank(): void
{
$this->assertEquals(3, $this->testobject->Tournament->Players[0]->Rank);
}
public function testReadPlayerPairing1Opponent(): void
{
$this->assertEquals($this->testobject->Tournament->Players[7], $this->testobject->Tournament->Players[0]->Pairings[0]->Opponent);
}
}