Reimplemented creation of results

This commit is contained in:
Jeroen De Meerleer 2019-03-20 12:46:46 +01:00
parent 728c25961d
commit 5625e0c7c0
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
4 changed files with 80 additions and 78 deletions

View File

@ -12,13 +12,16 @@ use MyCLabs\Enum\Enum;
class Gameresult extends Enum class Gameresult extends Enum
{ {
const WhiteWins = "1-0"; const WhiteWins = '1-0';
const WhiteWinsForfait = "1-0FF"; const Draw = '0.5-0.5';
const WhiteWinsAdjourned = "1-0A"; const BlackWins = '0-1';
const BlackWins = "0-1"; const WhiteWinsForfait = '1-0 FF';
const BlackWinsForfait = "0-1FF"; const BlackWinsForfait = '0-1 FF';
const BlackWinsAdjourned = "0-1A"; const BothLoseForfait = '0-0 FF';
const Draw = "0.5-0.5"; const BothWinAdjourned = '1-1 A';
const DrawAdjourned = "0.5-0.5A"; const WhiteWinsBlackDrawsAdjourned = '1-0.5 A';
const None = "-"; const WhiteDrawsBlackWinsAdjourned = '0.5-1 A';
const DrawAdjourned = '0.5-0.5 A';
const WhiteLoseBlackDrawsAdjourned = '0-0.5';
const WhiteDrawsBlackLoseAdjourned = '0.5-0';
} }

View File

@ -8,8 +8,36 @@
namespace JeroenED\Libpairtwo; namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Gameresult;
use JeroenED\Libpairtwo\Models\Game as GameModel; use JeroenED\Libpairtwo\Models\Game as GameModel;
class Game extends GameModel class Game extends GameModel
{ {
/**
* This function gets the result from the game
*/
public function getResult(): Gameresult
{
if (!is_null(parent::getResult())) {
return parent::getResult();
}
$whiteResult = $this->getWhite()->getResult();
$blackResult = $this->getBlack()->getResult();
$whitesplit = explode(" ", $whiteResult);
$blacksplit = explode(" ", $blackResult);
$special='';
if (isset($whitesplit[1]) && $whitesplit[1] != 'Bye') {
$special = ' ' . $whitesplit[1];
}
if (isset($blacksplit[1]) && $blacksplit[1] != 'Bye') {
$special = ' ' . $blacksplit[1];
}
$result = $whitesplit[0] . '-' . $blacksplit[0] . $special;
$this->setResult(new Gameresult($result));
return new Gameresult($result);
}
} }

View File

@ -10,20 +10,21 @@ namespace JeroenED\Libpairtwo\Models;
use JeroenED\Libpairtwo\Enums\Gameresult; use JeroenED\Libpairtwo\Enums\Gameresult;
use JeroenED\LibPairtwo\Player; use JeroenED\LibPairtwo\Player;
use JeroenED\LibPairtwo\Pairing;
class Game class Game
{ {
/** @var Player */ /** @var Pairing */
private $white; private $white;
/** @var Player */ /** @var Pairing */
private $black; private $black;
/** @var GameResult */ /** @var GameResult */
private $result; private $result;
/** /**
* @return Player * @return Pairing
*/ */
public function getWhite() public function getWhite()
{ {
@ -31,15 +32,15 @@ class Game
} }
/** /**
* @param Player $white * @param Pairing $white
*/ */
public function setWhite(Player $white): void public function setWhite($white): void
{ {
$this->white = $white; $this->white = $white;
} }
/** /**
* @return Player * @return Pairing
*/ */
public function getBlack() public function getBlack()
{ {
@ -47,9 +48,9 @@ class Game
} }
/** /**
* @param Player $black * @param Pairing $black
*/ */
public function setBlack(Player $black): void public function setBlack($black): void
{ {
$this->black = $black; $this->black = $black;
} }

View File

@ -81,71 +81,39 @@ class Tournament extends TournamentModel
public function pairingsToRounds(): void public function pairingsToRounds(): void
{ {
$pairings = $this->getPairings(); $pairings = $this->getPairings();
/** @var Pairing[] */
$cache = array();
foreach ($pairings as $pairing) { foreach ($pairings as $pairing) {
$round = $pairing->getRound(); $round = $pairing->getRound();
$player = $pairing->getPlayer();
$opponent = $pairing->getOpponent();
$color = $pairing->getColor(); $color = $pairing->getColor();
$result = $pairing->getResult(); $opponent = null;
foreach($cache as $key=>$cached) {
$game = new Game(); if (!is_null($cached)) {
if ($color->getValue() == Color::white) { if ($cached->getOpponent() == $pairing->getPlayer() && ($cached->getRound() == $pairing->getRound())) {
if(! is_null($player)) $game->setWhite($player); $opponent = $cached;
if(! is_null($opponent)) $game->setBlack($opponent); $cache[$key] == null;
switch ($result->getValue()) { break;
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::adjourned:
$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) {
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::adjourned:
$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;
} }
} }
$game = new Game();
if ($color->getValue() == Color::white) {
$game->setWhite($pairing);
$game->setBlack($opponent);
} elseif ($color->getValue() == Color::black) {
$game->setWhite($opponent);
$game->setBlack($pairing);
}
// Check if game already exists if (is_null($game->getWhite()) || is_null($game->getBlack())) {
if (!$this->GameExists($game, $round)) { $cache[] = $pairing;
$this->AddGame($game, $round); } else {
// Check if game already exists
if (!$this->GameExists($game, $round)) {
$this->AddGame($game, $round);
}
} }
} }
} }
@ -172,7 +140,9 @@ class Tournament extends TournamentModel
return false; return false;
} }
$games = $this->getRounds()[$round]->getGames(); $games = $this->getRounds()[$round]->getGames();
if(is_null($games)) return false; if (is_null($games)) {
return false;
}
foreach ($games as $roundgame) { foreach ($games as $roundgame) {
if ($roundgame->getWhite() == $game->getWhite() && if ($roundgame->getWhite() == $game->getWhite() &&
$roundgame->getBlack() == $game->getBlack() && $roundgame->getBlack() == $game->getBlack() &&