game/_incl_data/class/User/ItemsModel.php

219 lines
8.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\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
*/
public static function deleteItemsById(int $id, int $coldel = 1): void
{
Db::sql(
'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]
);
}
//вес предметов у юзера
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,
'items' => Db::getValue('select count(*) from items_users where uid = ? and `delete` = 0 and inShop = 0 and inOdet = 0', [$uid]),
];
}
/** Выбор предмета в инвентаре.
* @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 and is_arrested = 0', [$ownerId, $itemId]);
}
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);
}
}
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);
}
//предмет с настройками из подземелья
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;
}
public static function addIznos(int $itemId): void
{
Db::sql('update items_users set iznosNOW = iznosNOW + 1 where id = ?', [$itemId]);
}
}