Browse Source

refactor(fertilizer): 重构施肥逻辑并更新作物生长阶段- 修改施肥服务返回值类型,增加错误处理
- 将作物生长阶段计算逻辑移至 CropLogic 类
- 优化施肥成功后的处理流程,更新作物生长阶段- 调整 UpdateCropGrowthCommand 中的阶段计算方式

notfff 7 months ago
parent
commit
b7db0561bc

+ 3 - 1
app/Module/AppGame/Handler/Land/FertilizerHandler.php

@@ -67,7 +67,7 @@ class FertilizerHandler extends BaseHandler
 
             // 调用施肥服务
             $result = CropService::useFertilizer($userId, $landId,$crop_growth_time);
-            if (!$result) {
+            if ($result->error) {
                 throw new LogicException("施肥失败,请检查土地状态或作物生长阶段");
             }
 
@@ -90,6 +90,8 @@ class FertilizerHandler extends BaseHandler
             DB::rollBack();
             throw $e;
         }
+        CropService::updateGrowthStage($result->data['crop_id']);
+
 
         return $response;
     }

+ 3 - 28
app/Module/Farm/Commands/UpdateCropGrowthCommand.php

@@ -4,6 +4,7 @@ namespace App\Module\Farm\Commands;
 
 use App\Module\Farm\Enums\GROWTH_STAGE;
 use App\Module\Farm\Events\CropGrowthStageChangedEvent;
+use App\Module\Farm\Logics\CropLogic;
 use App\Module\Farm\Models\FarmCrop;
 use Illuminate\Console\Command;
 use Illuminate\Support\Facades\Log;
@@ -103,34 +104,8 @@ class UpdateCropGrowthCommand extends Command
      */
     private function calculateNextStage(FarmCrop $crop): int
     {
-        $currentStage = $crop->growth_stage;
-
-        // 如果当前是成熟期,且超过一定时间,则进入枯萎期
-        if ($currentStage === GROWTH_STAGE::MATURE->value) {
-            // 成熟期持续时间,默认为24小时
-            $matureDuration = 24 * 60 * 60;
-
-            // 如果成熟期已经超过指定时间,则进入枯萎期
-            if ($crop->stage_end_time && $crop->stage_end_time instanceof \Carbon\Carbon) {
-                $endTimeMinusDuration = $crop->stage_end_time->copy()->subSeconds($matureDuration);
-                if (now()->diffInSeconds($endTimeMinusDuration) > $matureDuration) {
-                    return GROWTH_STAGE::WITHERED->value;
-                }
-            }
-
-            return GROWTH_STAGE::MATURE->value;
-        }
-
-        // 使用阶段映射确定下一个阶段
-        $stageMap = [
-            GROWTH_STAGE::SEED->value => GROWTH_STAGE::SPROUT->value,
-            GROWTH_STAGE::SPROUT->value => GROWTH_STAGE::GROWTH->value,
-            GROWTH_STAGE::GROWTH->value => GROWTH_STAGE::MATURE->value,
-            GROWTH_STAGE::MATURE->value => GROWTH_STAGE::WITHERED->value,
-            GROWTH_STAGE::WITHERED->value => GROWTH_STAGE::WITHERED->value, // 枯萎期保持不变
-        ];
-
-        return $stageMap[$currentStage->value] ?? GROWTH_STAGE::WITHERED->value;
+        $logic = new CropLogic();
+        return $logic->calculateNextStage($crop);
     }
 
     /**

+ 7 - 12
app/Module/Farm/Logics/CropLogic.php

@@ -20,6 +20,7 @@ use App\Module\Farm\Models\FarmSowLog;
 use Illuminate\Support\Facades\DB;
 use Illuminate\Support\Facades\Log;
 use UCore\Db\Helper;
+use UCore\Dto\Res;
 
 /**
  * 作物管理逻辑
@@ -281,7 +282,7 @@ class CropLogic
      * @param int $landId
      * @return bool
      */
-    public function useFertilizer(int $userId, int $landId,int $crop_growth_time): bool
+    public function useFertilizer(int $userId, int $landId,int $crop_growth_time): Res
     {
         try {
             Helper::check_tr();
@@ -349,14 +350,6 @@ class CropLogic
 
             $crop->save();
 
-            // 触发土地状态变更事件,确保前端能够获取到土地状态变更
-            // 虽然土地状态没有实际变化,但我们需要通知前端作物状态已更新
-            event(new LandStatusChangedEvent(
-                $userId,
-                $landId,
-                $land->status,  // 旧状态
-                $land->status   // 新状态(实际上没有变化,但需要触发事件)
-            ));
 
             Log::info('使用化肥成功', [
                 'user_id' => $userId,
@@ -366,7 +359,9 @@ class CropLogic
                 'stage_end_time' => $crop->stage_end_time
             ]);
 
-            return true;
+            return Res::success('',[
+                'crop_id' => $crop->id,
+            ]);
         } catch (\Exception $e) {
             Log::error('使用化肥失败', [
                 'user_id' => $userId,
@@ -375,7 +370,7 @@ class CropLogic
                 'trace' => $e->getTraceAsString()
             ]);
 
-            return false;
+            return Res::error('使用化肥失败');
         }
     }
 
@@ -618,7 +613,7 @@ class CropLogic
      * @param FarmCrop $crop
      * @return int
      */
-    private function calculateNextStage(FarmCrop $crop): int
+    public function calculateNextStage(FarmCrop $crop): int
     {
         $currentStage = $crop->growth_stage;
 

+ 3 - 2
app/Module/Farm/Services/CropService.php

@@ -6,6 +6,7 @@ use App\Module\Farm\Dtos\CropInfoDto;
 use App\Module\Farm\Dtos\HarvestResultDto;
 use App\Module\Farm\Logics\CropLogic;
 use Illuminate\Support\Facades\Log;
+use UCore\Dto\Res;
 
 /**
  * 作物管理服务
@@ -113,7 +114,7 @@ class CropService
      * @param int $landId
      * @return bool
      */
-    public static function useFertilizer(int $userId, int $landId,int $crop_growth_time): bool
+    public static function useFertilizer(int $userId, int $landId,int $crop_growth_time): Res
     {
         try {
             $cropLogic = new CropLogic();
@@ -126,7 +127,7 @@ class CropService
                 'trace' => $e->getTraceAsString()
             ]);
 
-            return false;
+            return Res::error('');
         }
     }
 

+ 1 - 0
protophp/Uraus/Kku/Common/SEED_STATUS.php

@@ -37,6 +37,7 @@ class SEED_STATUS
      * Generated from protobuf enum <code>GROWING_STAGE = 30;</code>
      */
     const GROWING_STAGE = 30;
+
     /**
      * 成熟
      *