2019-01-11 20:26:10 +00:00
< ? php
2021-03-12 16:26:15 +00:00
2020-10-28 20:21:08 +00:00
namespace Battles ;
2021-03-12 16:26:15 +00:00
2021-01-28 21:05:34 +00:00
use Battles\Database\DBPDO ;
2021-08-20 17:40:06 +00:00
use Exceptions\GameException ;
2021-01-28 21:05:34 +00:00
2019-01-11 20:26:10 +00:00
class ShopItem extends Item
{
2021-08-20 17:40:06 +00:00
private const NO_ITEMS_IN_STOCK = " Товара нет в наличии! " ;
private const NO_MONEY = " У вас нет денег!" ;
private const BUTTON = [
'setmarket' => 'Сдать в магазин' ,
'unsetmarket' => 'Снять с продажи' ,
'buymarket' => 'Совершить обмен' ,
'sellshop' => 'Продать' ,
'buyshop' => 'Купить' ,
];
private const BUY_QUERY = ' insert into inventory ( owner_id , name , item_type , durability ,
need_strength , need_dexterity , need_intuition , need_endurance , need_intelligence , need_wisdom ,
add_strength , add_dexterity , add_intuition , add_endurance , add_intelligence , add_wisdom ,
add_accuracy , add_evasion , add_criticals , add_min_physical_damage , add_max_physical_damage ,
image , weight , price )
select
? ,
ifnull ( name , \ ' Неизвестный предмет\ ' ),
ifnull ( item_type , 0 ),
ifnull ( durability , 1 ),
ifnull ( need_strength , 0 ),
ifnull ( need_dexterity , 0 ),
ifnull ( need_intuition , 0 ),
ifnull ( need_endurance , 0 ),
ifnull ( need_intelligence , 0 ),
ifnull ( need_wisdom , 0 ),
ifnull ( add_strength , 0 ),
ifnull ( add_dexterity , 0 ),
ifnull ( add_intuition , 0 ),
ifnull ( add_endurance , 0 ),
ifnull ( add_intelligence , 0 ),
ifnull ( add_wisdom , 0 ),
ifnull ( add_accuracy , 0 ),
ifnull ( add_evasion , 0 ),
ifnull ( add_criticals , 0 ),
ifnull ( add_min_physical_damage , 0 ),
ifnull ( add_max_physical_damage , 0 ),
ifnull ( image , \ ' noitem . png\ ' ),
ifnull ( weight , 1 ),
2021-08-22 20:43:42 +00:00
greatest (
(
( add_strength + add_dexterity + add_intuition + add_endurance + add_intelligence + add_wisdom ) *
( 5 + floor (( add_strength + add_dexterity + add_intuition + add_endurance + add_intelligence + add_wisdom ) / 10 ))
) +
(
( add_accuracy + add_criticals + add_evasion ) *
( 2 + floor (( add_accuracy + add_criticals + add_evasion ) / 50 ))
) +
(
( add_min_physical_damage + add_max_physical_damage ) *
( 1 + floor (( add_min_physical_damage + add_max_physical_damage ) / 100 ))
)
, 1 )
2021-08-20 17:40:06 +00:00
from items where id = ? ' ;
// Тип операции в магазине. Для отображения разных блоков в разных случаях.
private $optype ;
private ? int $shop_item_quantity ;
private ? int $price ;
public function __construct ( $row , $operationType = null )
{
parent :: __construct ( $row );
if ( $operationType ) {
$this -> optype = $operationType ;
}
$this -> price = $row -> price ? ? null ;
$this -> shop_item_quantity = $row -> shop_item_quantity ? ? null ;
$this -> item_id = $row -> item_id ? ? $row -> id ;
}
2019-01-11 20:26:10 +00:00
public function printInfo ()
{
2021-08-20 17:40:06 +00:00
$this -> printAllInfo ();
2021-08-22 20:43:42 +00:00
if ( $this -> optype === 'buyshop' && $this -> shop_item_quantity > 0 && $this -> shop_item_quantity < 20 ) {
echo " <div style='margin-top: 9px; font-style: italic;'>Н а складе осталось $this->shop_item_quantity единиц товара!</div> " ;
2021-08-20 17:40:06 +00:00
}
if ( $this -> optype === 'sellshop' ) {
if ( $this -> getSellPriceMean () < 50 ) {
$goods = 'этот хлам' ;
} elseif ( $this -> getSellPriceMean () < 100 ) {
$goods = 'этот посредственный товар' ;
} elseif ( $this -> getSellPriceMean () < 500 ) {
$goods = 'этот неплохой предмет' ;
} elseif ( $this -> getSellPriceMean () < 1000 ) {
$goods = 'эту отличную штуку' ;
} else {
$goods = 'это превосходное изделие' ;
}
echo " <div style='margin-top: 9px; font-style: italic;'>В среднем за $goods можно выручить <span class='success'> { $this -> getSellPriceMean () } </span> кр.</div> " ;
}
}
2021-08-22 18:51:00 +00:00
public function printImage ()
2021-08-20 17:40:06 +00:00
{
if ( ! $this -> image ) {
$this -> image = 'noitem.png' ;
}
2021-08-22 18:51:00 +00:00
echo " <img src='/i/sh/ $this->image ' class='item-wrap-normal' alt=''> " ;
2019-01-11 20:26:10 +00:00
}
2021-03-12 16:26:15 +00:00
public static function buyItem ( $id , User $buyer ) : string
2019-01-15 15:56:15 +00:00
{
2021-03-12 16:26:15 +00:00
$db = new DBPDO ();
2021-08-20 17:40:06 +00:00
$check = $db -> ofetch ( " select * from trade_offers where shop_item_id = ? " , $id );
if ( empty ( $check -> shop_item_quantity ) || empty ( $check -> shop_item_id )) {
return self :: NO_ITEMS_IN_STOCK ;
}
// TODO БАРТЕР!
if ( isset ( $check -> barter_item_list_json )) {
echo " Работаем по бартеру! " ;
}
2021-08-22 20:43:42 +00:00
$db -> execute ( self :: BUY_QUERY , [ $buyer -> getId (), $check -> shop_item_id ]);
2021-08-20 17:40:06 +00:00
$item = $db -> ofetch ( " select item_id, name, price from inventory where item_id = ? " , $db -> lastInsertId ());
2021-08-22 20:43:42 +00:00
if ( empty ( $item -> item_id ) || empty ( $item -> name )) {
2021-08-20 17:40:06 +00:00
return 'Запрос в базу не прошёл.' ;
} else {
2021-08-22 20:43:42 +00:00
$user = new User ( $_SESSION [ 'uid' ]);
// Если не хватает налички, снимаем с банка с комиссией.
if ( $user -> getMoney () < $item -> price ) {
2021-08-20 17:40:06 +00:00
try {
$bank = new Bank ( $buyer -> getId ());
$bank -> withdrawMoney ( $item -> price );
} catch ( GameException $e ) {
echo 'Банковская ошибка!' ;
return self :: NO_MONEY ;
}
2021-08-22 20:43:42 +00:00
} else {
$user -> setMoney ( $user -> getMoney () - $item -> price );
$user -> saveMoney ();
2021-08-20 17:40:06 +00:00
}
2021-08-22 20:43:42 +00:00
2021-08-20 17:40:06 +00:00
}
if ( $check -> shop_item_quantity != - 1 ) {
$db -> execute ( " update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_id = ? " , $check -> shop_item_id );
}
2021-08-22 20:43:42 +00:00
$deloText = $buyer -> getLogin () . " купил товар « " . $item -> name . " » id:( " . $item -> item_id . " ) в магазине за " . $item -> price . " . " ;
2021-03-12 16:26:15 +00:00
GameLogs :: addUserLog ( $buyer -> getId (), $deloText );
2021-08-22 20:43:42 +00:00
return " Предмет " . $item -> name . " куплен за " . $item -> price . " . " ;
2021-03-12 16:26:15 +00:00
}
public static function sellItem ( $id , User $seller , $bankTrade = 0 ) : string
{
$db = new DBPDO ();
$item = $db -> ofetch ( 'select * from inventory where item_id = ?' , $id );
2021-08-20 17:40:06 +00:00
$sellingItemName = $item -> name ;
2021-03-12 16:26:15 +00:00
// Продажа за цену от нуля до половины стоимости.
2021-08-22 21:50:47 +00:00
$sellingPrice = $item -> price > 1 ? mt_rand ( 0 , $item -> price / 2 ) : mt_rand ( 0 , 1 );
2021-03-12 16:26:15 +00:00
$db -> execute ( 'delete from inventory where item_id = ?' , $id );
if ( $bankTrade ) {
$bank = new Bank ( $seller -> getId ());
2021-08-20 17:40:06 +00:00
$bank -> setMoney ( $bank -> getMoney () + $sellingPrice );
2021-08-22 18:51:00 +00:00
Bank :: setBankMoney ( $bank -> getMoney (), $seller -> getId (), 'sellShop' );
2021-03-12 16:26:15 +00:00
} else {
$db -> execute ( 'update users set money = money - ? where id = ?' , [ $sellingPrice , $_SESSION [ 'uid' ]]);
}
2021-08-20 17:40:06 +00:00
$deloText = " { $seller -> getLogin () } продал товар « { $sellingItemName } » id:( $id ) в магазине за $sellingPrice кр. " ;
2021-03-12 16:26:15 +00:00
GameLogs :: addUserLog ( $seller -> getId (), $deloText );
if ( $sellingPrice == 0 ) {
2021-08-20 17:40:06 +00:00
$status = " После длительных и изнурительных торгов вы плюнули на всё и просто подарили ваш « { $sellingItemName } » торговцу. " ;
2021-03-12 16:26:15 +00:00
} else {
2021-08-20 17:40:06 +00:00
$status = " Вы продали « { $sellingItemName } » за $sellingPrice кр. " ;
2019-01-15 15:56:15 +00:00
}
2021-03-12 16:26:15 +00:00
return $status ;
2019-01-15 15:56:15 +00:00
}
2021-08-20 17:40:06 +00:00
/** Подчсчёт средней суммы продажи .
* @ return int
*/
private function getSellPriceMean () : ? int
{
2021-08-22 21:50:47 +00:00
if ( $this -> price > 1 ) {
2021-08-20 17:40:06 +00:00
$arr = range ( 0 , $this -> price / 2 );
2021-08-22 18:51:00 +00:00
return array_sum ( $arr ) / count ( $arr );
2021-08-22 21:50:47 +00:00
} else {
return $this -> price == 1 ? 1 : null ;
2021-08-20 17:40:06 +00:00
}
}
2019-01-11 20:26:10 +00:00
/**
2021-01-28 21:05:34 +00:00
* Для кнопок управления под картинкой предмета в зависимости от ситуации .
2019-01-11 20:26:10 +00:00
*/
2021-08-20 17:40:06 +00:00
public function printControls ()
2019-01-11 20:26:10 +00:00
{
2021-08-20 17:40:06 +00:00
if ( in_array ( $this -> optype , [ 'setmarket' , 'unsetmarket' , 'buymarket' , 'sellshop' , 'buyshop' ,])) {
$str = $this -> optype == 'setmarket' ? '<input placeholder=" ' . $this -> price . ' " name="cost">' : '' ;
$button_name = self :: BUTTON [ $this -> optype ];
echo <<< FORM
< form method = " post " > $str
< input type = " hidden " name = " itemId " value = " $this->item_id " >
< br >< input type = " submit " name = " $this->optype " value = " $button_name " >
2019-02-15 15:00:01 +00:00
</ form >
2021-08-20 17:40:06 +00:00
FORM ;
2019-02-15 15:00:01 +00:00
}
2019-01-11 20:26:10 +00:00
}
2021-08-20 17:40:06 +00:00
/**
2021-08-22 20:43:42 +00:00
* @ return int
2021-08-20 17:40:06 +00:00
*/
2021-08-22 20:43:42 +00:00
public function getItemType () : int
2021-08-20 17:40:06 +00:00
{
return $this -> item_type ;
}
2019-01-11 20:26:10 +00:00
}