ItemInstance.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 int $item_id 关联的基础物品ID,外键关联kku_item_items表
  12. * @property string $name 物品名称(可以与基础物品不同)
  13. * @property object|array $display_attributes 展示属性,以JSON格式存储键值对
  14. * @property object|array $numeric_attributes 数值属性,以JSON格式存储键值对
  15. * @property int $tradable 是否可交易(0:不可交易, 1:可交易,默认)
  16. * @property int $is_bound 是否已绑定(0:未绑定, 1:已绑定)
  17. * @property string $bound_to 绑定对象(账号ID或角色ID)
  18. * @property string $bind_exp_time 绑定过期时间(为空表示永久绑定)
  19. * @property string $expire_at 物品过期时间(可为空)
  20. * @property \Carbon\Carbon $created_at 创建时间
  21. * @property \Carbon\Carbon $updated_at 更新时间
  22. * field end
  23. */
  24. class ItemInstance extends ModelCore
  25. {
  26. /**
  27. * 与模型关联的表名
  28. *
  29. * @var string
  30. */
  31. protected $table = 'item_instances';
  32. // attrlist start
  33. protected $fillable = [
  34. 'id',
  35. 'item_id',
  36. 'name',
  37. 'display_attributes',
  38. 'numeric_attributes',
  39. 'tradable',
  40. 'is_bound',
  41. 'bound_to',
  42. 'bind_exp_time',
  43. 'expire_at',
  44. ];
  45. // attrlist end
  46. /**
  47. * 应该被转换为日期的属性
  48. *
  49. * @var array
  50. */
  51. protected $dates = [
  52. 'bind_exp_time',
  53. 'expire_at',
  54. 'created_at',
  55. 'updated_at',
  56. ];
  57. /**
  58. * 应该被转换为原生类型的属性
  59. *
  60. * @var array
  61. */
  62. protected $casts = [
  63. 'display_attributes' => \App\Module\GameItems\Casts\DisplayAttributesCast::class,
  64. 'numeric_attributes' => \App\Module\GameItems\Casts\NumericAttributesCast::class,
  65. 'tradable' => 'boolean',
  66. 'is_bound' => 'boolean',
  67. ];
  68. /**
  69. * 获取物品实例对应的基础物品
  70. *
  71. * @return BelongsTo
  72. */
  73. public function item(): BelongsTo
  74. {
  75. return $this->belongsTo(ItemItem::class, 'item_id');
  76. }
  77. /**
  78. * 获取拥有该物品实例的用户
  79. *
  80. * @return HasMany
  81. */
  82. public function users(): HasMany
  83. {
  84. return $this->hasMany(ItemUser::class, 'instance_id');
  85. }
  86. /**
  87. * 检查物品实例是否已过期
  88. *
  89. * @return bool
  90. */
  91. public function isExpired(): bool
  92. {
  93. if (empty($this->expire_at)) {
  94. return $this->item->isExpired();
  95. }
  96. return $this->expire_at->isPast() || $this->item->isExpired();
  97. }
  98. /**
  99. * 检查物品是否已绑定
  100. *
  101. * @return bool
  102. */
  103. public function isBound(): bool
  104. {
  105. return $this->is_bound;
  106. }
  107. /**
  108. * 检查绑定是否已过期
  109. *
  110. * @return bool
  111. */
  112. public function isBindExpired(): bool
  113. {
  114. if (empty($this->bind_exp_time)) {
  115. return false;
  116. }
  117. return $this->bind_exp_time->isPast();
  118. }
  119. /**
  120. * 检查物品是否可交易
  121. *
  122. * @return bool
  123. */
  124. public function isTradable(): bool
  125. {
  126. if (!$this->tradable) {
  127. return false;
  128. }
  129. if ($this->is_bound && !$this->isBindExpired()) {
  130. return false;
  131. }
  132. return true;
  133. }
  134. }