| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576 |
- <?php
- namespace App\Module\GameItems\Logics;
- use App\Module\GameItems\Models\ItemUserRecipe;
- /**
- * 用户配方逻辑类
- */
- class UserRecipe
- {
- /**
- * 解锁配方
- *
- * @param ItemUserRecipe $userRecipe 用户配方模型
- * @return bool
- */
- public function unlock(ItemUserRecipe $userRecipe): bool
- {
- $userRecipe->is_unlocked = true;
- $userRecipe->unlock_time = now();
- return $userRecipe->save();
- }
- /**
- * 增加合成次数
- *
- * @param ItemUserRecipe $userRecipe 用户配方模型
- * @param int $count 增加的次数
- * @return bool
- */
- public function incrementCraftCount(ItemUserRecipe $userRecipe, int $count = 1): bool
- {
- $userRecipe->craft_count += $count;
- $userRecipe->last_craft_time = now();
- return $userRecipe->save();
- }
- /**
- * 检查是否在冷却中
- *
- * @param ItemUserRecipe $userRecipe 用户配方模型
- * @return bool
- */
- public function isInCooldown(ItemUserRecipe $userRecipe): bool
- {
- if (empty($userRecipe->last_craft_time)) {
- return false;
- }
- $cooldownSeconds = $userRecipe->recipe->cooldown_seconds ?? 0;
- if ($cooldownSeconds <= 0) {
- return false;
- }
- return $userRecipe->last_craft_time->addSeconds($cooldownSeconds)->isFuture();
- }
- /**
- * 获取剩余冷却时间(秒)
- *
- * @param ItemUserRecipe $userRecipe 用户配方模型
- * @return int
- */
- public function getRemainingCooldown(ItemUserRecipe $userRecipe): int
- {
- if (!$this->isInCooldown($userRecipe)) {
- return 0;
- }
- $cooldownSeconds = $userRecipe->recipe->cooldown_seconds ?? 0;
- $cooldownEnd = $userRecipe->last_craft_time->addSeconds($cooldownSeconds);
- return max(0, $cooldownEnd->diffInSeconds(now()));
- }
- }
|