Forráskód Böngészése

feat(farm): 添加作物当前阶段开始时间并优化生长逻辑

- 在 CropInfoDto 和 FarmCrop 模型中添加 stage_start_time 字段
- 更新作物生长逻辑,设置新阶段的开始时间
- 优化成熟期判断逻辑,修复可能的类型错误
- 调整 API 响应,增加作物当前阶段开始时间信息
notfff 7 hónapja
szülő
commit
9769052692

+ 17 - 2
app/Module/AppGame/Proto/CropInfoDto.php

@@ -36,16 +36,31 @@ class CropInfoDto
                 $dataLand->setSeedPlantingTimes($cropInfoDto->plantTime);
             }
         }
+
+        // 当前阶段开始时间
+        if (!empty($cropInfoDto->stageStartTime)) {
+            // 如果 stageStartTime 是字符串格式的日期时间,转换为时间戳
+            if (is_string($cropInfoDto->stageStartTime)) {
+                $$stageStartTime = strtotime($cropInfoDto->stageEndTime);
+                $dataLand->setStageNextTimes($stageStartTime);
+            }
+            // 如果已经是时间戳,直接使用
+            else if (is_numeric($cropInfoDto->stageStartTime)) {
+                $dataLand->setStageNextTimes($cropInfoDto->stageStartTime);
+            }
+
+        }
+
         // 下一个阶段的时间
         if (!empty($cropInfoDto->stageEndTime)) {
             // 如果stageEndTime是字符串格式的日期时间,转换为时间戳
             if (is_string($cropInfoDto->stageEndTime)) {
                 $stageEndTime = strtotime($cropInfoDto->stageEndTime);
-                $dataLand->setSeedNextTimes($stageEndTime);
+                $dataLand->setStageNextTimes($stageEndTime);
             }
             // 如果已经是时间戳,直接使用
             else if (is_numeric($cropInfoDto->stageEndTime)) {
-                $dataLand->setSeedNextTimes($cropInfoDto->stageEndTime);
+                $dataLand->setStageNextTimes($cropInfoDto->stageEndTime);
             }
 
         }

+ 10 - 7
app/Module/Farm/Commands/UpdateCropGrowthCommand.php

@@ -39,12 +39,11 @@ class UpdateCropGrowthCommand extends Command
         try {
             // 获取需要更新生长阶段的作物
             /**
-             * @var FarmCrop[] $crops
-             *
+             * @var \Illuminate\Database\Eloquent\Collection $crops
              */
             $crops = FarmCrop::whereNotNull('stage_end_time')
                 ->where('stage_end_time', '<=', now())
-                ->where('growth_stage', '<', GROWTH_STAGE::WITHERED)
+                ->where('growth_stage', '<', GROWTH_STAGE::WITHERED->value)
                 ->get();
 
             $this->info("找到 {$crops->count()} 个需要更新的作物");
@@ -63,6 +62,7 @@ class UpdateCropGrowthCommand extends Command
 
                 // 更新作物信息
                 $crop->growth_stage = $newStage;
+                $crop->stage_start_time = now(); // 设置新阶段的开始时间
                 $crop->stage_end_time = $stageEndTime;
                 $crop->fertilized = false; // 重置施肥状态
                 $crop->save();
@@ -106,16 +106,19 @@ class UpdateCropGrowthCommand extends Command
         $currentStage = $crop->growth_stage;
 
         // 如果当前是成熟期,且超过一定时间,则进入枯萎期
-        if ($currentStage === GROWTH_STAGE::MATURE) {
+        if ($currentStage === GROWTH_STAGE::MATURE->value) {
             // 成熟期持续时间,默认为24小时
             $matureDuration = 24 * 60 * 60;
 
             // 如果成熟期已经超过指定时间,则进入枯萎期
-            if ($crop->stage_end_time && now()->diffInSeconds($crop->stage_end_time->subSeconds($matureDuration)) > $matureDuration) {
-                return GROWTH_STAGE::WITHERED;
+            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;
+            return GROWTH_STAGE::MATURE->value;
         }
 
         // 正常阶段递增

+ 8 - 0
app/Module/Farm/Dtos/CropInfoDto.php

@@ -66,6 +66,13 @@ class CropInfoDto
      */
     public $growthStageName;
 
+    /**
+     * 当前阶段开始时间
+     *
+     * @var \Carbon\Carbon|null
+     */
+    public $stageStartTime;
+
     /**
      * 当前阶段结束时间
      *
@@ -105,6 +112,7 @@ class CropInfoDto
         $dto->plantTime = $crop->plant_time;
         $dto->growthStage = $crop->growth_stage;
 //        $dto->growthStageName = $crop->growth_stage->name;
+        $dto->stageStartTime = $crop->stage_start_time ? $crop->stage_start_time : null;
         $dto->stageEndTime = $crop->stage_end_time ? $crop->stage_end_time : null;
         $dto->disasters = $crop->disasters;
         $dto->fertilized = $crop->fertilized;

+ 9 - 3
app/Module/Farm/Logics/CropLogic.php

@@ -120,6 +120,7 @@ class CropLogic
             $crop->seed_id = $seedId;
             $crop->plant_time = now();
             $crop->growth_stage = GROWTH_STAGE::SEED->value;
+            $crop->stage_start_time = now(); // 设置当前阶段开始时间
             $crop->stage_end_time = now()->addSeconds($seed->seed_time);
             $crop->disasters = [];
             $crop->fertilized = false;
@@ -323,7 +324,8 @@ class CropLogic
                         'crop_id' => $crop->id,
                         'reduced_time' => $reducedTime,
                         'original_end_time' => $endTime->toDateTimeString(),
-                        'new_end_time' => $crop->stage_end_time->toDateTimeString()
+                        'new_end_time' => $crop->stage_end_time->toDateTimeString(),
+                        'stage_start_time' => $crop->stage_start_time->toDateTimeString()
                     ]);
                 }
             }
@@ -556,6 +558,7 @@ class CropLogic
 
             // 更新作物信息
             $crop->growth_stage = $newStage;
+            $crop->stage_start_time = now(); // 设置新阶段的开始时间
             $crop->stage_end_time = $stageEndTime;
             $crop->fertilized = false; // 重置施肥状态
             $crop->save();
@@ -599,8 +602,11 @@ class CropLogic
             $matureDuration = 24 * 60 * 60;
 
             // 如果成熟期已经超过指定时间,则进入枯萎期
-            if ($crop->stage_end_time && now()->diffInSeconds($crop->stage_end_time->copy()->subSeconds($matureDuration)) > $matureDuration) {
-                return GROWTH_STAGE::WITHERED->value;
+            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;

+ 3 - 1
app/Module/Farm/Models/FarmCrop.php

@@ -9,7 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
 /**
  * 作物信息模型
  *
- * field start 
+ * field start
  * @property  int  $id  主键ID
  * @property  int  $land_id  土地ID
  * @property  int  $user_id  用户ID
@@ -44,6 +44,7 @@ class FarmCrop extends Model
         'seed_id',
         'plant_time',
         'growth_stage',
+        'stage_start_time',
         'stage_end_time',
         'disasters',
         'fertilized',
@@ -56,6 +57,7 @@ class FarmCrop extends Model
      */
     protected $dates = [
         'plant_time',
+        'stage_start_time',
         'stage_end_time',
         'created_at',
         'updated_at',