game/_incl_data/class/Clan/ItemStorage.php
2024-12-10 13:09:03 +02:00

147 lines
6.6 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 Clan;
use Core\Db;
use Delo;
use Helper\Conversion;
use User;
use User\ItemsModel;
class ItemStorage
{
private string $error = '';
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']);
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
{
$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]) ?? '';
if (empty($clanName)) {
return 'Клан не найден.';
}
if (!$this->isGoodForStorage($item)) {
return $this->error;
}
$col = ItemsModel::itemsX($itemId);
$idata = Conversion::dataStringToArray($item['data']);
if (!isset($idata['toclan'])) {
$idata['toclan'] = $this->clanId . '#' . $this->userId;
$item['data'] = Conversion::arrayToDataString($idata);
Delo::add(
2,
'System.transfer.clan',
$this->userId,
'&quot;System.transfer.MONEY&quot;: Предмет &quot;' . $item['name'] . '&quot; (#id : "' . $itemId . '") (x' . $col . ') был пожертвован клану &quot;' . $clanName . '&quot; (' . $this->clanId . ').',
);
$sendType = 4;
} else {
$sendType = 3;
}
Db::sql(
'insert into clan_operations (clan, time, type, text, val, uid) values (?,unix_timestamp(),?,?,?,?)',
[$this->clanId, $sendType, $this->userLogin, $item['name'] . ("x$col Ид: [$itemId]"), $this->userId]
);
Db::sql(
'update items_users set lastUPD = unix_timestamp(), uid = ?, data = ? where id = ?',
['-21' . $this->clanId, $item['data'], $itemId]
);
return 'Вы передали предмет &quot;' . $item['name'] . '&quot; клану.';
}
private function isGoodForStorage(array $item): bool
{
if (empty($item['inslot']) || !empty($item['gift'])) {
$this->error = 'Предмет не найден.';
}
$idata = Conversion::dataStringToArray($item['data']);
if (
!empty($idata['frompisher']) ||
!empty($idata['sudba']) ||
!empty($idata['icos']) ||
!empty($idata['fromlaba'])
) {
$this->error = 'Предмет не подходит.';
}
if (!empty($idata['toclan']) && explode('#', $idata['toclan'])[0] == $this->clanId) {
$this->error = 'Предмет уже принадлежит клану.';
}
return empty($this->error);
}
}