| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace App\Module\Pet\Commands;
- use App\Module\Pet\Logic\PetLogic;
- use App\Module\Pet\Models\PetUser;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Log;
- /**
- * 测试宠物经验增加事件命令
- */
- class TestPetExpGainedEventCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'pet:test-exp-gained {pet_id} {exp_amount=10}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试宠物经验增加事件的触发和任务监听功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $petId = (int) $this->argument('pet_id');
- $expAmount = (int) $this->argument('exp_amount');
- try {
- // 检查宠物是否存在
- $pet = PetUser::find($petId);
- if (!$pet) {
- $this->error("宠物ID {$petId} 不存在");
- return 1;
- }
- $this->info("开始测试宠物经验增加事件...");
- $this->info("宠物ID: {$petId}");
- $this->info("用户ID: {$pet->user_id}");
- $this->info("当前经验: {$pet->experience}");
- $this->info("增加经验: {$expAmount}");
- // 使用宠物逻辑类增加经验(奖励方式)
- $petLogic = new PetLogic();
- $levelUpOccurred = $petLogic->addExperienceReward($petId, $expAmount, 'test_command', null);
- // 刷新宠物数据
- $pet->refresh();
- $this->info("经验增加成功!");
- $this->info("新经验值: {$pet->experience}");
- $this->info("是否升级: " . ($levelUpOccurred ? '是' : '否'));
- $this->info("请检查日志以确认事件已正确触发和处理");
- return 0;
- } catch (\Exception $e) {
- $this->error("测试失败: " . $e->getMessage());
- Log::error('宠物经验增加测试失败', [
- 'pet_id' => $petId,
- 'exp_amount' => $expAmount,
- 'error' => $e->getMessage()
- ]);
- return 1;
- }
- }
- }
|