| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Fund\Events\FundChangedEvent;
- use App\Module\Game\Services\DeductCollectorService;
- use App\Module\Game\Services\RewardCollectorService;
- use App\Module\GameItems\Events\ItemQuantityChanged;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Event;
- /**
- * 测试奖励扣除收集器命令
- *
- * 用于测试奖励和扣除数据收集功能是否正常工作
- */
- class TestRewardDeductCollectorCommand extends Command
- {
- /**
- * 命令签名
- *
- * @var string
- */
- protected $signature = 'game:test-reward-deduct-collector';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试奖励扣除收集器功能';
- /**
- * 执行命令
- *
- * @return int
- */
- public function handle(): int
- {
- $this->info('开始测试奖励扣除收集器功能...');
- // 测试手动添加数据
- $this->testManualCollection();
- // 测试事件触发自动收集
- $this->testEventCollection();
- // 测试数据查询
- $this->testDataQuery();
- // 测试数据清理
- $this->testDataClear();
- $this->info('奖励扣除收集器功能测试完成!');
- return 0;
- }
- /**
- * 测试手动收集功能
- */
- protected function testManualCollection(): void
- {
- $this->info('测试手动收集功能...');
- // 手动添加奖励数据
- RewardCollectorService::addItemReward(1001, 0, 10);
- RewardCollectorService::addCoinReward(1, 100);
- // 手动添加扣除数据
- DeductCollectorService::addItemDeduct(2001, 0, 5);
- DeductCollectorService::addCoinDeduct(2, 50);
- $this->line('✓ 手动添加奖励和扣除数据完成');
- }
- /**
- * 测试事件触发自动收集
- */
- protected function testEventCollection(): void
- {
- $this->info('测试事件触发自动收集...');
- // 触发物品数量增加事件(奖励)
- $itemRewardEvent = new ItemQuantityChanged(
- userId: 1,
- itemId: 1002,
- instanceId: 0,
- oldQuantity: 0,
- newQuantity: 20,
- userItemId: 1,
- options: []
- );
- Event::dispatch($itemRewardEvent);
- // 触发物品数量减少事件(扣除)
- $itemDeductEvent = new ItemQuantityChanged(
- userId: 1,
- itemId: 2002,
- instanceId: 0,
- oldQuantity: 30,
- newQuantity: 25,
- userItemId: 2,
- options: []
- );
- Event::dispatch($itemDeductEvent);
- // 触发资金增加事件(奖励)
- $fundRewardEvent = new FundChangedEvent(
- userId: 1,
- fundId: 1,
- amount: 200,
- beforeBalance: 1000,
- afterBalance: 1200,
- operateType: 1,
- operateId: 1,
- remark: '测试奖励'
- );
- Event::dispatch($fundRewardEvent);
- // 触发资金减少事件(扣除)
- $fundDeductEvent = new FundChangedEvent(
- userId: 1,
- fundId: 2,
- amount: -150,
- beforeBalance: 500,
- afterBalance: 350,
- operateType: 2,
- operateId: 2,
- remark: '测试扣除'
- );
- Event::dispatch($fundDeductEvent);
- $this->line('✓ 事件触发自动收集完成');
- }
- /**
- * 测试数据查询功能
- */
- protected function testDataQuery(): void
- {
- $this->info('测试数据查询功能...');
- // 查询奖励数据
- if (RewardCollectorService::hasRewards()) {
- $rewards = RewardCollectorService::getRewards();
- $this->line('奖励数据:');
- $this->line(' 物品奖励数量:' . count($rewards['items']));
- $this->line(' 代币奖励数量:' . count($rewards['coins']));
-
- foreach ($rewards['items'] as $item) {
- $this->line(" 物品ID: {$item['item_id']}, 实例ID: {$item['instance_id']}, 数量: {$item['quantity']}");
- }
-
- foreach ($rewards['coins'] as $coin) {
- $this->line(" 代币类型: {$coin['type']}, 数量: {$coin['quantity']}");
- }
- } else {
- $this->line('没有奖励数据');
- }
- // 查询扣除数据
- if (DeductCollectorService::hasDeducts()) {
- $deducts = DeductCollectorService::getDeducts();
- $this->line('扣除数据:');
- $this->line(' 物品扣除数量:' . count($deducts['items']));
- $this->line(' 代币扣除数量:' . count($deducts['coins']));
-
- foreach ($deducts['items'] as $item) {
- $this->line(" 物品ID: {$item['item_id']}, 实例ID: {$item['instance_id']}, 数量: {$item['quantity']}");
- }
-
- foreach ($deducts['coins'] as $coin) {
- $this->line(" 代币类型: {$coin['type']}, 数量: {$coin['quantity']}");
- }
- } else {
- $this->line('没有扣除数据');
- }
- $this->line('✓ 数据查询完成');
- }
- /**
- * 测试数据清理功能
- */
- protected function testDataClear(): void
- {
- $this->info('测试数据清理功能...');
- // 清理数据
- RewardCollectorService::clearRewards();
- DeductCollectorService::clearDeducts();
- // 验证清理结果
- $hasRewards = RewardCollectorService::hasRewards();
- $hasDeducts = DeductCollectorService::hasDeducts();
- if (!$hasRewards && !$hasDeducts) {
- $this->line('✓ 数据清理成功');
- } else {
- $this->error('✗ 数据清理失败');
- }
- }
- }
|