2019-02-01 15:53:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2019-06-19 19:49:39 +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 19:49:39 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Gender;
|
2019-09-28 10:33:59 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Title;
|
2019-06-19 19:49:39 +02:00
|
|
|
use DateTime;
|
2019-02-01 15:53:39 +01:00
|
|
|
|
2019-05-01 16:04:37 +02:00
|
|
|
/**
|
|
|
|
* Class Player
|
2019-06-19 19:49:39 +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:37 +02:00
|
|
|
*/
|
2019-06-19 19:49:39 +02:00
|
|
|
class Player
|
2019-02-01 15:53:39 +01:00
|
|
|
{
|
2019-06-19 19:49:39 +02:00
|
|
|
/** @var string */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Name;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var int[] */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Ids;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var int[] */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Elos;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var DateTime */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $DateOfBirth;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var float[] */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Tiebreaks = [];
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Nation;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
// TODO: Implement categories
|
|
|
|
/** @var string */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Category;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var Title */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Title;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var Gender */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Gender;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @var Pairing[] */
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Pairings = [];
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/** @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-11-16 14:24:07 +01:00
|
|
|
public function addPairing(Pairing $pairing): void
|
2019-04-20 16:55:39 +02:00
|
|
|
{
|
2019-11-15 17:16:38 +01:00
|
|
|
$newArray = $this->Pairings;
|
2019-04-20 16:55:39 +02:00
|
|
|
$newArray[] = $pairing;
|
2019-11-15 17:16:38 +01:00
|
|
|
$this->Pairings = $newArray;
|
2019-04-20 16:55:39 +02:00
|
|
|
}
|
2019-05-01 16:04:37 +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[] */
|
2019-11-15 17:16:38 +01:00
|
|
|
$players = $tournament->Players;
|
2019-05-01 16:04:37 +02:00
|
|
|
|
|
|
|
/** @var Player[] */
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
foreach ($players as $player) {
|
2019-11-15 17:16:38 +01:00
|
|
|
if (fnmatch($search, $player->Name)) {
|
2019-05-01 16:04:37 +02:00
|
|
|
$return[] = $player;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
}
|
2019-05-29 18:09:19 +02:00
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Returns the elo of elotype for the player
|
2019-05-29 18:09:19 +02:00
|
|
|
* @param string $type
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getElo(string $type): int
|
|
|
|
{
|
2019-11-15 17:16:38 +01:00
|
|
|
return $this->Elos[$type];
|
2019-05-29 18:09:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Sets the elo of elotype for the player
|
|
|
|
*
|
2019-05-29 18:09:19 +02:00
|
|
|
* @param string $type
|
|
|
|
* @param int $value
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public function setElo(string $type, int $value): void
|
2019-05-29 18:09:19 +02:00
|
|
|
{
|
2019-11-15 17:16:38 +01:00
|
|
|
$currentElos = $this->Elos;
|
2019-05-29 18:09:19 +02:00
|
|
|
$currentElos[$type] = $value;
|
2019-11-15 17:16:38 +01:00
|
|
|
$this->Elos = $currentElos;
|
2019-05-29 18:09:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Returns the identifier of type for the player
|
|
|
|
*
|
|
|
|
* Common possible values are Fide or National
|
|
|
|
*
|
2019-05-29 18:09:19 +02:00
|
|
|
* @param string $type
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getId(string $type): string
|
|
|
|
{
|
2019-11-15 17:16:38 +01:00
|
|
|
return $this->Ids[$type];
|
2019-05-29 18:09:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Sets the identifier of type for the player
|
|
|
|
*
|
|
|
|
* Common possible values are Fide or National
|
|
|
|
*
|
2019-05-29 18:09:19 +02:00
|
|
|
* @param string $type
|
|
|
|
* @param string $value
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public function setId(string $type, string $value): void
|
2019-05-29 18:09:19 +02:00
|
|
|
{
|
2019-11-15 17:16:38 +01:00
|
|
|
$currentIds = $this->Ids;
|
2019-05-29 18:09:19 +02:00
|
|
|
$currentIds[$type] = $value;
|
2019-11-15 17:16:38 +01:00
|
|
|
$this->Ids = $currentIds;
|
2019-05-29 18:09:19 +02:00
|
|
|
}
|
2019-05-30 21:01:30 +02:00
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Returns the number of won matches for the player
|
|
|
|
*
|
2019-05-30 21:01:30 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
2019-06-20 14:23:32 +02:00
|
|
|
public function getNoOfWins(): int
|
2019-05-30 21:01:30 +02:00
|
|
|
{
|
|
|
|
$wins = 0;
|
2019-11-15 17:16:38 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-05-30 21:01:30 +02:00
|
|
|
$wins++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $wins;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Returns the points of the player.
|
|
|
|
*
|
|
|
|
* 1 Point is awarded for winning
|
|
|
|
* 0.5 points are awarded for draw
|
|
|
|
*
|
2019-05-30 21:33:27 +02:00
|
|
|
* @return float
|
2019-05-30 21:01:30 +02:00
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public function calculatePoints(): float
|
2019-05-30 21:01:30 +02:00
|
|
|
{
|
|
|
|
$points = 0;
|
2019-11-15 17:16:38 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-05-30 21:01:30 +02:00
|
|
|
$points = $points + 1;
|
2019-11-15 17:16:38 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
2019-05-30 21:01:30 +02:00
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
2019-05-30 21:10:31 +02:00
|
|
|
|
2019-09-27 16:27:04 +02:00
|
|
|
/**
|
|
|
|
* Returns the points of the player that should be used for buchholz.
|
|
|
|
*
|
|
|
|
* 1 Point is awarded for winning
|
|
|
|
* 0.5 points are awarded for draw
|
|
|
|
* 0.5 points for not played
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getPointsForBuchholz(): float
|
|
|
|
{
|
|
|
|
$points = 0;
|
2019-11-15 17:16:38 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::NotPlayed) !== false) {
|
2019-09-27 16:27:04 +02:00
|
|
|
$points = $points + 0.5;
|
2019-11-15 17:16:38 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-09-27 16:27:04 +02:00
|
|
|
$points = $points + 1;
|
2019-11-15 17:16:38 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
2019-09-27 16:27:04 +02:00
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
2019-05-30 21:10:31 +02:00
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Returns the performance rating of the player
|
|
|
|
*
|
|
|
|
* WARNING: Calculation currently incorrect. Uses the rule of 400 as temporary solution
|
|
|
|
*
|
2019-05-30 21:33:27 +02:00
|
|
|
* @return int
|
2019-05-30 21:10:31 +02:00
|
|
|
*/
|
2019-06-20 14:23:32 +02:00
|
|
|
public function getPerformance(string $type, int $unratedElo): float
|
2019-05-30 21:10:31 +02:00
|
|
|
{
|
|
|
|
$total = 0;
|
|
|
|
$opponents = 0;
|
2019-11-15 17:16:38 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::NotPlayed) === false) {
|
|
|
|
$opponentElo = $pairing->Opponent->getElo($type);
|
2019-06-01 13:37:50 +02:00
|
|
|
$opponentElo = $opponentElo != 0 ? $opponentElo : $unratedElo;
|
2019-11-15 17:16:38 +01:00
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-05-31 10:24:06 +02:00
|
|
|
$total += $opponentElo + 400;
|
2019-11-15 17:16:38 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Lost) !== false) {
|
2019-05-31 10:24:06 +02:00
|
|
|
$total += $opponentElo - 400;
|
2019-11-15 17:16:38 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
2019-05-31 10:24:06 +02:00
|
|
|
$total += $opponentElo;
|
2019-05-30 21:10:31 +02:00
|
|
|
}
|
|
|
|
$opponents++;
|
|
|
|
}
|
|
|
|
}
|
2019-06-01 13:37:50 +02:00
|
|
|
return round($total / $opponents);
|
2019-05-30 21:10:31 +02:00
|
|
|
}
|
2019-06-05 13:23:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
2019-06-20 14:53:26 +02:00
|
|
|
* Returns the number of played games of the player
|
|
|
|
*
|
2019-06-05 13:23:16 +02:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getPlayedGames(): int
|
|
|
|
{
|
|
|
|
$total = 0;
|
2019-11-15 17:16:38 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::Played) !== false) {
|
2019-06-05 13:23:16 +02:00
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-15 17:16:38 +01:00
|
|
|
* Returns binary data that was read out the pairing file but was not needed immediately
|
2019-06-20 14:53:26 +02:00
|
|
|
*
|
2019-06-19 19:49:39 +02:00
|
|
|
* @param string $Key
|
2019-09-25 14:38:44 +02:00
|
|
|
* @return bool|DateTime|int|string|null
|
2019-06-19 19:49:39 +02:00
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public function __get(string $Key)
|
2019-06-19 19:49:39 +02:00
|
|
|
{
|
2019-09-25 14:38:44 +02:00
|
|
|
if (isset($this->BinaryData[$Key])) {
|
|
|
|
return $this->BinaryData[$Key];
|
|
|
|
}
|
|
|
|
return null;
|
2019-06-19 19:49:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-15 17:16:38 +01:00
|
|
|
* Sets binary data that is read out the pairing file but is not needed immediately
|
2019-06-20 14:53:26 +02:00
|
|
|
*
|
2019-06-19 19:49:39 +02:00
|
|
|
* @param string $Key
|
|
|
|
* @param bool|int|DateTime|string $Value
|
|
|
|
*/
|
2019-11-16 14:24:07 +01:00
|
|
|
public function __set(string $Key, $Value): void
|
2019-06-19 19:49:39 +02:00
|
|
|
{
|
|
|
|
$this->BinaryData[$Key] = $Value;
|
|
|
|
}
|
2019-02-11 17:37:30 +01:00
|
|
|
}
|