Recipe.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace App\Module\GameItems\Logics;
  3. use App\Module\GameItems\Models\ItemRecipe;
  4. /**
  5. * 配方逻辑类
  6. */
  7. class Recipe
  8. {
  9. /**
  10. * 检查用户是否可以合成该配方
  11. *
  12. * @param ItemRecipe $recipe 配方模型
  13. * @param int $userId 用户ID
  14. * @return array 检查结果,包含是否可合成和原因
  15. */
  16. public function canCraftByUser(ItemRecipe $recipe, int $userId): array
  17. {
  18. // 检查配方是否已解锁
  19. $userRecipe = $recipe->userRecipes()->where('user_id', $userId)->first();
  20. if (!$userRecipe || !$userRecipe->is_unlocked) {
  21. return [
  22. 'can_craft' => false,
  23. 'reason' => '配方未解锁',
  24. ];
  25. }
  26. // 检查冷却时间
  27. if ($recipe->cooldown_seconds > 0) {
  28. $lastCraft = $recipe->craftLogs()
  29. ->where('user_id', $userId)
  30. ->where('created_at', '>', now()->subSeconds($recipe->cooldown_seconds))
  31. ->first();
  32. if ($lastCraft) {
  33. $remainingSeconds = $recipe->cooldown_seconds - now()->diffInSeconds($lastCraft->created_at);
  34. return [
  35. 'can_craft' => false,
  36. 'reason' => '冷却中',
  37. 'remaining_seconds' => $remainingSeconds,
  38. ];
  39. }
  40. }
  41. return [
  42. 'can_craft' => true,
  43. 'reason' => '',
  44. ];
  45. }
  46. }