2019-02-06 17:23:37 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2019-06-19 21:54:19 +02:00
|
|
|
* Class Round
|
|
|
|
*
|
|
|
|
* Class for a round of the tournament
|
|
|
|
*
|
|
|
|
* @author Jeroen De Meerleer <schaak@jeroened.be>
|
|
|
|
* @category Main
|
|
|
|
* @package Libpairtwo
|
|
|
|
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
|
2019-02-06 17:23:37 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace JeroenED\Libpairtwo;
|
|
|
|
|
2019-06-19 21:54:19 +02:00
|
|
|
use DateTime;
|
2019-05-13 20:03:25 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Result;
|
2019-02-06 17:23:37 +01:00
|
|
|
|
2019-06-19 21:54:19 +02:00
|
|
|
/**
|
|
|
|
* Class Round
|
|
|
|
*
|
|
|
|
* Class for a round of the tournament
|
|
|
|
*
|
|
|
|
* @author Jeroen De Meerleer <schaak@jeroened.be>
|
|
|
|
* @category Main
|
|
|
|
* @package Libpairtwo
|
|
|
|
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
|
|
|
|
*/
|
|
|
|
class Round
|
2019-02-06 17:23:37 +01:00
|
|
|
{
|
2019-06-19 21:54:19 +02:00
|
|
|
/**
|
|
|
|
* Date of the round
|
|
|
|
*
|
|
|
|
* @var DateTime
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Date;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of all games
|
|
|
|
*
|
|
|
|
* @var Game[]
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Games = [];
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of the round
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public $RoundNo;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Array of all pairings for this round
|
|
|
|
*
|
|
|
|
* @var Pairing[]
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Pairings = [];
|
2019-06-19 21:54:19 +02:00
|
|
|
|
2019-11-17 01:23:11 +01:00
|
|
|
/**
|
2019-03-20 17:33:09 +01:00
|
|
|
* Adds a game to the round
|
|
|
|
*
|
|
|
|
* @param Game $game
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function addGame(Game $game): void
|
2019-02-11 22:41:44 +01:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$newarray = $this->Games;
|
2019-02-11 22:41:44 +01:00
|
|
|
$newarray[] = $game;
|
2019-11-16 15:23:37 +01:00
|
|
|
$this->Games = $newarray;
|
2019-02-11 22:41:44 +01:00
|
|
|
}
|
2019-05-01 15:50:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a pairing to the round
|
|
|
|
*
|
|
|
|
* @param Pairing $pairing
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function addPairing(Pairing $pairing): void
|
2019-05-01 15:50:44 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$newarray = $this->Pairings;
|
2019-05-01 15:50:44 +02:00
|
|
|
$newarray[] = $pairing;
|
2019-11-16 15:23:37 +01:00
|
|
|
$this->Pairings = $newarray;
|
2019-05-01 15:50:44 +02:00
|
|
|
}
|
2019-05-13 20:03:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of pairings where the player is bye
|
|
|
|
*
|
|
|
|
* @return Pairing[]
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
private function bye(): array
|
2019-05-13 20:03:25 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$allPairings = $this->Pairings;
|
2019-05-13 20:03:25 +02:00
|
|
|
$byePairings = [];
|
|
|
|
foreach ($allPairings as $pairing) {
|
2019-11-16 15:23:37 +01:00
|
|
|
if ($pairing->Result == Result::WonBye) {
|
2019-05-13 20:03:25 +02:00
|
|
|
$byePairings[] = $pairing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $byePairings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of pairings where the player is absent
|
|
|
|
*
|
|
|
|
* @return Pairing[]
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
private function absent(): array
|
2019-05-13 20:03:25 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$allPairings = $this->Pairings;
|
2019-05-13 20:03:25 +02:00
|
|
|
$absentPairings = [];
|
|
|
|
foreach ($allPairings as $pairing) {
|
2019-11-16 15:23:37 +01:00
|
|
|
if ($pairing->Result == Result::Absent) {
|
2019-05-13 20:03:25 +02:00
|
|
|
$absentPairings[] = $pairing;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $absentPairings;
|
|
|
|
}
|
2019-06-19 21:54:19 +02:00
|
|
|
|
2019-09-28 21:49:33 +02:00
|
|
|
/**
|
|
|
|
* Retuns an array with the games of this round sorted by board
|
|
|
|
*
|
|
|
|
* @return Game[]
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
private function gamesByBoard(): array
|
2019-09-28 21:49:33 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$allGames = $this->Games;
|
2019-09-28 21:49:33 +02:00
|
|
|
usort($allGames, array($this, 'sortByBoard'));
|
|
|
|
return $allGames;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort by board
|
|
|
|
*
|
|
|
|
* @param Game $a
|
|
|
|
* @param Game $b
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
private function sortByBoard(Game $a, Game $b): int
|
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
if (($a->Board == $b->Board) || ($a->Board === false) || ($b->Board === false)) {
|
2019-09-28 21:49:33 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-11-16 15:23:37 +01:00
|
|
|
return ($a->Board > $b->Board) ? +1 : -1;
|
2019-09-28 21:49:33 +02:00
|
|
|
}
|
|
|
|
|
2019-06-19 21:54:19 +02:00
|
|
|
/**
|
2019-11-16 15:23:37 +01:00
|
|
|
* Magic method to read out several fields. If field was not found it is being searched in the binary data fields
|
2019-06-19 21:54:19 +02:00
|
|
|
*
|
2019-11-16 15:23:37 +01:00
|
|
|
* @param string $key
|
|
|
|
* @return bool|DateTime|int|string|null
|
2019-06-19 21:54:19 +02:00
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function __get(string $key)
|
2019-06-19 21:54:19 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
if ($key == 'Bye') {
|
|
|
|
return $this->bye();
|
2019-12-22 18:17:17 +01:00
|
|
|
} elseif ($key == 'Absent') {
|
2019-11-16 15:23:37 +01:00
|
|
|
return $this->absent();
|
2019-12-22 18:17:17 +01:00
|
|
|
} elseif ($key == 'GamesByBoard') {
|
2019-11-16 15:23:37 +01:00
|
|
|
return $this->gamesByBoard();
|
2019-12-22 18:17:17 +01:00
|
|
|
} elseif (isset($this->BinaryData[$key])) {
|
2019-11-16 15:23:37 +01:00
|
|
|
return $this->BinaryData[$key];
|
|
|
|
}
|
|
|
|
return null;
|
2019-06-19 21:54:19 +02:00
|
|
|
}
|
2019-02-11 17:37:30 +01:00
|
|
|
}
|