Bladeren bron

添加物品缓存测试命令并调整事件监听方式

Your Name 8 maanden geleden
bovenliggende
commit
004579d426

+ 1 - 1
.roo/rules-code/rules.md

@@ -1,6 +1,7 @@
 # 引导
 - 这不是一个标准 Laravel 项目,不要用 Laravel 的思维进行开发
 - Validation不是Validator,有本质区别,阅读 ucore/Validation/README.md 了解
+- 命令执行一次只执行一个操作,避免使用`&&`进行批量操作
 
 # 开发流程
 1. 理解需求
@@ -33,7 +34,6 @@
 - `git status`查看变更的文件
 - 查看变更的内容,根据变更内容编写commit message
 - 提交,推送
-- 一次只执行一个命令
 
 # 其他规则
 - `php artisan thinker`禁止使用

+ 108 - 0
app/Module/Game/Commands/TestItemCacheCommand.php

@@ -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;
+    }
+}

+ 4 - 5
app/Module/Game/Listeners/ItemQuantityChangedListener.php

@@ -4,16 +4,15 @@ namespace App\Module\Game\Listeners;
 
 use App\Module\Game\Logics\ItemCache;
 use App\Module\GameItems\Events\ItemQuantityChanged;
-use Illuminate\Contracts\Queue\ShouldQueue;
 use Illuminate\Support\Facades\Log;
 
 /**
  * 物品数量变更事件监听器
- * 
+ *
  * 监听物品数量变更事件,并调用逻辑层处理缓存逻辑
- * 实现了ShouldQueue接口,表示它将在队列中异步处理事件
+ * 同步处理事件,确保事件处理完成后才返回
  */
-class ItemQuantityChangedListener implements ShouldQueue
+class ItemQuantityChangedListener
 {
     /**
      * 创建事件监听器
@@ -43,7 +42,7 @@ class ItemQuantityChangedListener implements ShouldQueue
                 'new_quantity' => $event->newQuantity,
                 'change_amount' => $event->changeAmount,
             ]);
-            
+
             // 调用逻辑层处理缓存
             ItemCache::handleItemQuantityChanged($event);
         } catch (\Exception $e) {