Added pairings to player object

This commit is contained in:
Jeroen De Meerleer 2019-04-20 16:55:39 +02:00
parent fc2292414f
commit b265e4c23d
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 39 additions and 2 deletions

View File

@ -10,6 +10,7 @@ namespace JeroenED\Libpairtwo\Models;
use JeroenED\Libpairtwo\Enums\Title; use JeroenED\Libpairtwo\Enums\Title;
use JeroenED\Libpairtwo\Enums\Gender; use JeroenED\Libpairtwo\Enums\Gender;
use JeroenED\Libpairtwo\Pairing;
use DateTime; use DateTime;
class Player class Player
@ -68,6 +69,9 @@ class Player
/** @var bool */ /** @var bool */
private $Absent; private $Absent;
/** @var Pairing[] */
private $Pairings;
/** /**
* @return string * @return string
*/ */
@ -359,4 +363,22 @@ class Player
{ {
$this->Absent = $Absent; $this->Absent = $Absent;
} }
/**
* @return Pairing[]
*/
public function getPairings(): array
{
return $this->Pairings;
}
/**
* @param Pairing[] $Pairings
*/
public function setPairings(array $Pairings): void
{
$this->Pairings = $Pairings;
}
} }

View File

@ -12,4 +12,16 @@ use JeroenED\Libpairtwo\Models\Player as PlayerModel;
class Player extends PlayerModel class Player extends PlayerModel
{ {
/**
* Adds a pairing to the tournament
*
* @param Pairing $pairing
*/
public function addPairing(Pairing $pairing)
{
$newArray = $this->GetPairings();
$newArray[] = $pairing;
$this->setPairings($newArray);
}
} }

View File

@ -89,20 +89,23 @@ class Tournament extends TournamentModel
*/ */
public function pairingsToRounds(): void public function pairingsToRounds(): void
{ {
/** @var Pairing[] $pairings */
$pairings = $this->getPairings(); $pairings = $this->getPairings();
/** @var Pairing[] */ /** @var Pairing[] */
$cache = array(); $cache = array();
foreach ($pairings as $pairing) { foreach ($pairings as $pairing) {
// Add pairing to player
$pairing->getPlayer()->addPairing($pairing);
$round = $pairing->getRound(); $round = $pairing->getRound();
$color = $pairing->getColor(); $color = $pairing->getColor();
$opponent = null; $opponent = null;
foreach ($cache as $key=>$cached) { foreach ($cache as $key=>$cached) {
if (!is_null($cached)) { if (!is_null($cached)) {
if ($cached->getOpponent() == $pairing->getPlayer() && ($cached->getRound() == $pairing->getRound())) { if (($cached->getOpponent() == $pairing->getPlayer()) && ($cached->getRound() == $pairing->getRound())) {
$opponent = $cached; $opponent = $cached;
$cache[$key] == null; $cache[$key] = null;
break; break;
} }
} }