libpairtwo/src/Tournament.php

859 lines
24 KiB
PHP
Raw Normal View History

2019-02-01 15:53:39 +01:00
<?php
/**
* Class Tournament
*
* Class for the tournament from the pairing file
*
* @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
*/
namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Color;
2019-09-28 10:33:59 +02:00
use JeroenED\Libpairtwo\Enums\Tiebreak;
use JeroenED\Libpairtwo\Enums\TournamentSystem;
2019-09-28 10:33:59 +02:00
use Closure;
use DateTime;
2019-02-01 15:53:39 +01:00
/**
* Class Tournament
*
* Class for the tournament from the pairing file
*
* @author Jeroen De Meerleer <schaak@jeroened.be>
* @category Main
* @package Libpairtwo
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
*/
class Tournament
2019-02-01 15:53:39 +01:00
{
/** @var string */
public $Name;
/** @var string */
public $Organiser;
/** @var int */
public $OrganiserClubNo;
/** @var string */
public $OrganiserClub;
/** @var string */
public $OrganiserPlace;
/** @var string */
public $OrganiserCountry;
/** @var int */
public $FideHomol;
/** @var DateTime */
public $StartDate;
/** @var DateTime */
public $EndDate;
/** @var string[] */
public $Arbiters;
/** @var int */
public $NoOfRounds;
/** @var Round[] */
public $Rounds = [];
/** @var string */
public $Tempo;
/** @var int */
public $NonRatedElo;
/** @var TournamentSystem */
public $System;
/** @var string */
public $FirstPeriod;
/** @var string */
public $SecondPeriod;
/** @var string */
public $Federation;
/** @var Player[] */
public $Players = [];
/** @var int */
public $Year;
/** @var Pairing[] */
public $Pairings = [];
/** @var Tiebreak[] */
public $Tiebreaks = [];
/** @var string */
public $PriorityElo = 'Fide';
/** @var string */
public $PriorityId = 'Nation';
2019-07-16 15:58:53 +02:00
/** @var bool|DateTime|int|string[] */
private $BinaryData = [];
2019-07-16 15:58:53 +02:00
2019-02-01 15:53:39 +01:00
/**
2019-03-20 17:33:09 +01:00
* Gets a player by its ID
*
2019-02-11 17:54:50 +01:00
* @param integer $id
2019-02-01 15:53:39 +01:00
* @return Player
*/
2019-06-20 14:23:32 +02:00
public function getPlayerById(int $id): Player
2019-02-01 15:53:39 +01:00
{
return $this->Players[$id];
2019-02-01 15:53:39 +01:00
}
/**
2019-03-20 17:33:09 +01:00
* Adds a player
*
2019-02-01 15:53:39 +01:00
* @param Player $Player
2019-06-20 14:23:32 +02:00
* @return Tournament
2019-02-01 15:53:39 +01:00
*/
2019-06-20 14:23:32 +02:00
public function addPlayer(Player $Player): Tournament
2019-02-01 15:53:39 +01:00
{
$newArray = $this->Players;
2019-02-01 15:53:39 +01:00
$newArray[] = $Player;
$this->Players = $newArray;
2019-06-20 14:23:32 +02:00
return $this;
2019-02-01 15:53:39 +01:00
}
2019-02-01 17:02:33 +01:00
/**
2019-03-20 17:33:09 +01:00
* Updates player on id to the given Player object
*
2019-02-11 17:54:50 +01:00
* @param int $id
2019-02-01 17:02:33 +01:00
* @param Player $player
2019-06-20 14:23:32 +02:00
* @return Tournament
2019-02-01 17:02:33 +01:00
*/
2019-06-20 14:23:32 +02:00
public function updatePlayer(int $id, Player $player): Tournament
2019-02-01 17:02:33 +01:00
{
$newArray = $this->Players;
2019-02-01 17:02:33 +01:00
$newArray[$id] = $player;
$this->Players = $newArray;
2019-06-20 14:23:32 +02:00
return $this;
2019-02-01 17:02:33 +01:00
}
/**
* Adds a Tiebreak
*
* @param Tiebreak $tiebreak
2019-06-20 14:23:32 +02:00
* @return Tournament
*/
2019-06-20 14:23:32 +02:00
public function addTiebreak(Tiebreak $tiebreak): Tournament
{
$newArray = $this->Tiebreaks;
$newArray[] = $tiebreak;
$this->Tiebreaks = $newArray;
2019-06-20 14:23:32 +02:00
return $this;
}
/**
2019-03-20 17:33:09 +01:00
* Adds a round with given Round object
*
* @param Round $round
2019-06-20 14:23:32 +02:00
* @return Tournament
*/
2019-06-20 14:23:32 +02:00
public function addRound(Round $round): Tournament
{
$newArray = $this->Rounds;
$newArray[$round->RoundNo] = $round;
$this->Rounds = $newArray;
2019-06-20 14:23:32 +02:00
return $this;
}
/**
2019-03-20 17:33:09 +01:00
* Gets a round by its number.
*
* @param int $roundNo
* @return Round
*/
public function getRoundByNo(int $roundNo): Round
{
return $this->Rounds[$roundNo];
}
2019-02-11 16:43:36 +01:00
/**
2019-03-20 17:33:09 +01:00
* Adds a pairing to the tournament
*
2019-02-11 16:43:36 +01:00
* @param Pairing $pairing
2019-06-20 14:23:32 +02:00
* @return Tournament
2019-02-11 16:43:36 +01:00
*/
2019-06-20 14:23:32 +02:00
public function addPairing(Pairing $pairing): Tournament
2019-02-11 16:43:36 +01:00
{
$newArray = $this->Pairings;
2019-02-11 16:43:36 +01:00
$newArray[] = $pairing;
$this->Pairings = $newArray;
2019-06-20 14:23:32 +02:00
return $this;
2019-02-11 16:43:36 +01:00
}
/**
* Adds an arbiter to the tournament
*
* @param string $Arbiter
* @return Tournament
*/
public function addArbiter(string $Arbiter): Tournament
{
$newArray = $this->Arbiters;
$newArray[] = $Arbiter;
$this->Arbiters = $newArray;
return $this;
}
/**
2019-03-20 17:33:09 +01:00
* Converts pairings into games with a black and white player
2019-06-20 14:23:32 +02:00
*
* @return Tournament
*/
2019-06-20 14:23:32 +02:00
public function pairingsToRounds(): Tournament
{
2019-04-20 16:55:39 +02:00
/** @var Pairing[] $pairings */
$pairings = $this->Pairings;
2019-03-20 12:46:46 +01:00
/** @var Pairing[] */
$cache = array();
/** @var int[] */
$lastboards;
/** @var Pairing $pairing */
foreach ($pairings as $pairing) {
2019-04-20 16:55:39 +02:00
// Add pairing to player
$pairing->Player->addPairing($pairing);
$round = $pairing->Round;
$color = $pairing->Color;
2019-05-01 15:49:12 +02:00
$this->getRoundByNo($round)->addPairing($pairing);
2019-03-20 12:46:46 +01:00
$opponent = null;
/**
* @var int $key
* @var Pairing $cached
*/
2019-03-20 17:33:09 +01:00
foreach ($cache as $key=>$cached) {
2019-03-20 12:46:46 +01:00
if (!is_null($cached)) {
if (($cached->Opponent == $pairing->Player) && ($cached->Round == $pairing->Round)) {
2019-03-20 12:46:46 +01:00
$opponent = $cached;
2019-04-20 16:55:39 +02:00
$cache[$key] = null;
2019-03-20 12:46:46 +01:00
break;
}
}
}
$game = new Game();
if ($color->getValue() == Color::White) {
$game->White = $pairing;
$game->Black = $opponent;
} elseif ($color->getValue() == Color::Black) {
$game->White = $opponent;
$game->Black = $pairing;
}
if (is_null($game->White) || is_null($game->Black)) {
2019-03-20 12:46:46 +01:00
$cache[] = $pairing;
} else {
// Check if game already exists
if (!$this->gameExists($game, $round)) {
$game->Board = $game->White->Board;
// Add board if inexistent
if ($game->Board == -1) {
if (isset($lastboards[$round])) {
$lastboards[$round] += 1;
} else {
$lastboards[$round] = 0;
}
$game->Board = $lastboards[$round];
$game->White->Board = $lastboards[$round];
$game->Black->Board = $lastboards[$round];
}
2019-03-20 12:46:46 +01:00
$this->AddGame($game, $round);
}
}
}
2019-06-20 14:23:32 +02:00
return $this;
}
/**
* Checks if a game already is already registered
*
* @param Game $game
* @param int $round
* @return bool
*/
public function gameExists(Game $game, int $round = -1): bool
{
$search = [ $round ];
if ($round == -1) {
$search = [];
for ($i = 0; $i < $this->NoOfRounds; $i++) {
$search[] = $i;
}
}
foreach ($search as $round) {
if (!isset($this->Rounds[$round])) {
return false;
}
$games = $this->Rounds[$round]->Games;
2019-03-20 12:46:46 +01:00
if (is_null($games)) {
return false;
}
foreach ($games as $roundgame) {
if ($roundgame->White == $game->White &&
$roundgame->Black == $game->Black &&
$roundgame->Result == $game->Result
) {
return true;
}
}
}
return false;
}
/**
2019-03-20 17:33:09 +01:00
* Adds a game to the tournament
*
* @param Game $game
* @param int $round
2019-06-20 14:53:26 +02:00
* @return Tournament
*/
2019-06-20 14:53:26 +02:00
public function addGame(Game $game, int $round): Tournament
{
if (!isset($this->Rounds[$round])) {
$roundObj = new Round();
$roundObj->RoundNo = $round;
$this->addRound($roundObj);
}
$this->getRoundByNo($round)->addGame($game);
2019-06-20 14:53:26 +02:00
return $this;
}
2019-02-06 17:24:10 +01:00
/**
2019-03-20 17:33:09 +01:00
* Gets the ranking of the tournament
*
2019-02-11 17:54:50 +01:00
* @return Player[]
2019-02-06 17:24:10 +01:00
*/
2019-06-20 14:53:26 +02:00
public function getRanking(): array
2019-02-06 17:24:10 +01:00
{
$players = $this->Players;
foreach ($this->Tiebreaks as $tbkey=>$tiebreak) {
foreach ($players as $pkey => $player) {
$break = $this->calculateTiebreak($tiebreak, $player, $tbkey);
$tiebreaks = $player->Tiebreaks;
$tiebreaks[$tbkey] = $break;
$player->Tiebreaks = $tiebreaks;
$this->updatePlayer($pkey, $player);
}
}
$sortedplayers[0] = $players;
foreach ($this->Tiebreaks as $tbkey=>$tiebreak) {
$newgroupkey = 0;
$tosortplayers = $sortedplayers;
$sortedplayers = [];
foreach ($tosortplayers as $groupkey=>$sortedplayerselem) {
2019-09-28 21:09:56 +02:00
usort($tosortplayers[$groupkey], $this->sortTiebreak($tbkey));
foreach ($tosortplayers[$groupkey] as $playerkey => $player) {
if (!is_null($player->Tiebreaks[$tbkey])) {
if ($playerkey != 0) {
$newgroupkey++;
if ($player->Tiebreaks[$tbkey] == $tosortplayers[$groupkey][$playerkey - 1]->Tiebreaks[$tbkey]) {
$newgroupkey--;
}
}
}
$sortedplayers[$newgroupkey][] = $player;
}
$newgroupkey++;
}
}
$finalarray = [];
foreach ($sortedplayers as $sort1) {
foreach ($sort1 as $player) {
$finalarray[] = $player;
}
}
return $finalarray;
2019-02-06 18:22:25 +01:00
}
2019-02-06 20:10:52 +01:00
/**
2019-06-20 15:29:02 +02:00
* Sort by tiebreak
*
2019-02-11 17:54:50 +01:00
* @param Player $a
* @param Player $b
2019-06-20 14:53:26 +02:00
* @return Closure
2019-02-06 20:10:52 +01:00
*/
2019-06-20 14:53:26 +02:00
private function sortTiebreak(int $key): Closure
2019-02-06 18:22:25 +01:00
{
return function (Player $a, Player $b) use ($key) {
if (($b->Tiebreaks[$key] == $a->Tiebreaks[$key]) || ($a->Tiebreaks[$key] === false) || ($b->Tiebreaks[$key] === false)) {
return 0;
2019-05-28 18:07:43 +02:00
}
return ($b->Tiebreaks[$key] > $a->Tiebreaks[$key]) ? +1 : -1;
};
2019-02-06 17:24:10 +01:00
}
2019-02-06 20:10:52 +01:00
2019-05-28 16:26:03 +02:00
2019-02-06 20:10:52 +01:00
/**
2019-06-20 15:29:02 +02:00
* Calculates a specific tiebreak for $player
*
* @param Tiebreak $tiebreak
* @param Player $player
* @param int $tbkey
2019-06-20 14:53:26 +02:00
* @return float | null
2019-05-28 16:26:03 +02:00
*/
private function calculateTiebreak(Tiebreak $tiebreak, Player $player, int $tbkey = 0): ?float
2019-05-28 16:26:03 +02:00
{
2019-05-28 18:07:43 +02:00
switch ($tiebreak) {
case Tiebreak::Keizer:
return $this->calculateKeizer($player);
2019-05-28 18:07:43 +02:00
break;
case Tiebreak::Points:
return $this->calculatePoints($player);
2019-05-28 18:07:43 +02:00
break;
case Tiebreak::Baumbach:
return $this->calculateBaumbach($player);
2019-05-28 18:07:43 +02:00
break;
case Tiebreak::BlackPlayed:
return $this->calculateBlackPlayed($player);
2019-05-28 18:07:43 +02:00
break;
case Tiebreak::BlackWin:
return $this->calculateBlackWin($player);
break;
case Tiebreak::Between:
return $this->calculateMutualResult($player, $this->Players, $tbkey);
2019-05-28 18:07:43 +02:00
break;
2019-05-29 17:56:30 +02:00
case Tiebreak::Aro:
return $this->calculateAverageRating($player, $this->PriorityElo);
2019-05-29 17:56:30 +02:00
break;
2019-05-30 08:33:52 +02:00
case Tiebreak::AroCut:
return $this->calculateAverageRating($player, $this->PriorityElo, 1);
2019-05-30 08:33:52 +02:00
break;
2019-05-30 21:07:05 +02:00
case Tiebreak::Koya:
return $this->calculateKoya($player);
break;
2019-05-30 21:13:14 +02:00
case Tiebreak::Buchholz:
return $this->calculateBuchholz($player);
break;
case Tiebreak::BuchholzCut:
return $this->calculateBuchholz($player, 1);
break;
case Tiebreak::BuchholzMed:
return $this->calculateBuchholz($player, 1, 1);
break;
2019-09-25 19:35:19 +02:00
case Tiebreak::BuchholzCut2:
return $this->calculateBuchholz($player, 2);
break;
case Tiebreak::BuchholzMed2:
return $this->calculateBuchholz($player, 2, 2);
break;
2019-05-30 21:16:12 +02:00
case Tiebreak::Sonneborn:
return $this->calculateSonneborn($player);
break;
2019-05-30 21:20:14 +02:00
case Tiebreak::Kashdan:
2019-06-20 15:29:02 +02:00
return $this->calculateKashdan($player, ["Won" => 4, "Draw" => 2, "Lost" => 1, "NotPlayed" => 0]);
2019-05-30 21:20:14 +02:00
break;
case Tiebreak::SoccerKashdan:
2019-09-25 16:39:46 +02:00
return $this->calculateKashdan($player, ["Won" => 3, "Draw" => 1, "Lost" => 0, "NotPlayed" => -1]);
break;
2019-05-30 21:25:37 +02:00
case Tiebreak::Cumulative:
return $this->calculateCumulative($player);
break;
case Tiebreak::AveragePerformance:
return $this->calculateAveragePerformance($player, $this->PriorityElo);
2019-05-30 21:30:28 +02:00
break;
case Tiebreak::Performance:
return $player->getPerformance($this->PriorityElo, $this->NonRatedElo);
break;
default:
return null;
2019-05-28 16:26:03 +02:00
}
}
2019-05-29 17:57:42 +02:00
/**
* Return the average rating for tournament
*
* @return int
*/
public function getAverageElo(): int
{
$totalrating = 0;
$players = 0;
foreach ($this->Players as $player) {
$toadd = $player->getElo($this->PriorityElo);
2019-05-29 17:57:42 +02:00
if ($toadd == 0) {
$toadd = $this->NonRatedElo;
2019-05-29 17:57:42 +02:00
}
$totalrating += $toadd;
$players++;
}
return intdiv($totalrating, $players);
}
/**
* Returns the number of participants
*
* @return int
*/
public function getParticipants(): int
{
return count($this->Players);
}
/**
2019-06-20 15:29:02 +02:00
* Points following keizer system
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateKeizer(Player $player): ?float
{
return $player->ScoreAmerican;
}
/**
2019-06-20 15:29:02 +02:00
* Number of points
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculatePoints(Player $player): ?float
{
return $player->calculatePoints();
}
/**
2019-06-20 15:29:02 +02:00
* Number of won games
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateBaumbach(Player $player): ?float
{
$totalwins = 0;
foreach ($player->Pairings as $pairing) {
if (array_search($pairing->Result, Constants::NotPlayed) === false) {
if (array_search($pairing->Result, Constants::Won) !== false) {
2019-09-27 17:41:27 +02:00
$totalwins++;
}
}
}
return $totalwins;
}
/**
2019-06-20 15:29:02 +02:00
* Number of played games with black
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateBlackPlayed(Player $player): ?float
{
$totalwins = 0;
foreach ($player->Pairings as $pairing) {
if (array_search($pairing->Color, Constants::Black) !== false) {
$totalwins++;
}
}
return $totalwins;
}
/**
2019-06-20 15:29:02 +02:00
* Number of won games with black
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateBlackWin(Player $player): ?float
{
$totalwins = 0;
foreach ($player->Pairings as $pairing) {
if (array_search($pairing->Color, Constants::Black) !== false && array_search($pairing->Result, Constants::Won) !== false) {
$totalwins++;
}
}
return $totalwins;
}
/**
2019-06-20 14:53:26 +02:00
* Result between the tied players
*
* @param Player $player
* @param array $opponents
* @param int $key
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateMutualResult(Player $player, array $opponents, int $key): ?float
{
$interestingplayers = $opponents;
if ($key != 0) {
$interestingplayers = [];
$playerstiebreaks = $player->Tiebreaks;
array_splice($playerstiebreaks, $key);
foreach ($opponents as $opponent) {
$opponenttiebreaks = $opponent->Tiebreaks;
array_splice($opponenttiebreaks, $key);
if (($playerstiebreaks == $opponenttiebreaks) && ($player != $opponent)) {
$interestingplayers[] = $opponent;
}
}
}
$points = 0;
$totalmatches = 0;
foreach ($player->Pairings as $pairing) {
if (array_search($pairing->Opponent, $interestingplayers) !== false) {
if (array_search($pairing->Result, Constants::Won) !== false) {
$points = $points + 1;
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
$points = $points + 0.5;
}
$totalmatches++;
}
}
if ($totalmatches != count($interestingplayers)) {
$points = 0;
}
return $points;
}
/**
2019-06-20 14:53:26 +02:00
* The average rating of the opponents
*
* @param Player $player
* @param int $cut
* @return float
*/
private function calculateAverageRating(Player $player, string $type, int $cut = 0): ?float
{
$pairings = $player->Pairings;
$allratings = [];
foreach ($pairings as $pairing) {
if (array_search($pairing->Result, Constants::NotPlayed) === false) {
$toadd = $pairing->Opponent->getElo($type);
if ($toadd != 0) {
$allratings[] = $toadd;
}
}
}
sort($allratings);
$allratings = array_slice($allratings, $cut);
2019-09-27 18:00:28 +02:00
$tiebreak = 0;
if (count($allratings) > 0) {
$tiebreak = round(array_sum($allratings) / count($allratings));
}
return $tiebreak;
}
/**
2019-06-20 14:53:26 +02:00
* The average performance of the opponents
*
* @param Player $player
* @param int $cut
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateAveragePerformance(Player $player, string $type, int $cut = 0): ?float
{
$pairings = $player->Pairings;
$allratings = [];
foreach ($pairings as $pairing) {
if (array_search($pairing->Result, Constants::NotPlayed) === false) {
$toadd = $pairing->Opponent->getPerformance($type, $this->NonRatedElo);
if ($toadd != 0) {
$allratings[] = $toadd;
}
}
}
sort($allratings);
$allratings = array_slice($allratings, $cut);
return round(array_sum($allratings) / count($allratings));
}
/**
2019-06-20 14:53:26 +02:00
* Points against players who have more than $cut % points
*
* @param Player $player
* @param int $cut
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateKoya(Player $player, int $cut = 50): ?float
{
$tiebreak = 0;
foreach ($player->Pairings as $plkey => $plpairing) {
if (($plpairing->Opponent->Points / count($plpairing->Opponent->Pairings) * 100) >= $cut) {
if (array_search($plpairing->Result, Constants::Won) !== false) {
$tiebreak += 1;
} elseif (array_search($plpairing->Result, Constants::Draw) !== false) {
$tiebreak += 0.5;
}
}
}
return $tiebreak;
}
/**
2019-06-20 14:53:26 +02:00
* The combined points of the opponents
* @param Player $player
* @param int $cutlowest
* @param int $cuthighest
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateBuchholz(Player $player, int $cutlowest = 0, int $cuthighest = 0): ?float
{
$tiebreak = 0;
$intpairingsWithBye = $player->Pairings;
$intpairings = [];
$curpoints = 0;
$curround = 1;
foreach ($intpairingsWithBye as $pairing) {
$roundstoplay = (count($intpairingsWithBye)) - $curround;
if (is_null($pairing->Opponent)) {
$forfait = explode(' ', $pairing->Result)[0]+0;
$notaplayer = $curpoints + (1 - $forfait) + 0.5 * $roundstoplay;
$intpairings[] = $notaplayer;
} else {
$intpairings[] = $pairing->Opponent->PointsForBuchholz;
if (array_search($pairing->Result, Constants::Won) !== false) {
$curpoints += 1;
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
$curpoints += 0.5;
}
}
$curround++;
}
usort($intpairings, function ($a, $b) {
if ($b == $a) {
return 0;
}
return ($a > $b) ? 1 : -1;
});
$intpairings = array_slice($intpairings, $cutlowest);
$intpairings = array_slice($intpairings, 0 - $cuthighest);
2019-09-27 17:03:58 +02:00
return array_sum($intpairings);
}
/**
2019-06-20 14:53:26 +02:00
* The points of $player's opponents who $player won against, plus half of the points of $player's opponents who $player drew against
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateSonneborn(Player $player): ?float
{
$tiebreak = 0;
foreach ($player->Pairings as $key => $pairing) {
if ($pairing->Opponent) {
if (array_search($pairing->Result, Constants::Won) !== false) {
$tiebreak += $pairing->Opponent->Points;
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
$tiebreak += $pairing->Opponent->Points / 2;
}
}
}
return $tiebreak;
}
/**
2019-06-20 15:29:02 +02:00
* $points["Won"] points for each win, $points["Draw"] for each draw and $points["Lost"] point for losing. $points["NotPlayed"] points for not played games
2019-06-20 14:53:26 +02:00
*
* @param Player $player
2019-06-20 15:29:02 +02:00
* @param int[] $points
2019-06-20 14:53:26 +02:00
* @return float | null
*/
2019-06-20 15:29:02 +02:00
private function calculateKashdan(Player $player, array $points): ?float
{
$tiebreak = 0;
foreach ($player->Pairings as $pairing) {
$toadd = 0;
if (array_search($pairing->Result, Constants::Won) !== false) {
2019-06-20 15:29:02 +02:00
$toadd = $points["Won"];
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
2019-06-20 15:29:02 +02:00
$toadd = $points["Draw"];
} elseif (array_search($pairing->Result, Constants::Lost) !== false) {
2019-06-20 15:29:02 +02:00
$toadd = $points["Lost"];
}
if (array_search($pairing->Result, Constants::NotPlayed) !== false) {
2019-06-20 15:29:02 +02:00
$toadd = $points["NotPlayed"];
}
$tiebreak += $toadd;
}
return $tiebreak; // - $player->NoOfWins;
}
/**
2019-06-20 14:53:26 +02:00
* Combined score of $player after each round
*
* @param Player $player
2019-06-20 14:53:26 +02:00
* @return float | null
*/
private function calculateCumulative(Player $player): ?float
{
$tiebreak = 0;
$score = [];
foreach ($player->Pairings as $pairing) {
$toadd = 0;
if (array_search($pairing->Result, Constants::Won) !== false) {
$toadd = 1;
} elseif (array_search($pairing->Result, Constants::Draw) !== false) {
$toadd = 0.5;
}
$tiebreak += $toadd;
$score[] = $tiebreak;
}
return array_sum($score);
}
2019-07-16 15:58:53 +02:00
/**
* Returns binary data that was read out the pairing file but was not needed immediately
*
* @param string $Key
* @return bool|DateTime|int|string|null
2019-07-16 15:58:53 +02:00
*/
public function __get(string $Key)
2019-07-16 15:58:53 +02:00
{
if (isset($this->BinaryData[$Key])) {
return $this->BinaryData[$Key];
}
return null;
2019-07-16 15:58:53 +02:00
}
/**
* Sets binary data that is read out the pairing file but is not needed immediately
*
* @param string $Key
* @param bool|int|DateTime|string $Value
* @return void
2019-07-16 15:58:53 +02:00
*/
public function __set(string $Key, $Value): void
2019-07-16 15:58:53 +02:00
{
$this->BinaryData[$Key] = $Value;
}
2019-02-01 15:53:39 +01:00
}