| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- <?php
- namespace App\Module\GameItems\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use Illuminate\Database\Eloquent\Relations\HasMany;
- use UCore\ModelCore;
- /**
- * 物品基础信息
- *
- * field start
- * @property int $id 物品ID,主键
- * @property string $name 物品名称
- * @property string $description 物品描述
- * @property int $category_id 物品分类ID,外键关联kku_item_categories表
- * @property int $type 物品类型(1:可使用, 2:可装备, 3:可合成, 4:可交任务, 5:可开启...)
- * @property int $is_unique 是否是单独属性物品(0:否,默认, 1:是)
- * @property int $rarity 稀有度(1:普通, 2:稀有, 3:史诗, 4:传说...)
- * @property string $icon 物品图标路径
- * @property int $max_stack 最大堆叠数量
- * @property int $sell_price 出售价格
- * @property int $tradable 是否可交易(0:不可交易, 1:可交易,默认)
- * @property int $dismantlable 是否可分解(0:不可分解, 1:可分解,默认)
- * @property int $default_expire_seconds 玩家获取物品后的默认有效秒数(0表示永久有效)
- * @property object|array $display_attributes 展示属性,以JSON格式存储键值对,用于界面展示和描述的属性
- * @property object|array $numeric_attributes 数值属性,以JSON格式存储键值对,用于计算和游戏逻辑的属性
- * @property string $global_expire_at 物品全局过期时间(可为空)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class Item extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_items';
- // attrlist start
- protected $fillable = [
- 'id',
- 'name',
- 'description',
- 'category_id',
- 'type',
- 'is_unique',
- 'rarity',
- 'icon',
- 'max_stack',
- 'sell_price',
- 'tradable',
- 'dismantlable',
- 'default_expire_seconds',
- 'display_attributes',
- 'numeric_attributes',
- 'global_expire_at',
- ];
- // attrlist end
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'global_expire_at',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'display_attributes' => \App\Module\GameItems\Casts\DisplayAttributesCast::class,
- 'numeric_attributes' => \App\Module\GameItems\Casts\NumericAttributesCast::class,
- 'is_unique' => 'boolean',
- 'tradable' => 'boolean',
- 'dismantlable' => 'boolean',
- ];
- /**
- * 获取物品所属分类
- *
- * @return BelongsTo
- */
- public function category(): BelongsTo
- {
- return $this->belongsTo(ItemCategory::class, 'category_id');
- }
- /**
- * 获取物品的所有实例
- *
- * @return HasMany
- */
- public function instances(): HasMany
- {
- return $this->hasMany(ItemInstance::class, 'item_id');
- }
- /**
- * 获取拥有该物品的用户
- *
- * @return HasMany
- */
- public function users(): HasMany
- {
- return $this->hasMany(ItemUser::class, 'item_id');
- }
- /**
- * 判断物品是否为宝箱
- *
- * @return bool
- */
- public function isChest(): bool
- {
- return $this->type == 5; // 5表示宝箱类型
- }
- /**
- * 获取宝箱内容配置
- *
- * @return HasMany
- */
- public function chestContents(): HasMany
- {
- if (!$this->isChest()) {
- return $this->hasMany(ItemChestContent::class, 'chest_id')->whereRaw('1 = 0'); // 返回空集合
- }
- return $this->hasMany(ItemChestContent::class, 'chest_id');
- }
- /**
- * 检查物品是否已过期(全局过期)
- *
- * @return bool
- */
- public function isExpired(): bool
- {
- if (empty($this->global_expire_at)) {
- return false;
- }
- return $this->global_expire_at->isPast();
- }
- }
|