| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- <?php
- namespace App\Module\GameItems\Models;
- use Illuminate\Database\Eloquent\Relations\BelongsTo;
- use UCore\ModelCore;
- /**
- * 用户配方解锁状态
- *
- * field start
- * @property int $id 记录ID,主键
- * @property int $user_id 用户ID
- * @property int $recipe_id 配方ID,外键关联kku_item_recipes表
- * @property int $is_unlocked 是否已解锁(0:否, 1:是)
- * @property string $unlock_time 解锁时间
- * @property string $last_craft_time 最后合成时间
- * @property int $craft_count 合成次数
- * @property \Carbon\Carbon $created_at 创建时间
- * @property \Carbon\Carbon $updated_at 更新时间
- * field end
- */
- class ItemUserRecipe extends ModelCore
- {
- /**
- * 与模型关联的表名
- *
- * @var string
- */
- protected $table = 'item_user_recipes';
- // attrlist start
- protected $fillable = [
- 'id',
- 'user_id',
- 'recipe_id',
- 'is_unlocked',
- 'unlock_time',
- 'last_craft_time',
- 'craft_count',
- ];
- // attrlist end
- /**
- * 可批量赋值的属性
- *
- * @var array
- */
- protected $fillable = [
- 'user_id',
- 'recipe_id',
- 'is_unlocked',
- 'unlock_time',
- 'craft_count',
- 'last_craft_time',
- ];
- /**
- * 应该被转换为日期的属性
- *
- * @var array
- */
- protected $dates = [
- 'unlock_time',
- 'last_craft_time',
- 'created_at',
- 'updated_at',
- ];
- /**
- * 应该被转换为原生类型的属性
- *
- * @var array
- */
- protected $casts = [
- 'is_unlocked' => 'boolean',
- 'craft_count' => 'integer',
- ];
- /**
- * 获取关联的配方
- *
- * @return BelongsTo
- */
- public function recipe(): BelongsTo
- {
- return $this->belongsTo(ItemRecipe::class, 'recipe_id');
- }
- /**
- * 解锁配方
- *
- * @return bool
- */
- public function unlock(): bool
- {
- $this->is_unlocked = true;
- $this->unlock_time = now();
- return $this->save();
- }
- /**
- * 增加合成次数
- *
- * @param int $count 增加的次数
- * @return bool
- */
- public function incrementCraftCount(int $count = 1): bool
- {
- $this->craft_count += $count;
- $this->last_craft_time = now();
- return $this->save();
- }
- /**
- * 检查是否在冷却中
- *
- * @return bool
- */
- public function isInCooldown(): bool
- {
- if (empty($this->last_craft_time)) {
- return false;
- }
- $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0;
- if ($cooldownSeconds <= 0) {
- return false;
- }
- return $this->last_craft_time->addSeconds($cooldownSeconds)->isFuture();
- }
- /**
- * 获取剩余冷却时间(秒)
- *
- * @return int
- */
- public function getRemainingCooldown(): int
- {
- if (!$this->isInCooldown()) {
- return 0;
- }
- $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0;
- $cooldownEnd = $this->last_craft_time->addSeconds($cooldownSeconds);
- return max(0, $cooldownEnd->diffInSeconds(now()));
- }
- }
|