63 lines
2.4 KiB
PHP
63 lines
2.4 KiB
PHP
|
<?php
|
|||
|
|
|||
|
namespace User;
|
|||
|
|
|||
|
use Core\Db;
|
|||
|
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
|
|||
|
*/
|
|||
|
public static function deleteItemsById(int $id, int $coldel = 1)
|
|||
|
{
|
|||
|
Db::sql(
|
|||
|
'update items_users set `delete` = unix_timestamp() 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]
|
|||
|
);
|
|||
|
}
|
|||
|
}
|