2019-01-11 20:26:10 +00:00
|
|
|
|
<?php
|
2021-03-12 16:26:15 +00:00
|
|
|
|
|
2020-10-28 20:21:08 +00:00
|
|
|
|
namespace Battles;
|
2021-03-12 16:26:15 +00:00
|
|
|
|
|
2021-01-28 21:05:34 +00:00
|
|
|
|
use Battles\Database\DBPDO;
|
|
|
|
|
|
2019-01-11 20:26:10 +00:00
|
|
|
|
class ShopItem extends Item
|
|
|
|
|
{
|
|
|
|
|
public function printInfo()
|
|
|
|
|
{
|
2020-07-21 15:03:46 +00:00
|
|
|
|
parent::printAllInfo();
|
2019-01-11 20:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-12 16:26:15 +00:00
|
|
|
|
public static function buyItem($id, User $buyer): string
|
2019-01-15 15:56:15 +00:00
|
|
|
|
{
|
2021-03-12 16:26:15 +00:00
|
|
|
|
//TODO Добавить снятие денег с проверками на их наличие.
|
|
|
|
|
$db = new DBPDO();
|
|
|
|
|
$item = $db->ofetch('select * from shop where item_id = ?', $id);
|
|
|
|
|
$query = "INSERT INTO inventory (
|
2021-01-28 21:05:34 +00:00
|
|
|
|
owner_id, name, item_type, durability, price,
|
|
|
|
|
need_strength, need_dexterity, need_intuition,
|
|
|
|
|
need_endurance, need_intelligence, need_wisdom,
|
|
|
|
|
add_strength, add_dexterity, add_intuition,
|
|
|
|
|
add_endurance, add_intelligence, add_wisdom,
|
|
|
|
|
add_accuracy, add_evasion, add_criticals,
|
|
|
|
|
add_min_physical_damage, add_max_physical_damage,
|
|
|
|
|
image, weight)
|
|
|
|
|
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
|
2021-03-12 16:26:15 +00:00
|
|
|
|
$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 кр.";
|
2019-01-15 15:56:15 +00:00
|
|
|
|
}
|
2021-03-12 16:26:15 +00:00
|
|
|
|
return $status;
|
2019-01-15 15:56:15 +00:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-11 20:26:10 +00:00
|
|
|
|
/**
|
2021-01-28 21:05:34 +00:00
|
|
|
|
* Для кнопок управления под картинкой предмета в зависимости от ситуации.
|
2021-03-12 16:26:15 +00:00
|
|
|
|
* @param null $shopType
|
2019-01-11 20:26:10 +00:00
|
|
|
|
*/
|
2021-03-12 16:26:15 +00:00
|
|
|
|
public function printControls($shopType = null)
|
2019-01-11 20:26:10 +00:00
|
|
|
|
{
|
2019-02-15 15:00:01 +00:00
|
|
|
|
if ($shopType === 'marketput') {
|
|
|
|
|
echo <<<BTN
|
|
|
|
|
<form method="post">
|
2021-01-28 21:05:34 +00:00
|
|
|
|
<input placeholder="{$this->price}" name="cost">
|
|
|
|
|
<input type="hidden" name="putId" value="{$this->item_id}">
|
2019-02-15 15:00:01 +00:00
|
|
|
|
<br><input type="submit" name="putToMarket" value="Cдать в магазин">
|
|
|
|
|
</form>
|
|
|
|
|
BTN;
|
|
|
|
|
} else {
|
|
|
|
|
switch ($shopType) {
|
|
|
|
|
default:
|
2021-01-28 21:05:34 +00:00
|
|
|
|
$btnValue = "Купить за " . intval($this->price) . " кр.";
|
|
|
|
|
$btnLink = "/shop.php?buy={$this->item_id}&rnd=" . mt_rand();
|
2019-02-15 15:00:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'sell':
|
|
|
|
|
$btnValue = "Продать";
|
2021-01-28 21:05:34 +00:00
|
|
|
|
$btnLink = "/shop.php?sell={$this->item_id}&rnd=" . mt_rand();
|
2019-02-15 15:00:01 +00:00
|
|
|
|
break;
|
|
|
|
|
case 'marketgetback':
|
|
|
|
|
$btnValue = "Снять с продажи";
|
2021-01-28 21:05:34 +00:00
|
|
|
|
$btnLink = "?back={$this->item_id}&rnd=" . mt_rand();
|
2019-02-15 15:00:01 +00:00
|
|
|
|
break;
|
2019-02-15 16:39:47 +00:00
|
|
|
|
case 'marketbuy':
|
2019-02-15 17:02:03 +00:00
|
|
|
|
$btnValue = "Купить за " . intval($this->setsale) . " кр.";
|
2021-01-28 21:05:34 +00:00
|
|
|
|
$btnLink = "?otdel={$this->item_type}&set={$this->item_id}&rnd=" . mt_rand();
|
2019-02-15 15:00:01 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
echo <<<BTN
|
2019-01-16 20:20:47 +00:00
|
|
|
|
<p><input type="button" style="background: darkgrey; border: 1px solid grey; border-radius: 2px;" value="{$btnValue}"
|
2019-01-14 16:42:45 +00:00
|
|
|
|
onclick="location='{$btnLink}'">
|
2019-01-11 20:26:10 +00:00
|
|
|
|
BTN;
|
2019-02-15 15:00:01 +00:00
|
|
|
|
}
|
2019-01-11 20:26:10 +00:00
|
|
|
|
}
|
|
|
|
|
}
|