Merge branch 'bugfix/buchholz-wrong' into develop

This commit is contained in:
Jeroen De Meerleer 2019-09-27 16:48:17 +02:00
commit 0c2abc1755
Signed by: JeroenED
GPG Key ID: 28CCCB8F62BFADD6
3 changed files with 49 additions and 15 deletions

View File

@ -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

View File

@ -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
*

View File

@ -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;
}