任务时间: 2025年05月26日 18:41
任务类型: Bug修复
涉及模块: Farm
施肥操作失败,错误日志显示:
[2025-05-26T17:40:10.489443+08:00] laravel.ERROR: 施肥失败:使用化肥失败 {"exception":"[object] (UCore\\Exception\\LogicException(code: 0): 施肥失败:使用化肥失败 at /var/www/html/app/Module/AppGame/Handler/Land/FertilizerHandler.php:77)"} []
通过代码分析,发现问题出现在CropLogic::useFertilizer方法中:
问题位置: app/Module/Farm/Logics/CropLogic.php 第377行
问题代码:
$crop->stage_end_time = $endTime->subSeconds($reducedTime);
问题原因:
$endTimesubSeconds()方法会修改原始对象,导致后续使用时出现问题问题位置: app/Module/Farm/Logics/CropLogic.php calculateNextStage方法
问题代码:
if ($currentStage === GROWTH_STAGE::MATURE->value) {
// ...
return $stageMap[$currentStage->value()] ?? GROWTH_STAGE::WITHERED->value;
问题原因:
$currentStage可能是枚举对象或整数值->value()方法可能导致错误修复前:
$crop->stage_end_time = $endTime->subSeconds($reducedTime);
修复后:
// 使用copy()方法创建副本,避免修改原始对象
$newEndTime = $endTime->copy()->subSeconds($reducedTime);
$crop->stage_end_time = $newEndTime;
修复效果:
新增逻辑:
if ($remainingTime > 0) {
// 正常处理
} else {
Log::warning('作物已经到达或超过结束时间,无法减少生长时间', [
'crop_id' => $crop->id,
'current_time' => $currentTime->toDateTimeString(),
'stage_end_time' => $endTime->toDateTimeString(),
'remaining_time' => $remainingTime
]);
}
修复效果:
修复前:
if ($currentStage === GROWTH_STAGE::MATURE->value) {
// ...
return $stageMap[$currentStage->value()] ?? GROWTH_STAGE::WITHERED->value;
修复后:
$currentStageValue = is_object($currentStage) ? $currentStage->value : $currentStage;
if ($currentStageValue === GROWTH_STAGE::MATURE->value) {
// ...
return $stageMap[$currentStageValue] ?? GROWTH_STAGE::WITHERED->value;
修复效果:
is_object()检查是否为枚举对象copy()方法本次修复解决了施肥操作中的关键问题:
copy()方法避免修改原始对象这些修复提高了施肥功能的稳定性和可靠性,避免了因时间计算错误导致的系统异常。