2022-12-30 19:03:37 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
2023-04-15 19:17:40 +00:00
|
|
|
|
namespace Tournament;
|
|
|
|
|
|
|
|
|
|
use Core\Db;
|
2022-12-30 19:03:37 +00:00
|
|
|
|
|
|
|
|
|
class Tournament
|
|
|
|
|
{
|
2023-04-15 19:17:40 +00:00
|
|
|
|
const IS_ENABLED = false;
|
2022-12-30 19:03:37 +00:00
|
|
|
|
private const SEND_CHAT_MESSAGE = 3;
|
2023-04-15 19:17:40 +00:00
|
|
|
|
public const START_TOURNAMENT = 5;
|
2022-12-30 19:03:37 +00:00
|
|
|
|
private const PRIZE1 = 25;
|
|
|
|
|
private const PRIZE2 = 10;
|
|
|
|
|
private const PRIZE3 = 5;
|
|
|
|
|
const MIN_EXP = 175000;
|
2023-01-10 16:29:32 +00:00
|
|
|
|
private const CHAT_MESSAGE = 'Внимание! Турнир для %d уровня открыт! Поспешите принять участие в Зале Воинов!';
|
|
|
|
|
private const VICTORY_MESSAGE = 'Турнир для %d уровней завершён! Первое место: %s. Второе место: %s. Третье место: %s.';
|
2022-12-30 19:03:37 +00:00
|
|
|
|
private const DELAY1 = '+12hour';
|
|
|
|
|
private const DELAY2 = '+6hour';
|
|
|
|
|
private const DELAY3 = '+3hour';
|
|
|
|
|
|
2023-01-10 16:29:32 +00:00
|
|
|
|
/** Формула рассчёта разрешённой стоимости одетых предметов.
|
2022-12-30 19:03:37 +00:00
|
|
|
|
*
|
|
|
|
|
* @param int $int
|
|
|
|
|
*
|
|
|
|
|
* @return float|int
|
|
|
|
|
*/
|
|
|
|
|
public static function ekrOverpriceFormula(int $int): int
|
|
|
|
|
{
|
|
|
|
|
return ($int - 7) * 151;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверяем может ли игрок учавствовать в турнире и либо присоединяем его, либо обламываем.
|
2022-12-30 19:03:37 +00:00
|
|
|
|
*
|
|
|
|
|
* @param int $uid
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function join(int $uid): void
|
|
|
|
|
{
|
2023-01-10 16:29:32 +00:00
|
|
|
|
/** Возвращает level, или 0 если провалены проверки. */
|
2022-12-30 19:03:37 +00:00
|
|
|
|
$userLevel = TournamentModel::getUserLevel($uid);
|
|
|
|
|
if (
|
|
|
|
|
$userLevel === 0 ||
|
|
|
|
|
TournamentModel::isEkrOverpriced($uid, $userLevel) ||
|
|
|
|
|
!TournamentModel::isEnoughExperience($uid) ||
|
|
|
|
|
TournamentModel::isRestrictedToJoin($uid) ||
|
|
|
|
|
TournamentModel::isStarted($uid)
|
|
|
|
|
) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$mq = TournamentModel::getWaitingMembersQuantity($userLevel);
|
|
|
|
|
if (empty($mq)) {
|
|
|
|
|
TournamentModel::createTournament($userLevel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TournamentModel::joinTournament($uid, $userLevel);
|
|
|
|
|
$mq++;
|
|
|
|
|
|
|
|
|
|
if ($mq == self::SEND_CHAT_MESSAGE) {
|
|
|
|
|
TournamentModel::sysMessage(sprintf(self::CHAT_MESSAGE, $userLevel));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ($mq == self::START_TOURNAMENT) {
|
|
|
|
|
TournamentModel::startTournament($userLevel);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Проверка для крона\конфига. Выбивает проигравших и перезапускает поединки во всех турнирах.
|
2022-12-30 19:03:37 +00:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function startAllBattles(): void
|
|
|
|
|
{
|
2023-04-15 19:17:40 +00:00
|
|
|
|
Db::sql(
|
2022-12-30 19:03:37 +00:00
|
|
|
|
'delete from tournaments where start_time + date_add(start_time,interval 30 minute) < unix_timestamp()'
|
|
|
|
|
);
|
|
|
|
|
TournamentModel::removeFighter(TournamentModel::getLooser());
|
2023-04-15 19:17:40 +00:00
|
|
|
|
$tournamentLevels = Db::getColumn('select tid from tournaments where start_time = -1');
|
2022-12-30 19:03:37 +00:00
|
|
|
|
foreach ($tournamentLevels as $level) {
|
|
|
|
|
$aliveFighters = TournamentModel::getFreeFighters($level);
|
|
|
|
|
if (count($aliveFighters) > 1) {
|
|
|
|
|
shuffle($aliveFighters);
|
|
|
|
|
$this->startBattle($aliveFighters);
|
|
|
|
|
} elseif (count($aliveFighters) === 1) {
|
|
|
|
|
TournamentModel::removeFighter($aliveFighters[0], true);
|
|
|
|
|
$this->victory($level);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Получаем пары бойцов и начинаем их поединки.
|
2022-12-30 19:03:37 +00:00
|
|
|
|
*
|
|
|
|
|
* @param array $fightersList
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function startBattle(array $fightersList): void
|
|
|
|
|
{
|
|
|
|
|
$teams = TournamentModel::getFightersTeams($fightersList);
|
|
|
|
|
foreach ($teams as $team) {
|
|
|
|
|
if (count($team) === 2) {
|
|
|
|
|
TournamentModel::startBattle($team[0], $team[1]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-01-10 16:29:32 +00:00
|
|
|
|
* Награждаем победителей турнира, чистим базу.
|
2022-12-30 19:03:37 +00:00
|
|
|
|
*
|
|
|
|
|
* @param int $tid
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function victory(int $tid): void
|
|
|
|
|
{
|
|
|
|
|
$winners = TournamentModel::getWinners($tid);
|
|
|
|
|
sleep(1);
|
|
|
|
|
TournamentModel::givePrizeItems($winners[1], self::PRIZE1);
|
|
|
|
|
TournamentModel::givePrizeItems($winners[2], self::PRIZE2);
|
|
|
|
|
TournamentModel::givePrizeItems($winners[3], self::PRIZE3);
|
|
|
|
|
sleep(1);
|
|
|
|
|
TournamentModel::giveDelay($winners[1], strtotime(self::DELAY1));
|
|
|
|
|
TournamentModel::giveDelay($winners[2], strtotime(self::DELAY2));
|
|
|
|
|
TournamentModel::giveDelay($winners[3], strtotime(self::DELAY3));
|
|
|
|
|
sleep(1);
|
|
|
|
|
TournamentModel::sysMessage(
|
|
|
|
|
sprintf(
|
|
|
|
|
self::VICTORY_MESSAGE,
|
|
|
|
|
$tid,
|
|
|
|
|
TournamentModel::uidToLogin($winners[1]),
|
|
|
|
|
TournamentModel::uidToLogin($winners[2]),
|
|
|
|
|
TournamentModel::uidToLogin($winners[3])
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
sleep(3);
|
|
|
|
|
TournamentModel::destroyTournament($tid);
|
|
|
|
|
}
|
|
|
|
|
}
|