Code smell.
This commit is contained in:
+92
-91
@@ -1,48 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Battles;
|
||||
|
||||
use Battles\Database\Db;
|
||||
|
||||
class Item
|
||||
{
|
||||
protected int $item_id;
|
||||
protected int $id;
|
||||
protected string $name = '';
|
||||
protected int $item_type = self::ITEM_TYPE_TRASH;
|
||||
protected int $type = self::TYPE_TRASH;
|
||||
protected int $durability = 0;
|
||||
protected int $need_strength = 0;
|
||||
protected int $need_dexterity = 0;
|
||||
protected int $need_intuition = 0;
|
||||
protected int $need_endurance = 0;
|
||||
protected int $need_intelligence = 0;
|
||||
protected int $need_wisdom = 0;
|
||||
protected int $add_strength = 0;
|
||||
protected int $add_dexterity = 0;
|
||||
protected int $add_intuition = 0;
|
||||
protected int $add_endurance = 0;
|
||||
protected int $add_intelligence = 0;
|
||||
protected int $add_wisdom = 0;
|
||||
protected int $add_accuracy = 0;
|
||||
protected int $add_evasion = 0;
|
||||
protected int $add_criticals = 0;
|
||||
protected int $add_min_physical_damage = 0;
|
||||
protected int $add_max_physical_damage = 0;
|
||||
protected int $needStrength = 0;
|
||||
protected int $needDexterity = 0;
|
||||
protected int $needIntuition = 0;
|
||||
protected int $needEndurance = 0;
|
||||
protected int $needIntelligence = 0;
|
||||
protected int $needWisdom = 0;
|
||||
protected int $addStrength = 0;
|
||||
protected int $addDexterity = 0;
|
||||
protected int $addIntuition = 0;
|
||||
protected int $addEndurance = 0;
|
||||
protected int $addIntelligence = 0;
|
||||
protected int $addWisdom = 0;
|
||||
protected int $addAccuracy = 0;
|
||||
protected int $addEvasion = 0;
|
||||
protected int $addCriticals = 0;
|
||||
protected int $addMinPhysicalDamage = 0;
|
||||
protected int $addMaxPhysicalDamage = 0;
|
||||
protected int $weight = 0;
|
||||
protected string $image = '';
|
||||
protected int $item_cost = 0;
|
||||
public const ITEM_TYPES_ALLOWED_IN_SLOTS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
||||
public const ITEM_TYPE_HELMET = 1;
|
||||
public const ITEM_TYPE_ARMOR = 2;
|
||||
public const ITEM_TYPE_LEGS = 3;
|
||||
public const ITEM_TYPE_BOOTS = 4;
|
||||
public const ITEM_TYPE_GLOVES = 5;
|
||||
public const ITEM_TYPE_WEAPON = 6;
|
||||
public const ITEM_TYPE_SHIELD = 7;
|
||||
public const ITEM_TYPE_BELT = 8;
|
||||
public const ITEM_TYPE_RING = 9;
|
||||
public const ITEM_TYPE_AMULET = 10;
|
||||
public const ITEM_TYPE_CONSUMABLE = 20;
|
||||
public const ITEM_TYPE_OTHER = 50;
|
||||
public const ITEM_TYPE_TRASH = 100;
|
||||
protected int $cost = 0;
|
||||
public const TYPES_ALLOWED_IN_SLOTS = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
|
||||
public const TYPE_HELMET = 1;
|
||||
public const TYPE_ARMOR = 2;
|
||||
public const TYPE_LEGS = 3;
|
||||
public const TYPE_BOOTS = 4;
|
||||
public const TYPE_GLOVES = 5;
|
||||
public const TYPE_WEAPON = 6;
|
||||
public const TYPE_SHIELD = 7;
|
||||
public const TYPE_BELT = 8;
|
||||
public const TYPE_RING = 9;
|
||||
public const TYPE_AMULET = 10;
|
||||
public const TYPE_CONSUMABLE = 20;
|
||||
public const TYPE_OTHER = 50;
|
||||
public const TYPE_TRASH = 100;
|
||||
private string $typename;
|
||||
|
||||
/**
|
||||
@@ -66,45 +67,45 @@ class Item
|
||||
}
|
||||
}
|
||||
|
||||
switch ($this->item_type) {
|
||||
case self::ITEM_TYPE_HELMET:
|
||||
switch ($this->type) {
|
||||
case self::TYPE_HELMET:
|
||||
$this->typename = 'Шлем';
|
||||
break;
|
||||
case self::ITEM_TYPE_ARMOR:
|
||||
case self::TYPE_ARMOR:
|
||||
$this->typename = 'Броня';
|
||||
break;
|
||||
case self::ITEM_TYPE_LEGS:
|
||||
case self::TYPE_LEGS:
|
||||
$this->typename = 'Поножи';
|
||||
break;
|
||||
case self::ITEM_TYPE_BOOTS:
|
||||
case self::TYPE_BOOTS:
|
||||
$this->typename = 'Сапоги';
|
||||
break;
|
||||
case self::ITEM_TYPE_GLOVES:
|
||||
case self::TYPE_GLOVES:
|
||||
$this->typename = 'Перчатки';
|
||||
break;
|
||||
case self::ITEM_TYPE_WEAPON:
|
||||
case self::TYPE_WEAPON:
|
||||
$this->typename = 'Оружие';
|
||||
break;
|
||||
case self::ITEM_TYPE_SHIELD:
|
||||
case self::TYPE_SHIELD:
|
||||
$this->typename = 'Щит';
|
||||
break;
|
||||
case self::ITEM_TYPE_BELT:
|
||||
case self::TYPE_BELT:
|
||||
$this->typename = 'Пояс';
|
||||
break;
|
||||
case self::ITEM_TYPE_RING:
|
||||
case self::TYPE_RING:
|
||||
$this->typename = 'Кольцо';
|
||||
break;
|
||||
case self::ITEM_TYPE_AMULET:
|
||||
case self::TYPE_AMULET:
|
||||
$this->typename = 'Амулет';
|
||||
break;
|
||||
case self::ITEM_TYPE_CONSUMABLE:
|
||||
case self::TYPE_CONSUMABLE:
|
||||
$this->typename = 'Расходуемый предмет';
|
||||
break;
|
||||
default:
|
||||
$this->typename = 'Хлам';
|
||||
}
|
||||
|
||||
$this->item_cost = $this->calculateItemCost();
|
||||
$this->cost = $this->calculateItemCost();
|
||||
}
|
||||
|
||||
/** Рассчёт стоимости предмета в зависимости от его характеристик.
|
||||
@@ -112,26 +113,26 @@ class Item
|
||||
*/
|
||||
protected function calculateItemCost(): int
|
||||
{
|
||||
$sum_stats =
|
||||
$this->add_strength +
|
||||
$this->add_dexterity +
|
||||
$this->add_intuition +
|
||||
$this->add_endurance +
|
||||
$this->add_intelligence +
|
||||
$this->add_wisdom;
|
||||
$sum_mods =
|
||||
$this->add_accuracy +
|
||||
$this->add_evasion +
|
||||
$this->add_criticals;
|
||||
$sum_damage =
|
||||
$this->add_min_physical_damage +
|
||||
$this->add_max_physical_damage;
|
||||
$sumStats =
|
||||
$this->addStrength +
|
||||
$this->addDexterity +
|
||||
$this->addIntuition +
|
||||
$this->addEndurance +
|
||||
$this->addIntelligence +
|
||||
$this->addWisdom;
|
||||
$sumMods =
|
||||
$this->addAccuracy +
|
||||
$this->addEvasion +
|
||||
$this->addCriticals;
|
||||
$sumDamage =
|
||||
$this->addMinPhysicalDamage +
|
||||
$this->addMaxPhysicalDamage;
|
||||
// За каждые N параметров повышаем множитель на 1 чтобы цена пропрорционально росла.
|
||||
$stats_cost_modifier = 5 + floor($sum_stats / 10);
|
||||
$mods_cost_modifier = 2 + floor($sum_mods / 50);
|
||||
$damage_cost_modifier = 1 + floor($sum_damage / 100);
|
||||
$result = intval($sum_stats * $stats_cost_modifier + $sum_mods * $mods_cost_modifier + $sum_damage * $damage_cost_modifier);
|
||||
return $result < 1 ? 1 : $result;
|
||||
$statsCostModifier = 5 + floor($sumStats / 10);
|
||||
$modsCostModifier = 2 + floor($sumMods / 50);
|
||||
$damageCostModifier = 1 + floor($sumDamage / 100);
|
||||
$result = intval($sumStats * $statsCostModifier + $sumMods * $modsCostModifier + $sumDamage * $damageCostModifier);
|
||||
return max($result, 1);
|
||||
}
|
||||
|
||||
protected function wrap(int $number): string
|
||||
@@ -146,26 +147,26 @@ class Item
|
||||
public function getAllInfo(): string
|
||||
{
|
||||
$needsLines = [
|
||||
'сила' => $this->need_strength,
|
||||
'ловкость' => $this->need_dexterity,
|
||||
'интуиция' => $this->need_intuition,
|
||||
'выносливость' => $this->need_endurance,
|
||||
'интеллект' => $this->need_intelligence,
|
||||
'мудрость' => $this->need_wisdom,
|
||||
'сила' => $this->needStrength,
|
||||
'ловкость' => $this->needDexterity,
|
||||
'интуиция' => $this->needIntuition,
|
||||
'выносливость' => $this->needEndurance,
|
||||
'интеллект' => $this->needIntelligence,
|
||||
'мудрость' => $this->needWisdom,
|
||||
];
|
||||
$addsLines = [
|
||||
'Сила' => $this->add_strength,
|
||||
'Ловкость' => $this->add_dexterity,
|
||||
'Интуиция' => $this->add_intuition,
|
||||
'Выносливость' => $this->add_endurance,
|
||||
'Интеллект' => $this->add_intelligence,
|
||||
'Мудрость' => $this->add_wisdom,
|
||||
'Точность' => $this->add_accuracy,
|
||||
'Увёртливость' => $this->add_evasion,
|
||||
'Шанс крита' => $this->add_criticals,
|
||||
'Сила' => $this->addStrength,
|
||||
'Ловкость' => $this->addDexterity,
|
||||
'Интуиция' => $this->addIntuition,
|
||||
'Выносливость' => $this->addEndurance,
|
||||
'Интеллект' => $this->addIntelligence,
|
||||
'Мудрость' => $this->addWisdom,
|
||||
'Точность' => $this->addAccuracy,
|
||||
'Увёртливость' => $this->addEvasion,
|
||||
'Шанс крита' => $this->addCriticals,
|
||||
];
|
||||
$str = "<b>$this->name</b> (Масса: $this->weight)";
|
||||
$str .= '<br> Стоимость: ' . $this->item_cost;
|
||||
$str .= '<br> Стоимость: ' . $this->cost;
|
||||
$str .= '<br> Долговечность: ' . $this->durability;
|
||||
$str .= "<br><em>$this->typename</em><br>";
|
||||
foreach ($needsLines as $stat => $value) {
|
||||
@@ -178,12 +179,12 @@ class Item
|
||||
$str .= "<br>$stat" . $this->wrap($value);
|
||||
}
|
||||
}
|
||||
if ($this->add_min_physical_damage && !$this->add_max_physical_damage) {
|
||||
$damage = $this->add_min_physical_damage . ' - ' . $this->add_min_physical_damage;
|
||||
} elseif (!$this->add_min_physical_damage && $this->add_max_physical_damage) {
|
||||
$damage = $this->add_max_physical_damage . ' - ' . $this->add_max_physical_damage;
|
||||
} elseif ($this->add_min_physical_damage && $this->add_max_physical_damage) {
|
||||
$damage = $this->add_min_physical_damage . ' - ' . $this->add_max_physical_damage;
|
||||
if ($this->addMinPhysicalDamage && !$this->addMaxPhysicalDamage) {
|
||||
$damage = $this->addMinPhysicalDamage . ' - ' . $this->addMinPhysicalDamage;
|
||||
} elseif (!$this->addMinPhysicalDamage && $this->addMaxPhysicalDamage) {
|
||||
$damage = $this->addMaxPhysicalDamage . ' - ' . $this->addMaxPhysicalDamage;
|
||||
} elseif ($this->addMinPhysicalDamage && $this->addMaxPhysicalDamage) {
|
||||
$damage = $this->addMinPhysicalDamage . ' - ' . $this->addMaxPhysicalDamage;
|
||||
}
|
||||
if (isset($damage)) {
|
||||
$str .= '<br>Урон: ' . $damage;
|
||||
@@ -191,8 +192,8 @@ class Item
|
||||
return $str;
|
||||
}
|
||||
|
||||
public static function getItemById($item_id): Item
|
||||
public static function getItemById($itemId): Item
|
||||
{
|
||||
return new Item(Db::getInstance()->ofetch('select * from items where id = ?', $item_id));
|
||||
return new Item(Db::getInstance()->ofetch('select * from items where id = ?', $itemId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user