'Сдать в магазин', 'buymarket' => 'Купить с рук', 'sellshop' => 'Продать', 'buyshop' => 'Купить', ]; private const BUY_QUERY = <<optype = $operationType; } $this->price = $row->price ?? null; $this->shopItemQuantity = $row->shop_item_quantity ?? null; $this->id = $row->item_id ?? $row->id; if ($operationType === 'buyshop' || $operationType === 'buymarket') { $this->offerId = $row->offer_id ?? 0; // Ид позиции в магазине. $this->jsonBarterList = $row->barter_items_list_json ?? null; } if ($operationType === 'buymarket') { $this->ownerId = $row->owner_id; } } public function printInfo(): string { $str = $this->getAllInfo(); if ($this->optype === 'buyshop') { $str .= $this->getLowItemQuantityNote(); $str .= $this->getBarterList(); } if ($this->optype === 'sellshop') { $str .= $this->getTextBasedOnPrice(); } if ($this->optype === 'buymarket') { $str .= $this->getBarterList(); $str .= '

Продавец: ' . Nick::id($this->ownerId)->full(1); } return $str; } private function getBarterList(): string { if (!$this->jsonBarterList) { return ''; } $str = '

Помимо денег требуются следующие товары:'; foreach (json_decode($this->jsonBarterList) as $item) { $str .= '
↣ ' . Item::getItemById($item->item_id)->name . ', ' . $item->quantity . ' шт.'; } $str .= '
'; return $str; } private function getLowItemQuantityNote(): string { if ($this->shopItemQuantity < 1 || $this->shopItemQuantity > 19) { return ''; } return "
На складе осталось $this->shopItemQuantity единиц товара!
"; } private function getTextBasedOnPrice(): string { if ($this->getSellPriceMean() < 50) { $goods = 'этот хлам'; } elseif ($this->getSellPriceMean() < 100) { $goods = 'этот посредственный товар'; } elseif ($this->getSellPriceMean() < 500) { $goods = 'этот неплохой предмет'; } elseif ($this->getSellPriceMean() < 1000) { $goods = 'эту отличную штуку'; } else { $goods = 'это превосходное изделие'; } return "
В среднем за $goods можно выручить {$this->getSellPriceMean()} кр.
"; } public function printImage(): string { if (!$this->image) { $this->image = 'noitem.png'; } return ""; } public static function buyItem($id) { $check = Db::getInstance()->ofetch("select * from trade_offers where offer_id = ?", $id); $item = new Item(Db::getInstance()->fetch('select * from items where id = ?', $check->shop_item_id)); $price = $item->calculateItemCost(); if ( !self::checkAndRemoveBarteredItems($check->barter_items_list_json, User::getInstance()->getId()) || !self::checkAndPayTheBills($price) || !self::checkAndChangeRemainingItems($check->shop_item_quantity, $check->shop_item_id) ) { return; } Db::getInstance()->execute(self::BUY_QUERY, [User::getInstance()->getId(), $check->shop_item_id]); $deloText = User::getInstance()->getLogin() . " купил товар «" . $item->name . "» id:(" . $check->shop_item_id . ") в магазине за " . $price . "."; GameLogs::addUserLog(User::getInstance()->getId(), $deloText); self::$status = "Предмет " . $item->name . " куплен за " . $price . "."; } private static function checkAndRemoveBarteredItems(?string $jsonList, int $userId): bool { if (empty($jsonList)) { return true; } $allowItemRemove = true; foreach (json_decode($jsonList) as $item) { $row = Db::getInstance()->ofetch('select sum(1) as s from inventory where name = ? and owner_id = ?', [Item::getItemById($item->item_id)->name, $userId]); if ($row->s < $item->quantity) { $allowItemRemove = false; } } if (!$allowItemRemove) { self::$status = self::NO_BARTER_ITEMS; return false; } foreach (json_decode($jsonList) as $item) { $query = 'delete from inventory where name = ? and owner_id = ? limit ' . (int)$item->quantity; // У-у-у, сука! https://phpdelusions.net/pdo#limit Db::getInstance()->execute($query, [Item::getItemById($item->item_id)->name, $userId]); } return true; } private static function checkAndPayTheBills(int $price): bool { if (User::getInstance()->money()->spend($price)) { return true; } (new Bank())->withdrawMoney($price); return true; } private static function checkAndChangeRemainingItems(int $currentQuantity, $itemId): bool { if (empty($currentQuantity)) { self::$status = self::NO_ITEMS_IN_STOCK; return false; } if ($currentQuantity === -1) { return true; } Db::getInstance()->execute("update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_quantity != -1 and shop_item_id = ? ", $itemId); return true; } public static function sellItem($id, $bankTrade = 0) { $item = Db::getInstance()->ofetch('select * from inventory where item_id = ?', $id); $sellingItemName = $item->name; // Продажа за цену от нуля до половины стоимости. $sellingPrice = $item->price > 1 ? mt_rand(0, $item->price / 2) : mt_rand(0, 1); Db::getInstance()->execute('delete from inventory where item_id = ?', $id); if ($bankTrade) { User::getInstance()->money()->modifyBank($sellingPrice, 'sellShop'); } else { User::getInstance()->money()->earn($sellingPrice); } $deloText = User::getInstance()->getLogin() . " продал товар «{$sellingItemName}» id:($id) в магазине за $sellingPrice кр."; GameLogs::addUserLog(User::getInstance()->getId(), $deloText); if ($sellingPrice == 0) { self::$status = "После длительных и изнурительных торгов вы плюнули на всё и просто подарили ваш «{$sellingItemName}» торговцу."; } else { self::$status = "Вы продали «{$sellingItemName}» за $sellingPrice кр."; } } /** Подчсчёт средней суммы продажи. * @return int */ private function getSellPriceMean(): ?int { if ($this->price > 1) { $arr = range(0, $this->price / 2); return array_sum($arr) / count($arr); } else { return $this->price == 1 ? 1 : null; } } /** * Для кнопок управления под картинкой предмета в зависимости от ситуации. */ public function printControls(): string { if (!in_array($this->optype, ['setmarket', 'buymarket', 'sellshop', 'buyshop',])) { return ''; } $str = $this->optype == 'setmarket' ? '' : ''; $hiddenValue = $this->optype === 'buyshop' ? $this->offerId : $this->id; $buttonName = self::BUTTON[$this->optype]; return <<
$str
FORM; } /** * @return int */ public function getItemType(): int { return $this->type; } /** Выдача магазинных предметов по запросу. * Ввелась чтобы перебить takeshopitem() в functions с идентичным функционалом. * * @param int $itemId ИД предмета. * @param int $to ИД пперсонажа-получателя. */ public static function giveNewItem(int $itemId, int $to): array { $check = Db::getInstance()->ofetch('select 1 from items where id = ?', $itemId); if (!$check) { return []; } Db::getInstance()->execute(self::BUY_QUERY, [$to, $itemId]); $return = Db::getInstance()->ofetch('select image, name from inventory where item_id = ?', Db::getInstance()->lastInsertId()); return [ 'img' => $return->image, 'name' => $return->name, 'id' => $itemId, ]; } }