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())); } }