libpairtwo/src/Tournament.php

261 lines
6.6 KiB
PHP

<?php
/**
* Created by PhpStorm.
* User: jeroen
* Date: 1/02/19
* Time: 11:18
*/
namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Tiebreak;
use JeroenED\Libpairtwo\Enums\Color;
class Tournament extends Tiebreaks
{
/**
* Gets a player by its ID
*
* @param integer $id
* @return Player
*/
public function getPlayerById(int $id)
{
return $this->GetPlayers()[$id];
}
/**
* Adds a player
*
* @param Player $Player
*/
public function addPlayer(Player $Player)
{
$newArray = $this->GetPlayers();
$newArray[] = $Player;
$this->setPlayers($newArray);
}
/**
* Updates player on id to the given Player object
*
* @param int $id
* @param Player $player
*/
public function updatePlayer(int $id, Player $player)
{
$newArray = $this->GetPlayers();
$newArray[$id] = $player;
$this->setPlayers($newArray);
}
/**
* Adds a Tiebreak
*
* @param Tiebreak $tiebreak
*/
public function addTiebreak(Tiebreak $tiebreak)
{
$newArray = $this->getTiebreaks();
$newArray[] = $tiebreak;
$this->setTiebreaks($newArray);
}
/**
* Adds a round with given Round object
*
* @param Round $round
*/
public function addRound(Round $round)
{
$newArray = $this->getRounds();
$newArray[$round->getRoundNo()] = $round;
$this->setRounds($newArray);
}
/**
* Gets a round by its number.
*
* @param int $roundNo
* @return Round
*/
public function getRoundByNo(int $roundNo): Round
{
return $this->getRounds()[$roundNo];
}
/**
* Adds a pairing to the tournament
*
* @param Pairing $pairing
*/
public function addPairing(Pairing $pairing)
{
$newArray = $this->GetPairings();
$newArray[] = $pairing;
$this->setPairings($newArray);
}
/**
* Converts pairings into games with a black and white player
*/
public function pairingsToRounds(): void
{
/** @var Pairing[] $pairings */
$pairings = $this->getPairings();
/** @var Pairing[] */
$cache = array();
foreach ($pairings as $pairing) {
// Add pairing to player
$pairing->getPlayer()->addPairing($pairing);
$round = $pairing->getRound();
$color = $pairing->getColor();
$this->getRoundByNo($round)->addPairing($pairing);
$opponent = null;
foreach ($cache as $key=>$cached) {
if (!is_null($cached)) {
if (($cached->getOpponent() == $pairing->getPlayer()) && ($cached->getRound() == $pairing->getRound())) {
$opponent = $cached;
$cache[$key] = null;
break;
}
}
}
$game = new Game();
if ($color->getValue() == Color::white) {
$game->setWhite($pairing);
$game->setBlack($opponent);
} elseif ($color->getValue() == Color::black) {
$game->setWhite($opponent);
$game->setBlack($pairing);
}
if (is_null($game->getWhite()) || is_null($game->getBlack())) {
$cache[] = $pairing;
} else {
// Check if game already exists
if (!$this->GameExists($game, $round)) {
$this->AddGame($game, $round);
}
}
}
}
/**
* 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->getNoOfRounds(); $i++) {
$search[] = $i;
}
}
foreach ($search as $round) {
if (!isset($this->getRounds()[$round])) {
return false;
}
$games = $this->getRounds()[$round]->getGames();
if (is_null($games)) {
return false;
}
foreach ($games as $roundgame) {
if ($roundgame->getWhite() == $game->getWhite() &&
$roundgame->getBlack() == $game->getBlack() &&
$roundgame->getResult() == $game->getResult()
) {
return true;
}
}
}
return false;
}
/**
* Adds a game to the tournament
*
* @param Game $game
* @param int $round
*/
public function addGame(Game $game, int $round)
{
if (!isset($this->getRounds()[$round])) {
$roundObj = new Round();
$roundObj->setRoundNo($round);
$this->addRound($roundObj);
}
$this->getRoundByNo($round)->addGame($game);
}
/**
* Gets the ranking of the tournament
*
* @return Player[]
*/
public function getRanking()
{
$players = $this->getPlayers();
usort($players, array($this, "SortTiebreak"));
return $players;
}
/**
* @param Player $a
* @param Player $b
* @return int
*/
private function sortTiebreak(Player $a, Player $b)
{
$result = 0;
foreach ($this->getTiebreaks() as $key=>$tiebreak) {
$result = $this->CalculateTiebreak($key, $b, $a) - $this->CalculateTiebreak($key, $a, $b);
if ($result != 0) {
return $result;
}
}
}
/**
* @return float
*/
private function calculateTiebreak(int $key, Player $player, Player $opponent): float
{
$tiebreak = $this->getTiebreaks()[$key];
switch ($tiebreak) {
case Tiebreak::Keizer:
return $this->calculateKeizer($key, $player);
break;
case Tiebreak::American:
return $this->calculateAmerican($key, $player);
break;
case Tiebreak::Points:
return $this->calculatePoints($key, $player);
break;
case Tiebreak::Baumbach:
return $this->calculateBaumbach($key, $player);
break;
case Tiebreak::BlackPlayed:
return $this->calculateBlackPlayed($key, $player);
break;
case Tiebreak::BlackWin:
return $this->calculateBlackWin($key, $player);
break;
}
}
}