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(); } }