|
|
@@ -0,0 +1,108 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Module\Game\Commands;
|
|
|
+
|
|
|
+use App\Module\Game\Services\ItemCacheService;
|
|
|
+use App\Module\GameItems\Events\ItemQuantityChanged;
|
|
|
+use Illuminate\Console\Command;
|
|
|
+use Illuminate\Support\Facades\Event;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 测试物品缓存命令
|
|
|
+ *
|
|
|
+ * 用于测试物品变更事件监听和缓存功能
|
|
|
+ */
|
|
|
+class TestItemCacheCommand extends Command
|
|
|
+{
|
|
|
+ /**
|
|
|
+ * 命令名称
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $signature = 'game:test-item-cache {user_id} {item_id} {old_quantity} {new_quantity}';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 命令描述
|
|
|
+ *
|
|
|
+ * @var string
|
|
|
+ */
|
|
|
+ protected $description = '测试物品变更事件监听和缓存功能';
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 物品缓存服务
|
|
|
+ *
|
|
|
+ * @var ItemCacheService
|
|
|
+ */
|
|
|
+ protected $itemCacheService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 创建命令实例
|
|
|
+ */
|
|
|
+ public function __construct(ItemCacheService $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;
|
|
|
+ }
|
|
|
+}
|