| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace App\Module\Game\Commands;
- use App\Module\Game\Services\ItemTempService;
- use App\Module\GameItems\Events\ItemQuantityChanged;
- use Illuminate\Console\Command;
- use Illuminate\Support\Facades\Event;
- /**
- * 测试物品暂存命令
- *
- * 用于测试物品变更事件监听和缓存功能
- */
- class TestItemTempCommand extends Command
- {
- /**
- * 命令名称
- *
- * @var string
- */
- protected $signature = 'game:test-item-temp {user_id} {item_id} {old_quantity} {new_quantity}';
- /**
- * 命令描述
- *
- * @var string
- */
- protected $description = '测试物品变更事件监听和缓存功能';
- /**
- * 物品缓存服务
- *
- * @var ItemTempService
- */
- protected $itemCacheService;
- /**
- * 创建命令实例
- */
- public function __construct(ItemTempService $itemCacheService)
- {
- parent::__construct();
- $this->itemCacheService = $itemCacheService;
- }
- /**
- * 执行命令
- */
- public function handle()
- {
- $userId = (int)$this->argument('user_id');
- $itemId = (int)$this->argument('item_id');
- $oldQuantity = (int)$this->argument('old_quantity');
- $newQuantity = (int)$this->argument('new_quantity');
- $this->info('测试物品变更事件监听和缓存功能');
- $this->info("用户ID: {$userId}");
- $this->info("物品ID: {$itemId}");
- $this->info("旧数量: {$oldQuantity}");
- $this->info("新数量: {$newQuantity}");
- // 清除之前的缓存
- $this->itemCacheService->clearUserItemChanges($userId);
- $this->info('已清除之前的缓存');
- // 触发物品数量变更事件
- $event = new ItemQuantityChanged(
- $userId,
- $itemId,
- null, // instanceId
- $oldQuantity,
- $newQuantity,
- 1, // userItemId
- ['source_type' => 'test', 'source_id' => 1]
- );
- Event::dispatch($event);
- $this->info('已触发物品数量变更事件');
- // 等待事件处理完成
- sleep(2);
- // 获取缓存数据
- $itemChanges = $this->itemCacheService->getUserItemChanges($userId);
- if (empty($itemChanges)) {
- $this->error('缓存数据为空,事件监听可能未正常工作');
- return 1;
- }
- $this->info('缓存数据:');
- $this->table(
- ['物品ID', '旧数量', '新数量', '变化量'],
- collect($itemChanges)->map(function ($item) {
- return [
- 'item_id' => $item['item_id'],
- 'old_quantity' => $item['old_quantity'],
- 'new_quantity' => $item['new_quantity'],
- 'change_amount' => $item['change_amount'],
- ];
- })->toArray()
- );
- $this->info('测试完成');
- return 0;
- }
- }
|