Implemented first tiebreaks

This commit is contained in:
Jeroen De Meerleer 2019-05-28 16:26:03 +02:00
parent 3d1af6b3ae
commit a5f620d3f1
9 changed files with 91 additions and 20 deletions

View File

@ -15,5 +15,5 @@ class TournamentSystem extends Enum
const Swiss = 'Swiss'; const Swiss = 'Swiss';
const Closed = 'Closed'; const Closed = 'Closed';
const American = 'American'; const American = 'American';
const Imperial = 'Imperial'; const Keizer = 'Keizer';
} }

View File

@ -7,6 +7,6 @@ use JeroenED\Libpairtwo\Tournament;
interface ReaderInterface interface ReaderInterface
{ {
public function read($filename); public function read($filename): ReaderInterface;
public function getTournament(): Tournament; public function getTournament(): Tournament;
} }

View File

@ -75,6 +75,4 @@ abstract class Game
$this->result = $result; $this->result = $result;
return $this; return $this;
} }
} }

View File

@ -28,7 +28,7 @@ abstract class Player
private $DateOfBirth; private $DateOfBirth;
/** @var float[] */ /** @var float[] */
private $Tiebreaks; private $Tiebreaks = [];
/** @var string */ /** @var string */
private $Nation; private $Nation;

View File

@ -520,5 +520,4 @@ abstract class Tournament
$this->Tiebreaks = $Tiebreaks; $this->Tiebreaks = $Tiebreaks;
return $this; return $this;
} }
} }

View File

@ -72,6 +72,4 @@ abstract class Pairtwo6
$this->BinaryData[$Key] = $Value; $this->BinaryData[$Key] = $Value;
return $this; return $this;
} }
} }

View File

@ -60,7 +60,7 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface
* @param string $filename * @param string $filename
* @return Pairtwo6 * @return Pairtwo6
*/ */
public function read($filename) public function read($filename): ReaderInterface
{ {
$swshandle = fopen($filename, 'rb'); $swshandle = fopen($filename, 'rb');
$swscontents = fread($swshandle, filesize($filename)); $swscontents = fread($swshandle, filesize($filename));
@ -556,7 +556,7 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface
$system = TournamentSystem::American; $system = TournamentSystem::American;
break; break;
case 6: case 6:
$system = TournamentSystem::Imperial; $system = TournamentSystem::Keizer;
break; break;
case 0: case 0:
default: default:
@ -694,6 +694,8 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface
} }
} }
$this->addTiebreaks();
$this->getTournament()->pairingsToRounds(); $this->getTournament()->pairingsToRounds();
return $this; return $this;
} }
@ -799,4 +801,27 @@ class Pairtwo6 extends Pairtwo6Model implements ReaderInterface
return DateTime::createFromFormat($format, $concat); return DateTime::createFromFormat($format, $concat);
} }
/**
* @return $this
*/
private function addTiebreaks(): Pairtwo6
{
switch ($this->getTournament()->getSystem()) {
case TournamentSystem::Keizer:
$firstElement = new Tiebreak(Tiebreak::Keizer);
break;
case TournamentSystem::American:
$firstElement = new Tiebreak(Tiebreak::American);
break;
case TournamentSystem::Closed:
case TournamentSystem::Swiss:
$firstElement = new Tiebreak(Tiebreak::Points);
}
$tiebreaks = $this->getTournament()->getTiebreaks();
array_unshift($tiebreaks, $firstElement);
$this->getTournament()->setTiebreaks($tiebreaks);
return $this;
}
} }

View File

@ -203,14 +203,14 @@ class Tournament extends TournamentModel
/** /**
* Gets the ranking of the tournament * Gets the ranking of the tournament
* *
* @param bool $americansort
* @return Player[] * @return Player[]
*/ */
public function getRanking(bool $americansort = false) public function getRanking()
{ {
$this->calculateTiebreaks();
$players = $this->getPlayers(); $players = $this->getPlayers();
$americansort ? usort($players, array($this, "SortAmerican")) : usort($players, array($this, "SortNormal")); usort($players, array($this, "SortTiebreak"));
return $players; return $players;
} }
@ -220,18 +220,66 @@ class Tournament extends TournamentModel
* @param Player $b * @param Player $b
* @return int * @return int
*/ */
private function sortNormal(Player $a, Player $b) private function sortTiebreak(Player $a, Player $b)
{ {
return $b->getPoints() - $a->getPoints(); return $b->getTiebreaks()[0] - $a->getTiebreaks()[0];
}
/**
* @return Tournament
*/
private function calculateTiebreaks(): Tournament
{
foreach ($this->getTiebreaks() as $key=>$tiebreak) {
switch ($tiebreak) {
case Tiebreak::Keizer:
$this->calculateKeizer($key);
break;
case Tiebreak::American:
$this->calculateAmerican($key);
break;
case Tiebreak::Points:
$this->calculatePoints($key);
break;
}
}
return $this;
} }
/** /**
* @param Player $a * @param int $key
* @param Player $b
* @return int
*/ */
private function sortAmerican(Player $a, Player $b) private function calculateKeizer(int $key)
{ {
return $b->getScoreAmerican() - $a->getScoreAmerican(); foreach ($this->getPlayers() as $player) {
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican');
$player->setTiebreaks($currentTiebreaks);
}
}
/**
* @param int $key
*/
private function calculateAmerican(int $key)
{
foreach ($this->getPlayers() as $player) {
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('ScoreAmerican');
$player->setTiebreaks($currentTiebreaks);
}
}
/**
* @param int $key
*/
private function calculatePoints(int $key)
{
foreach ($this->getPlayers() as $player) {
$currentTiebreaks = $player->getTiebreaks();
$currentTiebreaks[$key] = $player->getBinaryData('Points');
$player->setTiebreaks($currentTiebreaks);
}
} }
} }

View File

@ -73,3 +73,6 @@ echo "Player Pairing 3: " . $sws->getTournament()->getPairings()[2]->getPlayer()
echo "Tiebreak 1: " . $sws->getTournament()->getTiebreaks()[0]->getValue() . PHP_EOL; echo "Tiebreak 1: " . $sws->getTournament()->getTiebreaks()[0]->getValue() . PHP_EOL;
echo "Tiebreak 2: " . $sws->getTournament()->getTiebreaks()[1]->getValue() . PHP_EOL; echo "Tiebreak 2: " . $sws->getTournament()->getTiebreaks()[1]->getValue() . PHP_EOL;
echo "Tiebreak 3: " . $sws->getTournament()->getTiebreaks()[2]->getValue() . PHP_EOL; echo "Tiebreak 3: " . $sws->getTournament()->getTiebreaks()[2]->getValue() . PHP_EOL;
foreach ($sws->getTournament()->getRanking() as $player) {
echo $player->getName() . ' ' . $player->getTiebreaks()[0] . PHP_EOL;
}