battles/classes/Battles/DressedItems.php
Igor Barkov (iwork) 3502904656 Рефакторинг, очистка, работа над ошибками, связанными с базой, отказ от глобальной переменной $user во многих файлах.
Singleton в некоторых местах вместо решения #42.
Новые шаги для решения #16 и #52.
Closes #42.
Closes #32.
Closes #31.
2022-01-27 01:15:33 +02:00

61 lines
1.9 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
/**
* Author: lopiu
* Date: 06.07.2020
* Time: 22:41
*/
namespace Battles;
use Battles\Database\Db;
use stdClass;
class DressedItems
{
private $USERID;
private $dressedItem;
private static $db;
/**
* DressedItems constructor.
* @param int $user_id ID игрока.
*/
public function __construct(int $user_id)
{
self::$db = Db::getInstance();
$this->USERID = $user_id;
}
public static function getDressedItemBySlot($itemSlot, $ownerId)
{
return self::$db->ofetch('SELECT *, COUNT(1) AS count FROM inventory WHERE owner_id = ? AND dressed_slot = ?', [$ownerId, $itemSlot]);
}
public function getItemsInSlots(): stdClass
{
$items = self::$db->ofetchALL('SELECT * FROM inventory WHERE owner_id = ? AND dressed_slot > 0', $this->USERID);
$this->dressedItem = new stdClass();
foreach ($items as $item) {
$i = $item->dressed_slot;
$this->dressedItem->$i = $item;
}
return $this->dressedItem;
}
/**
* Снимает с предмета статус одетого на персонажа в определённом слоте персонажа.
* @param $slot_id - номер слота.
*/
public function undressItem($slot_id)
{
self::getItemsInSlots();
// Проверяем, что используется один из 12 слотов и наличие предмета в слоте.
if (in_array($slot_id, Item::ITEM_TYPES_ALLOWED_IN_SLOTS) && $this->dressedItem->$slot_id) {
self::$db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = ? AND owner_id = ?', [$slot_id, $this->USERID]);
}
}
public static function undressAllItems($user_id)
{
return self::$db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot BETWEEN 1 AND 12 AND owner_id = ?', $user_id);
}
}