first commit
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\ItemType;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
/**
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property string $slug
|
||||
* @property string|null $description
|
||||
* @property ItemType $type
|
||||
* @property int $value
|
||||
* @property int|null $buy_price
|
||||
* @property int|null $sell_price
|
||||
* @property int|null $requires_level
|
||||
* @property int $attack_bonus
|
||||
* @property int $defense_bonus
|
||||
* @property int $health_bonus
|
||||
* @property int $energy_bonus
|
||||
* @property bool $is_tradeable
|
||||
* @property bool $is_usable
|
||||
* @property \Illuminate\Support\Carbon $created_at
|
||||
* @property \Illuminate\Support\Carbon $updated_at
|
||||
*/
|
||||
class Item extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'slug',
|
||||
'description',
|
||||
'type',
|
||||
'value',
|
||||
'buy_price',
|
||||
'sell_price',
|
||||
'requires_level',
|
||||
'attack_bonus',
|
||||
'defense_bonus',
|
||||
'health_bonus',
|
||||
'energy_bonus',
|
||||
'is_tradeable',
|
||||
'is_usable',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'type' => ItemType::class,
|
||||
'is_tradeable' => 'boolean',
|
||||
'is_usable' => 'boolean',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the characters that have this item in their inventory.
|
||||
*/
|
||||
public function characters(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Character::class, 'inventories')
|
||||
->withPivot('quantity', 'equipped')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the shops that sell this item.
|
||||
*/
|
||||
public function shops(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(Shop::class, 'shop_items')
|
||||
->withPivot('stock', 'price')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this item can be equipped
|
||||
*/
|
||||
public function canEquip(): bool
|
||||
{
|
||||
return $this->type->canEquip();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this item can be used/consumed
|
||||
*/
|
||||
public function canUse(): bool
|
||||
{
|
||||
return $this->is_usable && $this->type->canUse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a character can use this item (level requirement)
|
||||
*/
|
||||
public function canBeUsedBy(Character $character): bool
|
||||
{
|
||||
if ($this->requires_level && $character->level < $this->requires_level) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the sell price or default to half of buy price
|
||||
*/
|
||||
public function getSellPriceAttribute(): int
|
||||
{
|
||||
if ($this->sell_price > 0) {
|
||||
return $this->sell_price;
|
||||
}
|
||||
|
||||
return (int) ($this->buy_price * 0.5);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a weapon
|
||||
*/
|
||||
public function isWeapon(): bool
|
||||
{
|
||||
return $this->type === ItemType::WEAPON;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is armor
|
||||
*/
|
||||
public function isArmor(): bool
|
||||
{
|
||||
return $this->type === ItemType::ARMOR;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if this is a consumable
|
||||
*/
|
||||
public function isConsumable(): bool
|
||||
{
|
||||
return $this->type === ItemType::CONSUMABLE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the display name for the item type
|
||||
*/
|
||||
public function getTypeLabelAttribute(): string
|
||||
{
|
||||
return $this->type->label();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user