libpairtwo/src/Game.php

126 lines
2.9 KiB
PHP
Raw Normal View History

<?php
2020-08-02 21:51:59 +02:00
/**
* Class Games
*
* Class for a game of the tournament
*
2020-08-02 21:51:59 +02:00
* @author Jeroen De Meerleer <schaak@jeroened.be>
* @category Main
* @package Libpairtwo
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
*/
namespace JeroenED\Libpairtwo;
2019-03-20 12:46:46 +01:00
use JeroenED\Libpairtwo\Enums\Gameresult;
use JeroenED\Libpairtwo\Pairing;
2019-09-28 10:33:59 +02:00
use DateTime;
/**
* Class Games
*
* Class for a game of the tournament
*
2020-08-02 21:51:59 +02:00
* @author Jeroen De Meerleer <schaak@jeroened.be>
* @category Main
* @package Libpairtwo
* @copyright Copyright (c) 2018-2019 Jeroen De Meerleer <schaak@jeroened.be>
*/
class Game
{
2019-11-17 01:20:15 +01:00
/**
* The pairing for this games as seen from white's side
*
* @var Pairing | null
*/
public $White;
2019-11-17 01:20:15 +01:00
/**
2020-08-02 21:51:59 +02:00
* The pairing for this games as seen from black's side
2019-11-17 01:20:15 +01:00
*
* @var Pairing | null
*/
public $Black;
2019-11-17 01:20:15 +01:00
/**
* The calculated game result
*
* @var GameResult | null
*/
private $CalculatedResult;
2019-11-17 01:20:15 +01:00
/**
* The board where this game is held
*
* @var int
*/
public $Board;
2019-11-17 01:20:15 +01:00
/**
* Returns fields that were not directly assigned.
* Class Game contains the special field Result containing the result of the game
2020-08-02 21:51:59 +02:00
*
* @param string $key
2019-11-17 01:20:15 +01:00
* @return Gameresult
*/
public function __get(string $key)
{
if ($key == 'Result') {
return $this->calculateResult();
}
2019-11-17 01:20:15 +01:00
return null;
}
2019-03-20 12:46:46 +01:00
/**
2019-11-17 01:20:15 +01:00
* Returns the result for the game.
* This method needs to be called from $Game->Result
*
* @return Gameresult
2019-03-20 12:46:46 +01:00
*/
private function calculateResult(): Gameresult
2019-03-20 12:46:46 +01:00
{
if (!is_null($this->CalculatedResult)) {
return $this->CalculatedResult;
2019-03-20 12:46:46 +01:00
}
$whiteResult = $this->White->Result;
$blackResult = $this->Black->Result;
2019-03-20 12:46:46 +01:00
$whitesplit = explode(" ", $whiteResult);
$blacksplit = explode(" ", $blackResult);
2020-08-02 21:51:59 +02:00
$special = '';
2019-03-20 12:46:46 +01:00
if (isset($whitesplit[1]) && $whitesplit[1] != 'Bye') {
$special = ' ' . $whitesplit[1];
}
if (isset($blacksplit[1]) && $blacksplit[1] != 'Bye') {
$special = ' ' . $blacksplit[1];
}
2019-05-28 10:07:40 +02:00
if ($whitesplit[0] == '*') {
$whitesplit[0] = '';
}
if ($blacksplit[0] == '*') {
$blacksplit[0] = '';
}
2019-03-20 17:33:09 +01:00
$result = new Gameresult($whitesplit[0] . '-' . $blacksplit[0] . $special);
$this->CalculatedResult = $result;
2019-03-20 12:46:46 +01:00
2019-03-20 17:33:09 +01:00
return $result;
2019-03-20 12:46:46 +01:00
}
2019-11-10 09:37:13 +01:00
/**
* Checks if 2 games are equal
*
2020-08-02 21:51:59 +02:00
* @param Game $game
2019-11-10 09:37:13 +01:00
* @return bool
*/
public function equals(Game $game): bool
{
return (
$this->White->Player === $game->White->Player &&
$this->Black->Player === $game->Black->Player &&
2020-08-02 21:51:59 +02:00
$this->Result->getKey() == $game->Result->getKey());
2019-11-10 09:37:13 +01:00
}
2019-02-11 17:37:30 +01:00
}