20) { throw new Exception("宠物名称不能为空且不能超过20个字符"); } // 检查用户宠物数量限制 - 一人一宠限制 $petCount = PetUser::where('user_id', $userId)->count(); $maxPets = 1; // 强制限制为1只宠物 if ($petCount >= $maxPets) { throw new Exception("每人只能拥有一只宠物"); } // 获取宠物配置 $petConfig = PetConfig::where('pet_type', $options['pet_type'] ?? 'default')->first(); $nextLevelConfig = PetLevelConfig::where('level', 2)->first(); if (!$petConfig) { $petConfig = PetConfig::first(); // 使用默认配置 if (!$petConfig) { throw new Exception("宠物配置不存在"); } } // 创建宠物 $pet = new PetUser(); $pet->user_id = $userId; $pet->name = $name; $pet->level = 1; $pet->experience = 0; // 体力值从等级配置获取 $levelConfig = PetLevelConfig::where('level', 1)->first(); $pet->stamina = $levelConfig ? ($levelConfig->numeric_attributes->stamina_max ?? 100) : 100; $pet->status = PetStatus::NORMAL; $pet->save(); // 触发宠物创建事件 event(new PetCreatedEvent( $userId, $pet->id )); Log::info('宠物创建成功', [ 'user_id' => $userId, 'pet_id' => $pet->id, 'name' => $name ]); return [ 'pet_id' => $pet->id ]; } /** * 宠物升级 * * @param int $petId 宠物ID * @return array 升级结果 */ public function levelUpPet(PetUser $pet): Res { $change = false; // 记录旧等级 $oldLevel = $pet->level; $nextLevelConfigOld = null; foreach (range(1, 20) as $level) { // 获取当前等级配置 $currentLevelConfig = PetLevelConfig::where('level', $pet->level)->first(); if (!$currentLevelConfig) { break; } // 获取下一级配置 $nextLevelConfig = PetLevelConfig::where('level', $pet->level + 1)->first(); if (!$nextLevelConfig) { break; } if($pet->experience < $nextLevelConfig->exp_required){ break; } // 升级宠物 $pet->level += 1; $pet->experience = $pet->experience - $nextLevelConfig->exp_required; $change = true; $nextLevelConfigOld = $nextLevelConfig; } if ($change) { $pet->save(); // 获取新解锁的技能 $unlockedSkills = []; if($nextLevelConfig){ if ($nextLevelConfig->unlock_skills) { $unlockedSkills = json_decode($nextLevelConfig->unlock_skills, true); } } if($nextLevelConfigOld){ if ($nextLevelConfigOld->unlock_skills) { $unlockedSkills = json_decode($nextLevelConfigOld->unlock_skills, true); } } // 触发宠物升级事件 event(new PetLevelUpEvent( $pet->user_id, $pet->id, $oldLevel, $pet->level, $unlockedSkills )); // 触发宠物更新事件,表示宠物数据发生重大变更 event(new PetUpdateEvent( $pet->user_id, $pet->id )); Log::info('宠物升级成功', [ 'pet_id' => $pet->id, 'old_level' => $oldLevel, 'new_level' => $pet->level, 'unlocked_skills' => $unlockedSkills ]); } return Res::success(''); } /** * 宠物喂养 * * @param int $petId 宠物ID * @param int $itemId 物品ID(狗粮) * @param int $amount 数量 * @return array 喂养结果 * @throws Exception */ public function feedPet(int $petId, int $itemId, int $amount): array { // 获取宠物信息 /** * @var PetUser $pet */ $pet = PetUser::findOrFail($petId); // 获取物品信息 /** * @var ItemDto $item */ $item = ItemService::getItemInfo($itemId); if (!$item) { throw new LogicException("物品不存在"); } // 消耗物品 $consumeResult = ItemService::consumeItem( $pet->user_id, $itemId, null, $amount, [ 'source_type' => 'pet_feed', 'source_id' => $petId, 'details' => [ 'pet_id' => $petId ] ] ); if (!$consumeResult['success']) { throw new LogicException("物品消耗失败: " . ($consumeResult['message'] ?? '未知错误')); } // 计算获得的经验值和体力 $expGained = ($item->numericAttributes['pet_exp'] ?? 0) * $amount; $staminaGained = ($item->numericAttributes['pet_power'] ?? 0) * $amount; if (!$expGained && !$staminaGained) { throw new LogicException('错误的参数'); } // 增加经验值 $pet->experience += $expGained; // 更新体力值,确保不超过上限 $pet->stamina = min($pet->max_stamina, $pet->stamina + $staminaGained); $pet->save(); // 喂养完成后恢复正常状态 // $pet->refresh(); // $pet->status = PetStatus::NORMAL; $pet->save(); // 检查升级 $this->check_uplevel($pet); // 如果有经验增加,触发宠物经验增加事件 if ($expGained > 0) { $petExpEvent = new PetExpGainedEvent( $pet->user_id, $pet->id, $expGained, 'pet_feed', $itemId ); // 异步事件处理(放入队列) event($petExpEvent); // 同步任务进度更新(立即执行,确保响应中包含最新进度) \App\Module\Task\Listeners\PetExpGainedListener::handleSync($petExpEvent); } // 创建旧状态数据 $oldStatusData = new \App\Module\Pet\Dtos\DataPetSimpleDto(); $oldStatusData->id = $pet->id; $oldStatusData->status = PetStatus::FEEDING->value; // 创建新状态数据 $newStatusData = new \App\Module\Pet\Dtos\DataPetSimpleDto(); $newStatusData->id = $pet->id; $newStatusData->status = PetStatus::NORMAL->value; // 创建完整的宠物数据 $petData = new \App\Module\Pet\Dtos\DataPetSimpleDto(); $petData->id = $pet->id; $petData->name = $pet->name; $petData->level = $pet->level; $petData->power = $pet->stamina; $petData->exp = $pet->experience; // 触发宠物状态变更事件 event(new PetStatusChangedEvent( $pet->user_id, $pet->id, $oldStatusData, $newStatusData, 'pet_feed_complete', $petData )); Log::info('宠物喂养成功', [ 'pet_id' => $petId, 'item_id' => $itemId, 'amount' => $amount, 'exp_gained' => $expGained, 'stamina_gained' => $staminaGained, ]); return [ 'exp_gained' => $expGained, 'stamina_gained' => $staminaGained, ]; } /** * 增加宠物经验值 * * @param int $petId 宠物ID * @param int $expAmount 经验值数量 * @return bool 是否触发升级 */ protected function check_uplevel(PetUser $pet): Res { if ($pet->max_experience === 0) { return Res::error('最高等级,无法升级'); } if ($pet->experience >= $pet->max_experience) { return $this->levelUpPet($pet); } return Res::error('经验不足'); } /** * 增加宠物体力 * * @param int $petId 宠物ID * @param int $staminaAmount 体力数量 * @return int 实际增加的体力值 */ protected function addStamina(PetUser $pet, int $staminaAmount): int { // 使用模型的访问器获取最大体力值 $maxStamina = $pet->max_stamina; // 计算实际增加的体力值 $oldStamina = $pet->stamina; $newStamina = min($maxStamina, $oldStamina + $staminaAmount); $actualGained = $newStamina - $oldStamina; return $actualGained; } /** * 增加宠物经验值(用于奖励系统) * * @param int $petId 宠物ID * @param int $expAmount 经验值数量 * @param string $sourceType 经验来源类型 * @param int|null $sourceId 经验来源ID * @return bool 是否触发升级 * @throws Exception */ public function addExperienceReward(int $petId, int $expAmount, string $sourceType = 'reward', ?int $sourceId = null): bool { // 获取宠物信息 $pet = PetUser::findOrFail($petId); // 验证经验值 if ($expAmount <= 0) { throw new Exception('经验值必须大于0'); } // 增加经验值 $pet->experience += $expAmount; $pet->save(); // 检查升级 $levelUpResult = $this->check_uplevel($pet); // 触发宠物经验增加事件 event(new PetExpGainedEvent( $pet->user_id, $pet->id, $expAmount, $sourceType, $sourceId )); Log::info('宠物经验奖励增加', [ 'pet_id' => $petId, 'user_id' => $pet->user_id, 'exp_gained' => $expAmount, 'new_experience' => $pet->experience, 'source_type' => $sourceType, 'source_id' => $sourceId, 'level_up' => $levelUpResult->success ]); return $levelUpResult->success; } /** * 使用宠物技能 * * @param int $petId 宠物ID * @param int $skillId 技能ID * @param array $params 技能参数 * @return array 技能使用结果 * @throws Exception */ public function useSkill(int $petId, int $skillId, array $params = []): array { // 获取宠物信息 $pet = PetUser::findOrFail($petId); // 获取技能信息 $skill = PetSkill::findOrFail($skillId); // 检查技能冷却时间 $lastUsed = PetSkillLog::where('pet_id', $petId) ->where('skill_id', $skillId) ->orderBy('used_at', 'desc') ->first(); if ($lastUsed) { $cooldownSeconds = $skill->cool_down; $now = now(); $lastUsedTime = $lastUsed->used_at; // 确保时间计算的正确性 if ($lastUsedTime instanceof \Carbon\Carbon) { $secondsSinceLastUse = $now->diffInSeconds($lastUsedTime, false); } else { // 如果不是Carbon对象,尝试解析 $lastUsedTime = \Carbon\Carbon::parse($lastUsedTime); $secondsSinceLastUse = $now->diffInSeconds($lastUsedTime, false); } // 如果secondsSinceLastUse为负数,说明lastUsed时间在未来,这是异常情况 if ($secondsSinceLastUse < 0) { Log::warning('检测到异常的技能使用时间', [ 'pet_id' => $petId, 'skill_id' => $skillId, 'last_used_at' => $lastUsedTime->toDateTimeString(), 'current_time' => $now->toDateTimeString(), 'seconds_diff' => $secondsSinceLastUse ]); // 异常情况下,重置为0,允许技能使用 $secondsSinceLastUse = 0; } if ($secondsSinceLastUse < $cooldownSeconds) { $remainingCooldown = $cooldownSeconds - $secondsSinceLastUse; // 记录详细的冷却信息用于调试 Log::info('技能冷却检查', [ 'pet_id' => $petId, 'skill_id' => $skillId, 'cooldown_seconds' => $cooldownSeconds, 'seconds_since_last_use' => $secondsSinceLastUse, 'remaining_cooldown' => $remainingCooldown, 'last_used_at' => $lastUsedTime->toDateTimeString(), 'current_time' => $now->toDateTimeString() ]); throw new Exception("技能冷却中,还需等待 {$remainingCooldown} 秒"); } } // 检查体力是否足够 if ($pet->stamina < $skill->stamina_cost) { throw new Exception("体力不足,无法使用技能"); } // 先执行技能效果,确保技能能够成功激活 $effectResult = $this->executeSkillEffect($pet, $skill, $params); // 检查技能效果是否成功 if (isset($effectResult['success']) && $effectResult['success'] === false) { // 技能效果执行失败,记录失败日志但不消耗体力,不进入冷却 Log::warning('宠物技能使用失败', [ 'pet_id' => $petId, 'skill_id' => $skillId, 'params' => $params, 'effect_result' => $effectResult, 'reason' => $effectResult['message'] ?? '技能使用失败' ]); throw new Exception($effectResult['message'] ?? '技能使用失败'); } // 技能效果成功,消耗体力 $pet->stamina -= $skill->stamina_cost; $pet->save(); // 记录技能使用日志(只有成功时才记录) $skillLog = PetSkillLog::create([ 'pet_id' => $petId, 'skill_id' => $skillId, 'used_at' => now(), 'effect_result' => json_encode($effectResult) ]); // 触发宠物技能使用事件 event(new PetSkillUsedEvent( $pet->user_id, $pet->id, $skillId, $params )); Log::info('宠物技能使用成功', [ 'pet_id' => $petId, 'skill_id' => $skillId, 'params' => $params, 'effect_result' => $effectResult ]); return [ 'effect_result' => $effectResult ]; } /** * 执行技能效果 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 技能效果结果 */ protected function executeSkillEffect(PetUser $pet, PetSkill $skill, array $params): array { // 根据技能名称执行不同的效果 switch ($skill->skill_name) { case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_HARVESTING->value: return $this->activateAutoHarvestSkill($pet, $skill, $params); case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_PLANTING->value: return $this->activateAutoPlantSkill($pet, $skill, $params); case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->value: return $this->activateAutoWeedingSkill($pet, $skill, $params); case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WATERING->value: return $this->activateAutoWateringSkill($pet, $skill, $params); case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_PEST_CONTROL->value: return $this->activateAutoPestControlSkill($pet, $skill, $params); case \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_FERTILIZING->value: return $this->activateAutoFertilizingSkill($pet, $skill, $params); default: return [ 'success' => false, 'message' => '未知技能效果' ]; } } /** * 激活自动收菜技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateAutoHarvestSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间, $duration = $params['duration'] ?? $skill->duration_time; // 2小时 $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'auto_harvest' => true, 'check_interval' => self::SKILL_CHECK_INTERVAL, // 每58秒检查一次 'last_check_time' => now()->toDateTimeString() ]) ]); Log::info('自动收菜技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'auto_harvest', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "自动收菜技能已激活,持续时间:{$duration}秒" ]; } /** * 激活自动播种技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateAutoPlantSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间(默认4小时) $duration = $params['duration'] ?? $skill->duration_time; // 2小时 $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'auto_plant' => true, 'check_interval' => self::SKILL_CHECK_INTERVAL, // 每58秒检查一次 'last_check_time' => now()->toDateTimeString(), 'preferred_seeds' => $params['preferred_seeds'] ?? [] // 优先使用的种子ID列表 ]) ]); Log::info('自动播种技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'auto_plant', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "自动播种技能已激活,持续时间:{$duration}秒" ]; } /** * 激活灾害防护技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateDisasterProtectionSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间(默认6小时) $duration = $params['duration'] ?? 21600; // 6小时 $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'disaster_protection' => true, 'protected_types' => $params['disaster_types'] ?? [ 'all' ], // 防护的灾害类型 'check_interval' => self::SKILL_CHECK_INTERVAL, // 每58秒检查一次 'last_check_time' => now()->toDateTimeString() ]) ]); Log::info('灾害防护技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'disaster_protection', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "灾害防护技能已激活,持续时间:{$duration}秒" ]; } /** * 变更宠物状态 * * @param int $petId 宠物ID * @param PetStatus $status 新状态 * @param string $reason 变更原因 * @return bool 是否变更成功 */ public function changeStatus(int $petId, PetStatus $status, string $reason = ''): bool { // 获取宠物信息 $pet = PetUser::findOrFail($petId); // 记录旧状态 $oldStatus = $pet->status; // 如果状态相同,则不需要变更 if ($oldStatus === $status) { return true; } // 更新状态 $pet->status = $status; $pet->save(); // 创建旧状态数据 $oldStatusData = new \App\Module\Pet\Dtos\DataPetSimpleDto(); $oldStatusData->id = $pet->id; $oldStatusData->status = $oldStatus->value; // 创建新状态数据 $newStatusData = new \App\Module\Pet\Dtos\DataPetSimpleDto(); $newStatusData->id = $pet->id; $newStatusData->status = $status->value; // 创建完整的宠物数据 $petData = new \App\Module\Pet\Dtos\DataPetSimpleDto(); $petData->id = $pet->id; $petData->name = $pet->name; $petData->typeId = $pet->type_id; $petData->level = $pet->level; $petData->status = $status->value; // 触发宠物状态变更事件 event(new PetStatusChangedEvent( $pet->user_id, $pet->id, $oldStatusData, $newStatusData, $reason, $petData )); Log::info('宠物状态变更成功', [ 'pet_id' => $petId, 'old_status' => $oldStatus->value, 'new_status' => $status->value, 'reason' => $reason ]); return true; } /** * 恢复宠物体力 * * @param int $petId 宠物ID * @param int $minutes 经过的分钟数 * @return int 恢复的体力值 */ public function recoverStamina(int $petId, int $minutes): int { // 获取宠物信息 $pet = PetUser::findOrFail($petId); // 使用模型的访问器获取最大体力值 $maxStamina = $pet->max_stamina; // 获取宠物等级配置中的恢复速度 $levelConfig = PetLevelConfig::where('level', $pet->level)->first(); $recoveryRate = $levelConfig ? ($levelConfig->numeric_attributes->stamina_recovery ?? 5) : 5; // 计算恢复的体力值 $recoveryAmount = $recoveryRate * $minutes; $oldStamina = $pet->stamina; $newStamina = min($maxStamina, $oldStamina + $recoveryAmount); $actualRecovered = $newStamina - $oldStamina; // 更新体力值 if ($actualRecovered > 0) { $pet->stamina = $newStamina; $pet->save(); Log::info('宠物体力恢复成功', [ 'pet_id' => $petId, 'minutes' => $minutes, 'recovery_rate' => $recoveryRate, 'old_stamina' => $oldStamina, 'new_stamina' => $newStamina, 'actual_recovered' => $actualRecovered ]); } return $actualRecovered; } /** * 计算宠物战力 * * @param int $petId 宠物ID * @return int 战力值 */ public function calculatePower(int $petId): int { // 获取宠物信息 $pet = PetUser::findOrFail($petId); // 获取宠物等级配置 $levelConfig = PetLevelConfig::where('level', $pet->level)->first(); // 基础战力 $basePower = $levelConfig ? ($levelConfig->numeric_attributes->base_power ?? 100) : 100; // 计算最终战力 $power = $basePower; return (int)$power; } /** * 激活自动除草技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateAutoWeedingSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间(默认3小时) $duration = $params['duration'] ?? $skill->duration_time; // 2小时 $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'auto_weeding' => true, 'disaster_type' => \App\Module\Farm\Enums\DISASTER_TYPE::WEED->value, // 专门处理杂草灾害 'check_interval' => self::SKILL_CHECK_INTERVAL, 'last_check_time' => now()->toDateTimeString(), 'auto_use_items' => $params['auto_use_items'] ?? true // 是否自动使用除草道具 ]) ]); Log::info('自动除草技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'auto_weeding', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "自动除草技能已激活,持续时间:{$duration}秒" ]; } /** * 激活自动浇水技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateAutoWateringSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间(默认4小时) $duration = $params['duration'] ?? $skill->duration_time; $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'auto_watering' => true, 'disaster_type' => \App\Module\Farm\Enums\DISASTER_TYPE::DROUGHT->value, // 专门处理干旱灾害 'check_interval' => self::SKILL_CHECK_INTERVAL, // 每58秒检查一次 'last_check_time' => now()->toDateTimeString(), 'auto_use_items' => $params['auto_use_items'] ?? true // 是否自动使用浇水道具 ]) ]); Log::info('自动浇水技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'auto_watering', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "自动浇水技能已激活,持续时间:{$duration}秒" ]; } /** * 激活自动杀虫技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateAutoPestControlSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间(默认3小时) $duration = $params['duration'] ?? $skill->duration_time; // 2小时 $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'auto_pest_control' => true, 'disaster_type' => \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value, // 专门处理虫害灾害 'check_interval' => self::SKILL_CHECK_INTERVAL, // 每58秒检查一次 'last_check_time' => now()->toDateTimeString(), 'auto_use_items' => $params['auto_use_items'] ?? true // 是否自动使用杀虫道具 ]) ]); Log::info('自动杀虫技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'auto_pest_control', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "自动杀虫技能已激活,持续时间:{$duration}秒" ]; } /** * 激活自动施肥技能 * * @param PetUser $pet 宠物对象 * @param PetSkill $skill 技能对象 * @param array $params 技能参数 * @return array 激活结果 */ protected function activateAutoFertilizingSkill(PetUser $pet, PetSkill $skill, array $params): array { // 检查是否已有相同技能在激活中 $existingActiveSkill = \App\Module\Pet\Models\PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $skill->skill_name) ->where('status', 'active') ->where('end_time', '>', now()) ->first(); if ($existingActiveSkill) { return [ 'success' => false, 'message' => '该技能已经在激活中,无法重复激活' ]; } // 技能持续时间(默认2小时) $duration = $params['duration'] ?? $skill->duration_time; $endTime = now()->addSeconds($duration); // 创建技能激活记录 $activeSkill = \App\Module\Pet\Models\PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'skill_name' => $skill->skill_name, 'start_time' => now(), 'end_time' => $endTime, 'status' => 'active', 'config' => json_encode([ 'auto_fertilizing' => true, 'check_interval' => self::SKILL_CHECK_INTERVAL, // 每58秒检查一次 'last_check_time' => now()->toDateTimeString(), 'auto_use_items' => $params['auto_use_items'] ?? true, // 是否自动使用肥料道具 'fertilizer_types' => $params['fertilizer_types'] ?? ['fertilizer'], // 允许使用的肥料类型 ]) ]); Log::info('自动施肥技能激活成功', [ 'pet_id' => $pet->id, 'skill_id' => $skill->id, 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'active_skill_id' => $activeSkill->id ]); return [ 'success' => true, 'skill_type' => 'auto_fertilizing', 'duration' => $duration, 'end_time' => $endTime->toDateTimeString(), 'message' => "自动施肥技能已激活,持续时间:{$duration}秒" ]; } }