Отображение предметов через stdClass вместо массивов.

This commit is contained in:
lopar
2021-03-10 23:20:56 +02:00
parent 9046670081
commit 279422ee9e
4 changed files with 27 additions and 20 deletions
+7 -7
View File
@@ -27,16 +27,16 @@ class DressedItems
public static function getDressedItemBySlot($itemSlot, $ownerId)
{
return self::$db->fetch('SELECT * FROM inventory WHERE owner_id = ? AND dressed_slot = ?', [$ownerId, $itemSlot]);
return self::$db->fetch('SELECT *, COUNT(1) AS count FROM inventory WHERE owner_id = ? AND dressed_slot = ?', [$ownerId, $itemSlot]);
}
public function getItemsInSlots()
public function getItemsInSlots(): \stdClass
{
$items = self::$db->ofetchALL('SELECT * FROM inventory WHERE owner_id = ? AND dressed_slot > 0', $this->USERID);
$i = 0;
while ($i < count($items)) {
$this->dressedItem[$items[$i]->dressed_slot] = $items;
$i++;
$this->dressedItem = new \stdClass();
foreach ($items as $item) {
$i = $item->dressed_slot;
$this->dressedItem->$i = $item;
}
return $this->dressedItem;
}
@@ -123,7 +123,7 @@ SQL;
{
self::getItemsInSlots();
// Проверяем, что используется один из 12 слотов и наличие предмета в слоте.
if (in_array($slot_id, Item::ITEM_TYPES_ALLOWED_IN_SLOTS) && $this->dressedItem[$slot_id]) {
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]);
}
}
+11 -3
View File
@@ -49,9 +49,17 @@ abstract class Item
*/
public function __construct($row)
{
foreach ($this as $key => $value) {
if (isset($row[$key])) {
$this->$key = $row[$key];
if (is_array($row)) {
foreach ($this as $key => $value) {
if (isset($row[$key])) {
$this->$key = $row[$key];
}
}
} elseif (is_object($row)) {
foreach ($this as $name => $value) {
if (isset($row->$name)) {
$this->$name = $row->$name;
}
}
}
+5 -5
View File
@@ -25,13 +25,13 @@ class UserInfo extends User
$dressedItems = $di->getItemsInSlots();
for ($i = 1; $i <= 12; $i++) {
echo sprintf('<div class="slot-%s">', $i);
if (!empty($dressedItems[$i])) {
if (!empty($dressedItems->$i)) {
if (!$isBattle && $isMain) {
$itemString = '<a href="?edit=%s&drop=%s"><img src="/i/sh/%s" class="item-wrap-normal" alt="%s" title="%s"></a>';
echo sprintf($itemString, mt_rand(), $i, $dressedItems[$i]['image'], $dressedItems[$i]['name'], $dressedItems[$i]['name']);
echo sprintf('<a href="?edit=%s&drop=%s"><img src="/i/sh/%s" class="item-wrap-normal" alt="%s" title="%s"></a>',
mt_rand(), $i, $dressedItems->$i->image, $dressedItems->$i->name, $dressedItems->$i->name);
} else {
$itemString = '<img src="/i/sh/%s" class="item-wrap-normal tip" alt="%s"><span class="tiptext"><strong>%s</strong></span>';
echo sprintf($itemString, $dressedItems[$i]['image'], $dressedItems[$i]['name'], $dressedItems[$i]['name']);
echo sprintf('<img src="/i/sh/%s" class="item-wrap-normal tip" alt="%s"><span class="tiptext"><strong>%s</strong></span>',
$dressedItems->$i->image, $dressedItems->$i->name, $dressedItems->$i->name);
}
} else {
echo sprintf('<img src="/i/sh/noitem.png" class="item-wrap-normal" title="Пустой слот [%s]" alt="Пустой слот [%s]">', $i, $i);