battles/classes/Battles/Shop.php
Ivor Barhansky 0398425205 Code smell.
2022-12-17 03:03:56 +02:00

96 lines
3.4 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
# Date: 29.08.2021 (21:34)
namespace Battles;
use Battles\Database\Db;
class Shop
{
public const GENERAL_SHOP = 1;
public const BARTER_SHOP = 2;
public const CATEGORY_SALE_ITEMS = -1;
public int $categoryType = 0;
private int $shopId;
private function __construct($shopId)
{
$this->shopId = $shopId;
}
public static function id($shopid): self
{
return new self($shopid);
}
private function showGoods(): string
{
if ($this->categoryType) {
$stmt = Db::getInstance()->ofetchAll('select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0 and item_type = ?', [$this->shopId, $this->categoryType]);
$stmt2 = Db::getInstance()->ofetchAll('select * from inventory where on_sale != 0 and present is null and item_type = ?', $this->categoryType);
} else {
$stmt = Db::getInstance()->ofetchAll('select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0', $this->shopId);
$stmt2 = Db::getInstance()->ofetchAll('select * from inventory where on_sale != 0 and present is null');
}
$iteminfo = [];
foreach ($stmt as $item) {
$iteminfo[] = new ShopItem($item, 'buyshop');
}
foreach ($stmt2 as $item) {
$iteminfo[] = new ShopItem($item, 'buymarket');
}
return $this->strFromArr($iteminfo);
}
private function showUserSellItems(): string
{
$stmt = Db::getInstance()->ofetchall('select * from inventory where on_sale = 0 and dressed_slot = 0 and durability > 0 and owner_id = ?', User::getInstance()->getId());
$iteminfo = [];
$operationType = 'sellshop';
if ($this->categoryType === self::BARTER_SHOP) {
$operationType = 'setmarket';
}
foreach ($stmt as $item) {
$iteminfo[] = new ShopItem($item, $operationType);
}
return $this->strFromArr($iteminfo);
}
private function strFromArr(array $array): string
{
$str = '';
$format = '<div class="row item"><div class="left column">%s%s</div><div class="right column">%s</div></div>';
foreach ($array as $a) {
$str .= sprintf($format, $a->printImage(), $a->printControls(), $a->printInfo());
}
return $str;
}
public function getCategoryName(): string
{
$names = [
Item::TYPE_HELMET => 'Шлемы',
Item::TYPE_ARMOR => 'Броня',
Item::TYPE_LEGS => 'Поножи',
Item::TYPE_BOOTS => 'Сапоги',
Item::TYPE_GLOVES => 'Перчатки',
Item::TYPE_WEAPON => 'Оружие',
Item::TYPE_SHIELD => 'Щиты',
Item::TYPE_BELT => 'Пояса',
Item::TYPE_RING => 'Кольца',
Item::TYPE_AMULET => 'Амулеты',
Item::TYPE_CONSUMABLE => 'Расходники',
Item::TYPE_OTHER => 'Разное',
self::CATEGORY_SALE_ITEMS => 'Предметы в инвентаре',
0 => 'Все товары',
];
return $names[$this->categoryType];
}
public function getItemsList(): string
{
return $this->categoryType !== self::CATEGORY_SALE_ITEMS || $this->categoryType !== self::BARTER_SHOP ? $this->showGoods() : $this->showUserSellItems();
}
}