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\Gender;
|
2019-09-28 10:34:43 +02:00
|
|
|
use JeroenED\Libpairtwo\Enums\Title;
|
2019-06-19 21:54:19 +02:00
|
|
|
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 */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Name;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var int[] */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Ids;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var int[] */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Elos;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var DateTime */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $DateOfBirth;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var float[] */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Tiebreaks = [];
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var string */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Nation;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
// TODO: Implement categories
|
|
|
|
/** @var string */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Category;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var Title */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Title;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var Gender */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Gender;
|
2019-06-19 21:54:19 +02:00
|
|
|
|
|
|
|
/** @var Pairing[] */
|
2019-11-16 15:23:37 +01:00
|
|
|
public $Pairings = [];
|
2019-06-19 21:54:19 +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 15:23:37 +01:00
|
|
|
public function addPairing(Pairing $pairing): void
|
2019-04-20 16:55:39 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$newArray = $this->Pairings;
|
2019-04-20 16:55:39 +02:00
|
|
|
$newArray[] = $pairing;
|
2019-11-16 15:23:37 +01:00
|
|
|
$this->Pairings = $newArray;
|
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[]
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public static function PlayersByName(string $search, Tournament $tournament): array
|
2019-05-01 16:04:42 +02:00
|
|
|
{
|
|
|
|
/** @var Player[] */
|
2019-11-16 15:23:37 +01:00
|
|
|
$players = $tournament->Players;
|
2019-05-01 16:04:42 +02:00
|
|
|
|
|
|
|
/** @var Player[] */
|
|
|
|
$return = [];
|
|
|
|
|
|
|
|
foreach ($players as $player) {
|
2019-11-16 15:23:37 +01:00
|
|
|
if (fnmatch($search, $player->Name)) {
|
2019-05-01 16:04:42 +02:00
|
|
|
$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
|
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
return $this->Elos[$type];
|
2019-06-01 16:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function setElo(string $type, int $value): void
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$currentElos = $this->Elos;
|
2019-06-01 16:39:58 +02:00
|
|
|
$currentElos[$type] = $value;
|
2019-11-16 15:23:37 +01:00
|
|
|
$this->Elos = $currentElos;
|
2019-06-01 16:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
return $this->Ids[$type];
|
2019-06-01 16:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function setId(string $type, string $value): void
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$currentIds = $this->Ids;
|
2019-06-01 16:39:58 +02:00
|
|
|
$currentIds[$type] = $value;
|
2019-11-16 15:23:37 +01:00
|
|
|
$this->Ids = $currentIds;
|
2019-06-01 16:39:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-11-16 15:23:37 +01:00
|
|
|
private function noOfWins(): int
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
|
|
|
$wins = 0;
|
2019-11-16 15:23:37 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-06-01 16:39:58 +02:00
|
|
|
$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
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function calculatePoints(): float
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
|
|
|
$points = 0;
|
2019-11-16 15:23:37 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-06-01 16:39:58 +02:00
|
|
|
$points = $points + 1;
|
2019-11-16 15:23:37 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
2019-06-01 16:39:58 +02:00
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
|
|
|
|
2019-09-27 16:48:48 +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
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
private function pointsForBuchholz(): float
|
2019-09-27 16:48:48 +02:00
|
|
|
{
|
|
|
|
$points = 0;
|
2019-11-16 15:23:37 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::NotPlayed) !== false) {
|
2019-09-27 16:48:48 +02:00
|
|
|
$points = $points + 0.5;
|
2019-11-16 15:23:37 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-09-27 16:48:48 +02:00
|
|
|
$points = $points + 1;
|
2019-11-16 15:23:37 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
2019-09-27 16:48:48 +02:00
|
|
|
$points = $points + 0.5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $points;
|
|
|
|
}
|
2019-06-01 16:39:58 +02:00
|
|
|
/**
|
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-11-16 15:23:37 +01:00
|
|
|
public function Performance(string $type, int $unratedElo): float
|
2019-06-01 16:39:58 +02:00
|
|
|
{
|
|
|
|
$total = 0;
|
|
|
|
$opponents = 0;
|
2019-11-16 15:23:37 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::NotPlayed) === false) {
|
|
|
|
$opponentElo = $pairing->Opponent->getElo($type);
|
2019-06-01 16:39:58 +02:00
|
|
|
$opponentElo = $opponentElo != 0 ? $opponentElo : $unratedElo;
|
2019-11-16 15:23:37 +01:00
|
|
|
if (array_search($pairing->Result, Constants::Won) !== false) {
|
2019-06-01 16:39:58 +02:00
|
|
|
$total += $opponentElo + 400;
|
2019-11-16 15:23:37 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Lost) !== false) {
|
2019-06-01 16:39:58 +02:00
|
|
|
$total += $opponentElo - 400;
|
2019-11-16 15:23:37 +01:00
|
|
|
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
|
2019-06-01 16:39:58 +02:00
|
|
|
$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
|
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
private function playedGames(): int
|
2019-06-05 13:25:14 +02:00
|
|
|
{
|
|
|
|
$total = 0;
|
2019-11-16 15:23:37 +01:00
|
|
|
foreach ($this->Pairings as $pairing) {
|
|
|
|
if (array_search($pairing->Result, Constants::Played) !== false) {
|
2019-06-05 13:25:14 +02:00
|
|
|
$total++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $total;
|
|
|
|
}
|
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-20 15:32:26 +02:00
|
|
|
*
|
2019-11-16 15:23:37 +01:00
|
|
|
* @param string $key
|
2019-09-25 14:44:00 +02:00
|
|
|
* @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 == 'PlayedGames') {
|
|
|
|
return $this->playedGames();
|
|
|
|
} elseif ($key == 'NoOfWins') {
|
|
|
|
return $this->noOfWins();
|
|
|
|
} elseif ($key == 'PointsForBuchholz') {
|
|
|
|
return $this->pointsForBuchholz();
|
|
|
|
} elseif (isset($this->BinaryData[$key])) {
|
|
|
|
return $this->BinaryData[$key];
|
2019-09-25 14:44:00 +02:00
|
|
|
}
|
|
|
|
return null;
|
2019-06-19 21:54:19 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-11-16 15:23:37 +01:00
|
|
|
* Sets binary data that is read out the pairing file but is not needed immediately
|
2019-06-20 15:32:26 +02:00
|
|
|
*
|
2019-11-16 15:23:37 +01:00
|
|
|
* @param string $key
|
|
|
|
* @param bool|int|DateTime|string $Valueey
|
2019-06-19 21:54:19 +02:00
|
|
|
*/
|
2019-11-16 15:23:37 +01:00
|
|
|
public function __set(string $key, $Valueey): void
|
2019-06-19 21:54:19 +02:00
|
|
|
{
|
2019-11-16 15:23:37 +01:00
|
|
|
$this->BinaryData[$key] = $Valueey;
|
2019-06-19 21:54:19 +02:00
|
|
|
}
|
2019-02-11 17:37:30 +01:00
|
|
|
}
|