TestRewardDeductCollectorCommand.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace App\Module\Game\Commands;
  3. use App\Module\Fund\Events\FundChangedEvent;
  4. use App\Module\Game\Services\DeductCollectorService;
  5. use App\Module\Game\Services\RewardCollectorService;
  6. use App\Module\GameItems\Events\ItemQuantityChanged;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Event;
  9. /**
  10. * 测试奖励扣除收集器命令
  11. *
  12. * 用于测试奖励和扣除数据收集功能是否正常工作
  13. */
  14. class TestRewardDeductCollectorCommand extends Command
  15. {
  16. /**
  17. * 命令签名
  18. *
  19. * @var string
  20. */
  21. protected $signature = 'game:test-reward-deduct-collector';
  22. /**
  23. * 命令描述
  24. *
  25. * @var string
  26. */
  27. protected $description = '测试奖励扣除收集器功能';
  28. /**
  29. * 执行命令
  30. *
  31. * @return int
  32. */
  33. public function handle(): int
  34. {
  35. $this->info('开始测试奖励扣除收集器功能...');
  36. // 测试手动添加数据
  37. $this->testManualCollection();
  38. // 测试事件触发自动收集
  39. $this->testEventCollection();
  40. // 测试数据查询
  41. $this->testDataQuery();
  42. // 测试数据清理
  43. $this->testDataClear();
  44. $this->info('奖励扣除收集器功能测试完成!');
  45. return 0;
  46. }
  47. /**
  48. * 测试手动收集功能
  49. */
  50. protected function testManualCollection(): void
  51. {
  52. $this->info('测试手动收集功能...');
  53. // 手动添加奖励数据
  54. RewardCollectorService::addItemReward(1001, 0, 10);
  55. RewardCollectorService::addCoinReward(1, 100);
  56. // 手动添加扣除数据
  57. DeductCollectorService::addItemDeduct(2001, 0, 5);
  58. DeductCollectorService::addCoinDeduct(2, 50);
  59. $this->line('✓ 手动添加奖励和扣除数据完成');
  60. }
  61. /**
  62. * 测试事件触发自动收集
  63. */
  64. protected function testEventCollection(): void
  65. {
  66. $this->info('测试事件触发自动收集...');
  67. // 触发物品数量增加事件(奖励)
  68. $itemRewardEvent = new ItemQuantityChanged(
  69. userId: 1,
  70. itemId: 1002,
  71. instanceId: 0,
  72. oldQuantity: 0,
  73. newQuantity: 20,
  74. userItemId: 1,
  75. options: []
  76. );
  77. Event::dispatch($itemRewardEvent);
  78. // 触发物品数量减少事件(扣除)
  79. $itemDeductEvent = new ItemQuantityChanged(
  80. userId: 1,
  81. itemId: 2002,
  82. instanceId: 0,
  83. oldQuantity: 30,
  84. newQuantity: 25,
  85. userItemId: 2,
  86. options: []
  87. );
  88. Event::dispatch($itemDeductEvent);
  89. // 触发资金增加事件(奖励)
  90. $fundRewardEvent = new FundChangedEvent(
  91. userId: 1,
  92. fundId: 1,
  93. amount: 200,
  94. beforeBalance: 1000,
  95. afterBalance: 1200,
  96. operateType: 1,
  97. operateId: 1,
  98. remark: '测试奖励'
  99. );
  100. Event::dispatch($fundRewardEvent);
  101. // 触发资金减少事件(扣除)
  102. $fundDeductEvent = new FundChangedEvent(
  103. userId: 1,
  104. fundId: 2,
  105. amount: -150,
  106. beforeBalance: 500,
  107. afterBalance: 350,
  108. operateType: 2,
  109. operateId: 2,
  110. remark: '测试扣除'
  111. );
  112. Event::dispatch($fundDeductEvent);
  113. $this->line('✓ 事件触发自动收集完成');
  114. }
  115. /**
  116. * 测试数据查询功能
  117. */
  118. protected function testDataQuery(): void
  119. {
  120. $this->info('测试数据查询功能...');
  121. // 查询奖励数据
  122. if (RewardCollectorService::hasRewards()) {
  123. $rewards = RewardCollectorService::getRewards();
  124. $this->line('奖励数据:');
  125. $this->line(' 物品奖励数量:' . count($rewards['items']));
  126. $this->line(' 代币奖励数量:' . count($rewards['coins']));
  127. foreach ($rewards['items'] as $item) {
  128. $this->line(" 物品ID: {$item['item_id']}, 实例ID: {$item['instance_id']}, 数量: {$item['quantity']}");
  129. }
  130. foreach ($rewards['coins'] as $coin) {
  131. $this->line(" 代币类型: {$coin['type']}, 数量: {$coin['quantity']}");
  132. }
  133. } else {
  134. $this->line('没有奖励数据');
  135. }
  136. // 查询扣除数据
  137. if (DeductCollectorService::hasDeducts()) {
  138. $deducts = DeductCollectorService::getDeducts();
  139. $this->line('扣除数据:');
  140. $this->line(' 物品扣除数量:' . count($deducts['items']));
  141. $this->line(' 代币扣除数量:' . count($deducts['coins']));
  142. foreach ($deducts['items'] as $item) {
  143. $this->line(" 物品ID: {$item['item_id']}, 实例ID: {$item['instance_id']}, 数量: {$item['quantity']}");
  144. }
  145. foreach ($deducts['coins'] as $coin) {
  146. $this->line(" 代币类型: {$coin['type']}, 数量: {$coin['quantity']}");
  147. }
  148. } else {
  149. $this->line('没有扣除数据');
  150. }
  151. $this->line('✓ 数据查询完成');
  152. }
  153. /**
  154. * 测试数据清理功能
  155. */
  156. protected function testDataClear(): void
  157. {
  158. $this->info('测试数据清理功能...');
  159. // 清理数据
  160. RewardCollectorService::clearRewards();
  161. DeductCollectorService::clearDeducts();
  162. // 验证清理结果
  163. $hasRewards = RewardCollectorService::hasRewards();
  164. $hasDeducts = DeductCollectorService::hasDeducts();
  165. if (!$hasRewards && !$hasDeducts) {
  166. $this->line('✓ 数据清理成功');
  167. } else {
  168. $this->error('✗ 数据清理失败');
  169. }
  170. }
  171. }