From 54f4c08678932b03780a411f886aa6438bc18c21 Mon Sep 17 00:00:00 2001 From: lopar Date: Mon, 30 Aug 2021 23:19:40 +0300 Subject: [PATCH] =?UTF-8?q?=D0=91=D0=B0=D1=80=D1=82=D0=B5=D1=80.=20#40.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- classes/Battles/ShopItem.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/classes/Battles/ShopItem.php b/classes/Battles/ShopItem.php index 9ecd644..214ad3f 100644 --- a/classes/Battles/ShopItem.php +++ b/classes/Battles/ShopItem.php @@ -63,8 +63,10 @@ SQL; $this->price = $row->price ?? null; $this->shop_item_quantity = $row->shop_item_quantity ?? null; $this->item_id = $row->item_id ?? $row->id; - $this->jsonBarterList = $row->barter_items_list_json; - $this->offerId = $row->offer_id; // Ид позиции в магазине. + if ($operationType === 'buyshop') { + $this->offerId = $row->offer_id; // Ид позиции в магазине. + $this->jsonBarterList = $row->barter_items_list_json; + } } public function printInfo(): string @@ -127,12 +129,12 @@ SQL; public static function buyItem($id, User $buyer) { - $check = DBPDO::$db->ofetch("select * from trade_offers where shop_item_id = ?", $id); - $item = new Item(DBPDO::$db->fetch('select * from items where id = ?', $id)); + $check = DBPDO::$db->ofetch("select * from trade_offers where offer_id = ?", $id); + $item = new Item(DBPDO::$db->fetch('select * from items where id = ?', $check->shop_item_id)); $price = $item->calculateItemCost(); if ( - !self::checkAndRemoveBarteredItems($check->barter_item_list_json, $buyer->getId()) || + !self::checkAndRemoveBarteredItems($check->barter_items_list_json, $buyer->getId()) || !self::checkAndPayTheBills($price, $buyer) || !self::checkAndChangeRemainingItems($check->shop_item_quantity, $check->shop_item_id) ) { @@ -140,12 +142,12 @@ SQL; } DBPDO::$db->execute(self::BUY_QUERY, [$buyer->getId(), $check->shop_item_id]); - $deloText = $buyer->getLogin() . " купил товар «" . Item::getItemById($item->item_id)->name . "» id:(" . $item->item_id . ") в магазине за " . $price . "."; + $deloText = $buyer->getLogin() . " купил товар «" . $item->name . "» id:(" . $check->shop_item_id . ") в магазине за " . $price . "."; GameLogs::addUserLog($buyer->getId(), $deloText); self::$status = "Предмет " . $item->name . " куплен за " . $price . "."; } - private static function checkAndRemoveBarteredItems(string $json_list, int $user_id): bool + private static function checkAndRemoveBarteredItems(?string $json_list, int $user_id): bool { if (empty($json_list)) { return true; @@ -162,10 +164,11 @@ SQL; return false; } foreach (json_decode($json_list) as $item) { - DBPDO::$db->execute('delete from inventory where name = ? and owner_id = ? limit ?', [Item::getItemById($item->item_id)->name, $user_id, $item->quantity]); + $query = 'delete from inventory where name = ? and owner_id = ? limit ' . (int)$item->quantity; + // У-у-у, сука! https://phpdelusions.net/pdo#limit + DBPDO::$db->execute($query, [Item::getItemById($item->item_id)->name, $user_id]); } return true; - } private static function checkAndPayTheBills(int $price, User $user): bool @@ -244,10 +247,11 @@ SQL; return ''; } $str = $this->optype == 'setmarket' ? '' : ''; + $hiddenValue = $this->optype === 'buyshop' ? $this->offerId : $this->item_id; $button_name = self::BUTTON[$this->optype]; return <<
$str - +
FORM;