libpairtwo/src/Player.php

282 lines
6.9 KiB
PHP
Raw Normal View History

2019-02-01 15:53:39 +01:00
<?php
/**
* 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
use JeroenED\Libpairtwo\Enums\Gender;
2019-09-28 10:33:59 +02:00
use JeroenED\Libpairtwo\Enums\Title;
use DateTime;
2019-02-01 15:53:39 +01:00
2019-05-01 16:04:37 +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-05-01 16:04:37 +02:00
*/
class Player
2019-02-01 15:53:39 +01:00
{
/** @var string */
public $Name;
/** @var int[] */
public $Ids;
/** @var int[] */
public $Elos;
/** @var DateTime */
public $DateOfBirth;
/** @var float[] */
public $Tiebreaks = [];
/** @var string */
public $Nation;
// TODO: Implement categories
/** @var string */
public $Category;
/** @var Title */
public $Title;
/** @var Gender */
public $Gender;
/** @var Pairing[] */
public $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-11-16 14:24:07 +01:00
public function addPairing(Pairing $pairing): void
2019-04-20 16:55:39 +02:00
{
$newArray = $this->Pairings;
2019-04-20 16:55:39 +02:00
$newArray[] = $pairing;
$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[] */
$players = $tournament->Players;
2019-05-01 16:04:37 +02:00
/** @var Player[] */
$return = [];
foreach ($players as $player) {
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
{
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
*/
public function setElo(string $type, int $value): void
2019-05-29 18:09:19 +02:00
{
$currentElos = $this->Elos;
2019-05-29 18:09:19 +02:00
$currentElos[$type] = $value;
$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
{
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
*/
public function setId(string $type, string $value): void
2019-05-29 18:09:19 +02:00
{
$currentIds = $this->Ids;
2019-05-29 18:09:19 +02:00
$currentIds[$type] = $value;
$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;
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
*/
public function calculatePoints(): float
2019-05-30 21:01:30 +02:00
{
$points = 0;
foreach ($this->Pairings as $pairing) {
if (array_search($pairing->Result, Constants::Won) !== false) {
2019-05-30 21:01:30 +02:00
$points = $points + 1;
} 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
/**
* 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;
foreach ($this->Pairings as $pairing) {
if (array_search($pairing->Result, Constants::NotPlayed) !== false) {
$points = $points + 0.5;
} elseif (array_search($pairing->Result, Constants::Won) !== false) {
$points = $points + 1;
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
$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;
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;
if (array_search($pairing->Result, Constants::Won) !== false) {
2019-05-31 10:24:06 +02:00
$total += $opponentElo + 400;
} elseif (array_search($pairing->Result, Constants::Lost) !== false) {
2019-05-31 10:24:06 +02:00
$total += $opponentElo - 400;
} 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;
foreach ($this->Pairings as $pairing) {
if (array_search($pairing->Result, Constants::Played) !== false) {
2019-06-05 13:23:16 +02:00
$total++;
}
}
return $total;
}
/**
* Returns binary data that was read out the pairing file but was not needed immediately
2019-06-20 14:53:26 +02:00
*
* @param string $key
2019-09-25 14:38:44 +02:00
* @return bool|DateTime|int|string|null
*/
public function __get(string $key)
{
if (isset($this->BinaryData[$key])) {
return $this->BinaryData[$key];
2019-09-25 14:38:44 +02:00
}
return null;
}
/**
* Sets binary data that is read out the pairing file but is not needed immediately
2019-06-20 14:53:26 +02:00
*
* @param string $key
* @param bool|int|DateTime|string $Valueey
*/
public function __set(string $key, $Valueey): void
{
$this->BinaryData[$key] = $Valueey;
}
2019-02-11 17:37:30 +01:00
}