| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- <?php
- namespace App\Module\GameItems\Logics;
- use App\Module\GameItems\Models\ItemRecipe;
- /**
- * 配方逻辑类
- */
- class Recipe
- {
- /**
- * 检查用户是否可以合成该配方
- *
- * @param ItemRecipe $recipe 配方模型
- * @param int $userId 用户ID
- * @return array 检查结果,包含是否可合成和原因
- */
- public function canCraftByUser(ItemRecipe $recipe, int $userId): array
- {
- // 检查配方是否已解锁
- $userRecipe = $recipe->userRecipes()->where('user_id', $userId)->first();
- if (!$userRecipe || !$userRecipe->is_unlocked) {
- return [
- 'can_craft' => false,
- 'reason' => '配方未解锁',
- ];
- }
- // 检查冷却时间
- if ($recipe->cooldown_seconds > 0) {
- $lastCraft = $recipe->craftLogs()
- ->where('user_id', $userId)
- ->where('created_at', '>', now()->subSeconds($recipe->cooldown_seconds))
- ->first();
- if ($lastCraft) {
- $remainingSeconds = $recipe->cooldown_seconds - now()->diffInSeconds($lastCraft->created_at);
- return [
- 'can_craft' => false,
- 'reason' => '冷却中',
- 'remaining_seconds' => $remainingSeconds,
- ];
- }
- }
- return [
- 'can_craft' => true,
- 'reason' => '',
- ];
- }
- }
|