105 lines
3.3 KiB
PHP
105 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace Insallah\Tournaments;
|
|
|
|
class Tournament
|
|
{
|
|
/**
|
|
* Ïðîâåðÿåì ìîæåò ëè èãðîê ó÷àâñòâîâàòü â òóðíèðå è ëèáî ïðèñîåäèíÿåì åãî, ëèáî îáëàìûâàåì.
|
|
*
|
|
* @param int $uid
|
|
*
|
|
* @return void
|
|
*/
|
|
public function join(int $uid)
|
|
{
|
|
/** Âîçâðàùàåò level, èëè 0 åñëè ïðîâàëåíû ïðîâåðêè. */
|
|
$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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Ïðîâåðêà äëÿ êðîíà\êîíôèãà. Âûáèâàåò ïðîèãðàâøèõ è ïåðåçàïóñêàåò ïîåäèíêè âî âñåõ òóðíèðàõ.
|
|
* @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);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Íàãðàæäàåì ïîáåäèòåëåé òóðíèðà, ÷èñòèì áàçó.
|
|
*
|
|
* @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);
|
|
}
|
|
}
|