| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Module\Pet\Logic\PetAutoSkillLogic;
- use App\Module\Pet\Models\PetActiveSkill;
- use App\Module\Pet\Models\PetUser;
- use App\Module\Farm\Models\FarmLand;
- use App\Module\Farm\Models\FarmCrop;
- use App\Module\Farm\Enums\DISASTER_TYPE;
- use App\Module\GameItems\Models\ItemUser;
- use Illuminate\Support\Facades\DB;
- /**
- * 测试自动技能优化功能
- */
- class TestAutoSkillsOptimization extends Command
- {
- /**
- * 命令签名
- */
- protected $signature = 'test:auto-skills-optimization {user_id=1} {--skill=all : 测试的技能类型 (weeding|watering|pest_control|all)}';
- /**
- * 命令描述
- */
- protected $description = '测试自动技能功能处理多个灾害的优化 (除草、浇水、杀虫)';
- /**
- * 技能配置映射
- */
- private $skillConfigs = [
- 'weeding' => [
- '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']}灾害未被清除,可能是道具不足");
- }
- }
- }
|