From 6e43f43a8df9e6865177c537fb83d57f7146e138 Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Fri, 27 Sep 2019 16:48:48 +0200 Subject: [PATCH] BUGFIX: Buchholz score did not return the correct score --- CHANGELOG.md | 1 + src/Player.php | 24 +++++++++++++++++++++++- src/Tournament.php | 39 +++++++++++++++++++++++++-------------- 3 files changed, 49 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42e57eb..703509e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ * ENHANCEMENT: `Class::getBinaryData` methods return null if field is non-existent * CHANGE: `Tournament::getArbiter` accepts a `int` parameter representing the order of the arbiters * BUGFIX: `Player:GetId` returns elo instead of id +* BUGFIX: `Tournament::CalculateBuchholz` did not return the correct score when player had onplayed rounds ## v1.1.2 (Release: 21-jun-2019) * ENHANCEMENT: Added update section to dist/readme.md diff --git a/src/Player.php b/src/Player.php index 12d54e7..9876f21 100644 --- a/src/Player.php +++ b/src/Player.php @@ -192,7 +192,29 @@ class Player return $points; } - + /** + * 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->getPairings() as $pairing) { + if (array_search($pairing->getResult(), Constants::NotPlayed) !== false) { + $points = $points + 0.5; + } elseif (array_search($pairing->getResult(), Constants::Won) !== false) { + $points = $points + 1; + } elseif (array_search($pairing->getResult(), Constants::Draw) !== false) { + $points = $points + 0.5; + } + } + return $points; + } /** * Returns the performance rating of the player * diff --git a/src/Tournament.php b/src/Tournament.php index da2aa20..1e1bb3b 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -201,7 +201,7 @@ class Tournament /** * Adds an arbiter to the tournament - * + * * @param string $Arbiter * @return Tournament */ @@ -710,29 +710,40 @@ class Tournament private function calculateBuchholz(Player $player, int $cutlowest = 0, int $cuthighest = 0): ?float { $tiebreak = 0; - $intpairings = $player->getPairings(); + $intpairingsWithBye = $player->getPairings(); + + $intpairings = []; + $curpoints = 0; + $curround = 1; + foreach ($intpairingsWithBye as $pairing) { + $roundstoplay = (count($intpairingsWithBye)) - $curround; + if (is_null($pairing->getOpponent())) { + $forfait = explode(' ', $pairing->getResult())[0]+0; + $notaplayer = $curpoints + (1 - $forfait) + 0.5 * $roundstoplay; + $intpairings[] = $notaplayer; + } else { + $intpairings[] = $pairing->getOpponent()->getPointsForBuchholz(); + if (array_search($pairing->getResult(), Constants::Won) !== false) { + $curpoints += 1; + } elseif (array_search($pairing->getResult(), Constants::Draw) !== false) { + $curpoints += 0.5; + } + } + $curround++; + } usort($intpairings, function ($a, $b) { - if (is_null($a->getOpponent())) { - return -1; - } - if (is_null($b->getOpponent())) { - return 1; - } - - if ($b->getOpponent()->getPoints() == $a->getOpponent()->getPoints()) { + if ($b == $a) { return 0; } - return ($a->getOpponent()->getPoints() > $b->getOpponent()->getPoints()) ? 1 : -1; + return ($a > $b) ? 1 : -1; }); $intpairings = array_slice($intpairings, $cutlowest); $intpairings = array_slice($intpairings, 0 - $cuthighest); foreach ($intpairings as $intkey => $intpairing) { - if (!is_null($intpairing->getOpponent())) { - $tiebreak += $intpairing->getOpponent()->getPoints(); - } + $tiebreak += $intpairing; } return $tiebreak; }