TestGodRewardCommand.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Services\RewardCollectorService;
  4. use App\Module\Game\Services\RewardService;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Support\Facades\DB;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 测试神像奖励收集命令
  10. *
  11. * 用于测试神像奖励是否能正确收集到Response.Reward中
  12. */
  13. class TestGodRewardCommand extends Command
  14. {
  15. /**
  16. * 命令签名
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'game:test-god-reward {user_id=10001} {reward_group_id=16}';
  21. /**
  22. * 命令描述
  23. *
  24. * @var string
  25. */
  26. protected $description = '测试神像奖励收集功能';
  27. /**
  28. * 执行命令
  29. *
  30. * @return int
  31. */
  32. public function handle(): int
  33. {
  34. $this->info('开始测试神像奖励收集功能...');
  35. // 测试手动添加神像奖励
  36. $this->testManualGodReward();
  37. // 测试通过奖励组发放神像奖励
  38. $this->testRewardGroupGodReward();
  39. $this->info('神像奖励收集功能测试完成!');
  40. return 0;
  41. }
  42. /**
  43. * 测试手动添加神像奖励
  44. */
  45. protected function testManualGodReward(): void
  46. {
  47. $this->info('测试手动添加神像奖励...');
  48. // 清空收集器
  49. RewardCollectorService::clearRewards();
  50. // 手动添加神像奖励
  51. RewardCollectorService::addGodReward(2, 86400, 1); // 雨露之神,24小时,1次
  52. // 检查收集器中的数据
  53. if (RewardCollectorService::hasRewards()) {
  54. $rewardData = RewardCollectorService::getRewards();
  55. $this->line("收集器中的奖励数据:");
  56. $this->line(" 物品奖励数量: " . count($rewardData['items']));
  57. $this->line(" 代币奖励数量: " . count($rewardData['coins']));
  58. $this->line(" 神像奖励数量: " . count($rewardData['gods']));
  59. if (!empty($rewardData['gods'])) {
  60. $this->line("✓ 神像奖励收集成功!");
  61. foreach ($rewardData['gods'] as $god) {
  62. $this->line(" 神像类型: {$god['type']}, 时间差值: {$god['diff']}秒, 数量: {$god['quantity']}");
  63. }
  64. } else {
  65. $this->error("✗ 神像奖励收集失败!");
  66. }
  67. } else {
  68. $this->error("✗ 收集器中没有任何奖励数据!");
  69. }
  70. }
  71. /**
  72. * 测试通过奖励组发放神像奖励
  73. */
  74. protected function testRewardGroupGodReward(): void
  75. {
  76. $userId = (int) $this->argument('user_id');
  77. $rewardGroupId = (int) $this->argument('reward_group_id');
  78. $this->info("测试通过奖励组发放神像奖励...");
  79. $this->line("用户ID: {$userId}");
  80. $this->line("奖励组ID: {$rewardGroupId}");
  81. try {
  82. // 开启事务
  83. DB::beginTransaction();
  84. // 清空之前的收集器数据
  85. RewardCollectorService::clearRewards();
  86. // 发放奖励
  87. $result = RewardService::grantReward(
  88. $userId,
  89. $rewardGroupId,
  90. 'test_command',
  91. 1
  92. );
  93. if (!$result->success) {
  94. $this->error("✗ 奖励发放失败: " . $result->errorMessage);
  95. DB::rollBack();
  96. return;
  97. }
  98. $this->line("✓ 奖励发放成功!");
  99. // 检查收集器中的数据
  100. if (RewardCollectorService::hasRewards()) {
  101. $rewardData = RewardCollectorService::getRewards();
  102. $this->line("收集器中的奖励数据:");
  103. $this->line(" 物品奖励数量: " . count($rewardData['items']));
  104. $this->line(" 代币奖励数量: " . count($rewardData['coins']));
  105. $this->line(" 神像奖励数量: " . count($rewardData['gods']));
  106. if (!empty($rewardData['gods'])) {
  107. $this->line("✓ 神像奖励收集成功!");
  108. foreach ($rewardData['gods'] as $god) {
  109. $this->line(" 神像类型: {$god['type']}, 时间差值: {$god['diff']}秒, 数量: {$god['quantity']}");
  110. }
  111. } else {
  112. $this->warn("⚠ 没有收集到神像奖励数据(可能奖励组中没有神像奖励)");
  113. }
  114. } else {
  115. $this->warn("⚠ 收集器中没有任何奖励数据!");
  116. }
  117. // 提交事务
  118. DB::commit();
  119. } catch (\Exception $e) {
  120. DB::rollBack();
  121. $this->error("✗ 测试失败: " . $e->getMessage());
  122. $this->line("错误堆栈: " . $e->getTraceAsString());
  123. }
  124. }
  125. }