libpairtwo/src/Readers/Swar4.php

669 lines
24 KiB
PHP
Raw Normal View History

2019-07-15 13:47:24 +02:00
<?php
2020-08-02 21:51:59 +02:00
2019-07-15 13:47:24 +02:00
/**
2019-11-17 01:20:15 +01:00
* Reader Swar-4
2019-07-15 13:47:24 +02:00
*
* Reads out Swar-4 files
*
2020-08-02 21:51:59 +02:00
* @author Jeroen De Meerleer <schaak@jeroened.be>
* @category Main
* @package Libpairtwo
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
2019-07-15 13:47:24 +02:00
*/
2020-08-02 21:51:59 +02:00
2019-07-15 13:47:24 +02:00
namespace JeroenED\Libpairtwo\Readers;
2020-08-02 21:51:59 +02:00
use InvalidArgumentException;
2019-09-25 16:06:13 +02:00
use JeroenED\Libpairtwo\Enums\Color;
2019-09-28 10:33:59 +02:00
use JeroenED\Libpairtwo\Enums\Gender;
use JeroenED\Libpairtwo\Enums\Result;
2019-09-25 19:35:19 +02:00
use JeroenED\Libpairtwo\Enums\Tiebreak;
2019-09-28 10:33:59 +02:00
use JeroenED\Libpairtwo\Enums\Title;
2019-08-03 13:51:21 +02:00
use JeroenED\Libpairtwo\Enums\TournamentSystem;
2019-08-03 13:51:52 +02:00
use JeroenED\Libpairtwo\Exceptions\IncompatibleReaderException;
2019-07-15 13:47:24 +02:00
use JeroenED\Libpairtwo\Interfaces\ReaderInterface;
2019-09-25 12:18:02 +02:00
use JeroenED\Libpairtwo\Pairing;
use JeroenED\Libpairtwo\Player;
2019-09-25 16:06:13 +02:00
use JeroenED\Libpairtwo\Round;
2019-07-15 13:47:24 +02:00
use JeroenED\Libpairtwo\Tournament;
2019-09-28 10:33:59 +02:00
use DateTime;
2019-07-15 13:47:24 +02:00
2019-07-15 14:43:23 +02:00
/**
2019-11-17 01:20:15 +01:00
* Reader Swar4
*
* Reads out Swar-4 files
*
2020-08-02 21:51:59 +02:00
* @author Jeroen De Meerleer <schaak@jeroened.be>
* @category Main
* @package Libpairtwo
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
2019-07-15 14:43:23 +02:00
*/
2019-07-15 13:47:24 +02:00
class Swar4 implements ReaderInterface
{
2019-11-17 01:20:15 +01:00
/**
* Version of Pairtwo this file was created with
*
* @var string
*/
public $Release;
2019-07-15 13:47:24 +02:00
2019-11-17 01:20:15 +01:00
/**
* The tournament
*
* @var Tournament
*/
public $Tournament;
/**
* Binary data that was read out of the pairing file
*
* @var bool|DateTime|int|string[]
*/
private $BinaryData;
2019-07-15 14:43:23 +02:00
2020-08-02 21:51:59 +02:00
/**
* @var array
*/
public const COMPATIBLE_VERSIONS = ['v4.'];
2019-08-03 13:51:52 +02:00
2020-08-02 21:51:59 +02:00
public const TEMPOS = [
2019-09-25 16:06:13 +02:00
[
'105 min/40 moves + 15 min. QPF',
'120 min/40 moves + 15 min. with incr. 30" starting from 40th move',
'120 min/40 moves + 30 min. QPF',
'120 min/10 moves + 30 min. avec incr. 30" starting from 40th move',
'120 min QPF',
'150 min QPF',
'60 min QPF',
'60 min with incr. 30"',
'65 min QPF',
'75 min with incr. 30"',
'90 min/40 moves + 15 min with incr. 30" starting from 1st move',
'90 min/40 moves + 30 min with incr. 30" starting from 1st move',
'90 min with incr. 30"',
'50 min with incr. 10"',
'other'
2020-08-02 21:51:59 +02:00
],
[
2019-09-25 16:06:13 +02:00
'10 min. with incr. 10"',
'10 min. with incr. 15"',
'10 min. with incr.5"',
'11 min. QPF',
'12 min. QPF',
'13 min. with incr.3"',
'13 min. with incr.5"',
'15 min. QPF',
'15 min. with incr. 10"',
'15 min. with incr. 15"',
'15 min. with incr.5"',
'20 min. QPF',
'20 min. with incr. 10"',
'20 min. with incr. 15"',
'20 min. with incr.5"',
'25 min. QPF',
'25 min. with incr. 10"',
'25 min. with incr. 15"',
'25 min. with incr.5"',
'30 min. QPF',
'45 min. QPF',
'8 min. with incr.4"',
'other'
2020-08-02 21:51:59 +02:00
],
[
2019-09-25 16:06:13 +02:00
'3 min. with incr. 2"',
'3 min. with incr. 3"',
'4 min. with incr. 2"',
'4 min. with incr. 3"',
'5 min. QPF',
'5 min. with incr. 2"',
'5 min. with incr. 3"',
'6 min. with incr. 2"',
'6 min. with incr. 3"',
'7 min. with incr. 2"',
'7 min. with incr. 3"',
'8 min. with incr. 2"',
'10 min. QPF',
'other'
]
];
2019-08-03 13:51:52 +02:00
2019-07-15 14:43:23 +02:00
/**
2019-11-17 01:20:15 +01:00
* Actually reads the Swar-File
*
2019-07-15 14:43:23 +02:00
* @param string $filename
2020-08-02 21:51:59 +02:00
*
2019-08-03 13:51:52 +02:00
* @throws IncompatibleReaderException
2019-07-15 14:43:23 +02:00
*/
2019-11-16 14:24:07 +01:00
public function read(string $filename): void
2019-07-15 13:47:24 +02:00
{
2019-07-15 14:43:23 +02:00
$swshandle = fopen($filename, 'rb');
$this->Release = $this->readData('String', $swshandle);
2020-08-02 21:51:59 +02:00
if (array_search(substr($this->Release, 0, 3), self::COMPATIBLE_VERSIONS) === false) {
2019-08-03 13:51:52 +02:00
throw new IncompatibleReaderException("This file was not created with Swar 4");
}
$this->Tournament = new Tournament();
2019-07-15 14:43:23 +02:00
$this->Guid = $this->readData('String', $swshandle);
$this->MacAddress = $this->readData('String', $swshandle);
2019-07-16 16:07:20 +02:00
// [Tournoi]
$this->readData('String', $swshandle);
2019-07-16 16:07:20 +02:00
$this->Tournament->Name = $this->readData('String', $swshandle);
$this->Tournament->Organiser = $this->readData('String', $swshandle);
$this->Tournament->OrganiserClub = $this->readData('String', $swshandle);
$this->Tournament->OrganiserPlace = $this->readData('String', $swshandle);
$this->Tournament->addArbiter($this->readData('String', $swshandle));
$this->Tournament->addArbiter($this->readData('String', $swshandle));
$this->Tournament->StartDate = $this->readData('Date', $swshandle);
$this->Tournament->EndDate = $this->readData('Date', $swshandle);
2019-07-16 16:07:20 +02:00
// Tempo string is not variable and dependant on kind of tournament
$this->Tournament->TempoIndex = $this->readData('Int', $swshandle);
2019-07-15 14:43:23 +02:00
$this->Tournament->NoOfRounds = $this->readData('Int', $swshandle);
2019-08-03 13:51:21 +02:00
$this->Tournament->FRBEfrom = $this->readData('Int', $swshandle);
$this->Tournament->FRBEto = $this->readData('Int', $swshandle);
$this->Tournament->FIDEfrom = $this->readData('Int', $swshandle);
$this->Tournament->FIDEto = $this->readData('Int', $swshandle);
$this->Tournament->CatSepares = $this->readData('Int', $swshandle);
$this->Tournament->AfficherEloOuPays = $this->readData('Int', $swshandle);
2019-08-03 13:51:21 +02:00
$this->Tournament->FideHomol = $this->readData('Int', $swshandle);
2019-08-03 13:51:21 +02:00
$this->Tournament->FideId = $this->readData('String', $swshandle);
$this->Tournament->FideArbitre1 = $this->readData('String', $swshandle);
$this->Tournament->FideArbitre2 = $this->readData('String', $swshandle);
$this->Tournament->FideEmail = $this->readData('String', $swshandle);
$this->Tournament->FideRemarques = $this->readData('String', $swshandle);
2019-08-03 13:51:21 +02:00
2019-09-25 16:30:19 +02:00
switch ($this->readData('Int', $swshandle)) {
case 0:
case 1:
case 2:
case 3:
case 4:
default:
2020-08-02 21:51:59 +02:00
$system = TournamentSystem::SWISS;
2019-09-25 16:30:19 +02:00
break;
case 5:
case 6:
case 7:
2020-08-02 21:51:59 +02:00
$system = TournamentSystem::CLOSED;
2019-09-25 16:30:19 +02:00
break;
case 8:
case 9:
2020-08-02 21:51:59 +02:00
$system = TournamentSystem::AMERICAN;
2019-09-25 16:30:19 +02:00
break;
}
$this->Tournament->System = new TournamentSystem($system);
$this->Tournament->Dummy1 = $this->readData('Int', $swshandle);
$this->Tournament->Dummy2 = $this->readData('Int', $swshandle);
$this->Tournament->SW_AmerPresence = $this->readData('Int', $swshandle);
$this->Tournament->Plusieurs = $this->readData('Int', $swshandle);
$this->Tournament->FirstTable = $this->readData('Int', $swshandle);
$this->Tournament->SW321_Win = $this->readData('Int', $swshandle);
$this->Tournament->SW321_Nul = $this->readData('Int', $swshandle);
$this->Tournament->SW321_Los = $this->readData('Int', $swshandle);
$this->Tournament->SW321_Bye = $this->readData('Int', $swshandle);
$this->Tournament->SW321_Pre = $this->readData('Int', $swshandle);
$this->Tournament->EloUsed = $this->readData('Int', $swshandle);
$this->Tournament->TournoiStd = $this->readData('Int', $swshandle);
$this->Tournament->TbPersonel = $this->readData('Int', $swshandle);
$this->Tournament->ApparOrder = $this->readData('Int', $swshandle);
$this->Tournament->EloEqual = $this->readData('Int', $swshandle);
$this->Tournament->ByeValue = $this->readData('Int', $swshandle);
$this->Tournament->AbsValue = $this->readData('Int', $swshandle);
$this->Tournament->FF_Value = $this->readData('Int', $swshandle);
2019-09-25 16:30:19 +02:00
switch ($this->readData('Int', $swshandle)) {
case 0:
default:
$federation = '';
break;
case 1:
$federation = 'FRBE';
break;
case 2:
$federation = 'KBSB';
break;
case 3:
$federation = 'FEFB';
break;
case 4:
$federation = 'VSF';
break;
case 5:
$federation = 'SVDB';
break;
case 6:
$federation = 'FIDE';
break;
}
$this->Tournament->Federation = $federation;
$this->Tournament->NonRatedElo = 0;
$this->Tournament->OrganiserClubNo = 0;
// [DATES]
$this->readData('String', $swshandle);
2019-08-03 13:51:21 +02:00
2020-08-02 21:51:59 +02:00
$this->Tournament->Tempo = self::TEMPOS[ $this->Tournament->TournoiStd ][ $this->Tournament->TempoIndex ];
2019-09-25 16:06:13 +02:00
for ($i = 0; $i < $this->Tournament->NoOfRounds; $i++) {
2019-09-25 16:06:13 +02:00
$round = new Round();
$round->RoundNo = $i;
$round->Date = $this->readData('Date', $swshandle);
$this->Tournament->addRound($round);
2019-08-03 13:51:21 +02:00
}
2019-08-28 20:49:01 +02:00
// [TIE_BREAK]
$this->readData('String', $swshandle);
2019-08-28 20:49:01 +02:00
2019-09-25 19:35:19 +02:00
$tiebreaks = [];
2019-08-28 20:49:01 +02:00
for ($i = 0; $i < 5; $i++) {
2019-09-25 19:50:04 +02:00
switch ($this->readData('Int', $swshandle)) {
2019-09-25 19:35:19 +02:00
case 0:
default:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::NONE;
2019-09-25 19:35:19 +02:00
break;
case 1:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BUCHHOLZ;
2019-09-25 19:35:19 +02:00
break;
case 2:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BUCHHOLZ_MED;
2019-09-25 19:35:19 +02:00
break;
case 3:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BUCHHOLZ_MED_2;
2019-09-25 19:35:19 +02:00
break;
case 4:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BUCHHOLZ_CUT;
2019-09-25 19:35:19 +02:00
break;
case 5:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BUCHHOLZ_CUT_2;
2019-09-25 19:35:19 +02:00
break;
case 6:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::SONNEBORN;
2019-09-25 19:35:19 +02:00
break;
case 7:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::CUMULATIVE;
2019-09-25 19:35:19 +02:00
break;
case 8:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BETWEEN;
2019-09-25 19:35:19 +02:00
break;
case 9:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::KOYA;
2019-09-25 19:35:19 +02:00
break;
case 10:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BAUMBACH;
2019-09-25 19:35:19 +02:00
break;
case 11:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::AVERAGE_PERFORMANCE;
2019-09-25 19:35:19 +02:00
break;
case 12:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::ARO;
2019-09-25 19:35:19 +02:00
break;
case 13:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::AROCUT;
2019-09-25 19:35:19 +02:00
break;
case 14:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BLACK_PLAYED;
2019-09-25 19:35:19 +02:00
break;
case 15:
2020-08-02 21:51:59 +02:00
$tiebreak = Tiebreak::BLACK_WIN;
2019-09-25 19:35:19 +02:00
break;
}
$tiebreaks[] = new Tiebreak($tiebreak);
2019-08-28 20:49:01 +02:00
}
$this->Tournament->Tiebreaks = $tiebreaks;
2019-08-28 20:49:01 +02:00
// [EXCLUSION]
$this->readData('String', $swshandle);
$this->Tournament->ExclusionType = $this->readData('Int', $swshandle);
$this->Tournament->ExclusionValue = $this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
// [CATEGORIES]
$this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
$this->Tournament->Catogory_type = $this->readData('Int', $swshandle);
2019-09-25 12:18:02 +02:00
for ($i = 0; $i <= 12; $i++) {
2020-08-02 21:51:59 +02:00
$category[ $i ][ 'Cat1' ] = $this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
}
for ($i = 0; $i <= 12; $i++) {
2020-08-02 21:51:59 +02:00
$category[ $i ][ 'Cat2' ] = $this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
}
2019-11-16 15:30:26 +01:00
$this->Tournament->Category = $category;
// [XTRA_POINTS]
$this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
for ($i = 0; $i < 4; $i++) {
2020-08-02 21:51:59 +02:00
$extrapoints[ $i ][ 'pts' ] = $this->readData('Int', $swshandle);
$extrapoints[ $i ][ 'elo' ] = $this->readData('Int', $swshandle);
2019-09-25 12:18:02 +02:00
}
2019-11-16 15:30:26 +01:00
$this->Tournament->Extrapoints = $extrapoints;
2019-09-25 12:18:02 +02:00
// [JOUEURS]
$this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
$roundNo = 0;
$playerNo = 0;
$this->Tournament->NumberOfPlayers = $this->readData('Int', $swshandle);
2019-09-25 12:18:02 +02:00
$pt = 0;
for ($i = 0; $i < $this->Tournament->NumberOfPlayers; $i++) {
2019-09-25 12:18:02 +02:00
$player = new Player();
$player->Classement = $this->readData('Int', $swshandle);
$player->Name = $this->readData('String', $swshandle);
2020-08-02 21:51:59 +02:00
$inscriptionNos[ $this->readData('Int', $swshandle) ] = $i;
$player->Rank = $this->readData('Int', $swshandle);
$player->CatIndex = $this->readData('Int', $swshandle);
$player->DateOfBirth = $this->readData('Date', $swshandle);
2019-09-25 12:18:02 +02:00
switch ($this->readData('Int', $swshandle)) {
case 1:
2020-08-02 21:51:59 +02:00
$gender = Gender::MALE;
2019-09-25 12:18:02 +02:00
break;
case 2:
2020-08-02 21:51:59 +02:00
$gender = Gender::FEMALE;
2019-09-25 12:18:02 +02:00
break;
default:
2020-08-02 21:51:59 +02:00
$gender = Gender::NEUTRAL;
2019-09-25 12:18:02 +02:00
break;
}
$player->Gender = new Gender($gender);
2019-09-25 14:33:52 +02:00
$player->Nation = $this->readData('String', $swshandle);
2019-09-25 12:18:02 +02:00
$player->setId('Nation', $this->readData('Int', $swshandle));
$player->setId('Fide', $this->readData('Int', $swshandle));
$player->Affliation = $this->readData('Int', $swshandle);
2019-09-25 12:18:02 +02:00
$player->setElo('Nation', $this->readData('Int', $swshandle));
$player->setElo('Fide', $this->readData('Int', $swshandle));
switch ($this->readData('Int', $swshandle)) {
case 1:
$title = Title::WCM;
break;
2019-09-25 14:33:52 +02:00
case 2:
2019-09-25 12:18:02 +02:00
$title = Title::WFM;
break;
2019-09-25 14:33:52 +02:00
case 3:
2019-09-25 12:18:02 +02:00
$title = Title::CM;
break;
2019-09-25 14:33:52 +02:00
case 4:
2019-09-25 12:18:02 +02:00
$title = Title::WIM;
break;
2019-09-25 14:33:52 +02:00
case 5:
2019-09-25 12:18:02 +02:00
$title = Title::FM;
break;
2019-09-25 14:33:52 +02:00
case 6:
2019-09-25 12:18:02 +02:00
$title = Title::WGM;
break;
2019-09-25 14:33:52 +02:00
case 7:
2019-09-25 12:18:02 +02:00
$title = Title::HM;
break;
2019-09-25 14:33:52 +02:00
case 8:
2019-09-25 12:18:02 +02:00
$title = Title::IM;
break;
2019-09-25 14:33:52 +02:00
case 9:
2019-09-25 12:18:02 +02:00
$title = Title::HG;
break;
2019-09-25 14:33:52 +02:00
case 10:
2019-09-25 12:18:02 +02:00
$title = Title::GM;
break;
case 0:
default:
$title = Title::NONE;
break;
}
$player->Title = new Title($title);
2019-09-25 12:18:02 +02:00
$player->setId('Club', $this->readData('Int', $swshandle));
$player->ClubName = $this->readData('String', $swshandle);
$player->NoOfMatchesNoBye = $this->readData('Int', $swshandle);
2020-08-02 21:51:59 +02:00
$player->Points = $this->readData('Int', $swshandle); // To Calculate by libpairtwo
$player->AmericanPoints = $this->readData('Int', $swshandle); // To Calculate by libpairtwo
2019-09-25 14:33:52 +02:00
for ($t = 0; $t < 5; $t++) {
2020-08-02 21:51:59 +02:00
$tiebreaks[ $t ] = $this->readData('Int', $swshandle); // To Calculate by libpairtwo
2019-09-25 12:18:02 +02:00
}
2019-11-16 15:30:26 +01:00
$player->Tiebreak = $tiebreaks;
$player->Performance = $this->readData('Int', $swshandle); // To Calculate by libpairtwo
$player->Absent = $this->readData('Int', $swshandle);
$player->AbsentRounds = $this->readData('String', $swshandle);
$player->ExtraPoints = $this->readData('Int', $swshandle);
$player->SpecialPoints = $this->readData('Int', $swshandle);
$player->AllocatedRounds = $this->readData('Int', $swshandle);
// [RONDE]
$this->readData('String', $swshandle);
if ($player->AllocatedRounds != 0) {
for ($j = 0; $j < $player->AllocatedRounds; $j++) {
2020-08-02 21:51:59 +02:00
$pairing[ $pt ][ 'player' ] = $i;
$pairing[ $pt ][ 'round' ] = $this->readData('Int', $swshandle) - 1;
$pairing[ $pt ][ 'table' ] = $this->readData('Int', $swshandle) - 1;
$pairing[ $pt ][ 'opponent' ] = $this->readData('Int', $swshandle);
$pairing[ $pt ][ 'result' ] = $this->readData('Hex', $swshandle);
$pairing[ $pt ][ 'color' ] = $this->readData('Int', $swshandle);
$pairing[ $pt ][ 'float' ] = $this->readData('Int', $swshandle);
$pairing[ $pt ][ 'extrapoints' ] = $this->readData('Int', $swshandle);
2019-09-25 12:18:02 +02:00
$pt++;
}
2019-11-16 15:30:26 +01:00
$this->Tournament->Pairing = $pairing;
2019-09-25 12:18:02 +02:00
}
$this->Tournament->addPlayer($player);
2019-09-25 12:18:02 +02:00
}
$ptn = 0;
2020-08-02 21:51:59 +02:00
while (isset($this->Tournament->Pairing[ $ptn ][ 'round' ])) {
2019-09-25 12:18:02 +02:00
$pairing = new Pairing();
2020-08-02 21:51:59 +02:00
$pairing->Player = $this->Tournament->playerById($this->Tournament->Pairing[ $ptn ][ 'player' ]);
$pairing->Round = $this->Tournament->Pairing[ $ptn ][ 'round' ];
if ($this->Tournament->Pairing[ $ptn ][ 'opponent' ] != 4294967295) {
$pairing->Opponent =
$this->Tournament->playerById($inscriptionNos[ $this->Tournament->Pairing[ $ptn ][ 'opponent' ] ]);
2019-09-25 14:33:52 +02:00
}
2020-08-02 21:51:59 +02:00
switch ($this->Tournament->Pairing[ $ptn ][ 'result' ]) {
2019-09-25 14:33:52 +02:00
case '1000':
2020-08-02 21:51:59 +02:00
$result = Result::LOST;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:33:52 +02:00
case '01':
2020-08-02 21:51:59 +02:00
$result = Result::ABSENT;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:56:16 +02:00
case '0010':
2020-08-02 21:51:59 +02:00
$result = Result::BYE;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:56:16 +02:00
case '2000':
2020-08-02 21:51:59 +02:00
$result = Result::DRAW;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:56:16 +02:00
case '4000':
2020-08-02 21:51:59 +02:00
$result = Result::WON;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:56:16 +02:00
case '04':
2020-08-02 21:51:59 +02:00
$result = Result::WON_FORFAIT;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:56:16 +02:00
case '40':
2020-08-02 21:51:59 +02:00
$result = Result::WON_BYE;
2019-09-25 12:18:02 +02:00
break;
2019-09-25 14:56:16 +02:00
case '00':
2019-09-25 12:18:02 +02:00
default:
2020-08-02 21:51:59 +02:00
$result = Result::NONE;
2019-09-25 12:18:02 +02:00
break;
}
2020-08-02 21:51:59 +02:00
if (array_search($this->Tournament->Pairing[ $ptn ][ 'table' ], [16383, 8191]) !== false) {
$result = Result::ABSENT;
2019-09-27 01:06:57 +02:00
}
$pairing->Result = new Result($result);
2019-09-25 16:06:13 +02:00
2020-08-02 21:51:59 +02:00
switch ($this->Tournament->Pairing[ $ptn ][ 'color' ]) {
2019-09-25 16:06:13 +02:00
case 4294967295:
2020-08-02 21:51:59 +02:00
$color = Color::BLACK;
2019-09-25 16:06:13 +02:00
break;
case 1:
2020-08-02 21:51:59 +02:00
$color = Color::WHITE;
2019-09-25 16:06:13 +02:00
break;
case 0:
default:
2020-08-02 21:51:59 +02:00
$color = Color::NONE;
2019-09-25 16:06:13 +02:00
break;
}
$pairing->Color = new Color($color);
2019-09-28 20:52:23 +02:00
2020-08-02 21:51:59 +02:00
$pairing->Board = $this->Tournament->Pairing[ $ptn ][ 'table' ];
2019-09-25 12:18:02 +02:00
$ptn++;
$this->Tournament->addPairing($pairing);
2019-09-25 12:18:02 +02:00
}
2019-07-15 14:43:23 +02:00
fclose($swshandle);
$this->Tournament->pairingsToRounds();
2019-09-25 19:47:28 +02:00
$this->addTiebreaks();
2019-07-15 13:47:24 +02:00
}
2019-07-15 14:43:23 +02:00
/**
2019-11-17 01:20:15 +01:00
* Reads data of the filehandle and converts to $type. defaults to $default if given
*
* Possible types for $type are:
* * String (UTF-8 String representation of $data. Default: empty string '')
* * Hex (Capitalized Hex Value of $data. Default: 00)
* * Int (Unsigned Integer value of $data Default: 0)
* * Bool (Boolean representation of $data. Default: false)
* * Date (Date representation of $data. Default: 1902/01/01)
*
2019-07-15 14:43:23 +02:00
* @param string $type
* @param $handle
2020-08-02 21:51:59 +02:00
* @param null $default
*
2019-08-03 13:51:21 +02:00
* @return array|bool|DateTime|false|float|int|string|null
2019-07-15 14:43:23 +02:00
*/
private function readData(string $type, $handle, $default = null)
{
switch ($type) {
case 'String':
2019-07-16 16:06:50 +02:00
case 'Date':
2019-07-15 14:43:23 +02:00
$length = $this->readData('Int', $handle);
2019-07-16 16:06:50 +02:00
if ($length == 0) {
return '';
}
2019-07-15 14:43:23 +02:00
$data = fread($handle, $length);
2019-07-16 16:06:50 +02:00
if ($type == 'String') {
if ($data == '') {
return (is_null($default)) ? '' : $default;
}
2020-08-02 21:51:59 +02:00
2019-07-16 16:06:50 +02:00
return iconv('windows-1252', 'utf-8', $data);
} elseif ($type == 'Date') {
if ($data == '') {
return (is_null($default)) ? $this->convertStringToDate('01/01/1900') : $default;
}
2020-08-02 21:51:59 +02:00
2019-07-16 16:06:50 +02:00
return $this->convertStringToDate($data);
2019-07-15 14:43:23 +02:00
}
break;
case 'Hex':
case 'Int':
case 'Bool':
$data = fread($handle, 4);
$hex = implode(unpack("H*", $data));
$hex = array_reverse(str_split($hex, 2));
foreach ($hex as $key => $item) {
if ($item == "00") {
2020-08-02 21:51:59 +02:00
$hex[ $key ] = "";
2019-07-15 14:43:23 +02:00
} else {
break;
}
}
$hex = implode($hex);
$hex = ($hex == "") ? "00" : $hex;
if ($type == 'Hex') {
if ($hex == '00') {
return (is_null($default)) ? '00' : $default;
}
2020-08-02 21:51:59 +02:00
2019-07-15 14:43:23 +02:00
return $hex;
} elseif ($type == 'Int') {
if ($hex == '00') {
return (is_null($default)) ? 0 : $default;
}
2020-08-02 21:51:59 +02:00
2019-07-15 14:43:23 +02:00
return hexdec($hex);
} elseif ($type == 'Bool') {
2020-08-02 21:51:59 +02:00
return ($hex == "01");
2019-07-15 14:43:23 +02:00
}
break;
default:
2020-08-02 21:51:59 +02:00
throw new InvalidArgumentException("Datatype not known");
2019-07-15 14:43:23 +02:00
}
return false;
}
/**
2019-07-16 16:06:50 +02:00
* Returns binary data that was read out the swar file but was not needed immediately
2019-07-15 14:43:23 +02:00
*
* @param string $key
2020-08-02 21:51:59 +02:00
*
2019-09-25 14:33:52 +02:00
* @return bool|DateTime|int|string|null
2019-07-15 14:43:23 +02:00
*/
public function __get(string $key)
2019-07-15 14:43:23 +02:00
{
2020-08-02 21:51:59 +02:00
if (isset($this->BinaryData[ $key ])) {
return $this->BinaryData[ $key ];
2019-09-25 14:33:52 +02:00
}
2020-08-02 21:51:59 +02:00
2019-09-25 14:33:52 +02:00
return null;
2019-07-15 14:43:23 +02:00
}
/**
2019-07-16 16:06:50 +02:00
* Sets binary data that is read out the swar file but is not needed immediately
2019-07-15 14:43:23 +02:00
*
2020-08-02 21:51:59 +02:00
* @param string $key
2019-11-17 01:20:15 +01:00
* @param bool|int|DateTime|string $value
2019-07-15 14:43:23 +02:00
*/
2019-11-17 01:20:15 +01:00
public function __set(string $key, $value): void
2019-07-15 14:43:23 +02:00
{
2020-08-02 21:51:59 +02:00
$this->BinaryData[ $key ] = $value;
2019-07-15 14:43:23 +02:00
}
2019-07-16 16:06:50 +02:00
2019-08-03 13:51:21 +02:00
/**
2019-11-17 01:20:15 +01:00
* Converts a swar-4 string to a \DateTime object
2020-08-02 21:51:59 +02:00
*
2019-08-03 13:51:21 +02:00
* @param string $string
2020-08-02 21:51:59 +02:00
*
2019-08-03 13:51:21 +02:00
* @return DateTime
*/
2019-09-28 10:28:33 +02:00
public function convertStringToDate(string $string): DateTime
2019-07-16 16:06:50 +02:00
{
2019-09-25 14:33:52 +02:00
if (strlen($string) == 10) {
return DateTime::createFromFormat('d/m/Y', $string);
} elseif (strlen($string) == 8) {
return DateTime::createFromFormat('Ymd', $string);
2020-08-02 21:51:59 +02:00
} else {
$default = new DateTime();
$default->setTimestamp(0);
return $default;
2019-09-25 14:33:52 +02:00
}
2019-07-16 16:06:50 +02:00
}
2019-09-25 19:47:28 +02:00
2019-11-17 01:20:15 +01:00
/**
* Adds the first tiebreak to the tournament
*/
2019-11-16 14:24:07 +01:00
private function addTiebreaks(): void
2019-09-25 19:47:28 +02:00
{
switch ($this->Tournament->System) {
2020-08-02 21:51:59 +02:00
case TournamentSystem::AMERICAN:
case TournamentSystem::CLOSED:
case TournamentSystem::SWISS:
2019-09-25 19:47:28 +02:00
default:
2020-08-02 21:51:59 +02:00
$firstElement = new Tiebreak(Tiebreak::POINTS);
2019-09-25 19:47:28 +02:00
}
$tiebreaks = $this->Tournament->Tiebreaks;
2019-09-25 19:47:28 +02:00
array_unshift($tiebreaks, $firstElement);
$this->Tournament->Tiebreaks = $tiebreaks;
2019-09-25 19:47:28 +02:00
}
2019-07-15 13:47:24 +02:00
}