2022-12-20 01:57:09 +00:00
|
|
|
<?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');
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
* Считаем сколько игроков ждут в заявке на турнир.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @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++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
* Игрок присоединяется к турниру и телепортируется в турнирную комнату.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
* Выбираем живых бойцов не сражающихся в данный момент.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @param int $tid
|
2023-01-10 16:29:32 +00:00
|
|
|
* @param bool $shuffle - установить true если нужно перетасовать массив.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
* Выбираем победителей.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @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]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
* Выбывший из турнира покидает комнату и получает время смерти.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @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);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
* Узнаём id турнира по id игрока.
|
2022-12-20 01:57:09 +00:00
|
|
|
*
|
|
|
|
* @param int $uid
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public static function getTournamentIdByUserId(int $uid): int
|
|
|
|
{
|
|
|
|
return Db::getValue('select tid from tournaments_users where uid = ?', [$uid]);
|
|
|
|
}
|
|
|
|
}
|