166 lines
3.6 KiB
PHP
166 lines
3.6 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Models;
|
||
|
|
|
||
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||
|
|
use Illuminate\Database\Eloquent\Model;
|
||
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @property int $id
|
||
|
|
* @property int $character_id
|
||
|
|
* @property int $item_id
|
||
|
|
* @property int $quantity
|
||
|
|
* @property bool $equipped
|
||
|
|
* @property \Illuminate\Support\Carbon $created_at
|
||
|
|
* @property \Illuminate\Support\Carbon $updated_at
|
||
|
|
*/
|
||
|
|
class Inventory extends Model
|
||
|
|
{
|
||
|
|
use HasFactory;
|
||
|
|
|
||
|
|
protected $fillable = [
|
||
|
|
'character_id',
|
||
|
|
'item_id',
|
||
|
|
'quantity',
|
||
|
|
'equipped',
|
||
|
|
];
|
||
|
|
|
||
|
|
protected $casts = [
|
||
|
|
'equipped' => 'boolean',
|
||
|
|
];
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the character that owns this inventory item.
|
||
|
|
*/
|
||
|
|
public function character(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Character::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the item.
|
||
|
|
*/
|
||
|
|
public function item(): BelongsTo
|
||
|
|
{
|
||
|
|
return $this->belongsTo(Item::class);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Add items to inventory
|
||
|
|
*/
|
||
|
|
public static function addItem(Character $character, Item $item, int $quantity = 1): self
|
||
|
|
{
|
||
|
|
$inventory = self::firstOrNew([
|
||
|
|
'character_id' => $character->id,
|
||
|
|
'item_id' => $item->id,
|
||
|
|
]);
|
||
|
|
|
||
|
|
$inventory->quantity += $quantity;
|
||
|
|
$inventory->save();
|
||
|
|
|
||
|
|
return $inventory;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Remove items from inventory
|
||
|
|
*/
|
||
|
|
public function remove(int $quantity = 1): bool
|
||
|
|
{
|
||
|
|
if ($this->quantity < $quantity) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->quantity -= $quantity;
|
||
|
|
|
||
|
|
if ($this->quantity <= 0) {
|
||
|
|
$this->delete();
|
||
|
|
} else {
|
||
|
|
$this->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Equip the item
|
||
|
|
*/
|
||
|
|
public function equip(): bool
|
||
|
|
{
|
||
|
|
if (!$this->item->canEquip()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Unequip any other item of the same type
|
||
|
|
$this->character->inventory()
|
||
|
|
->where('equipped', true)
|
||
|
|
->whereHas('item', function($q) {
|
||
|
|
$q->where('type', $this->item->type);
|
||
|
|
})
|
||
|
|
->update(['equipped' => false]);
|
||
|
|
|
||
|
|
$this->equipped = true;
|
||
|
|
$this->save();
|
||
|
|
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Unequip the item
|
||
|
|
*/
|
||
|
|
public function unequip(): void
|
||
|
|
{
|
||
|
|
$this->equipped = false;
|
||
|
|
$this->save();
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Use the item (consume it)
|
||
|
|
*/
|
||
|
|
public function use(Character $character): bool
|
||
|
|
{
|
||
|
|
if (!$this->item->canUse()) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!$this->item->canBeUsedBy($character)) {
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
// Apply item effects
|
||
|
|
$this->applyEffects($character);
|
||
|
|
|
||
|
|
// Remove one from inventory
|
||
|
|
return $this->remove(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Apply the item's effects to the character
|
||
|
|
*/
|
||
|
|
protected function applyEffects(Character $character): void
|
||
|
|
{
|
||
|
|
$item = $this->item;
|
||
|
|
|
||
|
|
if ($item->health_bonus > 0) {
|
||
|
|
$character->heal($item->health_bonus);
|
||
|
|
}
|
||
|
|
|
||
|
|
if ($item->energy_bonus > 0) {
|
||
|
|
$character->addEnergy($item->energy_bonus);
|
||
|
|
}
|
||
|
|
|
||
|
|
// Add money if it's a currency item
|
||
|
|
if ($item->type === ItemType::CURRENCY && $item->value > 0) {
|
||
|
|
$character->addMoney($item->value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the total value of this inventory item
|
||
|
|
*/
|
||
|
|
public function getTotalValueAttribute(): int
|
||
|
|
{
|
||
|
|
return $this->quantity * $this->item->value;
|
||
|
|
}
|
||
|
|
}
|