Item.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use Illuminate\Database\Eloquent\Relations\HasMany;
  5. use UCore\ModelCore;
  6. /**
  7. * 物品基础信息
  8. *
  9. * field start
  10. * @property int $id 物品ID,主键
  11. * @property string $name 物品名称
  12. * @property string $description 物品描述
  13. * @property int $category_id 物品分类ID,外键关联kku_item_categories表
  14. * @property int $type 物品类型(1:可使用, 2:可装备, 3:可合成, 4:可交任务, 5:可开启...)
  15. * @property int $is_unique 是否是单独属性物品(0:否,默认, 1:是)
  16. * @property int $rarity 稀有度(1:普通, 2:稀有, 3:史诗, 4:传说...)
  17. * @property string $icon 物品图标路径
  18. * @property int $max_stack 最大堆叠数量
  19. * @property int $sell_price 出售价格
  20. * @property int $tradable 是否可交易(0:不可交易, 1:可交易,默认)
  21. * @property int $dismantlable 是否可分解(0:不可分解, 1:可分解,默认)
  22. * @property int $default_expire_seconds 玩家获取物品后的默认有效秒数(0表示永久有效)
  23. * @property object|array $display_attributes 展示属性,以JSON格式存储键值对,用于界面展示和描述的属性
  24. * @property object|array $numeric_attributes 数值属性,以JSON格式存储键值对,用于计算和游戏逻辑的属性
  25. * @property string $global_expire_at 物品全局过期时间(可为空)
  26. * @property \Carbon\Carbon $created_at 创建时间
  27. * @property \Carbon\Carbon $updated_at 更新时间
  28. * field end
  29. */
  30. class Item extends ModelCore
  31. {
  32. /**
  33. * 与模型关联的表名
  34. *
  35. * @var string
  36. */
  37. protected $table = 'item_items';
  38. // attrlist start
  39. protected $fillable = [
  40. 'id',
  41. 'name',
  42. 'description',
  43. 'category_id',
  44. 'type',
  45. 'is_unique',
  46. 'rarity',
  47. 'icon',
  48. 'max_stack',
  49. 'sell_price',
  50. 'tradable',
  51. 'dismantlable',
  52. 'default_expire_seconds',
  53. 'display_attributes',
  54. 'numeric_attributes',
  55. 'global_expire_at',
  56. ];
  57. // attrlist end
  58. /**
  59. * 应该被转换为日期的属性
  60. *
  61. * @var array
  62. */
  63. protected $dates = [
  64. 'global_expire_at',
  65. 'created_at',
  66. 'updated_at',
  67. ];
  68. /**
  69. * 应该被转换为原生类型的属性
  70. *
  71. * @var array
  72. */
  73. protected $casts = [
  74. 'display_attributes' => \App\Module\GameItems\Casts\DisplayAttributesCast::class,
  75. 'numeric_attributes' => \App\Module\GameItems\Casts\NumericAttributesCast::class,
  76. 'is_unique' => 'boolean',
  77. 'tradable' => 'boolean',
  78. 'dismantlable' => 'boolean',
  79. ];
  80. /**
  81. * 获取物品所属分类
  82. *
  83. * @return BelongsTo
  84. */
  85. public function category(): BelongsTo
  86. {
  87. return $this->belongsTo(ItemCategory::class, 'category_id');
  88. }
  89. /**
  90. * 获取物品的所有实例
  91. *
  92. * @return HasMany
  93. */
  94. public function instances(): HasMany
  95. {
  96. return $this->hasMany(ItemInstance::class, 'item_id');
  97. }
  98. /**
  99. * 获取拥有该物品的用户
  100. *
  101. * @return HasMany
  102. */
  103. public function users(): HasMany
  104. {
  105. return $this->hasMany(ItemUser::class, 'item_id');
  106. }
  107. /**
  108. * 判断物品是否为宝箱
  109. *
  110. * @return bool
  111. */
  112. public function isChest(): bool
  113. {
  114. return $this->type == 5; // 5表示宝箱类型
  115. }
  116. /**
  117. * 获取宝箱内容配置
  118. *
  119. * @return HasMany
  120. */
  121. public function chestContents(): HasMany
  122. {
  123. if (!$this->isChest()) {
  124. return $this->hasMany(ItemChestContent::class, 'chest_id')->whereRaw('1 = 0'); // 返回空集合
  125. }
  126. return $this->hasMany(ItemChestContent::class, 'chest_id');
  127. }
  128. /**
  129. * 检查物品是否已过期(全局过期)
  130. *
  131. * @return bool
  132. */
  133. public function isExpired(): bool
  134. {
  135. if (empty($this->global_expire_at)) {
  136. return false;
  137. }
  138. return $this->global_expire_at->isPast();
  139. }
  140. }