tournaments 2.0

This commit is contained in:
2022-12-20 03:57:09 +02:00
parent cf51f4d996
commit 7a5dfd22a7
13 changed files with 631 additions and 685 deletions
@@ -0,0 +1,173 @@
<?php
namespace Insallah\Tournaments\Model;
use Insallah\Db;
use Insallah\Tournaments\Config;
class GameConnector
{
private array $u;
public function setUser(int $uid): GameConnector
{
$this->u = Db::getRow('select id, `level`, login, battle from users where id = ?', [$uid]);
return $this;
}
public function getAllowedLevel(): int
{
return $this->u['level'] >= Config::MINIMUM_USER_LEVEL && $this->u['battle'] === 0 ? $this->u['level'] : 0;
}
/**
* @return bool
*/
public function isEkrOverpriced(): bool
{
return Db::getValue('select sum(2price) from items_users where inOdet > 0 and uid = ?', [$this->u['id']])
> Config::ekrOverpriceFormula($this->u['level']);
}
/**
* @return bool
*/
public function isEnoughExperience(): bool
{
return Db::getValue('select exp from stats where id = ?', [$this->u['id']]) >= Config::MIN_EXP;
}
/**
* @return bool
*/
public function isRestrictedToJoin(): bool
{
return Db::getValue('select count(*) from eff_users where uid = ? and id_eff = ? and `delete` = 0', [
$this->u['id'], Config::DELAY_EFFECT_ID,]
) > 0;
}
/**
* Óçíà¸ì ëîãèí ïåðñîíàæà ïî åãî id.
*
* @return mixed
*/
public function uidToLogin()
{
return $this->u['login'];
}
/**
* Òåëåïîðò ïî êîìíàòàì.
*
* @param bool $out - îáðàòíûé òåëåïîðò.
*
* @return void
*/
public function teleport(bool $out = false)
{
$roomId = $out ? Config::RETURN_ROOM : Config::CUSTOM_BATTLE_ID + $this->u['level'];
Db::sql('update users set room = ? where id = ?', [$roomId, $this->u['id']]);
}
/**
* Ïîëó÷àåì ñïèñîê áîéöîâ è áü¸ì èõ íà ïàðû, ìåæäó êîòîðûìè ñòàðòóþò ïîåäèíêè 1õ1.
* noinc - çàïðåò íà âìåøàòåëüñòâî
* invis - íåâèäèìûé áîé
*
* @param array $fightersList
*
* @return void
*/
public static function startBattle(array $fightersList)
{
$teamsq = sprintf("select id from users where battle = 0 and id in (%s)", implode(', ', $fightersList));
$teams = array_chunk(Db::getColumn($teamsq), 2);
foreach ($teams as $team) {
if (count($team) !== 2) {
continue;
}
Db::exec(
'insert into battle (city, time_start, timeout, type, invis, noinc, travmChance, typeBattle)
values (\'capitalcity\', unix_timestamp(), 60, 0, 1, 1, 0, ' . Config::CUSTOM_BATTLE_ID . ')'
);
$battleId = Db::lastInsertId();
Db::sql('update stats set team = 1, zv = 0, hpNow = hpAll, mpNow = mpAll where id = ?', [$team[0]]);
Db::sql('update stats set team = 2, zv = 0, hpNow = hpAll, mpNow = mpAll where id = ?', [$team[1]]);
$query = sprintf('update users set battle = %d where id in (%d, %d)', $battleId, $team[0], $team[1]);
Db::exec($query);
}
}
/**
* Ãåíåðèðóåò ìíîæåñòâåííûé çàïðîñ ñðàçó íà $quantity îäíîòèïíûõ ïðåäìåòîâ â èíâåíòàðü ïîëüçîâàòåëÿ $uid.
*
* @param int $quantity
*
* @return void
*/
public function givePrizeItems(int $quantity)
{
$query = 'insert into items_users (item_id, uid, `data`, iznosMAX, lastUPD, time_create)
values (' . Config::PRIZE_ITEM_ID . ', :uid, :data, 1, unix_timestamp(), unix_timestamp())';
$args = [
'uid' => $this->u['id'],
'data' => 'nosale=1|musor=1|sudba=' . $this->uidToLogin() . '|lvl=8|tr_s1=0|tr_s2=0|tr_s3=0|tr_s4=0',
];
$stmt = Db::prepare($query);
for ($i = 0; $i < $quantity; $i++) {
$stmt->execute($args);
}
}
/** Ýôôåêò-îãðàíè÷èòåëü íà ó÷àñòèå â òóðíèðå.
*
* @param int $unixtime
*
* @return void
*/
public function giveDelay(int $unixtime)
{
$query = 'insert into eff_users (id_eff, uid, `name`, timeUse) select id2, ?, mname, ? from eff_main where id2 = ?';
$args = [Config::DELAY_EFFECT_ID, $this->u['id'], strtotime($unixtime)];
Db::sql($query, $args);
}
/**
* Ïðîáóåì âûêóñèòü ïðîèãðàâøåãî â ïîñëåäíåé òóðíèðíîé áèòâå è óäàëèòü ýòó ñàìóþ áèòâó âî èçáåæàíèå.
* @return mixed
*/
public static function getLooser(): int
{
$query = '
select uid
from
battle_users,
(select id, team_win
from battle
where
team_win > 0 and
typeBattle = ' . Config::CUSTOM_BATTLE_ID . '
order by time_over desc
limit 1) as last_battle
where
battle_users.battle = last_battle.id and
battle_users.team != last_battle.team_win';
return Db::getValue($query);
}
/**
* Íåò ïðîâåðîê $message ïîòîìó ÷òî îíî âñåãäà çàäà¸òñÿ â êîäå è èãðîê íà íåãî íå âëèÿåò.
*
* @param string $message
*
* @return void
*/
public static function sysMessage(string $message)
{
if (empty($message)) {
return;
}
Db::sql('insert into chat (`time`, type, `text`, new, da) values (unix_timestamp(), 6, ?, 1, 1)', [$message]);
}
}
@@ -0,0 +1,114 @@
<?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]);
}
}
@@ -0,0 +1,128 @@
<?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]);
}
}