condition_type) { case CONDITION_TYPE::LEVEL_REQUIREMENT: return $this->checkLevelRequirement($userId, $condition->condition_params); case CONDITION_TYPE::ITEM_REQUIREMENT: return $this->checkItemRequirement($userId, $condition->condition_params); case CONDITION_TYPE::TIME_REQUIREMENT: return $this->checkTimeRequirement($condition->condition_params); case CONDITION_TYPE::TASK_REQUIREMENT: return $this->checkTaskRequirement($userId, $condition->condition_params); case CONDITION_TYPE::FARM_REQUIREMENT: return $this->checkFarmRequirement($userId, $condition->condition_params); case CONDITION_TYPE::PET_REQUIREMENT: return $this->checkPetRequirement($userId, $condition->condition_params); default: Log::warning('未知的条件类型', [ 'condition_type' => $condition->condition_type, 'condition_id' => $condition->id ]); return false; } } catch (\Exception $e) { Log::error('检查条件时发生错误', [ 'condition_id' => $condition->id, 'error' => $e->getMessage() ]); return false; } } /** * 检查等级要求 * * @param int $userId 用户ID * @param array $params 条件参数 * @return bool */ private function checkLevelRequirement(int $userId, array $params): bool { // 获取用户等级 $userLevel = $this->getUserLevel($userId); // 检查最小等级要求 if (isset($params['min_level']) && $userLevel < $params['min_level']) { return false; } // 检查最大等级要求 if (isset($params['max_level']) && $userLevel > $params['max_level']) { return false; } return true; } /** * 检查物品要求 * * @param int $userId 用户ID * @param array $params 条件参数 * @return bool */ private function checkItemRequirement(int $userId, array $params): bool { // 检查是否需要特定物品 if (isset($params['items']) && is_array($params['items'])) { foreach ($params['items'] as $item) { $itemId = $item['item_id'] ?? 0; $quantity = $item['quantity'] ?? 1; if ($itemId > 0) { // 获取用户物品数量 $userItemCount = $this->getUserItemCount($userId, $itemId); if ($userItemCount < $quantity) { return false; } } } } return true; } /** * 检查时间要求 * * @param array $params 条件参数 * @return bool */ private function checkTimeRequirement(array $params): bool { $now = now(); // 检查开始时间 if (isset($params['start_time']) && $now < \Carbon\Carbon::parse($params['start_time'])) { return false; } // 检查结束时间 if (isset($params['end_time']) && $now > \Carbon\Carbon::parse($params['end_time'])) { return false; } // 检查星期几 if (isset($params['days_of_week']) && is_array($params['days_of_week'])) { $currentDayOfWeek = $now->dayOfWeek; if (!in_array($currentDayOfWeek, $params['days_of_week'])) { return false; } } // 检查每日时间段 if (isset($params['daily_start_time']) && isset($params['daily_end_time'])) { $dailyStartTime = \Carbon\Carbon::createFromFormat('H:i', $params['daily_start_time']); $dailyEndTime = \Carbon\Carbon::createFromFormat('H:i', $params['daily_end_time']); $currentTime = \Carbon\Carbon::createFromFormat('H:i', $now->format('H:i')); if ($currentTime < $dailyStartTime || $currentTime > $dailyEndTime) { return false; } } return true; } /** * 检查任务要求 * * @param int $userId 用户ID * @param array $params 条件参数 * @return bool */ private function checkTaskRequirement(int $userId, array $params): bool { // 检查是否需要完成特定任务 if (isset($params['task_ids']) && is_array($params['task_ids'])) { foreach ($params['task_ids'] as $taskId) { // 检查用户是否完成了任务 if (!$this->isTaskCompleted($userId, $taskId)) { return false; } } } return true; } /** * 检查农场要求 * * @param int $userId 用户ID * @param array $params 条件参数 * @return bool */ private function checkFarmRequirement(int $userId, array $params): bool { // 检查农场等级要求 if (isset($params['farm_level']) && $this->getFarmLevel($userId) < $params['farm_level']) { return false; } // 检查是否拥有特定作物 if (isset($params['crops']) && is_array($params['crops'])) { foreach ($params['crops'] as $crop) { $cropId = $crop['crop_id'] ?? 0; $quantity = $crop['quantity'] ?? 1; if ($cropId > 0) { // 获取用户作物数量 $userCropCount = $this->getUserCropCount($userId, $cropId); if ($userCropCount < $quantity) { return false; } } } } return true; } /** * 检查宠物要求 * * @param int $userId 用户ID * @param array $params 条件参数 * @return bool */ private function checkPetRequirement(int $userId, array $params): bool { // 检查是否拥有特定宠物 if (isset($params['pet_type_id']) && !$this->hasPet($userId, $params['pet_type_id'])) { return false; } // 检查宠物等级要求 if (isset($params['pet_level']) && isset($params['pet_type_id'])) { $petLevel = $this->getPetLevel($userId, $params['pet_type_id']); if ($petLevel < $params['pet_level']) { return false; } } return true; } /** * 获取用户等级 * * @param int $userId 用户ID * @return int */ private function getUserLevel(int $userId): int { // 这里应该调用用户等级服务获取用户等级 // 由于没有实际的用户等级服务,这里返回一个模拟值 return 1; } /** * 获取用户物品数量 * * @param int $userId 用户ID * @param int $itemId 物品ID * @return int */ private function getUserItemCount(int $userId, int $itemId): int { // 这里应该调用物品服务获取用户物品数量 // 由于没有实际的物品服务,这里返回一个模拟值 return 0; } /** * 检查用户是否完成了任务 * * @param int $userId 用户ID * @param int $taskId 任务ID * @return bool */ private function isTaskCompleted(int $userId, int $taskId): bool { // 这里应该调用任务服务检查用户是否完成了任务 // 由于没有实际的任务服务,这里返回一个模拟值 return false; } /** * 获取农场等级 * * @param int $userId 用户ID * @return int */ private function getFarmLevel(int $userId): int { // 这里应该调用农场服务获取农场等级 // 由于没有实际的农场服务,这里返回一个模拟值 return 1; } /** * 获取用户作物数量 * * @param int $userId 用户ID * @param int $cropId 作物ID * @return int */ private function getUserCropCount(int $userId, int $cropId): int { // 这里应该调用农场服务获取用户作物数量 // 由于没有实际的农场服务,这里返回一个模拟值 return 0; } /** * 检查用户是否拥有特定宠物 * * @param int $userId 用户ID * @param int $petTypeId 宠物类型ID * @return bool */ private function hasPet(int $userId, int $petTypeId): bool { // 这里应该调用宠物服务检查用户是否拥有特定宠物 // 由于没有实际的宠物服务,这里返回一个模拟值 return false; } /** * 获取宠物等级 * * @param int $userId 用户ID * @param int $petTypeId 宠物类型ID * @return int */ private function getPetLevel(int $userId, int $petTypeId): int { // 这里应该调用宠物服务获取宠物等级 // 由于没有实际的宠物服务,这里返回一个模拟值 return 1; } }