battles/classes/Battles/Item.php
Ivor Barhansky 0398425205 Code smell.
2022-12-17 03:03:56 +02:00

200 lines
6.9 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace Battles;
use Battles\Database\Db;
class Item
{
protected int $id;
protected string $name = '';
protected int $type = self::TYPE_TRASH;
protected int $durability = 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 $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;
/**
* Item constructor.
*
* @param $row
*/
public function __construct($row)
{
if (is_array($row)) {
foreach ($this as $key => $value) {
if (isset($row[$key])) {
$this->$key = $row[$key];
}
}
} elseif (is_object($row)) {
foreach ($this as $name => $value) {
if (isset($row->$name)) {
$this->$name = $row->$name;
}
}
}
switch ($this->type) {
case self::TYPE_HELMET:
$this->typename = 'Шлем';
break;
case self::TYPE_ARMOR:
$this->typename = 'Броня';
break;
case self::TYPE_LEGS:
$this->typename = 'Поножи';
break;
case self::TYPE_BOOTS:
$this->typename = 'Сапоги';
break;
case self::TYPE_GLOVES:
$this->typename = 'Перчатки';
break;
case self::TYPE_WEAPON:
$this->typename = 'Оружие';
break;
case self::TYPE_SHIELD:
$this->typename = 'Щит';
break;
case self::TYPE_BELT:
$this->typename = 'Пояс';
break;
case self::TYPE_RING:
$this->typename = 'Кольцо';
break;
case self::TYPE_AMULET:
$this->typename = 'Амулет';
break;
case self::TYPE_CONSUMABLE:
$this->typename = 'Расходуемый предмет';
break;
default:
$this->typename = 'Хлам';
}
$this->cost = $this->calculateItemCost();
}
/** Рассчёт стоимости предмета в зависимости от его характеристик.
* @return int
*/
protected function calculateItemCost(): int
{
$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 чтобы цена пропрорционально росла.
$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
{
if ($number > 0) {
return ": <b>" . $number . "</b>";
} else {
return ": <b style='color: maroon;'>" . $number . "</b>";
}
}
public function getAllInfo(): string
{
$needsLines = [
'сила' => $this->needStrength,
'ловкость' => $this->needDexterity,
'интуиция' => $this->needIntuition,
'выносливость' => $this->needEndurance,
'интеллект' => $this->needIntelligence,
'мудрость' => $this->needWisdom,
];
$addsLines = [
'Сила' => $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->cost;
$str .= '<br> Долговечность: ' . $this->durability;
$str .= "<br><em>$this->typename</em><br>";
foreach ($needsLines as $stat => $value) {
if ($value > 0) {
$str .= "<br>Требуется $stat" . $this->wrap($value);
}
}
foreach ($addsLines as $stat => $value) {
if ($value) {
$str .= "<br>$stat" . $this->wrap($value);
}
}
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;
}
return $str;
}
public static function getItemById($itemId): Item
{
return new Item(Db::getInstance()->ofetch('select * from items where id = ?', $itemId));
}
}