argument('user_id'); $skillType = $this->option('skill'); $this->info("开始测试用户 {$userId} 的自动技能优化功能 (技能类型: {$skillType})"); try { if ($skillType === 'all') { $this->testAllSkills($userId); } else { $this->testSingleSkill($userId, $skillType); } $this->info('测试完成!'); return 0; } catch (\Exception $e) { $this->error('测试失败: ' . $e->getMessage()); $this->error('堆栈跟踪: ' . $e->getTraceAsString()); return 1; } } /** * 测试所有技能 */ private function testAllSkills(int $userId): void { $skills = ['weeding', 'watering', 'pest_control']; foreach ($skills as $skill) { $this->info("\n" . str_repeat('=', 50)); $this->info("测试 {$skill} 技能"); $this->info(str_repeat('=', 50)); $this->testSingleSkill($userId, $skill); } } /** * 测试单个技能 */ private function testSingleSkill(int $userId, string $skillType): void { // 1. 创建测试数据 $this->createTestData($userId, $skillType); // 2. 执行自动技能 $this->executeAutoSkill($userId, $skillType); // 3. 验证结果 $this->verifyResults($userId, $skillType); } /** * 创建测试数据 */ private function createTestData(int $userId, string $skillType = 'weeding'): void { $this->info('创建测试数据...'); 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) { return ($disaster['type'] ?? 0) != DISASTER_TYPE::WEED->value; }); // 添加3个新的杂草灾害 for ($i = 1; $i <= 3; $i++) { $disasters[] = [ 'id' => time() + $i, 'type' => DISASTER_TYPE::WEED->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', 22) // 除草剂ID ->first(); if (!$userItem) { ItemUser::create([ 'user_id' => $userId, 'item_id' => 22, 'quantity' => 10 ]); } else { $userItem->quantity = 2; // 设置为只有2个除草剂,测试道具不足情况 $userItem->save(); } // 创建或更新激活的自动除草技能 $activeSkill = PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->value) ->first(); if (!$activeSkill) { PetActiveSkill::create([ 'pet_id' => $pet->id, 'skill_id' => 1, // 假设技能ID 'skill_name' => \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->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("- 杂草灾害数量: 3个"); } catch (\Exception $e) { DB::rollback(); throw $e; } } /** * 执行自动除草 */ private function executeAutoWeeding(int $userId): void { $this->info('执行自动除草...'); $pet = PetUser::where('user_id', $userId)->first(); $activeSkill = PetActiveSkill::where('pet_id', $pet->id) ->where('skill_name', \App\Module\Pet\Enums\PET_SKILL_NAME::AUTO_WEEDING->value) ->first(); if (!$activeSkill) { throw new \Exception('没有找到激活的自动除草技能'); } DB::beginTransaction(); $autoSkillLogic = new PetAutoSkillLogic(); $autoSkillLogic->processAutoWeeding($activeSkill); DB::commit(); $this->info('自动除草执行完成'); } /** * 验证结果 */ private function verifyResults(int $userId): void { $this->info('验证结果...'); $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; } // 统计剩余的活跃杂草灾害 $activeWeeds = array_filter($crop->disasters ?? [], function($disaster) { return ($disaster['type'] ?? 0) == DISASTER_TYPE::WEED->value && ($disaster['status'] ?? '') === 'active'; }); $clearedWeeds = array_filter($crop->disasters ?? [], function($disaster) { return ($disaster['type'] ?? 0) == DISASTER_TYPE::WEED->value && ($disaster['status'] ?? '') === 'cleared'; }); // 检查道具消耗 $userItem = ItemUser::where('user_id', $userId) ->where('item_id', 22) ->first(); $this->info('验证结果:'); $this->info("- 剩余活跃杂草灾害: " . count($activeWeeds) . "个"); $this->info("- 已清除杂草灾害: " . count($clearedWeeds) . "个"); $this->info("- 剩余除草剂数量: " . ($userItem ? $userItem->quantity : 0)); if (count($activeWeeds) == 0) { $this->info('✅ 优化成功!所有杂草灾害都被清除了'); } else { $this->warn('⚠️ 还有杂草灾害未被清除,可能是道具不足'); } } }