Code smell.

This commit is contained in:
Ivor Barhansky
2022-12-17 01:20:43 +02:00
parent b1f578f4b0
commit 0398425205
45 changed files with 875 additions and 851 deletions
+37 -37
View File
@@ -20,32 +20,32 @@ insert into inventory (
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
image, weight, price)
select
?, 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, 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) *
(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)
,1)
from items where id = ?
SQL;
// Тип операции в магазине. Для отображения разных блоков в разных случаях.
private $optype;
private ?int $shop_item_quantity;
private ?int $shopItemQuantity;
private ?int $price;
public static string $status = '';
private ?string $jsonBarterList;
@@ -59,8 +59,8 @@ SQL;
$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;
$this->shopItemQuantity = $row->shop_item_quantity ?? null;
$this->id = $row->item_id ?? $row->id;
if ($operationType === 'buyshop' || $operationType === 'buymarket') {
$this->offerId = $row->offer_id ?? 0; // Ид позиции в магазине.
$this->jsonBarterList = $row->barter_items_list_json ?? null;
@@ -102,10 +102,10 @@ SQL;
private function getLowItemQuantityNote(): string
{
if ($this->shop_item_quantity < 1 || $this->shop_item_quantity > 19) {
if ($this->shopItemQuantity < 1 || $this->shopItemQuantity > 19) {
return '';
}
return "<div style='margin-top: 9px; font-style: italic;'>На складе осталось $this->shop_item_quantity единиц товара!</div>";
return "<div style='margin-top: 9px; font-style: italic;'>На складе осталось $this->shopItemQuantity единиц товара!</div>";
}
private function getTextBasedOnPrice(): string
@@ -152,14 +152,14 @@ SQL;
self::$status = "Предмет " . $item->name . " куплен за " . $price . ".";
}
private static function checkAndRemoveBarteredItems(?string $json_list, int $user_id): bool
private static function checkAndRemoveBarteredItems(?string $jsonList, int $userId): bool
{
if (empty($json_list)) {
if (empty($jsonList)) {
return true;
}
$allowItemRemove = true;
foreach (json_decode($json_list) as $item) {
$row = Db::getInstance()->ofetch('select sum(1) as s from inventory where name = ? and owner_id = ?', [Item::getItemById($item->item_id)->name, $user_id]);
foreach (json_decode($jsonList) as $item) {
$row = Db::getInstance()->ofetch('select sum(1) as s from inventory where name = ? and owner_id = ?', [Item::getItemById($item->item_id)->name, $userId]);
if ($row->s < $item->quantity) {
$allowItemRemove = false;
}
@@ -168,10 +168,10 @@ SQL;
self::$status = self::NO_BARTER_ITEMS;
return false;
}
foreach (json_decode($json_list) as $item) {
foreach (json_decode($jsonList) as $item) {
$query = 'delete from inventory where name = ? and owner_id = ? limit ' . (int)$item->quantity;
// У-у-у, сука! https://phpdelusions.net/pdo#limit
Db::getInstance()->execute($query, [Item::getItemById($item->item_id)->name, $user_id]);
Db::getInstance()->execute($query, [Item::getItemById($item->item_id)->name, $userId]);
}
return true;
}
@@ -185,16 +185,16 @@ SQL;
return true;
}
private static function checkAndChangeRemainingItems(int $current_quantity, $item_id): bool
private static function checkAndChangeRemainingItems(int $currentQuantity, $itemId): bool
{
if (empty($current_quantity)) {
if (empty($currentQuantity)) {
self::$status = self::NO_ITEMS_IN_STOCK;
return false;
}
if ($current_quantity === -1) {
if ($currentQuantity === -1) {
return true;
}
Db::getInstance()->execute("update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_quantity != -1 and shop_item_id = ? ", $item_id);
Db::getInstance()->execute("update trade_offers set shop_item_quantity = shop_item_quantity -1 where shop_item_quantity != -1 and shop_item_id = ? ", $itemId);
return true;
}
@@ -241,12 +241,12 @@ SQL;
return '';
}
$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];
$hiddenValue = $this->optype === 'buyshop' ? $this->offerId : $this->id;
$buttonName = self::BUTTON[$this->optype];
return <<<FORM
<form method="post">$str
<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="$buttonName">
</form>
FORM;
}
@@ -256,28 +256,28 @@ FORM;
*/
public function getItemType(): int
{
return $this->item_type;
return $this->type;
}
/** Выдача магазинных предметов по запросу.
* Ввелась чтобы перебить takeshopitem() в functions с идентичным функционалом.
*
* @param int $item_id ИД предмета.
* @param int $itemId ИД предмета.
* @param int $to ИД пперсонажа-получателя.
*/
public static function giveNewItem(int $item_id, int $to): array
public static function giveNewItem(int $itemId, int $to): array
{
$check = Db::getInstance()->ofetch('select 1 from items where id = ?', $item_id);
$check = Db::getInstance()->ofetch('select 1 from items where id = ?', $itemId);
if (!$check) {
return [];
}
Db::getInstance()->execute(self::BUY_QUERY, [$to, $item_id]);
Db::getInstance()->execute(self::BUY_QUERY, [$to, $itemId]);
$return = Db::getInstance()->ofetch('select image, name from inventory where item_id = ?', Db::getInstance()->lastInsertId());
return [
'img' => $return->image,
'name' => $return->name,
'id' => $item_id,
'id' => $itemId,
];
}
}
}