Reimplemented creation of results

This commit is contained in:
Jeroen De Meerleer 2019-03-20 12:46:46 +01:00
parent 1e56abf2a9
commit c6d1afd171
4 changed files with 80 additions and 78 deletions

View File

@ -12,13 +12,16 @@ 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 = "-";
}
const WhiteWins = '1-0';
const Draw = '0.5-0.5';
const BlackWins = '0-1';
const WhiteWinsForfait = '1-0 FF';
const BlackWinsForfait = '0-1 FF';
const BothLoseForfait = '0-0 FF';
const BothWinAdjourned = '1-1 A';
const WhiteWinsBlackDrawsAdjourned = '1-0.5 A';
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;
use JeroenED\Libpairtwo\Enums\Gameresult;
use JeroenED\Libpairtwo\Models\Game as 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\Player;
use JeroenED\LibPairtwo\Pairing;
class Game
{
/** @var Player */
/** @var Pairing */
private $white;
/** @var Player */
/** @var Pairing */
private $black;
/** @var GameResult */
private $result;
/**
* @return Player
* @return Pairing
*/
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;
}
/**
* @return Player
* @return Pairing
*/
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;
}

View File

@ -81,71 +81,39 @@ class Tournament extends TournamentModel
public function pairingsToRounds(): void
{
$pairings = $this->getPairings();
/** @var Pairing[] */
$cache = array();
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) {
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::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;
$opponent = null;
foreach($cache as $key=>$cached) {
if (!is_null($cached)) {
if ($cached->getOpponent() == $pairing->getPlayer() && ($cached->getRound() == $pairing->getRound())) {
$opponent = $cached;
$cache[$key] == null;
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 (!$this->GameExists($game, $round)) {
$this->AddGame($game, $round);
if (is_null($game->getWhite()) || is_null($game->getBlack())) {
$cache[] = $pairing;
} 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;
}
$games = $this->getRounds()[$round]->getGames();
if(is_null($games)) return false;
if (is_null($games)) {
return false;
}
foreach ($games as $roundgame) {
if ($roundgame->getWhite() == $game->getWhite() &&
$roundgame->getBlack() == $game->getBlack() &&