Explorar o código

修复房屋升级后土地同步问题:新土地现在能正确进入暂存区

- 在LandLogic::createLand方法中添加LandCreatedEvent事件触发
- 在Game模块ServiceProvider中注册LandCreatedEvent监听器
- 确保房屋升级解锁的新土地能正确进入暂存区
- 添加测试命令验证修复效果
- 解决了房屋升级后新土地不进入暂存区的问题
notfff hai 6 meses
pai
achega
77970c0912

+ 79 - 0
app/Console/Commands/CheckTempDataCommand.php

@@ -0,0 +1,79 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Module\Game\Services\LandTempService;
+use App\Module\Game\Services\HouseTempService;
+use Illuminate\Console\Command;
+
+/**
+ * 检查暂存区数据命令
+ */
+class CheckTempDataCommand extends Command
+{
+    /**
+     * 命令签名
+     *
+     * @var string
+     */
+    protected $signature = 'test:check-temp {user_id}';
+
+    /**
+     * 命令描述
+     *
+     * @var string
+     */
+    protected $description = '检查用户的暂存区数据';
+
+    /**
+     * 执行命令
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $userId = (int) $this->argument('user_id');
+
+        $this->info("检查用户 {$userId} 的暂存区数据");
+
+        // 检查土地暂存数据
+        $this->info("\n=== 土地暂存数据 ===");
+        try {
+            $landChanges = LandTempService::getUserLandChanges($userId);
+            if (empty($landChanges)) {
+                $this->warn("没有土地变更暂存数据");
+            } else {
+                $this->info("找到 " . count($landChanges) . " 条土地变更记录:");
+                foreach ($landChanges as $landId => $change) {
+                    $this->line("土地ID: {$landId}");
+                    $this->line("  - 变更类型: " . ($change->changeType ?? 'N/A'));
+                    $this->line("  - 土地类型: " . ($change->landType ?? 'N/A'));
+                    $this->line("  - 更新时间: " . ($change->updatedAt ?? 'N/A'));
+                }
+            }
+        } catch (\Exception $e) {
+            $this->error("获取土地暂存数据失败: " . $e->getMessage());
+        }
+
+        // 检查房屋暂存数据
+        $this->info("\n=== 房屋暂存数据 ===");
+        try {
+            $houseChange = HouseTempService::getUserHouseChange($userId);
+            if (!$houseChange) {
+                $this->warn("没有房屋变更暂存数据");
+            } else {
+                $this->info("找到房屋变更记录:");
+                $this->line("  - 旧等级: " . ($houseChange->oldLevel ?? 'N/A'));
+                $this->line("  - 新等级: " . ($houseChange->newLevel ?? 'N/A'));
+                $this->line("  - 是否升级: " . ($houseChange->isUpgrade ? '是' : '否'));
+                $this->line("  - 产出加成: " . ($houseChange->outputBonus ?? 'N/A'));
+                $this->line("  - 特殊土地上限: " . ($houseChange->specialLandLimit ?? 'N/A'));
+                $this->line("  - 更新时间: " . ($houseChange->updatedAt ?? 'N/A'));
+            }
+        } catch (\Exception $e) {
+            $this->error("获取房屋暂存数据失败: " . $e->getMessage());
+        }
+
+        return 0;
+    }
+}

+ 74 - 0
app/Console/Commands/TestHouseUpgradeCommand.php

@@ -0,0 +1,74 @@
+<?php
+
+namespace App\Console\Commands;
+
+use App\Module\Farm\Events\HouseUpgradedEvent;
+use App\Module\Farm\Models\FarmUpgradeLog;
+use App\Module\Farm\Models\FarmUser;
+use App\Module\Farm\Enums\UPGRADE_TYPE;
+use Illuminate\Console\Command;
+
+/**
+ * 测试房屋升级事件命令
+ */
+class TestHouseUpgradeCommand extends Command
+{
+    /**
+     * 命令签名
+     *
+     * @var string
+     */
+    protected $signature = 'test:house-upgrade {user_id} {old_level} {new_level}';
+
+    /**
+     * 命令描述
+     *
+     * @var string
+     */
+    protected $description = '测试房屋升级事件,验证土地是否正确进入暂存区';
+
+    /**
+     * 执行命令
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $userId = (int) $this->argument('user_id');
+        $oldLevel = (int) $this->argument('old_level');
+        $newLevel = (int) $this->argument('new_level');
+
+        $this->info("开始测试用户 {$userId} 的房屋升级事件");
+        $this->info("从等级 {$oldLevel} 升级到等级 {$newLevel}");
+
+        // 获取用户农场信息
+        $farmUser = FarmUser::where('user_id', $userId)->first();
+        if (!$farmUser) {
+            $this->error("用户 {$userId} 的农场信息不存在");
+            return 1;
+        }
+
+        // 创建升级记录
+        $upgradeLog = new FarmUpgradeLog();
+        $upgradeLog->user_id = $userId;
+        $upgradeLog->upgrade_type = UPGRADE_TYPE::HOUSE->value;
+        $upgradeLog->old_level = $oldLevel;
+        $upgradeLog->new_level = $newLevel;
+        $upgradeLog->materials_consumed = [];
+        $upgradeLog->upgrade_time = now();
+        $upgradeLog->created_at = now();
+        $upgradeLog->save();
+
+        $this->info("创建升级记录,ID: {$upgradeLog->id}");
+
+        // 触发房屋升级事件
+        event(new HouseUpgradedEvent($userId, $farmUser, $oldLevel, $newLevel, $upgradeLog));
+
+        $this->info("房屋升级事件已触发");
+        $this->info("请检查:");
+        $this->info("1. 是否创建了新的土地");
+        $this->info("2. 新土地是否进入了暂存区");
+
+        return 0;
+    }
+}

+ 4 - 0
app/Module/Farm/Logics/LandLogic.php

@@ -6,6 +6,7 @@ use App\Module\Farm\Dtos\LandInfoDto;
 use App\Module\Farm\Enums\LAND_STATUS;
 use App\Module\Farm\Enums\LAND_TYPE;
 use App\Module\Farm\Enums\UPGRADE_TYPE;
+use App\Module\Farm\Events\LandCreatedEvent;
 use App\Module\Farm\Events\LandUpgradedEvent;
 use App\Module\Farm\Models\FarmLand;
 use App\Module\Farm\Models\FarmLandType;
@@ -112,6 +113,9 @@ class LandLogic
             $land->status    = LAND_STATUS::IDLE->value;
             $land->save();
 
+            // 触发土地创建事件
+            event(new LandCreatedEvent($userId, $land->id));
+
             Log::info('创建土地成功', [
                 'user_id'   => $userId,
                 'position'  => $position,

+ 7 - 1
app/Module/Game/Providers/GameServiceProvider.php

@@ -16,7 +16,7 @@ use App\Module\Farm\Events\DisasterClearedEvent;
 use App\Module\Farm\Events\BuffActivatedEvent;
 use App\Module\Farm\Events\HouseDowngradedEvent;
 use App\Module\Farm\Events\HouseUpgradedEvent;
-
+use App\Module\Farm\Events\LandCreatedEvent;
 use App\Module\Farm\Events\LandStatusChangedEvent;
 use App\Module\Farm\Events\LandUpgradedEvent;
 use App\Module\Fund\Events\FundChangedEvent;
@@ -28,6 +28,7 @@ use App\Module\Game\Listeners\FundChangedListener;
 use App\Module\Game\Listeners\HouseDowngradedListener;
 use App\Module\Game\Listeners\HouseUpgradedListener;
 use App\Module\Game\Listeners\ItemQuantityChangedListener;
+use App\Module\Game\Listeners\LandCreatedListener;
 use App\Module\Game\Listeners\LandStatusChangedListener;
 use App\Module\Game\Listeners\LandUpgradedListener;
 use App\Module\Game\Listeners\LogRewardGrantedListener;
@@ -123,6 +124,11 @@ class GameServiceProvider extends ServiceProvider
         );
 
         // 注册土地事件监听器
+        Event::listen(
+            LandCreatedEvent::class,
+            LandCreatedListener::class
+        );
+
         Event::listen(
             LandUpgradedEvent::class,
             LandUpgradedListener::class