ItemUserRecipe.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 int $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 $fillable = [
  45. 'user_id',
  46. 'recipe_id',
  47. 'is_unlocked',
  48. 'unlock_time',
  49. 'craft_count',
  50. 'last_craft_time',
  51. ];
  52. /**
  53. * 应该被转换为日期的属性
  54. *
  55. * @var array
  56. */
  57. protected $dates = [
  58. 'unlock_time',
  59. 'last_craft_time',
  60. 'created_at',
  61. 'updated_at',
  62. ];
  63. /**
  64. * 应该被转换为原生类型的属性
  65. *
  66. * @var array
  67. */
  68. protected $casts = [
  69. 'is_unlocked' => 'boolean',
  70. 'craft_count' => 'integer',
  71. ];
  72. /**
  73. * 获取关联的配方
  74. *
  75. * @return BelongsTo
  76. */
  77. public function recipe(): BelongsTo
  78. {
  79. return $this->belongsTo(ItemRecipe::class, 'recipe_id');
  80. }
  81. /**
  82. * 解锁配方
  83. *
  84. * @return bool
  85. */
  86. public function unlock(): bool
  87. {
  88. $this->is_unlocked = true;
  89. $this->unlock_time = now();
  90. return $this->save();
  91. }
  92. /**
  93. * 增加合成次数
  94. *
  95. * @param int $count 增加的次数
  96. * @return bool
  97. */
  98. public function incrementCraftCount(int $count = 1): bool
  99. {
  100. $this->craft_count += $count;
  101. $this->last_craft_time = now();
  102. return $this->save();
  103. }
  104. /**
  105. * 检查是否在冷却中
  106. *
  107. * @return bool
  108. */
  109. public function isInCooldown(): bool
  110. {
  111. if (empty($this->last_craft_time)) {
  112. return false;
  113. }
  114. $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0;
  115. if ($cooldownSeconds <= 0) {
  116. return false;
  117. }
  118. return $this->last_craft_time->addSeconds($cooldownSeconds)->isFuture();
  119. }
  120. /**
  121. * 获取剩余冷却时间(秒)
  122. *
  123. * @return int
  124. */
  125. public function getRemainingCooldown(): int
  126. {
  127. if (!$this->isInCooldown()) {
  128. return 0;
  129. }
  130. $cooldownSeconds = $this->recipe->cooldown_seconds ?? 0;
  131. $cooldownEnd = $this->last_craft_time->addSeconds($cooldownSeconds);
  132. return max(0, $cooldownEnd->diffInSeconds(now()));
  133. }
  134. }