libpairtwo/src/Tournament.php

207 lines
5.1 KiB
PHP
Raw Normal View History

2019-02-01 15:53:39 +01:00
<?php
/**
* Created by PhpStorm.
* User: jeroen
* Date: 1/02/19
* Time: 11:18
*/
namespace JeroenED\Libpairtwo;
use JeroenED\Libpairtwo\Enums\Result;
2019-02-01 15:53:39 +01:00
use JeroenED\Libpairtwo\Models\Tournament as TournamentModel;
use JeroenED\Libpairtwo\Player;
use JeroenED\Libpairtwo\Enums\Color;
use JeroenED\Libpairtwo\Enums\Gameresult;
2019-02-01 15:53:39 +01:00
class Tournament extends TournamentModel
{
/**
2019-02-11 17:54:50 +01:00
* @param integer $id
2019-02-01 15:53:39 +01:00
* @return Player
*/
2019-02-11 17:54:50 +01:00
public function getPlayerById(int $id)
2019-02-01 15:53:39 +01:00
{
return $this->GetPlayers()[$id];
}
/**
* @param Player $Player
*/
public function addPlayer(Player $Player)
{
$newArray = $this->GetPlayers();
$newArray[] = $Player;
$this->setPlayers($newArray);
}
2019-02-01 17:02:33 +01:00
/**
2019-02-11 17:54:50 +01:00
* @param int $id
2019-02-01 17:02:33 +01:00
* @param Player $player
*/
2019-02-11 17:54:50 +01:00
public function updatePlayer(int $id, Player $player)
2019-02-01 17:02:33 +01:00
{
$newArray = $this->GetPlayers();
$newArray[$id] = $player;
$this->setPlayers($newArray);
}
/**
* @param Round $round
*/
public function addRound(Round $round)
{
$newArray = $this->getRounds();
$newArray[$round->getRoundNo()] = $round;
$this->setRounds($newArray);
}
/**
* @param int $roundNo
* @return Round
*/
public function getRoundByNo(int $roundNo): Round
{
return $this->getRounds()[$roundNo];
}
2019-02-11 16:43:36 +01:00
/**
* @param Pairing $pairing
*/
public function addPairing(Pairing $pairing)
{
$newArray = $this->GetPairings();
$newArray[] = $pairing;
$this->setPairings($newArray);
}
/**
* This function converts the array of pairings into rounds
*/
public function pairingsToRounds(): void
{
$pairings = $this->getPairings();
2019-03-20 12:46:46 +01:00
/** @var Pairing[] */
$cache = array();
foreach ($pairings as $pairing) {
$round = $pairing->getRound();
$color = $pairing->getColor();
2019-03-20 12:46:46 +01:00
$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) {
2019-03-20 12:46:46 +01:00
$game->setWhite($pairing);
$game->setBlack($opponent);
} elseif ($color->getValue() == Color::black) {
2019-03-20 12:46:46 +01:00
$game->setWhite($opponent);
$game->setBlack($pairing);
}
2019-03-20 12:46:46 +01:00
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();
2019-03-20 12:46:46 +01:00
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;
}
/**
* @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);
}
2019-02-06 17:24:10 +01:00
/**
2019-02-11 17:54:50 +01:00
* @param bool $americansort
* @return Player[]
2019-02-06 17:24:10 +01:00
*/
2019-02-06 20:34:09 +01:00
public function getRanking(bool $americansort = false)
2019-02-06 17:24:10 +01:00
{
$players = $this->getPlayers();
2019-02-06 20:34:09 +01:00
$americansort ? usort($players, array($this, "SortAmerican")) : usort($players, array($this, "SortNormal"));
2019-02-06 17:24:10 +01:00
2019-02-06 18:22:25 +01:00
return $players;
}
2019-02-06 20:10:52 +01:00
/**
2019-02-11 17:54:50 +01:00
* @param Player $a
* @param Player $b
* @return int
2019-02-06 20:10:52 +01:00
*/
2019-02-11 17:54:50 +01:00
private function sortNormal(Player $a, Player $b)
2019-02-06 18:22:25 +01:00
{
return $b->getPoints() - $a->getPoints();
2019-02-06 17:24:10 +01:00
}
2019-02-06 20:10:52 +01:00
/**
2019-02-11 17:54:50 +01:00
* @param Player $a
* @param Player $b
* @return int
2019-02-06 20:10:52 +01:00
*/
2019-02-11 17:54:50 +01:00
private function sortAmerican(Player $a, Player $b)
2019-02-06 20:10:52 +01:00
{
return $b->getScoreAmerican() - $a->getScoreAmerican();
}
2019-02-01 15:53:39 +01:00
}