game/_incl_data/class/Insallah/Tournaments/Model/Tournament.php
2022-12-20 03:57:09 +02:00

115 lines
3.0 KiB
PHP

<?php
namespace Insallah\Tournaments\Model;
use Insallah\Db;
class Tournament
{
private array $t;
public function __construct()
{
$this->t = Db::getRows('select * from tournaments') ?? [];
}
public function getAllStarted(): array
{
$tidList = [];
foreach ($this->t as $row) {
if ($row['start_time'] === -1) {
$tidList[] = $row['tid'];
}
}
return $tidList;
}
public function isStarted(int $tid): bool
{
foreach ($this->t as $row) {
if ($row['start_time'] === -1 && $row['tid'] === $tid) {
return true;
}
}
return false;
}
public function getList(): string
{
$list = '';
$tournamentMembersId = new User();
foreach ($this->t as $row) {
$time = $row['start_time'] === -1 ? 'Òóðíèð óæå íà÷àëñÿ!' : date('G:i', $row['start_time']);
$members = [];
foreach ($tournamentMembersId->getAlive($row['tid']) as $member) {
$members[] = (new GameConnector())->setUser($member)->uidToLogin();
}
$list .= sprintf(
"<li>Òóðíèð äëÿ %d óðîâíåé.<br>Âðåìÿ ïîäà÷è çàÿâêè: %s<br>Ó÷àñòíèêè: %s</li>",
$row['tid'],
$time,
implode(', ', $members)
);
}
return $list ? "<div><strong>Àêòèâíûå òóðíèðû.</strong><br><ul>$list</ul></div>" : '';
}
public function getOne(int $tid): string
{
$str = '';
$tournamentMembersId = new User();
foreach ($this->t as $row) {
if ($this->t['tid'] === $tid) {
$time = $row['start_time'] === -1 ? 'Òóðíèð óæå íà÷àëñÿ!' : date('G:i', $row['start_time']);
$members = [];
foreach ($tournamentMembersId->getAlive($row['tid']) as $member) {
$members[] = (new GameConnector())->setUser($member)->uidToLogin();
}
$str = sprintf(
"<div>Òóðíèð äëÿ %d óðîâíåé.<br>Âðåìÿ ïîäà÷è çàÿâêè: %s<br>Ó÷àñòíèêè: %s</div>",
$row['tid'],
$time,
implode(', ', $members)
);
}
}
return $str;
}
/**
* Ñîçäàíèå íîâîãî òóðíèðà.
*
* @param int $tid
*
* @return void
*/
public static function create(int $tid)
{
Db::sql('insert into tournaments (tid) values (?)', [$tid]);
}
/**
* Ñòàðò òóðíèðà.
*
* @param int $tid
*
* @return void
*/
public static function start(int $tid)
{
Db::sql('update tournaments set start_time = -1 where tid = ?', [$tid]);
}
/**
* ×èñòèì áàçû îò ïðîøåäøåãî òóðíèðà.
*
* @param int $tid
*
* @return void
*/
public static function destroy(int $tid)
{
Db::sql('delete from tournaments where tid = ?', [$tid]);
}
}