game/_incl_data/class/Insallah/Tournament/TournamentModel.php
2022-12-30 21:03:50 +02:00

359 lines
10 KiB
PHP

<?php
namespace Insallah;
class TournamentModel
{
/**
* ïðîâåðêà óðîâíÿ, ñòîèìîñòè ýêâèïà, ïðî÷èå ïðîâåðêè, ÷òî ïåðñîíàæ ñâîáîäåí
* òàéìåð îæèäàíèÿ 30 ìèíóò
*
* @param int $uid
*
* @return int
*/
public static function getUserLevel(int $uid): int
{
$db = new Db();
$level = $db::getValue('select level from users where id = ? and level between 8 and 12 and battle = 0', [$uid]);
return $level ?: 0;
}
/**
* @param int $uid
* @param int|null $level
*
* @return bool
*/
public static function isEkrOverpriced(int $uid, ?int $level = null): bool
{
$db = new Db();
if (is_null($level)) {
$level = $db::getValue('select level from users where id = ?', [$uid]);
}
$wearedItemsEkrPrice = $db::getValue('select sum(2price) from items_users where inOdet > 0 and uid = ?', [$uid]);
return $wearedItemsEkrPrice > Tournament::ekrOverpriceFormula($level);
}
/**
* @param int $uid
*
* @return bool
*/
public static function isEnoughExperience(int $uid): bool
{
$db = new Db();
return $db::getValue('select exp from stats where id = ?', [$uid]) >= Tournament::MIN_EXP;
}
/**
* @param int $uid
*
* @return bool
*/
public static function isRestrictedToJoin(int $uid): bool
{
$db = new Db();
return $db::getValue('select count(*) from eff_users where uid = ? and id_eff = 486 and `delete` = 0', [$uid]);
}
/**
* @param int $tid
*
* @return bool
*/
public static function isStarted(int $tid): bool
{
$db = new Db();
return $db::getValue('select count(*) from tournaments where start_time = -1 and tid = ?', [$tid]);
}
/**
* Ñ÷èòàåì ñêîëüêî èãðîêîâ æäóò â çàÿâêå íà òóðíèð.
*
* @param int $tid
*
* @return int
*/
public static function getWaitingMembersQuantity(int $tid): int
{
$db = new Db();
return $db::getValue('select count(*) from tournaments_users where tid = ?', [$tid]);
}
/**
* Ñîçäàíèå íîâîãî òóðíèðà.
*
* @param int $tid
*
* @return void
*/
public static function createTournament(int $tid): void
{
$db = new Db();
$db::sql('insert into tournaments (tid) values (?)', [$tid]);
}
/**
* Èãðîê ïðèñîåäèíÿåòñÿ ê òóðíèðó è òåëåïîðòèðóåòñÿ â òóðíèðíóþ êîìíàòó.
*
* @param int $uid
* @param int $tid
*
* @return void
*/
public static function joinTournament(int $uid, int $tid): void
{
/** Êàñòîìíûå êîìíàòû 25008 - 25012. */
$roomId = 25000 + $tid;
$db = new Db();
$db::sql('insert into tournaments_users (tid, uid) values (?, ?)', [$tid, $uid]);
self::teleport($uid, $roomId);
}
/**
* Ñòàðò òóðíèðà.
*
* @param int $tid
*
* @return void
*/
public static function startTournament(int $tid): void
{
$db = new Db();
$db::sql('update tournaments set start_time = -1 where tid = ?', [$tid]);
}
/**
* ×èñòèì áàçû îò ïðîøåäøåãî òóðíèðà.
*
* @param int $tid
*
* @return void
*/
public static function destroyTournament(int $tid): void
{
$db = new Db();
//Óáåäèòüñÿ ÷òî â áàçå íàñòðîåí foreign_keys è ïîñëåäóåò àâòîî÷èñòêà tournaments_users !!!
$db::sql('delete from tournaments where tid = ?', [$tid]);
}
/**
* Ïîëó÷àåì ñïèñîê áîéöîâ è áü¸ì èõ íà ïàðû. Âîçâðàùàåì ñïèñêè ïàð + 1 ïîñëåäíèé áåç ïàðû åñëè åñòü.
*
* @param array $fightersList
*
* @return array
*/
public static function getFightersTeams(array $fightersList): array
{
$db = new Db();
$query = sprintf("select id from users where battle = 0 and id in (%s)", implode(', ', $fightersList));
return array_chunk($db::getColumn($query), 2);
}
/**
* Âûáèðàåì æèâûõ áîéöîâ íå ñðàæàþùèõñÿ â äàííûé ìîìåíò.
*
* @param int $tid
*
* @return array
*/
public static function getFreeFighters(int $tid): array
{
$db = new Db();
return $db::getColumn('select uid from tournaments_users where tid = ? and death_time = 0 order by uid', [$tid]);
}
/**
* Âûáèðàåì ïîáåäèòåëåé. Ñìåùàåì ìàññèâ, ÷òîáû âîçâðàò ø¸ë ñ åäèíèöû.
*
* @param int $tid
*
* @return array
*/
public static function getWinners(int $tid): array
{
$db = new Db();
$winners = $db::getColumn('select uid from tournaments_users where tid = ? order by death_time desc limit 3', [$tid]);
return [
1 => $winners[0],
2 => $winners[1],
3 => $winners[2]
];
}
/**
* Ïðîáóåì âûêóñèòü ïðîèãðàâøåãî â ïîñëåäíåé òóðíèðíîé áèòâå è óäàëèòü ýòó ñàìóþ áèòâó âî èçáåæàíèå.
* @return mixed
*/
public static function getLooser()
{
$query = '
select uid, battle
from
battle_users,
(select id, team_win
from battle
where
team_win > 0 and
typeBattle = 25000
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 and
battle_users.uid in (select uid from tournaments_users where death_time = 0)';
$query2 = 'select bu.uid from battle b
inner join battle_users bu on b.team_win != bu.team and b.id = bu.battle
inner join tournaments_users tu on bu.uid = tu.uid
where typeBattle = 25000 and death_time = 0 order by b.time_start desc limit 1';
$db = new Db;
$row = $db::getRow($query);
return $row['uid'] ?? 0;
}
/**
* Âûáûâøèé èç òóðíèðà ïîêèäàåò êîìíàòó è ïîëó÷àåò âðåìÿ ñìåðòè.
*
* @param int $uid
* @param bool $winner
* @return void
*/
public static function removeFighter(int $uid, bool $winner = false): void
{
if (!$uid) {
return;
}
//$winner_timer_add = $winner? 500 : 0; # Ïîñëåäíûé ÄÎËÆÅÍ áûòü ïîñëåäíèì.
$db = new Db();
$db::sql('update tournaments_users set death_time = unix_timestamp() + 500 where death_time = 0 and uid = ?', [$uid]);
self::teleport($uid, 9);
//fixme: Êëàññû íå ïîäêëþ÷àþòñÿ äðóã ê äðóãó. Íóæíî ìåíÿòü àðõèòåêòóðó èãðû. :(
Db::sql("update users_achiv set trn = trn + 1 where id = ?", [$uid]);
//(new Achievements(\user::start()))->updateCounter('trn');
}
/**
* Óçíà¸ì id òóðíèðà ïî id èãðîêà.
*
* @param int $uid
*
* @return mixed
*/
public static function getTournamentIdByUserId(int $uid)
{
$db = new Db();
return $db::getValue('select tid from tournaments_users where uid = ?', [$uid]);
}
/**
* Ïîïðîáóåì ñòàðòîíóòü ïîåäèíîê.
* 25000 - Óíèêàëüíûé id ïîåäèíêà ïîä òóðíèðû.
* noinc - çàïðåò íà âìåøàòåëüñòâî
* invis - íåâèäèìûé áîé
*
* @param int $uid1
* @param int $uid2
*
* @return void
*/
public static function startBattle(int $uid1, int $uid2): void
{
$db = new Db();
$check = Db::getValue('select count(*) from users where id in (?, ?) and battle = 0', [$uid1, $uid2]);
if ($check !== 2) {
return;
}
$db::exec('insert into battle (city, time_start, timeout, type, invis, noinc, travmChance, typeBattle)
values (\'capitalcity\', unix_timestamp(), 60, 0, 1, 1, 0, 25000)');
$bid = $db::lastInsertId(); // ÂÀÆÍÎ!
$db::sql('update stats set team = 1, hpNow = hpAll, mpNow = mpAll where id = ?', [$uid1]);
$db::sql('update stats set team = 2, hpNow = hpAll, mpNow = mpAll where id = ?', [$uid2]);
$db::sql('update users set battle = ? where id in (?, ?)', [$bid, $uid1, $uid2]);
}
/**
* Óçíà¸ì ëîãèí ïåðñîíàæà ïî åãî id.
*
* @param int $uid
*
* @return mixed
*/
public static function uidToLogin(int $uid)
{
$db = new Db();
return $db::getValue('select login from users where id = ?', [$uid]);
}
/**
* Òåëåïîðò ïî êîìíàòàì.
*
* @param int $uid
* @param int $roomId
*
* @return void
*/
private static function teleport(int $uid, int $roomId): void
{
$db = new Db();
$db::sql('update users set room = ? where id = ?', [$roomId, $uid]);
}
/**
* Íåò ïðîâåðîê $message ïîòîìó ÷òî îíî âñåãäà çàäà¸òñÿ â êîäå è èãðîê íà íåãî íå âëèÿåò.
*
* @param string $message
*
* @return void
*/
public static function sysMessage(string $message): void
{
if (!empty($message)) {
$db = new Db();
$message = "<span style='font-weight: bold; color: forestgreen;'>$message</span>";
$db::sql('insert into chat (time, type, text, new, da) values (unix_timestamp(), 6, ?, 1, 1)', [$message]);
}
}
/**
* Ãåíåðèðóåò ìíîæåñòâåííûé çàïðîñ ñðàçó íà $quantity îäíîòèïíûõ ïðåäìåòîâ â èíâåíòàðü ïîëüçîâàòåëÿ $uid.
*
* @param int $uid
* @param int $quantity
*
* @return void
*/
public static function givePrizeItems(int $uid, int $quantity): void
{
$query = 'insert into items_users (item_id, uid, data, iznosMAX, lastUPD, time_create)
values (4754, :uid, :data, 1, unix_timestamp(), unix_timestamp())';
$args = [
'uid' => $uid,
'data' => 'nosale=1|musor=1|sudba=' . self::uidToLogin($uid) . '|lvl=8|tr_s1=0|tr_s2=0|tr_s3=0|tr_s4=0'
];
$db = new Db();
$stmt = $db::prepare($query);
for ($i = 0; $i < $quantity; $i++) {
$stmt->execute($args);
}
}
/** Ýôôåêò-îãðàíè÷èòåëü íà ó÷àñòèå â òóðíèðå.
* @param int $uid
* @param int $unixtime
*
* @return void
*/
public static function giveDelay(int $uid, int $unixtime): void
{
$db = new Db();
$query = 'insert into eff_users (id_eff, uid, name, timeUse) VALUES (?,?,?,?)';
$args = [486, $uid, 'Ïðèç¸ð ãîðîäñêîãî òóðíèðà!', $unixtime];
$db::sql($query, $args);
}
}