notfff 8 months ago
parent
commit
7152118586

+ 2 - 12
app/Module/AppGame/Handler/Land/UpHandler.php

@@ -41,19 +41,9 @@ class UpHandler extends BaseHandler
 
             // 验证土地是否存在且属于当前用户
             // 获取用户所有土地
-            $userLands = LandService::getUserLands($userId);
-            $landInfo = null;
-
-            // 查找指定ID的土地
-            foreach ($userLands as $land) {
-                if ($land->id == $landId) {
-                    $landInfo = $land;
-                    break;
-                }
-            }
-
+            $landInfo = LandService::getUserLand($userId,$landId);
             if (!$landInfo) {
-                throw new LogicException("土地不存在或不属于当前用户");
+                throw new LogicException("土地不存在或不属于当前用户1");
             }
 
             // 获取可用的升级路径

+ 1 - 1
app/Module/AppGame/Handler/User/DataHandler.php

@@ -109,7 +109,7 @@ class DataHandler extends BaseHandler
     {
         // 调用Farm模块的LandService获取用户的所有土地
         $lands = LandService::getUserLands($this->user_id);
-
+//        dd($lands);
         // 将土地数据转换为响应格式
         $landDataList = [];
         foreach ($lands as $land) {

+ 8 - 3
app/Module/Farm/Logics/LandLogic.php

@@ -33,7 +33,7 @@ class LandLogic
             $lands = FarmLand::where('user_id', $userId)
                 ->orderBy('position')
                 ->get();
-
+//            dd($lands);
             return $lands->map(function ($land) {
                 return LandInfoDto::fromModel($land);
             });
@@ -283,7 +283,7 @@ class LandLogic
      * @param int $landId
      * @return array
      */
-    public function getAvailableUpgradePaths(int $userId, int $landId): array
+    public function getAvailableUpgradePaths(int $userId, int $landId ,$to_type =null): array
     {
         try {
             // 获取土地信息
@@ -299,6 +299,9 @@ class LandLogic
             $currentType = $land->land_type;
 
             // 获取用户房屋等级
+            /**
+             * @var FarmUser $farmUser
+             */
             $farmUser = FarmUser::where('user_id', $userId)->first();
 
             if (!$farmUser) {
@@ -317,12 +320,14 @@ class LandLogic
                 return $path->toType->unlock_house_level <= $houseLevel;
             });
 
+
             // 检查特殊土地数量限制
             $specialLandCount = FarmLand::where('user_id', $userId)
                 ->whereIn('land_type', [LAND_TYPE::GOLD->value, LAND_TYPE::BLUE->value, LAND_TYPE::PURPLE->value])
                 ->count();
 
             $houseConfig = $farmUser->houseConfig;
+
             $specialLandLimit = $houseConfig->special_land_limit;
 
             // 如果特殊土地已达上限,过滤掉特殊土地升级路径
@@ -333,7 +338,7 @@ class LandLogic
             }
 
             // 格式化返回结果
-            return $availablePaths->map(function ($path) use ($userId) {
+            return $availablePaths->map(function ( FarmLandUpgradeConfig $path) use ($userId) {
                 // 获取升级所需材料
                 $materials = $path->getUpgradeMaterials();
 

+ 29 - 3
app/Module/Farm/Services/LandService.php

@@ -4,6 +4,7 @@ namespace App\Module\Farm\Services;
 
 use App\Module\Farm\Dtos\LandInfoDto;
 use App\Module\Farm\Logics\LandLogic;
+use App\Module\Farm\Models\FarmLand;
 use Illuminate\Support\Collection;
 use Illuminate\Support\Facades\Log;
 
@@ -16,7 +17,7 @@ class LandService
      * 获取用户的所有土地
      *
      * @param int $userId
-     * @return Collection
+     * @return Collection|LandInfoDto[]
      */
     public static function getUserLands(int $userId): Collection
     {
@@ -35,10 +36,35 @@ class LandService
     }
 
 
-    public static function getUserLand(int $userId,int $landId)
+    /**
+     * 根据用户id和土地id获取土地信息
+     *
+     * @param int $userId 用户ID
+     * @param int $landId 土地ID
+     * @return LandInfoDto|null
+     */
+    public static function getUserLand(int $userId, int $landId): ?LandInfoDto
     {
-        // 根据用户id,和土地id获取土地信息
+        try {
+            $land = FarmLand::where('user_id', $userId)
+                ->where('id', $landId)
+                ->first();
+
+            if (!$land) {
+                return null;
+            }
 
+            return LandInfoDto::fromModel($land);
+        } catch (\Exception $e) {
+            Log::error('获取用户土地信息失败', [
+                'user_id' => $userId,
+                'land_id' => $landId,
+                'error' => $e->getMessage(),
+                'trace' => $e->getTraceAsString()
+            ]);
+
+            return null;
+        }
     }
 
     /**

+ 1 - 0
config/logging.php

@@ -71,6 +71,7 @@ return [
             'level' => env('LOG_LEVEL', 'debug'),
             'days' => env('LOG_DAILY_DAYS', 14),
             'replace_placeholders' => true,
+            'permission' => 0777,
         ],
 
         'slack' => [