game/_incl_data/class/User/ItemsModel.php

151 lines
6.1 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 User;
use Core\ConversionHelper;
use Core\Db;
use Delo;
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]
);
}
//вес предметов у юзера
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'] * 10) + $stats['s4'] + $stats['maxves'] + $stats['s1'] * 4,
'items' => Db::getValue('select count(*) from items_users where uid = ? and `delete` = 0 and inShop = 0 and inOdet = 0', [$uid]),
];
}
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 = ConversionHelper::dataStringToArray($data); // Если в функции имеются две одинаковых константы SROK?
$data = ConversionHelper::arrayToDataString($data);
}
//предмет с настройками из подземелья
if ($dn != null && $dn['dn_delete'] > 0) {
$i['dn_delete'] = 1;
}
if ($mxiznos > 0) {
$i['iznosMAXi'] = $mxiznos;
}
$args = [
$i['overTypei'] ?? 0,
$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(),?)',
$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);
}
}
return $rt;
}
/** Выбор предмета в инвентаре.
* @param int $itemId
* @param int $ownerId
* @return array
*/
public static function getOwnedItemById(int $itemId, int $ownerId): array
{
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 = 0 and inShop = 0', [$ownerId, $itemId]);
}
public static function delete(int $id)
{
Db::sql('update items_users set `delete` = unix_timestamp() where id = ?', [$id]);
}
}