game/_incl_data/class/Insallah/Tournaments/Tournament.php
2023-01-10 18:30:35 +02:00

105 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?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);
}
}