game/_incl_data/class/Insallah/Tournaments/Tournament.php

105 lines
3.5 KiB
PHP
Raw Normal View History

2022-12-20 01:57:09 +00:00
<?php
namespace Insallah\Tournaments;
class Tournament
{
/**
2023-01-10 16:29:32 +00:00
* Проверяем может ли игрок учавствовать в турнире и либо присоединяем его, либо обламываем.
2022-12-20 01:57:09 +00:00
*
* @param int $uid
*
* @return void
*/
public function join(int $uid)
{
2023-01-10 16:29:32 +00:00
/** Возвращает level, или 0 если провалены проверки. */
2022-12-20 01:57:09 +00:00
$checks = new Model\GameConnector();
$tournament = new Model\Tournament();
$userLevel = $checks->setUser($uid)->getAllowedLevel();
if (
$userLevel === 0 ||
$checks->isEkrOverpriced() ||
!$checks->isEnoughExperience() ||
$checks->isRestrictedToJoin() ||
$tournament->isStarted($uid)
) {
return;
}
$user = new Model\User();
if (empty($user->getWaitingQuantity($userLevel))) {
Model\Tournament::create($userLevel);
}
$user->joinTournament($uid, $userLevel);
$user->addWaitingUser();
if ($user->getWaitingQuantity($userLevel) == Config::SEND_CHAT_MESSAGE) {
Model\GameConnector::sysMessage(sprintf(Config::CHAT_MESSAGE, $userLevel));
}
if ($user->getWaitingQuantity($userLevel) == Config::START_TOURNAMENT) {
Model\Tournament::start($userLevel);
}
}
/**
2023-01-10 16:29:32 +00:00
* Проверка для крона\конфига. Выбивает проигравших и перезапускает поединки во всех турнирах.
2022-12-20 01:57:09 +00:00
* @return void
*/
public static function startAllBattles()
{
if (!Config::IS_ENABLED) {
return;
}
$tournamentlevels = new Model\Tournament();
$alivefighters = new Model\User();
Model\User::remove(Model\GameConnector::getLooser());
foreach ($tournamentlevels->getAllStarted() as $level) {
$count = count($alivefighters->getAlive($level, true));
if ($count > 1) {
Model\GameConnector::startBattle($alivefighters->getAlive($level, true));
} elseif ($count === 1) {
Model\User::remove($alivefighters->getAlive($level, true)[0]);
self::victory($level);
}
}
}
/**
2023-01-10 16:29:32 +00:00
* Награждаем победителей турнира, чистим базу.
2022-12-20 01:57:09 +00:00
*
* @param int $tid
*
* @return void
*/
private static function victory(int $tid)
{
$u = new Model\User();
$uid = new Model\GameConnector();
sleep(1);
$uid->setUser($u->getWinners($tid)[1])->givePrizeItems(Config::PRIZE1);
$uid->setUser($u->getWinners($tid)[2])->givePrizeItems(Config::PRIZE2);
$uid->setUser($u->getWinners($tid)[3])->givePrizeItems(Config::PRIZE3);
sleep(1);
$uid->setUser($u->getWinners($tid)[1])->giveDelay(Config::DELAY1);
$uid->setUser($u->getWinners($tid)[2])->giveDelay(Config::DELAY2);
$uid->setUser($u->getWinners($tid)[3])->giveDelay(Config::DELAY3);
sleep(1);
Model\GameConnector::sysMessage(
sprintf(
Config::VICTORY_MESSAGE,
$tid,
$uid->setUser($u->getWinners($tid)[1])->uidToLogin(),
$uid->setUser($u->getWinners($tid)[2])->uidToLogin(),
$uid->setUser($u->getWinners($tid)[3])->uidToLogin()
)
);
sleep(3);
Model\Tournament::destroy($tid);
}
}