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

129 lines
2.9 KiB
PHP
Raw Normal View History

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');
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @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++;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @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();
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @param int $tid
* @param bool $shuffle - <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> true <EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @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;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @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]
];
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @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);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> id <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD> id <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>.
*
* @param int $uid
*
* @return mixed
*/
public static function getTournamentIdByUserId(int $uid): int
{
return Db::getValue('select tid from tournaments_users where uid = ?', [$uid]);
}
}