| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275 |
- <?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\Farm\Enums\LAND_STATUS;
- use App\Module\Farm\Enums\GROWTH_STAGE;
- use App\Module\GameItems\Models\ItemUser;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试自动除草优化功能
- */
- class TestAutoWeedingOptimization extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'test:auto-skill-optimization {user_id=1} {--skill=all : 测试的技能类型 (weeding|watering|pest_control|all)}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试自动技能功能处理多个灾害的优化 (除草、浇水、杀虫)';
- /**
- * 执行命令
- */
- public function handle(): int
- {
- $userId = (int) $this->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('⚠️ 还有杂草灾害未被清除,可能是道具不足');
- }
- }
- }
|