115 lines
3.2 KiB
PHP
115 lines
3.2 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]);
|
|
}
|
|
}
|