Покупка товара за нал и за безнал в случае нехватки нала. Closes #25. #40.

This commit is contained in:
lopar 2021-08-22 23:43:42 +03:00
parent 2b1287397d
commit 8f506dced2
3 changed files with 38 additions and 25 deletions

View File

@ -45,12 +45,24 @@ class ShopItem extends Item
ifnull(add_max_physical_damage, 0),
ifnull(image, \'noitem.png\'),
ifnull(weight, 1),
?
greatest(
(
(add_strength + add_dexterity + add_intuition + add_endurance + add_intelligence + add_wisdom) *
(5 + floor((add_strength + add_dexterity + add_intuition + add_endurance + add_intelligence + add_wisdom) / 10))
) +
(
(add_accuracy + add_criticals + add_evasion) *
(2 + floor((add_accuracy + add_criticals + add_evasion) / 50))
) +
(
(add_min_physical_damage + add_max_physical_damage) *
(1 + floor((add_min_physical_damage + add_max_physical_damage) / 100))
)
,1)
from items where id = ?';
// Тип операции в магазине. Для отображения разных блоков в разных случаях.
private $optype;
private ?int $shop_item_quantity;
private ?int $bank_price;
private ?int $price;
public function __construct($row, $operationType = null)
@ -61,20 +73,14 @@ class ShopItem extends Item
}
$this->price = $row->price ?? null;
$this->shop_item_quantity = $row->shop_item_quantity ?? null;
$this->bank_price = $row->bank_price ?? null;
$this->item_id = $row->item_id ?? $row->id;
}
public function printInfo()
{
$this->printAllInfo();
if ($this->optype === 'buyshop') {
if ($this->bank_price) {
echo "<div>Цена: $this->bank_price банкнот.</div>";
}
if ($this->shop_item_quantity > 0 && $this->shop_item_quantity < 20) {
echo "<div style='margin-top: 9px; font-style: italic;'>На складе осталось $this->shop_item_quantity единиц товара!</div>";
}
if ($this->optype === 'buyshop' && $this->shop_item_quantity > 0 && $this->shop_item_quantity < 20) {
echo "<div style='margin-top: 9px; font-style: italic;'>На складе осталось $this->shop_item_quantity единиц товара!</div>";
}
if ($this->optype === 'sellshop') {
if ($this->getSellPriceMean() < 50) {
@ -104,7 +110,6 @@ class ShopItem extends Item
{
$db = new DBPDO();
$check = $db->ofetch("select * from trade_offers where shop_item_id = ?", $id);
$itemPrice = $check->bank_price ?? 0;
if (empty($check->shop_item_quantity) || empty($check->shop_item_id)) {
return self::NO_ITEMS_IN_STOCK;
}
@ -114,15 +119,14 @@ class ShopItem extends Item
echo "Работаем по бартеру!";
}
$db->execute(self::BUY_QUERY, [$buyer->getId(), $itemPrice, $check->shop_item_id]);
$db->execute(self::BUY_QUERY, [$buyer->getId(), $check->shop_item_id]);
$item = $db->ofetch("select item_id, name, price from inventory where item_id = ?", $db->lastInsertId());
if (empty($item->item_id) || empty($item->name) || empty($item->price)) {
if (empty($item->item_id) || empty($item->name)) {
return 'Запрос в базу не прошёл.';
} else {
$boughtItemId = $item->item_id;
$boughtItemName = $item->name;
$boughtItemPrice = $item->price;
if ($item->price > 0) {
$user = new User($_SESSION['uid']);
// Если не хватает налички, снимаем с банка с комиссией.
if ($user->getMoney() < $item->price) {
try {
$bank = new Bank($buyer->getId());
$bank->withdrawMoney($item->price);
@ -130,16 +134,20 @@ class ShopItem extends Item
echo 'Банковская ошибка!';
return self::NO_MONEY;
}
} else {
$user->setMoney($user->getMoney() - $item->price);
$user->saveMoney();
}
}
if ($check->shop_item_quantity != -1) {
$db->execute("update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_id = ?", $check->shop_item_id);
}
$deloText = $buyer->getLogin() . " купил товар «" . $boughtItemName . "» id:(" . $boughtItemId . ") в магазине за " . $boughtItemPrice . " банкнот.";
$deloText = $buyer->getLogin() . " купил товар «" . $item->name . "» id:(" . $item->item_id . ") в магазине за " . $item->price . ".";
GameLogs::addUserLog($buyer->getId(), $deloText);
return "Предмет " . $boughtItemName . " куплен за " . $boughtItemPrice . " банкнот.";
return "Предмет " . $item->name . " куплен за " . $item->price . ".";
}
public static function sellItem($id, User $seller, $bankTrade = 0): string
@ -197,9 +205,9 @@ FORM;
}
/**
* @return mixed
* @return int
*/
public function getItemType()
public function getItemType(): int
{
return $this->item_type;
}

View File

@ -237,7 +237,12 @@ class User
*/
public function setMoney(int $money): void
{
$this->money = $money;
$this->money = $money < 0 ? 0 : $money;
}
public function saveMoney()
{
self::$db->execute('update users set money = ? where id = ?', [$this->money, $this->id]);
}
@ -338,7 +343,7 @@ class User
public function saveShadow()
{
self::$db->execute('UPDATE users SET shadow = ? WHERE id = ?', [$this->getShadow(), $this->getId()]);
self::$db->execute('UPDATE users SET shadow = ? WHERE id = ?', [$this->shadow, $this->id]);
}
/**

View File

@ -200,8 +200,8 @@ Template::header('Магазин');
</table>
<td>
<strong>
Масса всех ваших вещей: <?= getItemsMassaInfo() ?> <br>
У вас в банке: <span><?= $bank->getMoney() ?></span> банкнот.
Масса всех вещей: <?= getItemsMassaInfo() ?> <br>
Деньги: <?= $user->getMoney() ?> [Б:<?= $bank->getMoney() ?>].
</strong>
<hr>
<div>Отделы магазина</div>