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-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* Name of the player
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Name;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The player ids for the player. Possible keys are, but not limited to nation and fide
|
|
|
|
*
|
|
|
|
* @var int[]
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Ids;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The Elos for the player. Possible keys are, but not limited to nation and fide
|
|
|
|
*
|
|
|
|
* @var int[]
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Elos;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* Birthday of the player
|
|
|
|
*
|
|
|
|
* @var DateTime
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $DateOfBirth;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* Tiebreak points of the player. These values are calculated when Tournament->Ranking is called
|
|
|
|
*
|
|
|
|
* @var float[]
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Tiebreaks = [];
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The nation the player belongs to. Be noted this does not actually mean this is his main nationality. A player can be signed USCF but may be Italian
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Nation;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
|
|
|
// TODO: Implement categories
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The category the player belongs to
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Category;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The title of the player. Possible values can be GM, IM, IA, etc.
|
|
|
|
*
|
|
|
|
* @var Title
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Title;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The gender of the player. Possible values contain Male, Female and Neutral
|
|
|
|
*
|
|
|
|
* @var Gender
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Gender;
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* The pairings of the player
|
|
|
|
*
|
|
|
|
* @var Pairing[]
|
|
|
|
*/
|
2019-11-15 17:16:38 +01:00
|
|
|
public $Pairings = [];
|
2019-06-19 19:49:39 +02:00
|
|
|
|
2019-11-17 01:20:15 +01:00
|
|
|
/**
|
|
|
|
* Binary data that was read out of the pairing file
|
|
|
|
*
|
|
|
|
* @var bool|DateTime|int|string[]
|
|
|
|
*/
|
|
|
|
|
2019-06-19 19:49:39 +02:00
|
|
|
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[]
|
|
|
|
*/
|
2019-11-16 14:46:01 +01:00
|
|
|
public static function PlayersByName(string $search, Tournament $tournament): array
|
2019-05-01 16:04:37 +02:00
|
|
|
{
|
|
|
|
/** @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-11-16 14:46:01 +01:00
|
|
|
private function noOfWins(): 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-12-22 17:58:40 +01:00
|
|
|
* Returns the points of the player after round $round
|
2019-06-20 14:53:26 +02:00
|
|
|
*
|
|
|
|
* 1 Point is awarded for winning
|
|
|
|
* 0.5 points are awarded for draw
|
2019-12-22 17:58:40 +01:00
|
|
|
* 0 points are awarded for loss
|
2019-06-20 14:53:26 +02:00
|
|
|
*
|
2019-12-22 17:58:40 +01:00
|
|
|
* @param int $round
|
2019-05-30 21:33:27 +02:00
|
|
|
* @return float
|
2019-05-30 21:01:30 +02:00
|
|
|
*/
|
2019-12-22 17:58:40 +01:00
|
|
|
public function calculatePoints(int $round = -1): float
|
2019-05-30 21:01:30 +02:00
|
|
|
{
|
|
|
|
$points = 0;
|
2019-12-22 17:58:40 +01:00
|
|
|
foreach ($this->Pairings as $key=>$pairing) {
|
|
|
|
if($key < $round || $round == -1) {
|
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
|
|
|
$points = $points + 1;
|
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
2019-05-30 21:01:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
2019-05-30 21:10:31 +02:00
|
|
|
|
2019-09-27 16:27:04 +02:00
|
|
|
/**
|
2019-12-22 17:58:40 +01:00
|
|
|
* Returns the points of a virtual player as described in the Fide Handbook C.02 chapter 13.15.2.
|
|
|
|
*
|
|
|
|
* 1 Point is awarded for winning
|
|
|
|
* 0.5 points are awarded for draw
|
|
|
|
* Unplayed results are conside
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function calculatePointsForVirtualPlayer(int $byeround): float
|
|
|
|
{
|
|
|
|
$points = $this->calculatePoints($byeround);
|
|
|
|
foreach (array_slice($this->Pairings, $byeround +1) as $key=>$pairing) {
|
|
|
|
$points += 0.5;
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Returns the points of the player that should be used for tiebreaking systems.
|
2019-09-27 16:27:04 +02:00
|
|
|
*
|
|
|
|
* 1 Point is awarded for winning
|
|
|
|
* 0.5 points are awarded for draw
|
|
|
|
* 0.5 points for not played
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2019-12-22 17:58:40 +01:00
|
|
|
public function calculatePointsForTiebreaks(): float
|
2019-09-27 16:27:04 +02:00
|
|
|
{
|
|
|
|
$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-11-17 01:20:15 +01:00
|
|
|
* @param $type
|
|
|
|
* @param $unratedElo
|
2019-05-30 21:33:27 +02:00
|
|
|
* @return int
|
2019-05-30 21:10:31 +02:00
|
|
|
*/
|
2019-11-16 14:46:01 +01:00
|
|
|
public function Performance(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
|
|
|
|
*/
|
2019-11-16 14:46:01 +01:00
|
|
|
private function playedGames(): int
|
2019-06-05 13:23:16 +02:00
|
|
|
{
|
|
|
|
$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-16 14:46:01 +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-20 14:53:26 +02:00
|
|
|
*
|
2019-11-16 14:43:12 +01: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-16 14:43:12 +01:00
|
|
|
public function __get(string $key)
|
2019-06-19 19:49:39 +02:00
|
|
|
{
|
2019-11-16 14:46:01 +01:00
|
|
|
if($key == 'PlayedGames') {
|
|
|
|
return $this->playedGames();
|
|
|
|
} elseif ($key == 'NoOfWins') {
|
|
|
|
return $this->noOfWins();
|
|
|
|
} elseif (isset($this->BinaryData[$key])) {
|
2019-11-16 14:43:12 +01:00
|
|
|
return $this->BinaryData[$key];
|
2019-09-25 14:38:44 +02:00
|
|
|
}
|
|
|
|
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-11-16 14:43:12 +01:00
|
|
|
* @param string $key
|
2019-11-17 01:20:15 +01:00
|
|
|
* @param bool|int|DateTime|string $value
|
2019-06-19 19:49:39 +02:00
|
|
|
*/
|
2019-11-17 01:20:15 +01:00
|
|
|
public function __set(string $key, $value): void
|
2019-06-19 19:49:39 +02:00
|
|
|
{
|
2019-11-17 01:20:15 +01:00
|
|
|
$this->BinaryData[$key] = $value;
|
2019-06-19 19:49:39 +02:00
|
|
|
}
|
2019-02-11 17:37:30 +01:00
|
|
|
}
|