2021-08-30 00:10:07 +00:00
< ? php
# Date: 29.08.2021 (21:34)
namespace Battles ;
2022-01-26 23:15:33 +00:00
use Battles\Database\Db ;
2021-08-30 00:10:07 +00:00
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 int $categoryType = 0 ;
private int $shopId ;
2022-12-16 23:20:43 +00:00
private function __construct ( $shopId )
2022-01-26 23:15:33 +00:00
{
2022-12-16 23:20:43 +00:00
$this -> shopId = $shopId ;
2021-08-30 00:10:07 +00:00
}
2022-01-26 23:15:33 +00:00
public static function id ( $shopid ) : self
{
return new self ( $shopid );
}
2021-08-30 00:10:07 +00:00
private function showGoods () : string
{
if ( $this -> categoryType ) {
2022-01-26 23:15:33 +00:00
$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 );
2021-08-30 00:10:07 +00:00
} else {
2022-01-26 23:15:33 +00:00
$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' );
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
{
2022-01-26 23:15:33 +00:00
$stmt = Db :: getInstance () -> ofetchall ( 'select * from inventory where on_sale = 0 and dressed_slot = 0 and durability > 0 and owner_id = ?' , User :: getInstance () -> getId ());
2021-08-30 00:10:07 +00:00
$iteminfo = [];
2022-01-21 16:17:56 +00:00
$operationType = 'sellshop' ;
if ( $this -> categoryType === self :: BARTER_SHOP ) {
$operationType = 'setmarket' ;
}
2021-08-30 00:10:07 +00:00
foreach ( $stmt as $item ) {
2022-01-21 16:17:56 +00:00
$iteminfo [] = new ShopItem ( $item , $operationType );
2021-08-30 00:10:07 +00:00
}
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 = [
2022-12-16 23:20:43 +00:00
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 => 'Разное' ,
2021-08-30 00:10:07 +00:00
self :: CATEGORY_SALE_ITEMS => 'Предметы в инвентаре' ,
0 => 'В с е товары' ,
];
return $names [ $this -> categoryType ];
}
public function getItemsList () : string
{
2022-01-21 16:17:56 +00:00
return $this -> categoryType !== self :: CATEGORY_SALE_ITEMS || $this -> categoryType !== self :: BARTER_SHOP ? $this -> showGoods () : $this -> showUserSellItems ();
2021-08-30 00:10:07 +00:00
}
2022-12-16 23:20:43 +00:00
}