| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?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 int $item_id 关联的基础物品ID,外键关联kku_item_items表
- * @property string $name 物品名称(可以与基础物品不同)
- * @property object|array $display_attributes 展示属性,以JSON格式存储键值对
- * @property object|array $numeric_attributes 数值属性,以JSON格式存储键值对
- * @property int $tradable 是否可交易(0:不可交易, 1:可交易,默认)
- * @property int $is_bound 是否已绑定(0:未绑定, 1:已绑定)
- * @property string $bound_to 绑定对象(账号ID或角色ID)
- * @property string $bind_exp_time 绑定过期时间(为空表示永久绑定)
- * @property string $expire_at 物品过期时间(可为空)
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemInstance extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_instances';
- // attrlist start
- protected $fillable = [
- 'id',
- 'item_id',
- 'name',
- 'display_attributes',
- 'numeric_attributes',
- 'tradable',
- 'is_bound',
- 'bound_to',
- 'bind_exp_time',
- 'expire_at',
- ];
- // attrlist end
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'bind_exp_time',
- '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,
- 'tradable' => 'boolean',
- 'is_bound' => 'boolean',
- ];
- /**
- * 获取物品实例对应的基础物品
- *
- * @return BelongsTo
- */
- public function item(): BelongsTo
- {
- return $this->belongsTo(Item::class, 'item_id');
- }
- /**
- * 获取拥有该物品实例的用户
- *
- * @return HasMany
- */
- public function users(): HasMany
- {
- return $this->hasMany(ItemUser::class, 'instance_id');
- }
- /**
- * 检查物品实例是否已过期
- *
- * @return bool
- */
- public function isExpired(): bool
- {
- if (empty($this->expire_at)) {
- return $this->item->isExpired();
- }
- return $this->expire_at->isPast() || $this->item->isExpired();
- }
- /**
- * 检查物品是否已绑定
- *
- * @return bool
- */
- public function isBound(): bool
- {
- return $this->is_bound;
- }
- /**
- * 检查绑定是否已过期
- *
- * @return bool
- */
- public function isBindExpired(): bool
- {
- if (empty($this->bind_exp_time)) {
- return false;
- }
- return $this->bind_exp_time->isPast();
- }
- /**
- * 检查物品是否可交易
- *
- * @return bool
- */
- public function isTradable(): bool
- {
- if (!$this->tradable) {
- return false;
- }
- if ($this->is_bound && !$this->isBindExpired()) {
- return false;
- }
- return true;
- }
- }
|