game/_incl_data/class/User/ItemsModel.php

239 lines
8.8 KiB
PHP
Raw Permalink Normal View History

<?php
namespace User;
use Core\Db;
use Delo;
use Helper\Conversion;
use User;
class ItemsModel
{
/** Проверяет предметы на отсутствие в инвентаре.
* @param array $itemIds id предметов.
* @return array id отсутствующих предметов.
*/
public static function hasNoItemsInInventory(array $itemIds): array
{
/* Очень прóклятая конструкция для очень сломанной проверки.
Скотина, если заработает с первого раза без проверки я буду горд собой. */
$uid = User::start()->info['id'];
$cols = count($itemIds);
$check = Db::getValue(
'select count(*) from items_users
where item_id in (?) and uid = ? and (`delete` = 0 or `delete` = 1000)',
[implode(',', $itemIds), $uid]
);
if ($cols === $check) {
return [];
}
$arr = $itemIds;
$query = 'select item_id from (select ' . $arr[0] . ' item_id';
$arr[0] = '';
$query .= implode(' union select ', $arr);
$query .= ') params left join items_users using (item_id) where items_users.item_id is null ';
$query .= 'and uid = ? and (`delete` = 0 or `delete` = 1000)';
//select item_id from (select 1 item_id union select 2 union select 3) params left join items_users using (item_id) where items_users.item_id is null
return Db::getColumn($query, [$uid]);
}
public static function getNamesByIds(array $ids, string $separator = ', '): string
{
$namesArray = Db::getColumn('select name from items_main where id in (?)', [$ids]);
return implode($separator, $namesArray);
}
//Удаление определенного типа предметов
/** Удаление предметов из инвентаря активного игрока.
* @param int $id id предмета.
* @param int $coldel количество предметов.
* @return void
*/
2023-11-02 13:57:39 +00:00
public static function deleteItemsById(int $id, int $coldel = 1): void
{
Db::sql(
2023-11-02 13:57:39 +00:00
'delete from items_users
where id in (select id from items_users where item_id = ? and uid = ? and (`delete` = 0 or `delete` = 1000) order by inGroup desc limit ?)',
[$id, User::start()->info['id'], $coldel]
);
}
2023-04-15 23:54:34 +00:00
//вес предметов у юзера
public static function inventoryWeightAndItemQuantity(): array
{
$uid = User::start()->info['id'];
$stats = User::start()->stats;
return [
'now' => Db::getValue('select sum(massa) from items_users left join items_main on item_id = items_main.id
where uid = ? and (`delete` = 0 or (`delete` = 1000 and inGroup > 0)) and inShop = 0 and inOdet = 0', [$uid]),
'max' => 40 + (($stats['os7'] ?? 0) * 10) + $stats['s4'] + $stats['maxves'] + $stats['s1'] * 4,
2023-04-15 23:54:34 +00:00
'items' => Db::getValue('select count(*) from items_users where uid = ? and `delete` = 0 and inShop = 0 and inOdet = 0', [$uid]),
];
}
2023-11-02 13:57:39 +00:00
/** Выбор предмета в инвентаре.
* @param int $itemId
* @param int $ownerId
2024-12-10 11:09:03 +00:00
* @param bool $includeWeared
2023-11-02 13:57:39 +00:00
* @return array
*/
2024-12-10 11:09:03 +00:00
public static function getOwnedItemById(int $itemId, int $ownerId, bool $includeWeared = false): array
2023-11-02 13:57:39 +00:00
{
2024-12-10 11:09:03 +00:00
$iw = $includeWeared ? 1 : 0;
return Db::getRow("select * from items_users left join items_main on item_id = items_main.id
where uid = ? and items_users.id = ? and `delete` = 0 and inOdet = $iw and inShop = 0 and is_arrested = 0", [$ownerId, $itemId]);
2023-11-02 13:57:39 +00:00
}
public static function delete(int $id): void
{
Db::sql('delete from items_users where id = ?', [$id]);
}
public static function getDressedEkrTotalPrice($uid)
{
return Db::getValue('select sum(2price) from items_users where inOdet > 0 and uid = ?', [$uid]);
}
public static function getWearedItems(int $userId): array
{
return Db::getRows('select * from items_users where inOdet > 0 and uid = ?', [$userId]);
}
/** Порча предмета
* @param $it
* @param $tp
* @param $uid
* @param $id2
* @param $type
* @param $name
* @return void
*/
public static function itemDecay($it, $tp, $uid, $id2, $type, $name): void
{
if ($it == 0) {
return;
}
if ($id2 == 4708) {
$tp = 1;
}
$sql = 'delete from items_users where id = ?';
$sql2 = 'update items_users set inGroup = 0, timeOver = unix_timestamp() where inGroup = ?';
$args = [$it];
if ($uid != 0) {
$sql .= 'and uid = ?';
$sql2 .= 'and uid = ?';
$args[] = $uid;
}
Db::sql($sql, $args);
Db::sql($sql2, $args);
Delo::add(2, 'system.inventory', $uid, "Предмет <b>$name</b> [id:$it] был испорчен.");
if ($tp == 0) {
return;
}
//Добавляем испорченый предмет в инвентарь, в зависимости от типа
if ($id2 > 0) {
$moddata = '|noodet=1';
if ($id2 == 4708) {
$moddata = '|notransfer=1|nosale=1';
}
ItemsModel::addItem($id2, $uid, $moddata);
} elseif ($type == 30) {
//испорченный эликсир
$id2 = 4036;
$moddata = "|renameadd=$name|noodet=1";
ItemsModel::addItem($id2, $uid, $moddata);
}
}
2023-04-15 23:54:34 +00:00
public static function addItem($id, $uid, $md = null, $dn = null, $mxiznos = null, $nosudba = null, $plavka = null)
{
$user = User::start();
$rt = -1;
$i = Db::getRow('select * from items_main where id = ?', [$id]);
if (isset($i['id'])) {
$d = Db::getRow('select id, items_id, data from items_main_data where items_id = ?', [$i['id']]);
//новая дата
$data = $d['data'];
if ($i['ts'] > 0 && $nosudba == null) {
$ui = Db::getValue('select login from users where id = ?', [$uid]);
$data .= '|sudba=' . $ui;
}
if ($md != null) {
$data .= $md;
$data = Conversion::dataStringToArray($data); // Если в функции имеются две одинаковых константы SROK?
$data = Conversion::arrayToDataString($data);
2023-04-15 23:54:34 +00:00
}
//предмет с настройками из подземелья
if ($dn != null && $dn['dn_delete'] > 0) {
$i['dn_delete'] = 1;
}
if ($mxiznos > 0) {
$i['iznosMAXi'] = $mxiznos;
}
2023-04-15 23:54:34 +00:00
$args = [
$i['overTypei'] ?? 0,
2023-04-15 23:54:34 +00:00
$i['id'],
$uid,
$data,
$i['iznosMAXi'],
$i['magic_inci'],
$i['dn_delete'] ?? 0,
];
Db::sql(
'insert into items_users (overType, item_id, uid, data, iznosMAX, magic_inc, lastUPD, time_create, dn_delete) values (?,?,?,?,?,?,unix_timestamp(),unix_timestamp(),?)',
2023-04-15 23:54:34 +00:00
$args
);
$rt = Db::lastInsertId() ?? 0;
if ($rt !== 0) {
Db::sql('update items_users set dn_delete = 1 where id = ? and data like ?', [$rt, '%dn_delete=%']);
if ($uid == $user->info['id']) {
$user->stack($rt);
}
$ads = '';
if ($plavka != null) {
$ads = 'Расплавлен предмет : [' . $plavka . ']';
}
//Записываем в личное дело что предмет получен
Delo::add(1, 'additems', $uid, 'Получен предмет «' . $i['name'] . '» [id:' . $i['iid'] . ']' . $ads);
2023-04-15 23:54:34 +00:00
}
}
return $rt;
}
public static function addIznos(int $itemId): void
{
Db::sql('update items_users set iznosNOW = iznosNOW + 1 where id = ?', [$itemId]);
}
2024-12-10 11:09:03 +00:00
public static function itemsX(int $id, $uid = null, $item_id = null): bool|int
{
//fixme какая-то дичь тут.
$item = Db::getRow('select uid, item_id, ingroup, inshop from items_users where id = ?', [$id]);
if ($uid) {
$item['uid'] = $uid;
}
if ($item_id) {
$item['item_id'] = $item_id;
}
return Db::getValue(
'select count(id) from items_users where inshop = ? and item_id = ? and uid = ? and ingroup = ?',
[$item['inShop'], $item['item_id'], $item['uid'], $item['ingroup']]
);
}
}