Отображение предметов через 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

View File

@ -27,16 +27,16 @@ class DressedItems
public static function getDressedItemBySlot($itemSlot, $ownerId) 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); $items = self::$db->ofetchALL('SELECT * FROM inventory WHERE owner_id = ? AND dressed_slot > 0', $this->USERID);
$i = 0; $this->dressedItem = new \stdClass();
while ($i < count($items)) { foreach ($items as $item) {
$this->dressedItem[$items[$i]->dressed_slot] = $items; $i = $item->dressed_slot;
$i++; $this->dressedItem->$i = $item;
} }
return $this->dressedItem; return $this->dressedItem;
} }
@ -123,7 +123,7 @@ SQL;
{ {
self::getItemsInSlots(); self::getItemsInSlots();
// Проверяем, что используется один из 12 слотов и наличие предмета в слоте. // Проверяем, что используется один из 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]); self::$db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = ? AND owner_id = ?', [$slot_id, $this->USERID]);
} }
} }

View File

@ -49,9 +49,17 @@ abstract class Item
*/ */
public function __construct($row) public function __construct($row)
{ {
foreach ($this as $key => $value) { if (is_array($row)) {
if (isset($row[$key])) { foreach ($this as $key => $value) {
$this->$key = $row[$key]; 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;
}
} }
} }

View File

@ -25,13 +25,13 @@ class UserInfo extends User
$dressedItems = $di->getItemsInSlots(); $dressedItems = $di->getItemsInSlots();
for ($i = 1; $i <= 12; $i++) { for ($i = 1; $i <= 12; $i++) {
echo sprintf('<div class="slot-%s">', $i); echo sprintf('<div class="slot-%s">', $i);
if (!empty($dressedItems[$i])) { if (!empty($dressedItems->$i)) {
if (!$isBattle && $isMain) { 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('<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']); mt_rand(), $i, $dressedItems->$i->image, $dressedItems->$i->name, $dressedItems->$i->name);
} else { } else {
$itemString = '<img src="/i/sh/%s" class="item-wrap-normal tip" alt="%s"><span class="tiptext"><strong>%s</strong></span>'; echo sprintf('<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']); $dressedItems->$i->image, $dressedItems->$i->name, $dressedItems->$i->name);
} }
} else { } else {
echo sprintf('<img src="/i/sh/noitem.png" class="item-wrap-normal" title="Пустой слот [%s]" alt="Пустой слот [%s]">', $i, $i); echo sprintf('<img src="/i/sh/noitem.png" class="item-wrap-normal" title="Пустой слот [%s]" alt="Пустой слот [%s]">', $i, $i);

View File

@ -36,10 +36,9 @@ $edit = $_GET['edit'] ?? null;
// Подготавливаем отображение инфы и предметов. // Подготавливаем отображение инфы и предметов.
$userInfo = new UserInfo($user->getId()); $userInfo = new UserInfo($user->getId());
$getItemsBonuses = new DressedItems($_SESSION['uid']); $getItemsBonuses = new DressedItems($_SESSION['uid']);
$data_query = 'SELECT * FROM inventory WHERE owner_id = ?i AND dressed_slot = 0 AND on_sale = 0'; $data = \Battles\Database\DBPDO::INIT()->ofetchAll('SELECT * FROM inventory WHERE owner_id = ? AND dressed_slot = 0 AND on_sale = 0', $user->getId());
$data = db::c()->query($data_query, $_SESSION['uid']);
$iteminfo = []; $iteminfo = [];
while ($row = $data->fetch_assoc()) { foreach ($data as $row) {
$iteminfo[] = new InventoryItem($row); $iteminfo[] = new InventoryItem($row);
} }
@ -456,8 +455,8 @@ Template::header('Игра');
echo "<td valign='top' bgcolor='#d3d3d3'>"; echo "<td valign='top' bgcolor='#d3d3d3'>";
$ii->printInfo(); $ii->printInfo();
} }
if ($data->getNumRows() == 0) { if (!$data) {
echo "<tr><th colspan='3' align=center bgcolor=#C7C7C7>Пусто"; echo "<tr><th colspan='3' style='text-align: center; background-color: #c7c7c7'>Пусто";
} }
?> ?>
</table> </table>