2019-02-01 15:53:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2019-06-19 21:54:19 +02:00
|
|
|
* Class Player
|
|
|
|
*
|
|
|
|
* Class for a player 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-01 15:53:39 +01:00
|
|
|
*/
|
|
|
|
|
2019-03-20 17:33:09 +01:00
|
|
|
namespace JeroenED\Libpairtwo;
|
2019-02-01 15:53:39 +01:00
|
|
|
|
2019-06-19 21:54:19 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Title;
|
|
|
|
use JeroenED\Libpairtwo\Enums\Gender;
|
|
|
|
use DateTime;
|
2019-02-01 15:53:39 +01:00
|
|
|
|
2019-05-01 16:04:42 +02:00
|
|
|
/**
|
|
|
|
* Class Player
|
2019-06-19 21:54:19 +02:00
|
|
|
*
|
|
|
|
* Class for a player 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-05-01 16:04:42 +02:00
|
|
|
*/
|
2019-06-19 21:54:19 +02:00
|
|
|
class Player
|
2019-02-01 15:53:39 +01:00
|
|
|
{
|
2019-06-19 21:54:19 +02:00
|
|
|
/** @var string */
|
|
|
|
private $Name;
|
|
|
|
|
|
|
|
/** @var int[] */
|
|
|
|
private $Ids;
|
|
|
|
|
|
|
|
/** @var int[] */
|
|
|
|
private $Elos;
|
|
|
|
|
|
|
|
/** @var DateTime */
|
|
|
|
private $DateOfBirth;
|
|
|
|
|
|
|
|
/** @var float[] */
|
|
|
|
private $Tiebreaks = [];
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $Nation;
|
|
|
|
|
|
|
|
// TODO: Implement categories
|
|
|
|
/** @var string */
|
|
|
|
private $Category;
|
|
|
|
|
|
|
|
/** @var Title */
|
|
|
|
private $Title;
|
|
|
|
|
|
|
|
/** @var Gender */
|
|
|
|
private $Gender;
|
|
|
|
|
|
|
|
/** @var Pairing[] */
|
|
|
|
private $Pairings = [];
|
|
|
|
|
|
|
|
/** @var bool|DateTime|int|string[] */
|
|
|
|
private $BinaryData;
|
|
|
|
|
2019-04-20 16:55:39 +02:00
|
|
|
/**
|
|
|
|
* Adds a pairing to the tournament
|
|
|
|
*
|
|
|
|
* @param Pairing $pairing
|
2019-06-20 15:32:26 +02:00
|
|
|
* @return Player
|
2019-04-20 16:55:39 +02:00
|
|
|
*/
|
2019-06-20 15:32:26 +02:00
|
|
|
public function addPairing(Pairing $pairing): Player
|
2019-04-20 16:55:39 +02:00
|
|
|
{
|
|
|
|
$newArray = $this->GetPairings();
|
|
|
|
$newArray[] = $pairing;
|
|
|
|
$this->setPairings($newArray);
|
2019-06-20 15:32:26 +02:00
|
|
|
return $this;
|
2019-04-20 16:55:39 +02:00
|
|
|
}
|
2019-05-01 16:04:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array of Player objects where name matches $search
|
|
|
|
*
|
|
|
|
* @param string $search
|
|
|
|
* @param Tournament $tournament
|
|
|
|
* @return Player[]
|
|
|
|
*/
|
|
|
|
public static function getPlayersByName(string $search, Tournament $tournament): array
|
|
|
|
{
|
|
|
|
/** @var Player[] */
|
|
|
|
$players = $tournament->getPlayers();
|
|
|
|
|
|
|
|
/** @var Player[] */
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
foreach ($players as $player) {
|
|
|
|
if (fnmatch($search, $player->getName())) {
|
|
|
|
$return[] = $player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2019-06-01 16:39:58 +02:00
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the elo of elotype for the player
|
2019-06-01 16:39:58 +02:00
|
|
|
* @param string $type
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getElo(string $type): int
|
|
|
|
{
|
|
|
|
return $this->getElos()[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the elo of elotype for the player
|
|
|
|
*
|
2019-06-01 16:39:58 +02:00
|
|
|
* @param string $type
|
|
|
|
* @param int $value
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setElo(string $type, int $value): Player
|
|
|
|
{
|
|
|
|
$currentElos = $this->getElos();
|
|
|
|
$currentElos[$type] = $value;
|
|
|
|
$this->setElos($currentElos);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the identifier of type for the player
|
|
|
|
*
|
|
|
|
* Common possible values are Fide or National
|
|
|
|
*
|
2019-06-01 16:39:58 +02:00
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId(string $type): string
|
|
|
|
{
|
|
|
|
return $this->getElos()[$type];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the identifier of type for the player
|
|
|
|
*
|
|
|
|
* Common possible values are Fide or National
|
|
|
|
*
|
2019-06-01 16:39:58 +02:00
|
|
|
* @param string $type
|
|
|
|
* @param string $value
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setId(string $type, string $value): Player
|
|
|
|
{
|
|
|
|
$currentIds = $this->getIds();
|
|
|
|
$currentIds[$type] = $value;
|
|
|
|
$this->setIds($currentIds);
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the number of won matches for the player
|
|
|
|
*
|
2019-06-01 16:39:58 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2019-06-20 15:32:26 +02:00
|
|
|
public function getNoOfWins(): int
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
|
|
|
$wins = 0;
|
|
|
|
foreach ($this->getPairings() as $pairing) {
|
|
|
|
if (array_search($pairing->getResult(), Constants::Won) !== false) {
|
|
|
|
$wins++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $wins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the points of the player.
|
|
|
|
*
|
|
|
|
* 1 Point is awarded for winning
|
|
|
|
* 0.5 points are awarded for draw
|
|
|
|
*
|
2019-06-01 16:39:58 +02:00
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getPoints(): float
|
|
|
|
{
|
|
|
|
$points = 0;
|
|
|
|
foreach ($this->getPairings() as $pairing) {
|
|
|
|
if (array_search($pairing->getResult(), Constants::Won) !== false) {
|
|
|
|
$points = $points + 1;
|
|
|
|
} elseif (array_search($pairing->getResult(), Constants::Draw) !== false) {
|
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the performance rating of the player
|
|
|
|
*
|
|
|
|
* WARNING: Calculation currently incorrect. Uses the rule of 400 as temporary solution
|
|
|
|
*
|
2019-06-01 16:39:58 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2019-06-20 15:32:26 +02:00
|
|
|
public function getPerformance(string $type, int $unratedElo): float
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
|
|
|
$total = 0;
|
|
|
|
$opponents = 0;
|
|
|
|
foreach ($this->getPairings() as $pairing) {
|
|
|
|
if (array_search($pairing->getResult(), Constants::NotPlayed) === false) {
|
|
|
|
$opponentElo = $pairing->getOpponent()->getElo($type);
|
|
|
|
$opponentElo = $opponentElo != 0 ? $opponentElo : $unratedElo;
|
|
|
|
if (array_search($pairing->getResult(), Constants::Won) !== false) {
|
|
|
|
$total += $opponentElo + 400;
|
|
|
|
} elseif (array_search($pairing->getResult(), Constants::Lost) !== false) {
|
|
|
|
$total += $opponentElo - 400;
|
|
|
|
} elseif (array_search($pairing->getResult(), Constants::Draw) !== false) {
|
|
|
|
$total += $opponentElo;
|
|
|
|
}
|
|
|
|
$opponents++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return round($total / $opponents);
|
|
|
|
}
|
2019-06-05 13:25:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the number of played games of the player
|
|
|
|
*
|
2019-06-05 13:25:14 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPlayedGames(): int
|
|
|
|
{
|
|
|
|
$total = 0;
|
|
|
|
foreach ($this->getPairings() as $pairing) {
|
|
|
|
if (array_search($pairing->getResult(), Constants::Played) !== false) {
|
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the name of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName(): string
|
|
|
|
{
|
|
|
|
return $this->Name;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the name of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param string $Name
|
2019-06-20 15:32:26 +02:00
|
|
|
* @return Player
|
2019-06-19 21:54:19 +02:00
|
|
|
*/
|
|
|
|
public function setName(string $Name): Player
|
|
|
|
{
|
|
|
|
$this->Name = $Name;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns an array of all ID's of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getIds(): ?array
|
|
|
|
{
|
|
|
|
return $this->Ids;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets an array of all ID's of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param string[] $Ids
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setIds(array $Ids): Player
|
|
|
|
{
|
|
|
|
$this->Ids = $Ids;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns an array of all elos of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return int[]
|
|
|
|
*/
|
|
|
|
public function getElos(): ?array
|
|
|
|
{
|
|
|
|
return $this->Elos;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets an array of all elos of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param int[] $Elos
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setElos(array $Elos): Player
|
|
|
|
{
|
|
|
|
$this->Elos = $Elos;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the date of birth of the player
|
|
|
|
*
|
|
|
|
* @return DateTime
|
2019-06-19 21:54:19 +02:00
|
|
|
*/
|
2019-06-20 15:32:26 +02:00
|
|
|
public function getDateOfBirth(): DateTime
|
2019-06-19 21:54:19 +02:00
|
|
|
{
|
|
|
|
return $this->DateOfBirth;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the date of birth of the player
|
|
|
|
*
|
|
|
|
* @param DateTime $DateOfBirth
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return Player
|
|
|
|
*/
|
2019-06-20 15:32:26 +02:00
|
|
|
public function setDateOfBirth(DateTime $DateOfBirth): Player
|
2019-06-19 21:54:19 +02:00
|
|
|
{
|
|
|
|
$this->DateOfBirth = $DateOfBirth;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns an array of all tiebreaks for the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return float[]
|
|
|
|
*/
|
|
|
|
public function getTiebreaks(): array
|
|
|
|
{
|
|
|
|
return $this->Tiebreaks;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets an array of all tiebreaks for the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param float[] $Tiebreaks
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setTiebreaks(array $Tiebreaks): Player
|
|
|
|
{
|
|
|
|
$this->Tiebreaks = $Tiebreaks;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the nation of the player
|
2019-06-19 21:54:19 +02:00
|
|
|
* example value: BEL
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getNation(): string
|
|
|
|
{
|
|
|
|
return $this->Nation;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the nation of the player
|
2019-06-19 21:54:19 +02:00
|
|
|
* example value: BEL
|
|
|
|
*
|
|
|
|
* @param string $Nation
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setNation(string $Nation): Player
|
|
|
|
{
|
|
|
|
$this->Nation = $Nation;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the category of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getCategory(): string
|
|
|
|
{
|
|
|
|
return $this->Category;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the category of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param string $Category
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setCategory(string $Category): Player
|
|
|
|
{
|
|
|
|
$this->Category = $Category;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the title of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return Title
|
|
|
|
*/
|
|
|
|
public function getTitle(): Title
|
|
|
|
{
|
|
|
|
return $this->Title;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the title of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param Title $Title
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setTitle(Title $Title): Player
|
|
|
|
{
|
|
|
|
$this->Title = $Title;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns the gender of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return Gender
|
|
|
|
*/
|
|
|
|
public function getGender(): Gender
|
|
|
|
{
|
|
|
|
return $this->Gender;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets the gender of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param Gender $Gender
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setGender(Gender $Gender): Player
|
|
|
|
{
|
|
|
|
$this->Gender = $Gender;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns an array of all pairings of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @return Pairing[]
|
|
|
|
*/
|
|
|
|
public function getPairings(): array
|
|
|
|
{
|
|
|
|
return $this->Pairings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets an array of all pairings of the player
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param Pairing[] $Pairings
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setPairings(array $Pairings): Player
|
|
|
|
{
|
|
|
|
$this->Pairings = $Pairings;
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Returns binary data that was read out the pairtwo file but was not needed immediately
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param string $Key
|
|
|
|
* @return bool|DateTime|int|string
|
|
|
|
*/
|
|
|
|
public function getBinaryData(string $Key)
|
|
|
|
{
|
|
|
|
return $this->BinaryData[$Key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 15:32:26 +02:00
|
|
|
* Sets binary data that is read out the pairtwo file but is not needed immediately
|
|
|
|
*
|
2019-06-19 21:54:19 +02:00
|
|
|
* @param string $Key
|
|
|
|
* @param bool|int|DateTime|string $Value
|
|
|
|
* @return Player
|
|
|
|
*/
|
|
|
|
public function setBinaryData(string $Key, $Value): Player
|
|
|
|
{
|
|
|
|
$this->BinaryData[$Key] = $Value;
|
|
|
|
return $this;
|
|
|
|
}
|
2019-02-11 17:37:30 +01:00
|
|
|
}
|