battles/classes/Battles/Item.php

200 lines
6.9 KiB
PHP
Raw Normal View History

2019-01-11 16:02:57 +00:00
<?php
namespace Battles;
2022-12-16 23:20:43 +00:00
use Battles\Database\Db;
2021-08-30 16:30:56 +00:00
class Item
2019-01-11 16:02:57 +00:00
{
2022-12-16 23:20:43 +00:00
protected int $id;
protected string $name = '';
2022-12-16 23:20:43 +00:00
protected int $type = self::TYPE_TRASH;
protected int $durability = 0;
2022-12-16 23:20:43 +00:00
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 = '';
2022-12-16 23:20:43 +00:00
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;
2019-01-14 21:54:20 +00:00
2019-01-11 16:26:33 +00:00
/**
* Item constructor.
2019-02-15 11:38:30 +00:00
*
2019-01-11 16:26:33 +00:00
* @param $row
*/
2019-01-11 16:02:57 +00:00
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;
}
2019-01-14 21:31:59 +00:00
}
2019-01-14 13:46:20 +00:00
}
2019-01-14 14:18:04 +00:00
2022-12-16 23:20:43 +00:00
switch ($this->type) {
case self::TYPE_HELMET:
$this->typename = 'Шлем';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_ARMOR:
$this->typename = 'Броня';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_LEGS:
$this->typename = 'Поножи';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_BOOTS:
$this->typename = 'Сапоги';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_GLOVES:
$this->typename = 'Перчатки';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_WEAPON:
$this->typename = 'Оружие';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_SHIELD:
$this->typename = 'Щит';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_BELT:
$this->typename = 'Пояс';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_RING:
$this->typename = 'Кольцо';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_AMULET:
$this->typename = 'Амулет';
break;
2022-12-16 23:20:43 +00:00
case self::TYPE_CONSUMABLE:
$this->typename = 'Расходуемый предмет';
break;
default:
$this->typename = 'Хлам';
2019-01-11 22:50:10 +00:00
}
2022-12-16 23:20:43 +00:00
$this->cost = $this->calculateItemCost();
}
/** Рассчёт стоимости предмета в зависимости от его характеристик.
* @return int
*/
2021-08-30 16:30:56 +00:00
protected function calculateItemCost(): int
{
2022-12-16 23:20:43 +00:00
$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 чтобы цена пропрорционально росла.
2022-12-16 23:20:43 +00:00
$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);
2019-01-11 16:02:57 +00:00
}
2019-01-11 19:39:37 +00:00
protected function wrap(int $number): string
2019-01-11 20:05:09 +00:00
{
if ($number > 0) {
return ": <b>" . $number . "</b>";
} else {
return ": <b style='color: maroon;'>" . $number . "</b>";
}
2019-01-11 20:05:09 +00:00
}
public function getAllInfo(): string
2019-01-11 20:05:09 +00:00
{
$needsLines = [
2022-12-16 23:20:43 +00:00
'сила' => $this->needStrength,
'ловкость' => $this->needDexterity,
'интуиция' => $this->needIntuition,
'выносливость' => $this->needEndurance,
'интеллект' => $this->needIntelligence,
'мудрость' => $this->needWisdom,
];
$addsLines = [
2022-12-16 23:20:43 +00:00
'Сила' => $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)";
2022-12-16 23:20:43 +00:00
$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);
}
}
2022-12-16 23:20:43 +00:00
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;
}
2020-07-22 09:50:14 +00:00
if (isset($damage)) {
$str .= '<br>Урон: ' . $damage;
2019-01-14 13:46:20 +00:00
}
return $str;
2019-01-14 13:46:20 +00:00
}
2021-08-30 16:30:56 +00:00
2022-12-16 23:20:43 +00:00
public static function getItemById($itemId): Item
2021-08-30 16:30:56 +00:00
{
2022-12-16 23:20:43 +00:00
return new Item(Db::getInstance()->ofetch('select * from items where id = ?', $itemId));
2021-08-30 16:30:56 +00:00
}
2022-12-16 23:20:43 +00:00
}