129 lines
2.9 KiB
PHP
129 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace Insallah\Tournaments\Model;
|
|
|
|
use Insallah\Db;
|
|
|
|
class User
|
|
{
|
|
private array $u;
|
|
private int $waitingUsers = 0;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->u = Db::getRows('select * from tournaments_users');
|
|
}
|
|
|
|
|
|
/**
|
|
* Ñ÷èòàåì ñêîëüêî èãðîêîâ æäóò â çàÿâêå íà òóðíèð.
|
|
*
|
|
* @param int $tid
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getWaitingQuantity(int $tid): int
|
|
{
|
|
foreach ($this->u as $row) {
|
|
if ($row['tid'] === $tid) {
|
|
$this->addWaitingUser();
|
|
}
|
|
}
|
|
return $this->waitingUsers;
|
|
}
|
|
|
|
public function addWaitingUser()
|
|
{
|
|
$this->waitingUsers++;
|
|
}
|
|
|
|
/**
|
|
* Èãðîê ïðèñîåäèíÿåòñÿ ê òóðíèðó è òåëåïîðòèðóåòñÿ â òóðíèðíóþ êîìíàòó.
|
|
*
|
|
* @param int $uid
|
|
* @param int $tid
|
|
*
|
|
* @return void
|
|
*/
|
|
public function joinTournament(int $uid, int $tid)
|
|
{
|
|
Db::sql('insert into tournaments_users (tid, uid) values (?, ?)', [$tid, $uid]);
|
|
(new GameConnector())->setUser($uid)->teleport();
|
|
}
|
|
|
|
/**
|
|
* Âûáèðàåì æèâûõ áîéöîâ íå ñðàæàþùèõñÿ â äàííûé ìîìåíò.
|
|
*
|
|
* @param int $tid
|
|
* @param bool $shuffle - óñòàíîâèòü true åñëè íóæíî ïåðåòàñîâàòü ìàññèâ.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getAlive(int $tid, bool $shuffle = false): array
|
|
{
|
|
$arr = [];
|
|
foreach ($this->u as $row) {
|
|
if ($row['tid'] === $tid && $row['death_time'] === 0) {
|
|
$arr[] = $row['uid'];
|
|
}
|
|
}
|
|
if ($shuffle) {
|
|
shuffle($arr);
|
|
}
|
|
return $arr;
|
|
}
|
|
|
|
/**
|
|
* Âûáèðàåì ïîáåäèòåëåé.
|
|
*
|
|
* @param int $tid
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getWinners(int $tid): array
|
|
{
|
|
$users = [];
|
|
$dt = [];
|
|
foreach ($this->u as $item) {
|
|
if ($item['tid'] === $tid) {
|
|
$users[]['uid'] = $item['uid'];
|
|
$users[]['death_time'] = $item['death_time'];
|
|
}
|
|
}
|
|
foreach ($users as $k => $v) {
|
|
$dt[$k] = $v['death_time'];
|
|
}
|
|
array_multisort($dt, SORT_DESC, $users);
|
|
return [
|
|
1 => $users[0],
|
|
2 => $users[1],
|
|
3 => $users[2]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Âûáûâøèé èç òóðíèðà ïîêèäàåò êîìíàòó è ïîëó÷àåò âðåìÿ ñìåðòè.
|
|
*
|
|
* @param int $uid
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function remove(int $uid)
|
|
{
|
|
Db::sql('update tournaments_users set death_time = unix_timestamp() where death_time = 0 and uid = ?', [$uid]);
|
|
(new GameConnector())->setUser($uid)->teleport(true);
|
|
}
|
|
|
|
/**
|
|
* Óçíà¸ì id òóðíèðà ïî id èãðîêà.
|
|
*
|
|
* @param int $uid
|
|
*
|
|
* @return mixed
|
|
*/
|
|
public static function getTournamentIdByUserId(int $uid): int
|
|
{
|
|
return Db::getValue('select tid from tournaments_users where uid = ?', [$uid]);
|
|
}
|
|
}
|