2021-08-30 00:10:07 +00:00
< ? php
# Date: 29.08.2021 (21:34)
namespace Battles ;
use Battles\Database\DBPDO ;
class Shop
{
public const GENERAL_SHOP = 1 ;
2021-08-31 07:43:48 +00:00
public const BARTER_SHOP = 2 ;
2021-08-30 00:10:07 +00:00
public const CATEGORY_SALE_ITEMS = - 1 ;
public static Shop $current ;
public int $categoryType = 0 ;
private int $shopId ;
public function __construct ( $shop_id ) {
$this -> shopId = $shop_id ;
}
private function showGoods () : string
{
if ( $this -> categoryType ) {
$stmt = DBPDO :: $db -> 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 ]);
2021-08-31 07:43:48 +00:00
$stmt2 = DBPDO :: $db -> ofetchAll ( 'select * from inventory where on_sale != 0 and present is null and item_type = ?' , $this -> categoryType );
2021-08-30 00:10:07 +00:00
} else {
$stmt = DBPDO :: $db -> ofetchAll ( 'select * from items inner join trade_offers on id = shop_item_id where shop_id = ? and shop_item_quantity !=0' , $this -> shopId );
2021-08-31 07:43:48 +00:00
$stmt2 = DBPDO :: $db -> ofetchAll ( 'select * from inventory where on_sale != 0 and present is null' );
2021-08-30 00:10:07 +00:00
}
$iteminfo = [];
foreach ( $stmt as $item ) {
$iteminfo [] = new ShopItem ( $item , 'buyshop' );
}
2021-08-31 07:43:48 +00:00
foreach ( $stmt2 as $item ) {
$iteminfo [] = new ShopItem ( $item , 'buymarket' );
}
2021-08-30 00:10:07 +00:00
return $this -> strFromArr ( $iteminfo );
}
private function showUserSellItems () : string
{
$stmt = DBPDO :: $db -> ofetchall ( 'select * from inventory where on_sale = 0 and dressed_slot = 0 and durability > 0 and owner_id = ?' , User :: $current -> getId ());
$iteminfo = [];
foreach ( $stmt as $item ) {
$iteminfo [] = new ShopItem ( $item , 'sellshop' );
}
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 :: ITEM_TYPE_HELMET => 'Шлемы' ,
Item :: ITEM_TYPE_ARMOR => 'Броня' ,
Item :: ITEM_TYPE_LEGS => 'Поножи' ,
Item :: ITEM_TYPE_BOOTS => 'Сапоги' ,
Item :: ITEM_TYPE_GLOVES => 'Перчатки' ,
Item :: ITEM_TYPE_WEAPON => 'Оружие' ,
Item :: ITEM_TYPE_SHIELD => 'Щиты' ,
Item :: ITEM_TYPE_BELT => 'Пояса' ,
Item :: ITEM_TYPE_RING => 'Кольца' ,
Item :: ITEM_TYPE_AMULET => 'Амулеты' ,
Item :: ITEM_TYPE_CONSUMABLE => 'Расходники' ,
Item :: ITEM_TYPE_OTHER => 'Разное' ,
self :: CATEGORY_SALE_ITEMS => 'Предметы в инвентаре' ,
0 => 'В с е товары' ,
];
return $names [ $this -> categoryType ];
}
public function getItemsList () : string
{
return $this -> categoryType !== self :: CATEGORY_SALE_ITEMS ? $this -> showGoods () : $this -> showUserSellItems ();
}
}