Compare commits

...

4 Commits

Author SHA1 Message Date
99c27eb48a
WIP: TRF16Reader 2023-01-21 16:49:05 +01:00
f32de68a41
Updated dependencies 2023-01-21 16:46:23 +01:00
70efdc3713
Added test for arbitter 2023-01-21 16:46:14 +01:00
0d5aee6bef
UPDATED TEST FILES 2023-01-21 16:45:58 +01:00
8 changed files with 329 additions and 22 deletions

16
composer.lock generated
View File

@ -12,7 +12,7 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://git.jeroened.be/blackbirdchess/blackbirdchess-service-results.git", "url": "https://git.jeroened.be/blackbirdchess/blackbirdchess-service-results.git",
"reference": "a016fcb012ec4af48314d6fb966cdf6de9c31d7f" "reference": "466bb2bfec51ed3526985c96714daca71a9335db"
}, },
"require": { "require": {
"ext-iconv": "*", "ext-iconv": "*",
@ -42,7 +42,7 @@
"chess", "chess",
"pairing" "pairing"
], ],
"time": "2023-01-14T13:34:19+00:00" "time": "2023-01-21T14:55:36+00:00"
} }
], ],
"packages-dev": [ "packages-dev": [
@ -177,16 +177,16 @@
}, },
{ {
"name": "nikic/php-parser", "name": "nikic/php-parser",
"version": "v4.15.2", "version": "v4.15.3",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/nikic/PHP-Parser.git", "url": "https://github.com/nikic/PHP-Parser.git",
"reference": "f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc" "reference": "570e980a201d8ed0236b0a62ddf2c9cbb2034039"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc", "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/570e980a201d8ed0236b0a62ddf2c9cbb2034039",
"reference": "f59bbe44bf7d96f24f3e2b4ddc21cd52c1d2adbc", "reference": "570e980a201d8ed0236b0a62ddf2c9cbb2034039",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -227,9 +227,9 @@
], ],
"support": { "support": {
"issues": "https://github.com/nikic/PHP-Parser/issues", "issues": "https://github.com/nikic/PHP-Parser/issues",
"source": "https://github.com/nikic/PHP-Parser/tree/v4.15.2" "source": "https://github.com/nikic/PHP-Parser/tree/v4.15.3"
}, },
"time": "2022-11-12T15:38:23+00:00" "time": "2023-01-16T22:05:37+00:00"
}, },
{ {
"name": "phar-io/manifest", "name": "phar-io/manifest",

Binary file not shown.

Binary file not shown.

View File

@ -1,4 +1,4 @@
012 Blackbird Chess Unit Test Tournament Rounds 1 - 7 012 Blackbird Chess Unit Test Tournament
022 Waregem 022 Waregem
032 BEL 032 BEL
042 2023/01/14 042 2023/01/14
@ -7,14 +7,15 @@
072 0 072 0
082 0 082 0
092 Standard - Round Robin - Individual 092 Standard - Round Robin - Individual
102 0 102 Jeroen De Meerleer
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 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 0 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 mWIM Player 1 0 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 m Player 7 0 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
001 4 m Player 5 0 BEL 0 1900/01/01 3.5 5 7 b 1 5 b = 8 w 1 3 b 0 2 w 0 6 b 1 1 w 0 001 4 m Player 5 1750 BEL 0 1900/01/01 3.5 5 7 b 1 5 b = 8 w 1 3 b 0 2 w 0 6 b 1 1 w 0
001 5 m Player 8 0 BEL 0 1900/01/01 3.0 8 2 b 0 4 w = 6 b = 8 w = 1 b 0 3 w 1 7 b = 001 5 w Player 8 1600 BEL 0 1900/01/01 3.0 8 2 b 0 4 w = 6 b = 8 w = 1 b 0 3 w 1 7 b =
001 6 m CM Player 2 0 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 m Player 4 0 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 0 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 =

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];
}
}
}

View File

@ -68,4 +68,9 @@ class Pairtwo6Test extends TestCase
$player = Player::PlayersByName('Player 3', $this->testobject->Tournament)[0]; $player = Player::PlayersByName('Player 3', $this->testobject->Tournament)[0];
$this->assertNull($player->Title); $this->assertNull($player->Title);
} }
public function testReadTournamentArbiter(): void
{
$this->assertEquals("Jeroen De Meerleer", $this->testobject->Tournament->Arbiters[0]);
}
} }

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);
}
}