Зимние правки. MVC/

Signed-off-by: lopar <lopar.4ever@gmail.com>
This commit is contained in:
lopar
2022-08-09 22:57:43 +03:00
parent 0f62ee20e7
commit b9b4c01cf0
104 changed files with 2254 additions and 2086 deletions
+17 -29
View File
@@ -3,13 +3,10 @@
namespace Battles;
use Battles\Database\Db;
use Battles\Models\PresentsModel;
use Exceptions\GameException;
class ShopItem extends Item
{
private const NO_ITEMS_IN_STOCK = "Товара нет в наличии!";
private const NO_MONEY = "У вас нет денег!";
private const NO_BARTER_ITEMS = 'У вас нет требуемых предметов!';
private const BUTTON = [
'setmarket' => 'Сдать в магазин',
@@ -135,23 +132,23 @@ SQL;
return "<img src='/i/sh/$this->image' class='item-wrap-normal' alt=''>";
}
public static function buyItem($id, User $buyer)
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, $buyer->getId()) ||
!self::checkAndPayTheBills($price, $buyer) ||
!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, [$buyer->getId(), $check->shop_item_id]);
$deloText = $buyer->getLogin() . " купил товар «" . $item->name . "» id:(" . $check->shop_item_id . ") в магазине за " . $price . ".";
GameLogs::addUserLog($buyer->getId(), $deloText);
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 . ".";
}
@@ -179,21 +176,13 @@ SQL;
return true;
}
private static function checkAndPayTheBills(int $price, User $user): bool
private static function checkAndPayTheBills(int $price): bool
{
if (User::getInstance()->getMoney() > $price) {
User::getInstance()->setMoney(User::getInstance()->getMoney() - $price);
User::getInstance()->saveMoney();
if (User::getInstance()->money()->spend($price)) {
return true;
}
try {
$bank = new Bank(User::getInstance()->getId());
$bank->withdrawMoney($price);
return true;
} catch (GameException $e) {
self::$status = 'Банковская ошибка! ' . self::NO_MONEY;
return false;
}
(new Bank())->withdrawMoney($price);
return true;
}
private static function checkAndChangeRemainingItems(int $current_quantity, $item_id): bool
@@ -209,7 +198,7 @@ SQL;
return true;
}
public static function sellItem($id, User $seller, $bankTrade = 0)
public static function sellItem($id, $bankTrade = 0)
{
$item = Db::getInstance()->ofetch('select * from inventory where item_id = ?', $id);
$sellingItemName = $item->name;
@@ -217,14 +206,12 @@ SQL;
$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) {
$bank = new Bank($seller->getId());
$bank->setMoney($bank->getMoney() + $sellingPrice);
Bank::setBankMoney($bank->getMoney(), $seller->getId(), 'sellShop');
User::getInstance()->money()->modifyBank($sellingPrice, 'sellShop');
} else {
Db::getInstance()->execute('update users set money = money - ? where id = ?', [$sellingPrice, $_SESSION['uid']]);
User::getInstance()->money()->earn($sellingPrice);
}
$deloText = "{$seller->getLogin()} продал товар «{$sellingItemName}» id:($id) в магазине за $sellingPrice кр.";
GameLogs::addUserLog($seller->getId(), $deloText);
$deloText = User::getInstance()->getLogin() . " продал товар «{$sellingItemName}» id:($id) в магазине за $sellingPrice кр.";
GameLogs::addUserLog(User::getInstance()->getId(), $deloText);
if ($sellingPrice == 0) {
self::$status = "После длительных и изнурительных торгов вы плюнули на всё и просто подарили ваш «{$sellingItemName}» торговцу.";
} else {
@@ -274,8 +261,9 @@ FORM;
/** Выдача магазинных предметов по запросу.
* Ввелась чтобы перебить takeshopitem() в functions с идентичным функционалом.
*
* @param int $item_id ИД предмета.
* @param int $to ИД пперсонажа-получателя.
* @param int $to ИД пперсонажа-получателя.
*/
public static function giveNewItem(int $item_id, int $to): array
{