From 0efd0a2baaa11fb43d2952304e940642f379754b Mon Sep 17 00:00:00 2001 From: Jeroen De Meerleer Date: Thu, 30 May 2019 21:30:28 +0200 Subject: [PATCH] Added average performance tiebreak --- src/Tiebreaks.php | 25 +++++++++++++++++++++++++ src/Tournament.php | 3 +++ 2 files changed, 28 insertions(+) diff --git a/src/Tiebreaks.php b/src/Tiebreaks.php index 652b789..efdb580 100644 --- a/src/Tiebreaks.php +++ b/src/Tiebreaks.php @@ -122,8 +122,10 @@ abstract class Tiebreaks extends Tournament return $points; } + /** * @param Player $player + * @param int $cut * @return float */ protected function calculateAverageRating(Player $player, int $cut = 0) @@ -145,6 +147,29 @@ abstract class Tiebreaks extends Tournament return round(array_sum($allratings) / count($allratings)); } + + /** + * @param Player $player + * @param int $cut + * @return float + */ + protected function calculateAveragePerformance(Player $player, int $cut = 0) + { + $pairings = $player->getPairings(); + $allratings = []; + foreach ($pairings as $pairing) { + if (array_search($pairing->getResult(), Constants::NotPlayed) === false) { + $toadd = $pairing->getOpponent()->getPerformance(); + if ($toadd != 0) { + $allratings[] = $toadd; + } + } + } + sort($allratings); + $allratings = array_slice($allratings, $cut); + return round(array_sum($allratings) / count($allratings)); + } + /** * @param Player $player * @param int $cut diff --git a/src/Tournament.php b/src/Tournament.php index 9841ec8..9d45bf7 100644 --- a/src/Tournament.php +++ b/src/Tournament.php @@ -317,6 +317,9 @@ class Tournament extends Tiebreaks case Tiebreak::Cumulative: return $this->calculateCumulative($player); break; + case Tiebreak::Performance: + return $this->calculateAveragePerformance($player); + break; default: return null; }