TestPetExpGainedEventCommand.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. namespace App\Module\Pet\Commands;
  3. use App\Module\Pet\Logic\PetLogic;
  4. use App\Module\Pet\Models\PetUser;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 测试宠物经验增加事件命令
  9. */
  10. class TestPetExpGainedEventCommand extends Command
  11. {
  12. /**
  13. * 命令签名
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'pet:test-exp-gained {pet_id} {exp_amount=10}';
  18. /**
  19. * 命令描述
  20. *
  21. * @var string
  22. */
  23. protected $description = '测试宠物经验增加事件的触发和任务监听功能';
  24. /**
  25. * 执行命令
  26. *
  27. * @return int
  28. */
  29. public function handle(): int
  30. {
  31. $petId = (int) $this->argument('pet_id');
  32. $expAmount = (int) $this->argument('exp_amount');
  33. try {
  34. // 检查宠物是否存在
  35. $pet = PetUser::find($petId);
  36. if (!$pet) {
  37. $this->error("宠物ID {$petId} 不存在");
  38. return 1;
  39. }
  40. $this->info("开始测试宠物经验增加事件...");
  41. $this->info("宠物ID: {$petId}");
  42. $this->info("用户ID: {$pet->user_id}");
  43. $this->info("当前经验: {$pet->experience}");
  44. $this->info("增加经验: {$expAmount}");
  45. // 使用宠物逻辑类增加经验(奖励方式)
  46. $petLogic = new PetLogic();
  47. $levelUpOccurred = $petLogic->addExperienceReward($petId, $expAmount, 'test_command', null);
  48. // 刷新宠物数据
  49. $pet->refresh();
  50. $this->info("经验增加成功!");
  51. $this->info("新经验值: {$pet->experience}");
  52. $this->info("是否升级: " . ($levelUpOccurred ? '是' : '否'));
  53. $this->info("请检查日志以确认事件已正确触发和处理");
  54. return 0;
  55. } catch (\Exception $e) {
  56. $this->error("测试失败: " . $e->getMessage());
  57. Log::error('宠物经验增加测试失败', [
  58. 'pet_id' => $petId,
  59. 'exp_amount' => $expAmount,
  60. 'error' => $e->getMessage()
  61. ]);
  62. return 1;
  63. }
  64. }
  65. }