[ 'disaster_type' => DISASTER_TYPE::WEED, 'item_id' => 22, // 除草剂 'skill_name' => \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING, 'method' => 'processAutoWeeding', 'display_name' => '除草' ], 'watering' => [ 'disaster_type' => DISASTER_TYPE::DROUGHT, 'item_id' => 24, // 洒水壶 'skill_name' => \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WATERING, 'method' => 'processAutoWatering', 'display_name' => '浇水' ], 'pest_control' => [ 'disaster_type' => DISASTER_TYPE::PEST, 'item_id' => 23, // 杀虫剂 'skill_name' => \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_PEST_CONTROL, 'method' => 'processAutoPestControl', 'display_name' => '杀虫' ] ]; /** * 执行命令 */ public function handle(): int { $userId = (int) $this->argument('user_id'); $skillType = $this->option('skill'); $this->info("开始测试用户 {$userId} 的自动技能优化功能"); try { if ($skillType === 'all') { $this->testAllSkills($userId); } else { $this->testSingleSkill($userId, $skillType); } $this->info("\n✅ 所有测试完成!"); return 0; } catch (\Exception $e) { $this->error('测试失败: ' . $e->getMessage()); $this->error('堆栈跟踪: ' . $e->getTraceAsString()); return 1; } } /** * 测试所有技能 */ private function testAllSkills(int $userId): void { foreach ($this->skillConfigs as $skillType => $config) { $this->info("\n" . str_repeat('=', 60)); $this->info("🧪 测试 {$config['display_name']} 技能"); $this->info(str_repeat('=', 60)); $this->testSingleSkill($userId, $skillType); } } /** * 测试单个技能 */ private function testSingleSkill(int $userId, string $skillType): void { if (!isset($this->skillConfigs[$skillType])) { $this->error("不支持的技能类型: {$skillType}"); return; } $config = $this->skillConfigs[$skillType]; // 1. 创建测试数据 $this->createTestData($userId, $skillType, $config); // 2. 执行自动技能 $this->executeAutoSkill($userId, $skillType, $config); // 3. 验证结果 $this->verifyResults($userId, $skillType, $config); } /** * 创建测试数据 */ private function createTestData(int $userId, string $skillType, array $config): void { $this->info("📝 创建{$config['display_name']}测试数据..."); DB::beginTransaction(); try { // 查找宠物 $pet = PetUser::where('user_id', $userId)->first(); if (!$pet) { $this->warn("用户 {$userId} 没有宠物,跳过测试"); DB::rollback(); return; } // 查找有作物的土地 $land = FarmLand::where('user_id', $userId) ->where('has_crop', true) ->first(); if (!$land) { $this->warn("用户 {$userId} 没有种植作物的土地,跳过测试"); DB::rollback(); return; } // 获取作物并添加多个指定类型的灾害 $crop = FarmCrop::where('land_id', $land->id)->first(); if (!$crop) { $this->warn("土地 {$land->id} 没有作物,跳过测试"); DB::rollback(); return; } // 清除现有的同类型灾害 $disasters = $crop->disasters ?? []; $disasters = array_filter($disasters, function($disaster) use ($config) { return ($disaster['type'] ?? 0) != $config['disaster_type']->value; }); // 添加3个新的指定类型灾害 for ($i = 1; $i <= 3; $i++) { $disasters[] = [ 'id' => time() + $i, 'type' => $config['disaster_type']->value, 'status' => 'active', 'created_at' => now()->subMinutes(30 - $i * 5)->toDateTimeString() ]; } $crop->disasters = $disasters; $crop->save(); // 确保用户有足够的道具 $userItem = ItemUser::where('user_id', $userId) ->where('item_id', $config['item_id']) ->first(); if (!$userItem) { ItemUser::create([ 'user_id' => $userId, 'item_id' => $config['item_id'], 'quantity' => 10 ]); } else { $userItem->quantity = 10; $userItem->save(); } // 创建或更新激活的技能 $activeSkill = PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $config['skill_name']->value) ->first(); if (!$activeSkill) { PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => 1, 'skill_name' => $config['skill_name']->value, 'start_time' => now()->subMinutes(10), 'end_time' => now()->addHours(3), 'status' => 'active', 'last_check_time' => now()->subMinutes(5) ]); } DB::commit(); $this->info("✅ 测试数据创建完成:"); $this->info(" - 宠物ID: {$pet->id}"); $this->info(" - 土地ID: {$land->id}"); $this->info(" - 作物ID: {$crop->id}"); $this->info(" - {$config['display_name']}灾害数量: 3个"); $this->info(" - 道具数量: 10个"); } catch (\Exception $e) { DB::rollback(); throw $e; } } /** * 执行自动技能 */ private function executeAutoSkill(int $userId, string $skillType, array $config): void { $this->info("🚀 执行自动{$config['display_name']}..."); $pet = PetUser::where('user_id', $userId)->first(); $activeSkill = PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', $config['skill_name']->value) ->first(); if (!$activeSkill) { throw new \Exception("没有找到激活的自动{$config['display_name']}技能"); } DB::beginTransaction(); $autoSkillLogic = new PetAutoSkillLogic(); $method = $config['method']; $autoSkillLogic->$method($activeSkill); DB::commit(); $this->info("✅ 自动{$config['display_name']}执行完成"); } /** * 验证结果 */ private function verifyResults(int $userId, string $skillType, array $config): void { $this->info("🔍 验证{$config['display_name']}结果..."); $land = FarmLand::where('user_id', $userId) ->where('has_crop', true) ->first(); if (!$land) { $this->warn('没有找到有作物的土地'); return; } $crop = FarmCrop::where('land_id', $land->id)->first(); if (!$crop) { $this->warn('没有找到作物'); return; } // 统计剩余的活跃灾害 $activeDisasters = array_filter($crop->disasters ?? [], function($disaster) use ($config) { return ($disaster['type'] ?? 0) == $config['disaster_type']->value && ($disaster['status'] ?? '') === 'active'; }); $clearedDisasters = array_filter($crop->disasters ?? [], function($disaster) use ($config) { return ($disaster['type'] ?? 0) == $config['disaster_type']->value && ($disaster['status'] ?? '') === 'cleared'; }); // 检查道具消耗 $userItem = ItemUser::where('user_id', $userId) ->where('item_id', $config['item_id']) ->first(); $this->info("📊 验证结果:"); $this->info(" - 剩余活跃{$config['display_name']}灾害: " . count($activeDisasters) . "个"); $this->info(" - 已清除{$config['display_name']}灾害: " . count($clearedDisasters) . "个"); $this->info(" - 剩余道具数量: " . ($userItem ? $userItem->quantity : 0)); if (count($activeDisasters) == 0) { $this->info("✅ 优化成功!所有{$config['display_name']}灾害都被清除了"); } else { $this->warn("⚠️ 还有{$config['display_name']}灾害未被清除,可能是道具不足"); } } }