diff --git a/classes/Battles/ShopItem.php b/classes/Battles/ShopItem.php index e0b9fd9..b01041c 100644 --- a/classes/Battles/ShopItem.php +++ b/classes/Battles/ShopItem.php @@ -1,5 +1,7 @@ ofetch('select * from shop where item_id = ?', $id); + $query = "INSERT INTO inventory ( owner_id, name, item_type, durability, price, need_strength, need_dexterity, need_intuition, need_endurance, need_intelligence, need_wisdom, @@ -23,24 +26,51 @@ class ShopItem extends Item add_min_physical_damage, add_max_physical_damage, image, weight) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; - $values = [ - $owner, $this->name, $this->item_type, $this->durability, $this->price, - $this->need_strength, $this->need_dexterity, $this->need_intuition, - $this->need_endurance, $this->need_intelligence, $this->need_wisdom, - $this->add_strength, $this->add_dexterity, $this->add_intuition, - $this->add_endurance, $this->add_intelligence, $this->add_wisdom, - $this->add_accuracy, $this->add_evasion, $this->add_criticals, - $this->add_min_physical_damage, $this->add_max_physical_damage, - $this->image, $this->weight - ]; - $db->execute($query, $values); + $values = [ + $buyer->getId(), $item->name, $item->item_type, $item->durability, $item->price, + $item->need_strength, $item->need_dexterity, $item->need_intuition, + $item->need_endurance, $item->need_intelligence, $item->need_wisdom, + $item->add_strength, $item->add_dexterity, $item->add_intuition, + $item->add_endurance, $item->add_intelligence, $item->add_wisdom, + $item->add_accuracy, $item->add_evasion, $item->add_criticals, + $item->add_min_physical_damage, $item->add_max_physical_damage, + $item->image, $item->weight + ]; + $db->execute($query, $values); + $deloText = "{$buyer->getLogin()} купил товар «{$item->name}» id:($id) в магазине за $item->price кр."; + GameLogs::addUserLog($buyer->getId(), $deloText); + return "Предмет $item->name куплен за $item->price."; + } + + public static function sellItem($id, User $seller, $bankTrade = 0): string + { + $db = new DBPDO(); + $item = $db->ofetch('select * from inventory where item_id = ?', $id); + // Продажа за цену от нуля до половины стоимости. + $sellingPrice = mt_rand(0, $item->price / 2); + $db->execute('delete from inventory where item_id = ?', $id); + if ($bankTrade) { + $bank = new Bank($seller->getId()); + $bank->setMoney($bank->getMoney() - $sellingPrice); + Bank::setBankMoney($bank->getMoney(), $seller->getId(), 'shopsell'); + } else { + $db->execute('update users set money = money - ? where id = ?', [$sellingPrice, $_SESSION['uid']]); } + $deloText = "{$seller->getLogin()} продал товар «{$item->name}» id:($id) в магазине за $sellingPrice кр."; + GameLogs::addUserLog($seller->getId(), $deloText); + if ($sellingPrice == 0) { + $status = "После длительных и изнурительных торгов вы плюнули на всё и просто подарили ваш «{$item->name}» торговцу."; + } else { + $status = "Вы продали «{$item->name}» за $sellingPrice кр."; + } + return $status; } /** * Для кнопок управления под картинкой предмета в зависимости от ситуации. + * @param null $shopType */ - public function printControls($shopType = false) + public function printControls($shopType = null) { if ($shopType === 'marketput') { echo <<query('SELECT * FROM `ahero_shop` WHERE `count` > 0 AND `razdel` = ?i ORDER by `point` ASC', $_GET['otdel']); @@ -124,30 +126,12 @@ ORDER BY `update` DESC', null, $_SESSION['uid']); $sellItemId = $_GET['sell'] ?? 0; if ($sellItemId) { - $dress = db::c()->query('SELECT `id`, `name`, `cost`, `koll` FROM `inventory` WHERE `dressed` = 0 AND `artefact` = 0 AND `id` = ?i AND `owner` = ?i', $sellItemId, $user['id'])->fetch_assoc(); - $kols = ''; - if ($dress['koll']) { - $dress['cost'] *= $dress['koll']; - $kols = "(x{$dress['koll']}) "; - } - - $allcost = mt_rand(0, $dress['cost'] / 2); - db::c()->query('DELETE FROM `inventory` WHERE `id` = ?i', $dress['id']); - db::c()->query('UPDATE `users` SET `money` = `money` + ?i WHERE `id` = ?i', $allcost, $_SESSION['uid']); - if ($allcost == 0) { - $status = "После длительных и изнурительных торгов вы плюнули на всё и просто подарили ваш «{$dress['name']}» торговцу."; - } else { - $status = "Вы продали «{$dress['name']}» $kols за " . $allcost . " кр."; - } - - $deloText = "{$user->getLogin()} продал товар «{$dress['name']}» {$kols}id:({$dress['id']}) в магазине за {$allcost} кр."; - GameLogs::addUserLog($user->getId(), $deloText); -// Для обновления данных о деньгах на странице - $user['money'] += $allcost; + ShopItem::sellItem($sellItemId, $user); } if (!empty($_GET['buy'])) { - $dress = db::c()->query('SELECT * FROM `shop` WHERE `id`=?i', $_GET['buy'])->fetch_assoc(); + ShopItem::buyItem($_GET['buy'], $user); +/* $dress = db::c()->query('SELECT * FROM `shop` WHERE `id`=?i', $_GET['buy'])->fetch_assoc(); $itemWeight = db::c()->query('SELECT sum(`massa`) AS `m` FROM `inventory` WHERE `owner` = ?i AND `setsale` = 0', $_SESSION['uid'])->fetch_assoc(); if (($dress['massa'] + $itemWeight['m']) > (get_meshok())) { @@ -236,7 +220,7 @@ if (!empty($_GET['buy'])) { } else { $status = "Недостаточно денег или нет вещей в наличии."; - } + }*/ } Template::header('Магазин'); ?>