battles/classes/Battles/Arena.php

117 lines
4.2 KiB
PHP
Raw Normal View History

<?php
2022-02-12 23:50:04 +00:00
# Date: 12.02.2022 (20:33)
namespace Battles;
2022-02-12 23:50:04 +00:00
use Battles\Database\Db;
class Arena
{
2022-02-12 23:50:04 +00:00
private int $fight_id;
private int $team_id;
2022-02-12 23:50:04 +00:00
function addNew(int $membersLimit = 5, int $groupsLimit = 2, int $startTime = 3)
{
2022-02-12 23:50:04 +00:00
if (
$this->isOnArena() &&
$this->hasNoPendingFights() &&
$this->hasNoActiveFights() &&
in_array($startTime, [1,3,5,10])
) {
$query1 = 'insert into fights_pending (fight_id, start_time, members_limit, groups_limit) VALUES (?,?,?,?)';
$query2 = 'insert into fights_pending_users (fight_id, user_id, team_id) values (?,?,?)';
$startTime = strtotime("+{$startTime}minutes");
$uid = User::getInstance()->getId();
//последовательность важна!
Db::getInstance()->execute($query1, [$uid, $startTime, $membersLimit, $groupsLimit]);
Db::getInstance()->execute($query2, [$uid, $uid, 1]);
}
}
2022-02-12 23:50:04 +00:00
function join(int $fight_id, int $team_id)
2022-01-26 22:56:03 +00:00
{
2022-02-12 23:50:04 +00:00
$this->fight_id = $fight_id;
$this->team_id = $team_id;
if (
$this->hasNoClanEnemies() &&
$this->hasFreeSpace() &&
$this->isOnArena() &&
$this->hasNoPendingFights() &&
$this->hasNoActiveFights()
) {
$query = 'insert into fights_pending_users (fight_id, user_id, team_id) values (?,?,?)';
Db::getInstance()->execute($query, [$fight_id, User::getInstance()->getId(), $team_id]);
}
2022-01-26 22:56:03 +00:00
}
2022-02-12 23:50:04 +00:00
function leave()
{
2022-02-12 23:50:04 +00:00
// чтобы не вылететь из заявки в момент начала поединка
if (
$this->hasNoActiveFights() &&
!$this->isFightStarter()
) {
Db::getInstance()->execute('delete from fights_pending_users where user_id = ?', User::getInstance()->getId());
}
}
2022-02-12 23:50:04 +00:00
function getPendingList(): object
{
2022-02-12 23:50:04 +00:00
return new \stdClass();
/** !!PLACEHOLDER!! */
}
2022-01-26 22:56:03 +00:00
2022-02-12 23:50:04 +00:00
public static function fight(): self
{
return new self();
}
2022-02-12 23:50:04 +00:00
// проверка на соклана
private function hasNoClanEnemies(): bool
{
$query = 'select user_id from fights_pending_users where fight_id = ? and team_id = ?';
$enemies = Db::getInstance()->ofetchAll($query, [$this->fight_id, $this->team_id]);
foreach ($enemies as $enemy) {
if (User::getInstance()->getClan() && User::getInstance()->getClan() === User::getInstance($enemy->user_id)->getClan()) {
return false;
}
}
2022-02-12 23:50:04 +00:00
return true;
}
2022-02-12 23:50:04 +00:00
// проверка на переполнение
private function hasFreeSpace(): bool
{
$query = 'select members_limit, groups_limit from fights_pending where fight_id = ?';
$query2 = 'select count(*) from fights_pending_users where fight_id = ? and team_id = ?';
$limits = Db::getInstance()->ofetch($query, $this->fight_id);
$currentUsers = Db::getInstance()->fetchColumn($query2, [$this->fight_id, $this->team_id]);
return $limits->members_limit > $currentUsers && $limits->groups_limit >= $this->team_id;
}
2022-02-12 23:50:04 +00:00
// проверка на нахождение в комнате (1 = арена)
private function isOnArena(): bool
{
return User::getInstance()->getRoom() === 1;
}
2022-02-12 23:50:04 +00:00
// проверка на нахождение в другой заявке
public function hasNoPendingFights(): bool
{
$query = 'select count(*) from fights_pending_users where user_id = ?';
return Db::getInstance()->fetchColumn($query, User::getInstance()->getId()) > 0;
}
// проверка на нахождение в поединке
public function hasNoActiveFights(): bool
{
$query = 'select count(*) from fighters where user_id = ?';
return Db::getInstance()->fetchColumn($query, User::getInstance()->getId()) > 0;
}
2022-01-26 22:56:03 +00:00
2022-02-12 23:50:04 +00:00
// проверка на создателя поединка
private function isFightStarter(): bool
{
$query = 'select count(*) from fights_pending_users where user_id = fight_id and user_id = ?';
return Db::getInstance()->fetchColumn($query, User::getInstance()->getId()) > 0;
}
}