ItemUserRecipe.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace App\Module\GameItems\Models;
  3. use Illuminate\Database\Eloquent\Relations\BelongsTo;
  4. use UCore\ModelCore;
  5. /**
  6. * 用户配方解锁状态
  7. *
  8. * field start
  9. * @property int $id 记录ID,主键
  10. * @property int $user_id 用户ID
  11. * @property int $recipe_id 配方ID,外键关联kku_item_recipes表
  12. * @property bool $is_unlocked 是否已解锁(0:否, 1:是)
  13. * @property string $unlock_time 解锁时间
  14. * @property string $last_craft_time 最后合成时间
  15. * @property int $craft_count 合成次数
  16. * @property \Carbon\Carbon $created_at 创建时间
  17. * @property \Carbon\Carbon $updated_at 更新时间
  18. * field end
  19. */
  20. class ItemUserRecipe extends ModelCore
  21. {
  22. /**
  23. * 与模型关联的表名
  24. *
  25. * @var string
  26. */
  27. protected $table = 'item_user_recipes';
  28. // attrlist start
  29. protected $fillable = [
  30. 'id',
  31. 'user_id',
  32. 'recipe_id',
  33. 'is_unlocked',
  34. 'unlock_time',
  35. 'last_craft_time',
  36. 'craft_count',
  37. ];
  38. // attrlist end
  39. /**
  40. * 应该被转换为日期的属性
  41. *
  42. * @var array
  43. */
  44. protected $dates = [
  45. 'unlock_time',
  46. 'last_craft_time',
  47. 'created_at',
  48. 'updated_at',
  49. ];
  50. /**
  51. * 应该被转换为原生类型的属性
  52. *
  53. * @var array
  54. */
  55. protected $casts = [
  56. 'is_unlocked' => 'boolean',
  57. 'craft_count' => 'integer',
  58. ];
  59. /**
  60. * 获取关联的配方
  61. *
  62. * @return BelongsTo
  63. */
  64. public function recipe(): BelongsTo
  65. {
  66. return $this->belongsTo(ItemRecipe::class, 'recipe_id');
  67. }
  68. /**
  69. * 解锁配方
  70. *
  71. * @return bool
  72. */
  73. public function unlock(): bool
  74. {
  75. $this->is_unlocked = true;
  76. $this->unlock_time = now();
  77. return $this->save();
  78. }
  79. /**
  80. * 增加合成次数
  81. *
  82. * @param int $count 增加的次数
  83. * @return bool
  84. */
  85. public function incrementCraftCount(int $count = 1): bool
  86. {
  87. $this->craft_count += $count;
  88. $this->last_craft_time = now();
  89. return $this->save();
  90. }
  91. /**
  92. * 检查是否在冷却中
  93. *
  94. * @return bool
  95. */
  96. public function isInCooldown(): bool
  97. {
  98. if (empty($this->last_craft_time)) {
  99. return false;
  100. }
  101. $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0;
  102. if ($cooldownSeconds <= 0) {
  103. return false;
  104. }
  105. return $this->last_craft_time->addSeconds($cooldownSeconds)->isFuture();
  106. }
  107. /**
  108. * 获取剩余冷却时间(秒)
  109. *
  110. * @return int
  111. */
  112. public function getRemainingCooldown(): int
  113. {
  114. if (!$this->isInCooldown()) {
  115. return 0;
  116. }
  117. $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0;
  118. $cooldownEnd = $this->last_craft_time->addSeconds($cooldownSeconds);
  119. return max(0, $cooldownEnd->diffInSeconds(now()));
  120. }
  121. }