$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 ": " . $number . "";
} else {
return ": " . $number . "";
}
}
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 = "$this->name (Масса: $this->weight)";
$str .= '
Стоимость: ' . $this->cost;
$str .= '
Долговечность: ' . $this->durability;
$str .= "
$this->typename
";
foreach ($needsLines as $stat => $value) {
if ($value > 0) {
$str .= "
Требуется $stat" . $this->wrap($value);
}
}
foreach ($addsLines as $stat => $value) {
if ($value) {
$str .= "
$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 .= '
Урон: ' . $damage;
}
return $str;
}
public static function getItemById($itemId): Item
{
return new Item(Db::getInstance()->ofetch('select * from items where id = ?', $itemId));
}
}