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

View File

@ -237,7 +237,12 @@ class User
*/ */
public function setMoney(int $money): void 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() 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> </table>
<td> <td>
<strong> <strong>
Масса всех ваших вещей: <?= getItemsMassaInfo() ?> <br> Масса всех вещей: <?= getItemsMassaInfo() ?> <br>
У вас в банке: <span><?= $bank->getMoney() ?></span> банкнот. Деньги: <?= $user->getMoney() ?> [Б:<?= $bank->getMoney() ?>].
</strong> </strong>
<hr> <hr>
<div>Отделы магазина</div> <div>Отделы магазина</div>