Бартер. #40.

This commit is contained in:
lopar 2021-08-30 23:19:40 +03:00
parent 2760e17c6b
commit 54f4c08678
1 changed files with 14 additions and 10 deletions

View File

@ -63,8 +63,10 @@ SQL;
$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->item_id = $row->item_id ?? $row->id; $this->item_id = $row->item_id ?? $row->id;
$this->jsonBarterList = $row->barter_items_list_json; if ($operationType === 'buyshop') {
$this->offerId = $row->offer_id; // Ид позиции в магазине. $this->offerId = $row->offer_id; // Ид позиции в магазине.
$this->jsonBarterList = $row->barter_items_list_json;
}
} }
public function printInfo(): string public function printInfo(): string
@ -127,12 +129,12 @@ SQL;
public static function buyItem($id, User $buyer) public static function buyItem($id, User $buyer)
{ {
$check = DBPDO::$db->ofetch("select * from trade_offers where shop_item_id = ?", $id); $check = DBPDO::$db->ofetch("select * from trade_offers where offer_id = ?", $id);
$item = new Item(DBPDO::$db->fetch('select * from items where id = ?', $id)); $item = new Item(DBPDO::$db->fetch('select * from items where id = ?', $check->shop_item_id));
$price = $item->calculateItemCost(); $price = $item->calculateItemCost();
if ( if (
!self::checkAndRemoveBarteredItems($check->barter_item_list_json, $buyer->getId()) || !self::checkAndRemoveBarteredItems($check->barter_items_list_json, $buyer->getId()) ||
!self::checkAndPayTheBills($price, $buyer) || !self::checkAndPayTheBills($price, $buyer) ||
!self::checkAndChangeRemainingItems($check->shop_item_quantity, $check->shop_item_id) !self::checkAndChangeRemainingItems($check->shop_item_quantity, $check->shop_item_id)
) { ) {
@ -140,12 +142,12 @@ SQL;
} }
DBPDO::$db->execute(self::BUY_QUERY, [$buyer->getId(), $check->shop_item_id]); DBPDO::$db->execute(self::BUY_QUERY, [$buyer->getId(), $check->shop_item_id]);
$deloText = $buyer->getLogin() . " купил товар «" . Item::getItemById($item->item_id)->name . "» id:(" . $item->item_id . ") в магазине за " . $price . "."; $deloText = $buyer->getLogin() . " купил товар «" . $item->name . "» id:(" . $check->shop_item_id . ") в магазине за " . $price . ".";
GameLogs::addUserLog($buyer->getId(), $deloText); GameLogs::addUserLog($buyer->getId(), $deloText);
self::$status = "Предмет " . $item->name . " куплен за " . $price . "."; self::$status = "Предмет " . $item->name . " куплен за " . $price . ".";
} }
private static function checkAndRemoveBarteredItems(string $json_list, int $user_id): bool private static function checkAndRemoveBarteredItems(?string $json_list, int $user_id): bool
{ {
if (empty($json_list)) { if (empty($json_list)) {
return true; return true;
@ -162,10 +164,11 @@ SQL;
return false; return false;
} }
foreach (json_decode($json_list) as $item) { foreach (json_decode($json_list) as $item) {
DBPDO::$db->execute('delete from inventory where name = ? and owner_id = ? limit ?', [Item::getItemById($item->item_id)->name, $user_id, $item->quantity]); $query = 'delete from inventory where name = ? and owner_id = ? limit ' . (int)$item->quantity;
// У-у-у, сука! https://phpdelusions.net/pdo#limit
DBPDO::$db->execute($query, [Item::getItemById($item->item_id)->name, $user_id]);
} }
return true; return true;
} }
private static function checkAndPayTheBills(int $price, User $user): bool private static function checkAndPayTheBills(int $price, User $user): bool
@ -244,10 +247,11 @@ SQL;
return ''; return '';
} }
$str = $this->optype == 'setmarket' ? '<input placeholder=" ' . $this->price . ' " name="cost">' : ''; $str = $this->optype == 'setmarket' ? '<input placeholder=" ' . $this->price . ' " name="cost">' : '';
$hiddenValue = $this->optype === 'buyshop' ? $this->offerId : $this->item_id;
$button_name = self::BUTTON[$this->optype]; $button_name = self::BUTTON[$this->optype];
return <<<FORM return <<<FORM
<form method="post">$str <form method="post">$str
<input type="hidden" name="itemId" value="$this->offerId"> <input type="hidden" name="itemId" value="$hiddenValue">
<br><input type="submit" name="$this->optype" value="$button_name"> <br><input type="submit" name="$this->optype" value="$button_name">
</form> </form>
FORM; FORM;