battles/classes/Battles/Clan.php

119 lines
4.1 KiB
PHP

<?php
# Date: 23.08.2021 (23:05)
namespace Battles;
use Battles\Database\DBPDO;
class Clan
{
private DBPDO $db;
private User $user;
private $clan;
public static Clan $current;
public function __construct()
{
$this->db = DBPDO::$db;
$this->user = User::$current;
$this->clan = $this->db->ofetch('select * from clans where owner_id = ?', $this->user->getId());
}
public function addMember(string $login): string
{
$target = new User($login);
$error = null;
if (!$this->getProverka($target->getId())) {
$error .= '<br>Нет проверки!';
}
if ($target->getClan()) {
$error .= '<br>Персонаж уже состоит в клане!';
}
if ($target->getLevel() < 1) {
$error .= '<br>Персонаж 0 уровня не может быть принят!';
}
if ($this->user->getMoney() < GameConfigs::CLAN['add_member_cost']) {
$error .= '<br>Недостаточно денег!';
}
if ($error) {
return $error;
}
$this->user->setMoney($this->user->getMoney() - GameConfigs::CLAN['add_member_cost']);
$this->user->saveMoney();
$target->setClan($this->user->getClan());
return "Персонаж «{$login}» успешно принят в клан.";
}
public function removeMember(string $login): string
{
$target = new User($login);
$error = null;
if ($this->user->getMoney() < GameConfigs::CLAN['remove_member_cost']) {
$error .= '<br>Недостаточно денег!';
}
if ($target->getId() === $this->user->getId()) {
$error .= '<br>Себя выгонять нельзя!';
}
if ($target->getClan() !== $this->user->getClan()) {
$error .= '<br>Персонаж не состоит в этом клане!';
}
if ($error) {
return $error;
}
$this->user->setMoney($this->user->getMoney() - GameConfigs::CLAN['remove_member_cost']);
$this->user->saveMoney();
$target->setClan(null);
return "Персонаж «{$login}» покинул клан.";
}
public function changeOwner(string $login): string
{
$target = new User($login);
$error = null;
if ($target->getId() === $this->user->getId()) {
$error .= '<br>Самоудовлетворяетесь? ;)';
}
if ($target->getClan() !== $this->user->getClan()) {
$error .= '<br>Персонаж не состоит в этом клане!';
}
if ($error) {
return $error;
}
$this->db->execute('update clans set owner_id = ? where owner_id = ?', [$target->getId(), $this->user->getId()]);
return 'Вы передали управление кланом персонажу «' . $login . '».';
}
public function setClanInfo(string $text): string
{
$check = $this->db->ofetch('select id from users where clan = (select short_name from clans where owner_id = ?)', $this->user->getId());
if ($check->id !== $this->user->getId()) {
return 'Ошибка доступа!';
}
$this->db->execute('update clans set info = ? where owner_id = ?', [$text, $check->id]);
return 'Описание клана изменено!';
}
public function getMemberlist(): array
{
return $this->db->ofetchAll('select id, (select 1 from clans where short_name = clan and owner_id = id) as clan_owner, room from users where clan = ? order by clan_owner desc, room, login', $this->user->getClan());
}
private function getProverka($user_id)
{
return $this->db->fetch('select 1 from users_effects where type = 20 and owner_id = ?', $user_id);
}
public function getClanOwnerId(): ?int
{
return $this->clan->owner_id;
}
public function getClanName(): ?string
{
return $this->clan->full_name;
}
public function getClanShortName(): ?string
{
return $this->clan->short_name;
}
}