2021-08-23 20:23:38 +00:00
< ? php
# Date: 23.08.2021 (23:05)
namespace Battles ;
2022-01-26 23:15:33 +00:00
use Battles\Database\Db ;
2022-08-09 19:57:43 +00:00
use Battles\Models\User\Effects ;
2021-08-23 20:23:38 +00:00
class Clan
{
2022-01-26 23:15:33 +00:00
private static ? self $_instance = null ;
2021-08-23 20:23:38 +00:00
private $clan ;
2022-01-26 23:15:33 +00:00
private function __construct ()
2021-08-23 20:23:38 +00:00
{
2022-01-26 23:15:33 +00:00
$this -> clan = Db :: getInstance () -> ofetch ( 'select * from clans where owner_id = ?' , User :: getInstance () -> getId ());
}
public static function getInstance () : self
{
if ( is_null ( self :: $_instance )) {
self :: $_instance = new self ();
}
return self :: $_instance ;
2021-08-23 20:23:38 +00:00
}
public function addMember ( string $login ) : string
{
$error = null ;
2022-01-26 23:15:33 +00:00
if ( ! $this -> getProverka ( User :: getInstance ( $login ) -> getId ())) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Нет проверки!' ;
}
2022-01-26 23:15:33 +00:00
if ( User :: getInstance ( $login ) -> getClan ()) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Персонаж уже состоит в клане!' ;
}
2022-01-26 23:15:33 +00:00
if ( User :: getInstance ( $login ) -> getLevel () < 1 ) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Персонаж 0 уровня не может быть принят!' ;
}
2022-08-09 19:57:43 +00:00
if ( ! User :: getInstance () -> money () -> spend ( GameConfigs :: CLAN [ 'add_member_cost' ])) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Недостаточно денег!' ;
}
if ( $error ) {
return $error ;
}
2022-08-09 19:57:43 +00:00
2022-01-26 23:15:33 +00:00
User :: getInstance ( $login ) -> setClan ( User :: getInstance () -> getClan ());
2021-08-23 20:23:38 +00:00
return " Персонаж « { $login } » успешно принят в клан. " ;
}
public function removeMember ( string $login ) : string
{
$error = null ;
2022-01-26 23:15:33 +00:00
if ( User :: getInstance ( $login ) -> getId () === User :: getInstance () -> getId ()) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Себя выгонять нельзя!' ;
}
2022-01-26 23:15:33 +00:00
if ( User :: getInstance ( $login ) -> getClan () !== User :: getInstance () -> getClan ()) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Персонаж не состоит в этом клане!' ;
}
2022-08-09 19:57:43 +00:00
if ( ! User :: getInstance () -> money () -> spend ( GameConfigs :: CLAN [ 'remove_member_cost' ])) {
$error .= '<br>Недостаточно денег!' ;
}
2021-08-23 20:23:38 +00:00
if ( $error ) {
return $error ;
}
2022-01-26 23:15:33 +00:00
User :: getInstance ( $login ) -> setClan ( null );
2021-08-23 20:23:38 +00:00
return " Персонаж « { $login } » покинул клан. " ;
}
public function changeOwner ( string $login ) : string
{
$error = null ;
2022-01-26 23:15:33 +00:00
if ( User :: getInstance ( $login ) -> getId () === User :: getInstance () -> getId ()) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Самоудовлетворяетесь? ;)' ;
}
2022-01-26 23:15:33 +00:00
if ( User :: getInstance ( $login ) -> getClan () !== User :: getInstance () -> getClan ()) {
2021-08-23 20:23:38 +00:00
$error .= '<br>Персонаж не состоит в этом клане!' ;
}
if ( $error ) {
return $error ;
}
2022-01-26 23:15:33 +00:00
Db :: getInstance () -> execute ( 'update clans set owner_id = ? where owner_id = ?' , [ User :: getInstance ( $login ) -> getId (), User :: getInstance () -> getId ()]);
2021-08-23 20:23:38 +00:00
return 'Вы передали управление кланом персонажу «' . $login . '».' ;
}
2021-08-25 01:44:36 +00:00
public function setClanInfo ( string $text ) : string
2021-08-23 20:23:38 +00:00
{
2022-01-26 23:15:33 +00:00
$checkId = Db :: getInstance () -> fetchColumn ( 'select id from users where clan = (select short_name from clans where owner_id = ?)' , User :: getInstance () -> getId ());
if ( $checkId !== User :: getInstance () -> getId ()) {
2021-08-23 20:23:38 +00:00
return 'Ошибка доступа!' ;
}
2022-01-26 23:15:33 +00:00
Db :: getInstance () -> execute ( 'update clans set info = ? where owner_id = ?' , [ $text , $checkId ]);
2021-08-23 20:23:38 +00:00
return 'Описание клана изменено!' ;
}
public function getMemberlist () : array
{
2022-01-26 23:15:33 +00:00
return Db :: getInstance () -> fetchAll ( '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' , User :: getInstance () -> getClan ());
2021-08-23 20:23:38 +00:00
}
private function getProverka ( $user_id )
{
2022-08-09 19:57:43 +00:00
return Effects :: count ( $user_id , 20 );
2021-08-23 20:23:38 +00:00
}
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 ;
}
}