Added conversion of pairings into rounds

This commit is contained in:
Jeroen De Meerleer 2019-02-11 22:41:44 +01:00
parent 99ccb56aeb
commit b478d99d05
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
9 changed files with 285 additions and 12 deletions

24
src/Enums/Gameresult.php Normal file
View File

@ -0,0 +1,24 @@
<?php
/**
* Created by PhpStorm.
* User: jeroen
* Date: 11/02/19
* Time: 21:28
*/
namespace JeroenED\Libpairtwo\Enums;
use MyCLabs\Enum\Enum;
class Gameresult extends Enum
{
const WhiteWins = "1-0";
const WhiteWinsForfait = "1-0FF";
const WhiteWinsAdjourned = "1-0A";
const BlackWins = "0-1";
const BlackWinsForfait = "0-1FF";
const BlackWinsAdjourned = "0-1A";
const Draw = "0.5-0.5";
const DrawAdjourned = "0.5-0.5A";
const None = "-";
}

View File

@ -8,6 +8,69 @@
namespace JeroenED\Libpairtwo\Models; namespace JeroenED\Libpairtwo\Models;
use JeroenED\Libpairtwo\Enums\Gameresult;
use JeroenED\LibPairtwo\Player;
class Game class Game
{ {
/** @var Player */
private $white;
/** @var Player */
private $black;
/** @var GameResult */
private $result;
/**
* @return Player
*/
public function getWhite()
{
return $this->white;
}
/**
* @param Player $white
*/
public function setWhite(Player $white): void
{
$this->white = $white;
}
/**
* @return Player
*/
public function getBlack()
{
return $this->black;
}
/**
* @param Player $black
*/
public function setBlack(Player $black): void
{
$this->black = $black;
}
/**
* @return GameResult
*/
public function getResult()
{
return $this->result;
}
/**
* @param GameResult $result
*/
public function setResult(GameResult $result): void
{
$this->result = $result;
}
} }

View File

@ -23,7 +23,7 @@ class Pairing
/** /**
* @return Player * @return Player
*/ */
public function getPlayer(): Player public function getPlayer()
{ {
return $this->Player; return $this->Player;
} }
@ -39,7 +39,7 @@ class Pairing
/** /**
* @return Player * @return Player
*/ */
public function getOpponent(): Player public function getOpponent()
{ {
return $this->Opponent; return $this->Opponent;
} }

View File

@ -15,6 +15,9 @@ class Round
private $date; private $date;
private $games; private $games;
/** @var int */
private $roundNo;
/** /**
* @return DateTime * @return DateTime
* *
@ -47,4 +50,21 @@ class Round
{ {
$this->games = $games; $this->games = $games;
} }
/**
* @return int
*/
public function getRoundNo(): int
{
return $this->roundNo;
}
/**
* @param int $roundNo
*/
public function setRoundNo(int $roundNo): void
{
$this->roundNo = $roundNo;
}
} }

View File

@ -47,7 +47,11 @@ class Tournament
private $StartDate; private $StartDate;
private $EndDate; private $EndDate;
private $Arbiter; private $Arbiter;
private $NoOfRounds;
/** @var Round[] */
private $Rounds; private $Rounds;
private $Participants; private $Participants;
private $Tempo; private $Tempo;
private $NonRatedElo; private $NonRatedElo;
@ -222,15 +226,31 @@ class Tournament
/** /**
* @return int * @return int
*/ */
public function getRounds(): int public function getNoOfRounds(): int
{
return $this->NoOfRounds;
}
/**
* @param int $NoOfRounds
*/
public function setNoOfRounds(int $NoOfRounds): void
{
$this->NoOfRounds = $NoOfRounds;
}
/**
* @return Round[]
*/
public function getRounds()
{ {
return $this->Rounds; return $this->Rounds;
} }
/** /**
* @param int $Rounds * @param Round[] $Rounds
*/ */
public function setRounds(int $Rounds): void public function setRounds(array $Rounds): void
{ {
$this->Rounds = $Rounds; $this->Rounds = $Rounds;
} }

View File

@ -12,4 +12,12 @@ use JeroenED\Libpairtwo\Models\Round as RoundModel;
class Round extends RoundModel class Round extends RoundModel
{ {
public function addGame(Game $game)
{
$newarray = $this->getGames();
$newarray[] = $game;
$this->setGames($newarray);
}
} }

View File

@ -385,7 +385,7 @@ class Sws extends SwsModel
// Rounds // Rounds
$length = 4; $length = 4;
$sws->getTournament()->setRounds(self::ReadData('Int', substr($swscontents, $offset, $length))); $sws->getTournament()->setNoOfRounds(self::ReadData('Int', substr($swscontents, $offset, $length)));
$offset += $length; $offset += $length;
// Participants // Participants
@ -469,7 +469,7 @@ class Sws extends SwsModel
$offset += $length; $offset += $length;
// Round dates // Round dates
for ($i = 1; $i < $sws->getTournament()->getRounds(); $i++) { for ($i = 1; $i < $sws->getTournament()->getNoOfRounds(); $i++) {
$length = 4; $length = 4;
$sws->setBinaryData('Round_' . $i . '_date', self::ReadData('Date', substr($swscontents, $offset, $length))); $sws->setBinaryData('Round_' . $i . '_date', self::ReadData('Date', substr($swscontents, $offset, $length)));
$offset += $length; $offset += $length;
@ -505,6 +505,7 @@ class Sws extends SwsModel
} }
} }
$sws->getTournament()->pairingsToRounds();
return $sws; return $sws;
} }

View File

@ -8,8 +8,11 @@
namespace JeroenED\Libpairtwo; namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Result;
use JeroenED\Libpairtwo\Models\Tournament as TournamentModel; use JeroenED\Libpairtwo\Models\Tournament as TournamentModel;
use JeroenED\LibPairtwo\Player; use JeroenED\Libpairtwo\Player;
use JeroenED\Libpairtwo\Enums\Color;
use JeroenED\Libpairtwo\Enums\Gameresult;
class Tournament extends TournamentModel class Tournament extends TournamentModel
{ {
@ -48,11 +51,20 @@ class Tournament extends TournamentModel
*/ */
public function addRound(Round $round) public function addRound(Round $round)
{ {
$newArray = $this->GetRounds(); $newArray = $this->getRounds();
$newArray[] = $round; $newArray[$round->getRoundNo()] = $round;
$this->setRounds($newArray); $this->setRounds($newArray);
} }
/**
* @param int $roundNo
* @return Round
*/
public function getRoundByNo(int $roundNo): Round
{
return $this->getRounds()[$roundNo];
}
/** /**
* @param Pairing $pairing * @param Pairing $pairing
*/ */
@ -63,6 +75,131 @@ class Tournament extends TournamentModel
$this->setPairings($newArray); $this->setPairings($newArray);
} }
/**
* This function converts the array of pairings into rounds
*/
public function pairingsToRounds(): void
{
$pairings = $this->getPairings();
foreach ($pairings as $pairing) {
$round = $pairing->getRound();
$player = $pairing->getPlayer();
$opponent = $pairing->getOpponent();
$color = $pairing->getColor();
$result = $pairing->getResult();
$game = new Game();
if ($color->getValue() == Color::white || $color->getValue() == Color::white3) {
if(! is_null($player)) $game->setWhite($player);
if(! is_null($opponent)) $game->setBlack($opponent);
switch ($result->getValue()) {
case Result::won:
case Result::wonbye:
$game->setResult(new Gameresult("1-0")); break;
case Result::wonforfait:
$game->setResult(new Gameresult("1-0FF")); break;
case Result::wonadjourned:
$game->setResult(new Gameresult("1-0A")); break;
case Result::lost:
case Result::bye:
$game->setResult(new Gameresult("0-1")); break;
case Result::absent:
$game->setResult(new Gameresult("0-1FF")); break;
case Result::adjourn:
$game->setResult(new Gameresult("0-1A")); break;
case Result::draw:
$game->setResult(new Gameresult("0.5-0.5")); break;
case Result::drawadjourned:
$game->setResult(new Gameresult("0.5-0.5A")); break;
case Result::none:
default:
$game->setResult(new Gameresult('-')); break;
}
} elseif ($color->getValue() == Color::black || $color->getValue() == Color::black3) {
if(! is_null($player)) $game->setBlack($player);
if(! is_null($opponent)) $game->setWhite($opponent);
switch ($result->getValue()) {
case Result::won:
case Result::wonbye:
$game->setResult(new Gameresult("0-1")); break;
case Result::wonforfait:
$game->setResult(new Gameresult("0-1FF")); break;
case Result::wonadjourned:
$game->setResult(new Gameresult("0-1A")); break;
case Result::lost:
case Result::bye:
$game->setResult(new Gameresult("1-0")); break;
case Result::absent:
$game->setResult(new Gameresult("1-0FF")); break;
case Result::adjourn:
$game->setResult(new Gameresult("1-0A")); break;
case Result::draw:
$game->setResult(new Gameresult("0.5-0.5")); break;
case Result::drawadjourned:
$game->setResult(new Gameresult("0.5-0.5A")); break;
case Result::none:
default:
$game->setResult(new Gameresult('-')); break;
}
}
// Check if game already exists
if (!$this->GameExists($game, $round)) {
$this->AddGame($game, $round);
}
}
}
/**
* Checks if a game already is already registered
*
* @param Game $game
* @param int $round
* @return bool
*/
public function GameExists(Game $game, int $round = -1): bool
{
$search = [ $round ];
if ($round == -1) {
$search = [];
for ($i = 0; $i < $this->getNoOfRounds(); $i++) {
$search[] = $i;
}
}
foreach ($search as $round) {
if (!isset($this->getRounds()[$round])) {
return false;
}
$games = $this->getRounds()[$round]->getGames();
foreach ($games as $roundgame) {
if ($roundgame->getWhite() == $game->getWhite() &&
$roundgame->getBlack() == $game->getBlack() &&
$roundgame->getResult() == $game->getResult()
) {
return true;
}
}
}
return false;
}
/**
* @param Game $game
* @param int $round
*/
public function addGame(Game $game, int $round)
{
if (!isset($this->getRounds()[$round])) {
$roundObj = new Round();
$roundObj->setRoundNo($round);
$this->addRound($roundObj);
}
$this->getRoundByNo($round)->addGame($game);
}
/** /**
* @param bool $americansort * @param bool $americansort
* @return Player[] * @return Player[]

View File

@ -34,8 +34,8 @@ echo "Organiser: " . $sws->getTournament()->getOrganiser(). PHP_EOL;
echo "Tempo: " . $sws->getTournament()->getTempo() . PHP_EOL; echo "Tempo: " . $sws->getTournament()->getTempo() . PHP_EOL;
echo "Country: " . $sws->getTournament()->getOrganiserCountry() . PHP_EOL; echo "Country: " . $sws->getTournament()->getOrganiserCountry() . PHP_EOL;
echo "Arbiter: " . $sws->getTournament()->getArbiter() . PHP_EOL; echo "Arbiter: " . $sws->getTournament()->getArbiter() . PHP_EOL;
echo "Rounds: " . $sws->getTournament()->getRounds() . PHP_EOL; echo "Rounds: " . $sws->getTournament()->getNoOfRounds() . PHP_EOL;
echo "Participants: " . $sws->getTournament()->getRounds() . PHP_EOL; echo "Participants: " . $sws->getTournament()->getNoOfRounds() . PHP_EOL;
echo "Fidehomol: " . $sws->getTournament()->getFideHomol() . PHP_EOL; echo "Fidehomol: " . $sws->getTournament()->getFideHomol() . PHP_EOL;
echo "Start-Date: " . $sws->getTournament()->getStartDate()->format('d/m/Y') . PHP_EOL; echo "Start-Date: " . $sws->getTournament()->getStartDate()->format('d/m/Y') . PHP_EOL;
echo "End-Date: " . $sws->getTournament()->getEndDate()->format('d/m/Y') . PHP_EOL; echo "End-Date: " . $sws->getTournament()->getEndDate()->format('d/m/Y') . PHP_EOL;