2023-04-18 09:21:54 +00:00
< ? php
namespace Clan ;
use Core\Db ;
2023-12-01 21:05:11 +00:00
use Delo ;
2023-08-14 15:15:05 +00:00
use Helper\Conversion ;
2023-04-18 09:21:54 +00:00
use User ;
use User\ItemsModel ;
class ItemStorage
{
2024-12-10 11:09:03 +00:00
2023-04-18 09:21:54 +00:00
private string $error = '' ;
2024-12-10 11:09:03 +00:00
private int $clanId ;
private int $userId ;
private string $userLogin ;
public function __construct ()
{
$userinfo = User :: start () -> info ;
$this -> userId = $userinfo [ 'id' ];
$this -> clanId = $userinfo [ 'clan' ];
$this -> userLogin = $userinfo [ 'login' ];
}
public function take ( int $itemId ) : string
{
$item = Db :: getRows ( 'select items_users.id, name, data from items_users inner join items_main on item_id = items_main.id where uid = ? and items_users.id = ?' , [ " -21 $this->clanId " , $itemId ]);
if ( ! isset ( $item [ 'id' ])) {
return 'Предмет не найден.<br>' . $itemId ;
}
$data = Conversion :: dataStringToArray ( $item [ 'data' ]);
$cls = isset ( $data [ 'toclan' ]) ? explode ( '#' , $data [ 'toclan' ])[ 1 ] : 0 ;
$col = ItemsModel :: itemsX ( $itemId );
Db :: sql ( 'update items_users set lastUPD = unix_timestamp(), uid = ? where id = ?' , [ $this -> userId , $itemId ]);
Db :: sql ( 'insert into clan_operations (clan, type, text, time, val, uid) values (?,5,?,unix_timestamp(),?,?)' , [ $this -> clanId , $this -> userLogin , " { $item [ 'name' ] } (x $col ) Id: $itemId Хозяин: $cls " , $this -> userId ]);
return " Вы успешно взяли предмет « { $item [ 'name' ] } » из хранилища. " ;
}
public function remove ( int $type ) : void
{
$storedItems = Db :: getRows ( 'select items_users.id, uid, name, data, inOdet from items_users inner join items_main on item_id = items_main.id where uid = ? or data like ?' , [ " -21 $this->clanId " , " %toclan= $this->clanId #% " ]);
foreach ( $storedItems as $storedItem ) {
$data = Conversion :: dataStringToArray ( $storedItem [ 'data' ]);
$cls = isset ( $data [ 'toclan' ]) ? explode ( '#' , $data [ 'toclan' ])[ 1 ] : 0 ;
$col = ItemsModel :: itemsX ( $storedItem [ 'id' ]);
2023-04-18 09:21:54 +00:00
2024-12-10 11:09:03 +00:00
if ( $cls == $this -> userId ) {
if ( $storedItem [ 'uid' ] != $this -> userId && $storedItem [ 'inOdet' ] != 0 ) {
Db :: sql ( 'update items_users set inOdet = 0 where id = ?' , [ $storedItem [ 'id' ]]);
}
unset ( $data [ 'toclan' ]);
$storedItem [ 'data' ] = Conversion :: arrayToDataString ( $data );
Db :: sql ( 'update items_users set lastUPD = unix_timestamp(), uid = ?, data = ? where id = ?' , [ $this -> userId , $storedItem [ 'data' ], $storedItem [ 'id' ]]);
Db :: sql ( 'insert into clan_operations (clan, type, text, time, val, uid) values (?,?,?,unix_timestamp(),?,?)' , [ $this -> clanId , $type , $this -> userLogin , " { $storedItem [ 'name' ] } (x $col ) Id: { $storedItem [ 'id' ] } " , $this -> userId ]);
} elseif ( $storedItem [ 'uid' ] == $this -> userId ) {
Db :: sql ( 'update items_users set lastUPD = unix_timestamp(), uid = ? where id = ?' , [ " -21 $this->clanId " , $storedItem [ 'id' ]]);
Db :: sql ( 'insert into clan_operations (clan, type, text, time, val, uid) values (?,9,?,unix_timestamp(),?,?)' , [ $this -> clanId , $this -> userLogin , " { $storedItem [ 'name' ] } (x $col ) Id: { $storedItem [ 'id' ] } " , $this -> userId ]);
}
}
}
public function backToOwner ( int $itemId ) : string
2023-04-18 09:21:54 +00:00
{
2024-12-10 11:09:03 +00:00
$item = ItemsModel :: getOwnedItemById ( $itemId , $this -> userId , true );
if ( empty ( $item )) {
return 'Предмет не найден.' ;
}
$ownerIsNotReal = ! Db :: getValue ( 'select count(id) from stats where id = ? and (bot = 0 or clone = 0)' , [ $item [ 'uid' ]]);
if ( $ownerIsNotReal ) {
return 'Вы не можете изьять данный предмет, он не принадлежит клану.' ;
}
$iw = $item [ 'inOdet' ] > 0 ? 'inOdet = 0' : '' ;
$col = ItemsModel :: itemsX ( $itemId );
Db :: sql ( 'insert into clan_operations (clan, type, text, time, val, uid) values (?,6,?,unix_timestamp(),?,?)' , [ $this -> clanId , $this -> userLogin , " { $item [ 'name' ] } (x $col ) Id: $itemId У персонажа: { $item [ 'uid' ] } " , $this -> userId ]);
Db :: sql ( " update items_users set lastUPD = unix_timestamp(), $iw , uid = ? where id = ? " , [ " -21 $this->clanId " , $itemId ]);
return " Вы успешно изъяли предмет « { $item [ 'name' ] } » " ;
}
public function put ( int $itemId ) : string
{
$item = ItemsModel :: getOwnedItemById ( $itemId , $this -> userId );
$clanName = Db :: getValue ( 'select name from clan where id = ?' , [ $this -> clanId ]) ? ? '' ;
2023-04-18 09:21:54 +00:00
if ( empty ( $clanName )) {
2023-07-19 12:36:13 +00:00
return 'Клан не найден.' ;
2023-04-18 09:21:54 +00:00
}
2024-12-10 11:09:03 +00:00
if ( ! $this -> isGoodForStorage ( $item )) {
2023-04-18 09:21:54 +00:00
return $this -> error ;
}
2024-12-10 11:09:03 +00:00
$col = ItemsModel :: itemsX ( $itemId );
2023-08-14 15:15:05 +00:00
$idata = Conversion :: dataStringToArray ( $item [ 'data' ]);
2023-04-18 09:21:54 +00:00
if ( ! isset ( $idata [ 'toclan' ])) {
2024-12-10 11:09:03 +00:00
$idata [ 'toclan' ] = $this -> clanId . '#' . $this -> userId ;
2023-08-14 15:15:05 +00:00
$item [ 'data' ] = Conversion :: arrayToDataString ( $idata );
2023-12-01 21:05:11 +00:00
Delo :: add (
2023-04-18 09:21:54 +00:00
2 ,
2023-12-01 21:05:11 +00:00
'System.transfer.clan' ,
2024-12-10 11:09:03 +00:00
$this -> userId ,
'"System.transfer.MONEY": Предмет "' . $item [ 'name' ] . '" (#id : "' . $itemId . '") (x' . $col . ') был пожертвован клану "' . $clanName . '" (' . $this -> clanId . ').' ,
2023-04-18 09:21:54 +00:00
);
$sendType = 4 ;
} else {
$sendType = 3 ;
}
Db :: sql (
'insert into clan_operations (clan, time, type, text, val, uid) values (?,unix_timestamp(),?,?,?,?)' ,
2024-12-10 11:09:03 +00:00
[ $this -> clanId , $sendType , $this -> userLogin , $item [ 'name' ] . ( " x $col Ид: [ $itemId ] " ), $this -> userId ]
2023-04-18 09:21:54 +00:00
);
Db :: sql (
'update items_users set lastUPD = unix_timestamp(), uid = ?, data = ? where id = ?' ,
2024-12-10 11:09:03 +00:00
[ '-21' . $this -> clanId , $item [ 'data' ], $itemId ]
2023-04-18 09:21:54 +00:00
);
2023-07-19 12:36:13 +00:00
return 'Вы передали предмет "' . $item [ 'name' ] . '" клану.' ;
2023-04-18 09:21:54 +00:00
}
2024-12-10 11:09:03 +00:00
private function isGoodForStorage ( array $item ) : bool
2023-07-31 17:06:51 +00:00
{
if ( empty ( $item [ 'inslot' ]) || ! empty ( $item [ 'gift' ])) {
$this -> error = 'Предмет не найден.' ;
}
2023-08-14 15:15:05 +00:00
$idata = Conversion :: dataStringToArray ( $item [ 'data' ]);
2023-07-31 17:06:51 +00:00
if (
! empty ( $idata [ 'frompisher' ]) ||
! empty ( $idata [ 'sudba' ]) ||
! empty ( $idata [ 'icos' ]) ||
! empty ( $idata [ 'fromlaba' ])
) {
$this -> error = 'Предмет не подходит.' ;
}
2024-12-10 11:09:03 +00:00
if ( ! empty ( $idata [ 'toclan' ]) && explode ( '#' , $idata [ 'toclan' ])[ 0 ] == $this -> clanId ) {
2023-07-31 17:06:51 +00:00
$this -> error = 'Предмет уже принадлежит клану.' ;
}
return empty ( $this -> error );
}
2023-07-19 12:36:13 +00:00
}