battles/classes/Battles/DressedItems.php

105 lines
3.2 KiB
PHP
Raw Normal View History

2020-07-06 19:54:50 +00:00
<?php
/**
* Author: lopiu
* Date: 06.07.2020
* Time: 22:41
*/
namespace Battles;
use Battles\Database\DBPDO;
use stdClass;
2020-07-06 19:54:50 +00:00
class DressedItems
{
private $USERID;
2020-07-06 19:54:50 +00:00
private $dressedItem;
private static $db;
2020-07-06 19:54:50 +00:00
/**
* DressedItems constructor.
* @param int $user_id ID игрока.
*/
public function __construct(int $user_id)
{
self::$db = DBPDO::INIT();
$this->USERID = $user_id;
}
2020-07-06 20:34:34 +00:00
public static function getDressedItemBySlot($itemSlot, $ownerId)
{
return self::$db->fetch('SELECT *, COUNT(1) AS count FROM inventory WHERE owner_id = ? AND dressed_slot = ?', [$ownerId, $itemSlot]);
}
public function getItemsInSlots(): stdClass
2020-07-22 14:04:15 +00:00
{
$items = self::$db->ofetchALL('SELECT * FROM inventory WHERE owner_id = ? AND dressed_slot > 0', $this->USERID);
$this->dressedItem = new stdClass();
foreach ($items as $item) {
$i = $item->dressed_slot;
$this->dressedItem->$i = $item;
}
return $this->dressedItem;
2020-07-22 14:04:15 +00:00
}
private function getBonuses(): array
{
$query = <<<SQL
SELECT SUM(add_strength) as sum_strength,
2020-07-20 13:51:23 +00:00
SUM(add_dexterity) as sum_dexterity,
SUM(add_intuition) as sum_intuition,
SUM(add_endurance) as sum_endurance,
SUM(add_intelligence) as sum_intelligence,
SUM(add_wisdom) as sum_wisdom,
SUM(add_accuracy) as sum_accuracy,
SUM(add_evasion) as sum_evasion,
SUM(add_criticals) as sum_criticals,
SUM(add_min_physical_damage) as sum_min_phys_damage,
2020-07-22 14:04:15 +00:00
SUM(add_max_physical_damage) as sum_max_phys_damage
FROM inventory WHERE owner_id = ? AND dressed_slot > 0
SQL;
return self::$db->fetch($query, $this->USERID);
2020-07-06 19:54:50 +00:00
}
public function getAccuracyBonus(): ?int
{
return self::getBonuses()['sum_accuracy'] ?? 0;
}
public function getEvasionBonus(): ?int
{
return self::getBonuses()['sum_evasion'] ?? 0;
}
public function getCriticalsBonus(): ?int
{
return self::getBonuses()['sum_criticals'] ?? 0;
}
public function getMinPhysDamage(): ?int
{
return self::getBonuses()['sum_min_phys_damage'];
}
public function getMaxPhysDamage(): ?int
{
return self::getBonuses()['sum_max_phys_damage'];
}
/**
* Снимает с предмета статус одетого на персонажа в определённом слоте персонажа.
* @param $slot_id - номер слота.
*/
public function undressItem($slot_id)
{
self::getItemsInSlots();
// Проверяем, что используется один из 12 слотов и наличие предмета в слоте.
if (in_array($slot_id, Item::ITEM_TYPES_ALLOWED_IN_SLOTS) && $this->dressedItem->$slot_id) {
self::$db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot = ? AND owner_id = ?', [$slot_id, $this->USERID]);
2020-07-22 14:04:15 +00:00
}
}
public static function undressAllItems($user_id)
{
return self::$db->execute('UPDATE inventory SET dressed_slot = 0 WHERE dressed_slot BETWEEN 1 AND 12 AND owner_id = ?', $user_id);
}
2020-07-06 19:54:50 +00:00
}