game/_incl_data/class/Insallah/Tournaments/Model/Tournament.php

115 lines
3.2 KiB
PHP
Raw Normal View History

2022-12-20 01:57:09 +00:00
<?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) {
2023-01-10 16:29:32 +00:00
$time = $row['start_time'] === -1 ? 'Турнир уже начался!' : date('G:i', $row['start_time']);
2022-12-20 01:57:09 +00:00
$members = [];
foreach ($tournamentMembersId->getAlive($row['tid']) as $member) {
$members[] = (new GameConnector())->setUser($member)->uidToLogin();
}
$list .= sprintf(
2023-01-10 16:29:32 +00:00
"<li>Турнир для %d уровней.<br>Время подачи заявки: %s<br>Участники: %s</li>",
2022-12-20 01:57:09 +00:00
$row['tid'],
$time,
implode(', ', $members)
);
}
2023-01-10 16:29:32 +00:00
return $list ? "<div><strong>Активные турниры.</strong><br><ul>$list</ul></div>" : '';
2022-12-20 01:57:09 +00:00
}
public function getOne(int $tid): string
{
$str = '';
$tournamentMembersId = new User();
foreach ($this->t as $row) {
if ($this->t['tid'] === $tid) {
2023-01-10 16:29:32 +00:00
$time = $row['start_time'] === -1 ? 'Турнир уже начался!' : date('G:i', $row['start_time']);
2022-12-20 01:57:09 +00:00
$members = [];
foreach ($tournamentMembersId->getAlive($row['tid']) as $member) {
$members[] = (new GameConnector())->setUser($member)->uidToLogin();
}
$str = sprintf(
2023-01-10 16:29:32 +00:00
"<div>Турнир для %d уровней.<br>Время подачи заявки: %s<br>Участники: %s</div>",
2022-12-20 01:57:09 +00:00
$row['tid'],
$time,
implode(', ', $members)
);
}
}
return $str;
}
/**
2023-01-10 16:29:32 +00:00
* Создание нового турнира.
2022-12-20 01:57:09 +00:00
*
* @param int $tid
*
* @return void
*/
public static function create(int $tid)
{
Db::sql('insert into tournaments (tid) values (?)', [$tid]);
}
/**
2023-01-10 16:29:32 +00:00
* Старт турнира.
2022-12-20 01:57:09 +00:00
*
* @param int $tid
*
* @return void
*/
public static function start(int $tid)
{
Db::sql('update tournaments set start_time = -1 where tid = ?', [$tid]);
}
/**
2023-01-10 16:29:32 +00:00
* Чистим базы от прошедшего турнира.
2022-12-20 01:57:09 +00:00
*
* @param int $tid
*
* @return void
*/
public static function destroy(int $tid)
{
Db::sql('delete from tournaments where tid = ?', [$tid]);
}
}