TestRewardSystemCommand.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Game\Services\RewardService;
  4. use App\Module\Game\Models\GameRewardGroup;
  5. use App\Module\Game\Models\GameRewardItem;
  6. use App\Module\Game\Enums\REWARD_TYPE;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\DB;
  9. use Exception;
  10. /**
  11. * 测试奖励系统命令
  12. *
  13. * 用于测试奖励发放功能是否正常工作
  14. */
  15. class TestRewardSystemCommand extends Command
  16. {
  17. /**
  18. * 命令签名
  19. *
  20. * @var string
  21. */
  22. protected $signature = 'game:test-reward-system {--user-id=1 : 测试用户ID}';
  23. /**
  24. * 命令描述
  25. *
  26. * @var string
  27. */
  28. protected $description = '测试奖励系统功能';
  29. /**
  30. * 执行命令
  31. *
  32. * @return int
  33. */
  34. public function handle(): int
  35. {
  36. $userId = (int) $this->option('user-id');
  37. $this->info("开始测试奖励系统功能,用户ID: {$userId}");
  38. try {
  39. // 创建测试奖励组
  40. $this->createTestRewardGroup();
  41. // 测试物品奖励发放
  42. $this->testItemReward($userId);
  43. // 测试账户种类奖励发放
  44. $this->testFundConfigReward($userId);
  45. // 测试币种奖励发放
  46. $this->testCurrencyReward($userId);
  47. // 测试宠物经验奖励发放
  48. $this->testPetExpReward($userId);
  49. // 测试宠物体力奖励发放
  50. $this->testPetEnergyReward($userId);
  51. // 测试其他类型奖励发放
  52. $this->testOtherReward($userId);
  53. $this->info('奖励系统功能测试完成!');
  54. return 0;
  55. } catch (Exception $e) {
  56. $this->error('测试失败: ' . $e->getMessage());
  57. return 1;
  58. }
  59. }
  60. /**
  61. * 创建测试奖励组
  62. */
  63. protected function createTestRewardGroup(): void
  64. {
  65. $this->info('创建测试奖励组...');
  66. // 检查是否已存在测试奖励组
  67. $existingGroup = GameRewardGroup::where('code', 'TEST_REWARD_GROUP')->first();
  68. if ($existingGroup) {
  69. $this->line('测试奖励组已存在,跳过创建');
  70. return;
  71. }
  72. DB::transaction(function () {
  73. // 创建奖励组
  74. $group = GameRewardGroup::create([
  75. 'name' => '测试奖励组',
  76. 'code' => 'TEST_REWARD_GROUP',
  77. 'description' => '用于测试奖励系统的奖励组',
  78. 'is_random' => false,
  79. 'random_count' => 1,
  80. 'is_active' => true
  81. ]);
  82. // 创建物品奖励项
  83. GameRewardItem::create([
  84. 'group_id' => $group->id,
  85. 'reward_type' => REWARD_TYPE::ITEM->value,
  86. 'target_id' => 1001, // 假设物品ID为1001
  87. 'param1' => 0,
  88. 'param2' => 0,
  89. 'quantity' => 10,
  90. 'weight' => 1.0,
  91. 'is_guaranteed' => true,
  92. 'extra_data' => []
  93. ]);
  94. // 创建账户种类奖励项
  95. GameRewardItem::create([
  96. 'group_id' => $group->id,
  97. 'reward_type' => REWARD_TYPE::FUND_CONFIG->value,
  98. 'target_id' => 1, // 假设账户种类ID为1
  99. 'param1' => 0,
  100. 'param2' => 0,
  101. 'quantity' => 100,
  102. 'weight' => 1.0,
  103. 'is_guaranteed' => true,
  104. 'extra_data' => []
  105. ]);
  106. $this->line('✓ 测试奖励组创建完成');
  107. });
  108. }
  109. /**
  110. * 测试物品奖励发放
  111. */
  112. protected function testItemReward(int $userId): void
  113. {
  114. $this->info('测试物品奖励发放...');
  115. try {
  116. DB::transaction(function () use ($userId) {
  117. $result = RewardService::grantReward(
  118. $userId,
  119. 'TEST_REWARD_GROUP',
  120. 'test_command', // 来源类型:测试命令
  121. time() // 来源ID:当前时间戳作为唯一标识
  122. );
  123. if ($result->success) {
  124. $this->line('✓ 奖励发放成功');
  125. $this->line(' 发放的奖励项数量: ' . count($result->items));
  126. $this->line(' 来源类型: ' . $result->sourceType);
  127. $this->line(' 来源ID: ' . $result->sourceId);
  128. foreach ($result->items as $item) {
  129. $this->line(" 奖励类型: {$item->rewardType}, 目标ID: {$item->targetId}, 数量: {$item->quantity}");
  130. }
  131. } else {
  132. $this->error('✗ 奖励发放失败: ' . $result->errorMessage);
  133. }
  134. });
  135. } catch (Exception $e) {
  136. $this->error('✗ 奖励发放异常: ' . $e->getMessage());
  137. }
  138. }
  139. /**
  140. * 测试账户种类奖励发放
  141. */
  142. protected function testFundConfigReward(int $userId): void
  143. {
  144. $this->info('测试账户种类奖励发放...');
  145. $this->line('注意:此测试需要确保用户账户已创建且fund_config表中有对应记录');
  146. // 实际测试代码可以在这里实现
  147. }
  148. /**
  149. * 测试币种奖励发放
  150. */
  151. protected function testCurrencyReward(int $userId): void
  152. {
  153. $this->info('测试币种奖励发放...');
  154. $this->line('注意:此测试需要确保fund_currency和fund_config表中有对应记录');
  155. // 实际测试代码可以在这里实现
  156. }
  157. /**
  158. * 测试宠物经验奖励发放
  159. */
  160. protected function testPetExpReward(int $userId): void
  161. {
  162. $this->info('测试宠物经验奖励发放...');
  163. $this->line('注意:此测试需要确保用户有宠物');
  164. // 实际测试代码可以在这里实现
  165. }
  166. /**
  167. * 测试宠物体力奖励发放
  168. */
  169. protected function testPetEnergyReward(int $userId): void
  170. {
  171. $this->info('测试宠物体力奖励发放...');
  172. $this->line('注意:此测试需要确保用户有宠物');
  173. // 实际测试代码可以在这里实现
  174. }
  175. /**
  176. * 测试其他类型奖励发放
  177. */
  178. protected function testOtherReward(int $userId): void
  179. {
  180. $this->info('测试其他类型奖励发放...');
  181. $this->line('其他类型奖励目前只记录日志,不执行具体操作');
  182. // 实际测试代码可以在这里实现
  183. }
  184. }