game/_incl_data/class/Clan/ItemStorage.php

147 lines
6.6 KiB
PHP
Raw Permalink Normal View History

<?php
namespace Clan;
use Core\Db;
2023-12-01 21:05:11 +00:00
use Delo;
use Helper\Conversion;
use User;
use User\ItemsModel;
class ItemStorage
{
2024-12-10 11:09:03 +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']);
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
{
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]) ?? '';
if (empty($clanName)) {
return 'Клан не найден.';
}
2024-12-10 11:09:03 +00:00
if (!$this->isGoodForStorage($item)) {
return $this->error;
}
2024-12-10 11:09:03 +00:00
$col = ItemsModel::itemsX($itemId);
$idata = Conversion::dataStringToArray($item['data']);
if (!isset($idata['toclan'])) {
2024-12-10 11:09:03 +00:00
$idata['toclan'] = $this->clanId . '#' . $this->userId;
$item['data'] = Conversion::arrayToDataString($idata);
2023-12-01 21:05:11 +00:00
Delo::add(
2,
2023-12-01 21:05:11 +00:00
'System.transfer.clan',
2024-12-10 11:09:03 +00:00
$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(),?,?,?,?)',
2024-12-10 11:09:03 +00:00
[$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 = ?',
2024-12-10 11:09:03 +00:00
['-21' . $this->clanId, $item['data'], $itemId]
);
return 'Вы передали предмет &quot;' . $item['name'] . '&quot; клану.';
}
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 = 'Предмет не найден.';
}
$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);
}
}