|
|
@@ -17,6 +17,7 @@ use App\Module\Farm\Models\FarmLand;
|
|
|
use App\Module\Farm\Models\FarmSeed;
|
|
|
use App\Module\Farm\Models\FarmSeedOutput;
|
|
|
use App\Module\Farm\Models\FarmSowLog;
|
|
|
+use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
|
|
|
use App\Module\GameItems\Services\ItemService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
@@ -28,6 +29,7 @@ use UCore\Dto\Res;
|
|
|
*/
|
|
|
class CropLogic
|
|
|
{
|
|
|
+
|
|
|
/**
|
|
|
* 获取作物信息
|
|
|
*
|
|
|
@@ -47,8 +49,8 @@ class CropLogic
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取作物信息失败', [
|
|
|
'crop_id' => $cropId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return null;
|
|
|
@@ -74,8 +76,8 @@ class CropLogic
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('获取土地作物信息失败', [
|
|
|
'land_id' => $landId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return null;
|
|
|
@@ -121,26 +123,26 @@ class CropLogic
|
|
|
$seedId = $seed->id;
|
|
|
|
|
|
// 创建作物记录
|
|
|
- $crop = new FarmCrop();
|
|
|
- $crop->land_id = $landId;
|
|
|
- $crop->user_id = $userId;
|
|
|
- $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;
|
|
|
- $crop->last_disaster_check_time = null; // 初始化灾害检查时间
|
|
|
- $crop->can_disaster = false; // 种子期不能产生灾害
|
|
|
+ $crop = new FarmCrop();
|
|
|
+ $crop->land_id = $landId;
|
|
|
+ $crop->user_id = $userId;
|
|
|
+ $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;
|
|
|
+ $crop->last_disaster_check_time = null; // 初始化灾害检查时间
|
|
|
+ $crop->can_disaster = false; // 种子期不能产生灾害
|
|
|
$crop->save();
|
|
|
|
|
|
// 创建种植日志
|
|
|
- $sowLog = new FarmSowLog();
|
|
|
- $sowLog->user_id = $userId;
|
|
|
- $sowLog->land_id = $landId;
|
|
|
- $sowLog->crop_id = $crop->id;
|
|
|
- $sowLog->seed_id = $seedId;
|
|
|
+ $sowLog = new FarmSowLog();
|
|
|
+ $sowLog->user_id = $userId;
|
|
|
+ $sowLog->land_id = $landId;
|
|
|
+ $sowLog->crop_id = $crop->id;
|
|
|
+ $sowLog->seed_id = $seedId;
|
|
|
$sowLog->sow_time = now();
|
|
|
$sowLog->save();
|
|
|
|
|
|
@@ -153,15 +155,15 @@ class CropLogic
|
|
|
event(new CropPlantedEvent($userId, $land, $crop));
|
|
|
|
|
|
Log::info('作物种植成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
- 'seed_id' => $seedId,
|
|
|
- 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
+ 'seed_id' => $seedId,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
'sow_log_id' => $sowLog->id
|
|
|
]);
|
|
|
|
|
|
return [
|
|
|
- 'crop' => CropInfoDto::fromModel($crop),
|
|
|
+ 'crop' => CropInfoDto::fromModel($crop),
|
|
|
'log_id' => $sowLog->id
|
|
|
];
|
|
|
} catch (\Exception $e) {
|
|
|
@@ -172,8 +174,8 @@ class CropLogic
|
|
|
'user_id' => $userId,
|
|
|
'land_id' => $landId,
|
|
|
'seed_id' => $seedId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return null;
|
|
|
@@ -263,22 +265,22 @@ class CropLogic
|
|
|
]);
|
|
|
|
|
|
// 创建收获记录
|
|
|
- $harvestLog = new FarmHarvestLog();
|
|
|
- $harvestLog->user_id = $userId;
|
|
|
- $harvestLog->land_id = $landId;
|
|
|
- $harvestLog->crop_id = $crop->id;
|
|
|
- $harvestLog->seed_id = $seed->id;
|
|
|
+ $harvestLog = new FarmHarvestLog();
|
|
|
+ $harvestLog->user_id = $userId;
|
|
|
+ $harvestLog->land_id = $landId;
|
|
|
+ $harvestLog->crop_id = $crop->id;
|
|
|
+ $harvestLog->seed_id = $seed->id;
|
|
|
$harvestLog->output_amount = $outputAmount;
|
|
|
- $harvestLog->harvest_time = now();
|
|
|
- $harvestLog->created_at = now();
|
|
|
+ $harvestLog->harvest_time = now();
|
|
|
+ $harvestLog->created_at = now();
|
|
|
$harvestLog->save();
|
|
|
|
|
|
// 收获后作物进入枯萎期,而不是直接删除
|
|
|
- $oldStage = $crop->growth_stage;
|
|
|
- $crop->growth_stage = GROWTH_STAGE::WITHERED;
|
|
|
+ $oldStage = $crop->growth_stage;
|
|
|
+ $crop->growth_stage = GROWTH_STAGE::WITHERED;
|
|
|
$crop->stage_start_time = now();
|
|
|
- $crop->stage_end_time = null; // 枯萎期没有结束时间
|
|
|
- $crop->fertilized = false; // 重置施肥状态
|
|
|
+ $crop->stage_end_time = null; // 枯萎期没有结束时间
|
|
|
+ $crop->fertilized = false; // 重置施肥状态
|
|
|
$crop->save();
|
|
|
|
|
|
// 更新土地状态为枯萎状态
|
|
|
@@ -290,27 +292,27 @@ class CropLogic
|
|
|
event(new CropGrowthStageChangedEvent($userId, $crop, $oldStage->value, GROWTH_STAGE::WITHERED->value));
|
|
|
|
|
|
|
|
|
-
|
|
|
-
|
|
|
// 触发作物收获事件
|
|
|
event(new CropHarvestedEvent($userId, $land, $crop, $harvestLog, $outputItemId, $outputAmount));
|
|
|
|
|
|
Log::info('作物收获成功,进入枯萎期', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'seed_id' => $seed->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'seed_id' => $seed->id,
|
|
|
'output_item_id' => $outputItemId,
|
|
|
- 'output_amount' => $outputAmount,
|
|
|
+ 'output_amount' => $outputAmount,
|
|
|
'harvest_log_id' => $harvestLog->id,
|
|
|
- 'old_stage' => $oldStage->value,
|
|
|
- 'new_stage' => GROWTH_STAGE::WITHERED->value,
|
|
|
- 'land_status' => LAND_STATUS::WITHERED->value
|
|
|
+ 'old_stage' => $oldStage->value,
|
|
|
+ 'new_stage' => GROWTH_STAGE::WITHERED->value,
|
|
|
+ 'land_status' => LAND_STATUS::WITHERED->value
|
|
|
]);
|
|
|
// 物品入包
|
|
|
- ItemService::addItem($userId, $outputItemId, $outputAmount,[
|
|
|
- 'source'=>'FarmHarve',
|
|
|
- 'FarmHarvestLog'=>$harvestLog->id
|
|
|
+ ItemService::addItem($userId, $outputItemId, $outputAmount, [
|
|
|
+ 'source' => REWARD_SOURCE_TYPE::FARM_HARVEST->valueString(),
|
|
|
+ 'source_type' => REWARD_SOURCE_TYPE::FARM_HARVEST->valueString(),
|
|
|
+ 'source_id' => $harvestLog->id,
|
|
|
+ 'FarmHarvestLog' => $harvestLog->id
|
|
|
]);
|
|
|
|
|
|
|
|
|
@@ -322,8 +324,8 @@ class CropLogic
|
|
|
Log::error('作物收获失败', [
|
|
|
'user_id' => $userId,
|
|
|
'land_id' => $landId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return Res::error('');
|
|
|
@@ -362,30 +364,30 @@ class CropLogic
|
|
|
|
|
|
// 根据 cropGrowthTime 参数减少当前阶段时间
|
|
|
if ($crop->stage_end_time) {
|
|
|
- $currentTime = now();
|
|
|
- $endTime = $crop->stage_end_time;
|
|
|
+ $currentTime = now();
|
|
|
+ $endTime = $crop->stage_end_time;
|
|
|
$remainingTime = $currentTime->diffInSeconds($endTime, false);
|
|
|
|
|
|
if ($remainingTime > 0) {
|
|
|
// 确保减少的时间不超过剩余时间
|
|
|
$reducedTime = min($cropGrowthTime, $remainingTime);
|
|
|
// 使用copy()方法创建副本,避免修改原始对象
|
|
|
- $newEndTime = $endTime->copy()->subSeconds($reducedTime);
|
|
|
+ $newEndTime = $endTime->copy()->subSeconds($reducedTime);
|
|
|
$crop->stage_end_time = $newEndTime;
|
|
|
|
|
|
Log::info('化肥减少生长时间', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'reduced_time' => $reducedTime,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'reduced_time' => $reducedTime,
|
|
|
'original_end_time' => $endTime->format('Y-m-d H:i:s'),
|
|
|
- 'new_end_time' => $crop->stage_end_time->format('Y-m-d H:i:s'),
|
|
|
- 'stage_start_time' => $crop->stage_start_time ? $crop->stage_start_time->format('Y-m-d H:i:s') : null
|
|
|
+ 'new_end_time' => $crop->stage_end_time->format('Y-m-d H:i:s'),
|
|
|
+ 'stage_start_time' => $crop->stage_start_time ? $crop->stage_start_time->format('Y-m-d H:i:s') : null
|
|
|
]);
|
|
|
event(new CropGrowthStageChangedEvent($crop->user_id, $crop, $crop->growth_stage->value, $crop->growth_stage->value));
|
|
|
|
|
|
} else {
|
|
|
Log::warning('作物已经到达或超过结束时间,无法减少生长时间', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'current_time' => $currentTime->format('Y-m-d H:i:s'),
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'current_time' => $currentTime->format('Y-m-d H:i:s'),
|
|
|
'stage_end_time' => $endTime->format('Y-m-d H:i:s'),
|
|
|
'remaining_time' => $remainingTime
|
|
|
]);
|
|
|
@@ -395,10 +397,10 @@ class CropLogic
|
|
|
$crop->save();
|
|
|
|
|
|
Log::info('使用化肥成功', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'user_id' => $crop->user_id,
|
|
|
- 'land_id' => $crop->land_id,
|
|
|
- 'growth_stage' => $crop->growth_stage,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $crop->user_id,
|
|
|
+ 'land_id' => $crop->land_id,
|
|
|
+ 'growth_stage' => $crop->growth_stage,
|
|
|
'stage_end_time' => $crop->stage_end_time
|
|
|
]);
|
|
|
|
|
|
@@ -407,10 +409,10 @@ class CropLogic
|
|
|
]);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('使用化肥失败', [
|
|
|
- 'crop_id' => $cropId,
|
|
|
+ 'crop_id' => $cropId,
|
|
|
'crop_growth_time' => $cropGrowthTime,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return Res::error('使用化肥失败');
|
|
|
@@ -473,28 +475,28 @@ class CropLogic
|
|
|
|
|
|
// 根据 crop_growth_time 参数减少当前阶段时间
|
|
|
if ($crop->stage_end_time) {
|
|
|
- $currentTime = now();
|
|
|
- $endTime = $crop->stage_end_time;
|
|
|
+ $currentTime = now();
|
|
|
+ $endTime = $crop->stage_end_time;
|
|
|
$remainingTime = $currentTime->diffInSeconds($endTime, false);
|
|
|
|
|
|
if ($remainingTime > 0) {
|
|
|
// 确保减少的时间不超过剩余时间
|
|
|
$reducedTime = min($crop_growth_time, $remainingTime);
|
|
|
// 使用copy()方法创建副本,避免修改原始对象
|
|
|
- $newEndTime = $endTime->copy()->subSeconds($reducedTime);
|
|
|
+ $newEndTime = $endTime->copy()->subSeconds($reducedTime);
|
|
|
$crop->stage_end_time = $newEndTime;
|
|
|
|
|
|
Log::info('化肥减少生长时间', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'reduced_time' => $reducedTime,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'reduced_time' => $reducedTime,
|
|
|
'original_end_time' => $endTime->toDateTimeString(),
|
|
|
- 'new_end_time' => $crop->stage_end_time->toDateTimeString(),
|
|
|
- 'stage_start_time' => $crop->stage_start_time->toDateTimeString()
|
|
|
+ 'new_end_time' => $crop->stage_end_time->toDateTimeString(),
|
|
|
+ 'stage_start_time' => $crop->stage_start_time->toDateTimeString()
|
|
|
]);
|
|
|
} else {
|
|
|
Log::warning('作物已经到达或超过结束时间,无法减少生长时间', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'current_time' => $currentTime->toDateTimeString(),
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'current_time' => $currentTime->toDateTimeString(),
|
|
|
'stage_end_time' => $endTime->toDateTimeString(),
|
|
|
'remaining_time' => $remainingTime
|
|
|
]);
|
|
|
@@ -504,22 +506,22 @@ class CropLogic
|
|
|
$crop->save();
|
|
|
|
|
|
Log::info('使用化肥成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'growth_stage' => $crop->growth_stage,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'growth_stage' => $crop->growth_stage,
|
|
|
'stage_end_time' => $crop->stage_end_time
|
|
|
]);
|
|
|
|
|
|
- return Res::success('',[
|
|
|
+ return Res::success('', [
|
|
|
'crop_id' => $crop->id,
|
|
|
]);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('使用化肥失败', [
|
|
|
'user_id' => $userId,
|
|
|
'land_id' => $landId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return Res::error('使用化肥失败');
|
|
|
@@ -559,14 +561,14 @@ class CropLogic
|
|
|
}
|
|
|
|
|
|
// 检查灾害是否存在
|
|
|
- $disasters = $crop->disasters ?? [];
|
|
|
+ $disasters = $crop->disasters ?? [];
|
|
|
$disasterIndex = -1;
|
|
|
- $disasterInfo = null;
|
|
|
+ $disasterInfo = null;
|
|
|
|
|
|
foreach ($disasters as $index => $disaster) {
|
|
|
if (($disaster['type'] ?? 0) == $disasterType && ($disaster['status'] ?? '') === 'active') {
|
|
|
$disasterIndex = $index;
|
|
|
- $disasterInfo = $disaster;
|
|
|
+ $disasterInfo = $disaster;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
@@ -576,9 +578,9 @@ class CropLogic
|
|
|
}
|
|
|
|
|
|
// 更新灾害状态
|
|
|
- $disasters[$disasterIndex]['status'] = 'cleared';
|
|
|
+ $disasters[$disasterIndex]['status'] = 'cleared';
|
|
|
$disasters[$disasterIndex]['cleared_at'] = now()->toDateTimeString();
|
|
|
- $crop->disasters = $disasters;
|
|
|
+ $crop->disasters = $disasters;
|
|
|
|
|
|
// 检查是否还有其他活跃灾害
|
|
|
$hasActiveDisaster = false;
|
|
|
@@ -610,20 +612,20 @@ class CropLogic
|
|
|
event(new DisasterClearedEvent($userId, $crop, $disasterType, $disasterInfo));
|
|
|
|
|
|
Log::info('灾害清理成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
- 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
'disaster_type' => $disasterType
|
|
|
]);
|
|
|
|
|
|
return true;
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('灾害清理失败', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
'disaster_type' => $disasterType,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return false;
|
|
|
@@ -663,14 +665,14 @@ class CropLogic
|
|
|
if (!$crop) {
|
|
|
// 如果没有作物但土地状态不是空闲,修正土地状态
|
|
|
$oldLandStatus = $land->status;
|
|
|
- $land->status = LAND_STATUS::IDLE->value;
|
|
|
+ $land->status = LAND_STATUS::IDLE->value;
|
|
|
$land->updateHasCrop();
|
|
|
$land->save();
|
|
|
|
|
|
// 记录状态变更信息,由调用方处理事件触发
|
|
|
Log::info('土地状态已修正', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
'old_status' => $oldLandStatus,
|
|
|
'new_status' => $land->status
|
|
|
]);
|
|
|
@@ -692,9 +694,9 @@ class CropLogic
|
|
|
// 记录状态变更信息,由调用方处理事件触发和事务提交
|
|
|
|
|
|
Log::info('铲除作物成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $landId,
|
|
|
- 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
'old_status' => $oldLandStatus,
|
|
|
'new_status' => $land->status
|
|
|
]);
|
|
|
@@ -704,8 +706,8 @@ class CropLogic
|
|
|
Log::error('铲除作物失败', [
|
|
|
'user_id' => $userId,
|
|
|
'land_id' => $landId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
throw $e; // 重新抛出异常,由调用方处理事务回滚
|
|
|
@@ -748,10 +750,10 @@ class CropLogic
|
|
|
$stageEndTime = $this->calculateStageEndTime($crop, $newStage);
|
|
|
|
|
|
// 更新作物信息
|
|
|
- $crop->growth_stage = $newStage;
|
|
|
+ $crop->growth_stage = $newStage;
|
|
|
$crop->stage_start_time = now(); // 设置新阶段的开始时间
|
|
|
- $crop->stage_end_time = $stageEndTime;
|
|
|
- $crop->fertilized = false; // 重置施肥状态
|
|
|
+ $crop->stage_end_time = $stageEndTime;
|
|
|
+ $crop->fertilized = false; // 重置施肥状态
|
|
|
|
|
|
// 如果进入发芽期,必须确定最终产出果实ID
|
|
|
if ($newStage === GROWTH_STAGE::SPROUT->value) {
|
|
|
@@ -760,28 +762,28 @@ class CropLogic
|
|
|
|
|
|
// 如果是神秘种子,使用土地影响逻辑
|
|
|
if ($seed && $seed->type == \App\Module\Farm\Enums\SEED_TYPE::MYSTERIOUS->value) {
|
|
|
- $land = $crop->land;
|
|
|
- $mysteryLogic = new \App\Module\Farm\Logics\MysterySeeLLogic();
|
|
|
+ $land = $crop->land;
|
|
|
+ $mysteryLogic = new \App\Module\Farm\Logics\MysterySeeLLogic();
|
|
|
$selectedOutput = $mysteryLogic->selectFinalOutput($seed->id, $land->land_type);
|
|
|
|
|
|
$crop->final_output_item_id = $selectedOutput['item_id'];
|
|
|
|
|
|
Log::info('神秘种子确定最终产出(基于土地影响)', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'user_id' => $crop->user_id,
|
|
|
- 'seed_id' => $seed->id,
|
|
|
- 'land_type' => $land->land_type,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $crop->user_id,
|
|
|
+ 'seed_id' => $seed->id,
|
|
|
+ 'land_type' => $land->land_type,
|
|
|
'final_output_item_id' => $selectedOutput['item_id']
|
|
|
]);
|
|
|
} else {
|
|
|
// 普通种子使用原有逻辑
|
|
|
- $outputInfo = $this->getRandomOutput($crop->seed_id);
|
|
|
+ $outputInfo = $this->getRandomOutput($crop->seed_id);
|
|
|
$crop->final_output_item_id = $outputInfo['item_id'];
|
|
|
|
|
|
Log::info('作物进入发芽期,确定最终产出果实', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'user_id' => $crop->user_id,
|
|
|
- 'seed_id' => $crop->seed_id,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $crop->user_id,
|
|
|
+ 'seed_id' => $crop->seed_id,
|
|
|
'final_output_item_id' => $crop->final_output_item_id
|
|
|
]);
|
|
|
}
|
|
|
@@ -791,11 +793,11 @@ class CropLogic
|
|
|
// 验证:如果进入成熟期但没有final_output_item_id,这是一个严重错误
|
|
|
if ($newStage === GROWTH_STAGE::MATURE->value && !$crop->final_output_item_id) {
|
|
|
Log::error('严重错误:作物进入成熟期但没有确定最终产出果实ID', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'user_id' => $crop->user_id,
|
|
|
- 'seed_id' => $crop->seed_id,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'user_id' => $crop->user_id,
|
|
|
+ 'seed_id' => $crop->seed_id,
|
|
|
'current_stage' => $oldStage,
|
|
|
- 'new_stage' => $newStage
|
|
|
+ 'new_stage' => $newStage
|
|
|
]);
|
|
|
|
|
|
throw new \Exception("作物ID {$crop->id} 进入成熟期但没有确定最终产出果实ID,这是系统错误");
|
|
|
@@ -826,8 +828,8 @@ class CropLogic
|
|
|
$land->save();
|
|
|
|
|
|
Log::info('作物进入枯萎期,更新土地状态', [
|
|
|
- 'crop_id' => $crop->id,
|
|
|
- 'land_id' => $land->id,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
+ 'land_id' => $land->id,
|
|
|
'land_status' => LAND_STATUS::WITHERED->value
|
|
|
]);
|
|
|
}
|
|
|
@@ -837,10 +839,10 @@ class CropLogic
|
|
|
event(new CropGrowthStageChangedEvent($crop->user_id, $crop, $oldStage->value, $newStage));
|
|
|
|
|
|
Log::info('作物生长阶段更新成功', [
|
|
|
- 'crop_id' => $cropId,
|
|
|
- 'user_id' => $crop->user_id,
|
|
|
- 'old_stage' => $oldStage,
|
|
|
- 'new_stage' => $newStage,
|
|
|
+ 'crop_id' => $cropId,
|
|
|
+ 'user_id' => $crop->user_id,
|
|
|
+ 'old_stage' => $oldStage,
|
|
|
+ 'new_stage' => $newStage,
|
|
|
'stage_end_time' => $stageEndTime
|
|
|
]);
|
|
|
|
|
|
@@ -848,8 +850,8 @@ class CropLogic
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('作物生长阶段更新失败', [
|
|
|
'crop_id' => $cropId,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
]);
|
|
|
|
|
|
return false;
|
|
|
@@ -880,15 +882,16 @@ class CropLogic
|
|
|
|
|
|
// 使用阶段映射确定下一个阶段
|
|
|
$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::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, // 枯萎期保持不变
|
|
|
];
|
|
|
|
|
|
// 确保返回整数值
|
|
|
$currentStageValue = is_object($currentStage) ? $currentStage->value : $currentStage;
|
|
|
+
|
|
|
return $stageMap[$currentStageValue] ?? GROWTH_STAGE::WITHERED->value;
|
|
|
}
|
|
|
|
|
|
@@ -949,7 +952,7 @@ class CropLogic
|
|
|
$seed = FarmSeed::find($seedId);
|
|
|
|
|
|
return [
|
|
|
- 'item_id' => $seed->item_id,
|
|
|
+ 'item_id' => $seed->item_id,
|
|
|
'min_amount' => $seed->min_output,
|
|
|
'max_amount' => $seed->max_output,
|
|
|
];
|
|
|
@@ -962,7 +965,7 @@ class CropLogic
|
|
|
$defaultOutput = $outputs->firstWhere('is_default', true);
|
|
|
|
|
|
// 随机选择产出
|
|
|
- $random = mt_rand(1, 100);
|
|
|
+ $random = mt_rand(1, 100);
|
|
|
$cumulativeProbability = 0;
|
|
|
|
|
|
foreach ($outputs as $output) {
|
|
|
@@ -970,7 +973,7 @@ class CropLogic
|
|
|
|
|
|
if ($random <= $cumulativeProbability) {
|
|
|
return [
|
|
|
- 'item_id' => $output->item_id,
|
|
|
+ 'item_id' => $output->item_id,
|
|
|
'min_amount' => $output->min_amount,
|
|
|
'max_amount' => $output->max_amount,
|
|
|
];
|
|
|
@@ -980,7 +983,7 @@ class CropLogic
|
|
|
// 如果随机值超过了所有概率之和,使用默认产出
|
|
|
if ($defaultOutput) {
|
|
|
return [
|
|
|
- 'item_id' => $defaultOutput->item_id,
|
|
|
+ 'item_id' => $defaultOutput->item_id,
|
|
|
'min_amount' => $defaultOutput->min_amount,
|
|
|
'max_amount' => $defaultOutput->max_amount,
|
|
|
];
|
|
|
@@ -990,7 +993,7 @@ class CropLogic
|
|
|
$firstOutput = $outputs->first();
|
|
|
|
|
|
return [
|
|
|
- 'item_id' => $firstOutput->item_id,
|
|
|
+ 'item_id' => $firstOutput->item_id,
|
|
|
'min_amount' => $firstOutput->min_amount,
|
|
|
'max_amount' => $firstOutput->max_amount,
|
|
|
];
|
|
|
@@ -1038,9 +1041,9 @@ class CropLogic
|
|
|
]);
|
|
|
|
|
|
return [
|
|
|
- 'item_id' => $itemId,
|
|
|
- 'min_amount' => 1,
|
|
|
- 'max_amount' => 10,
|
|
|
+ 'item_id' => $targetOutput->item_id,
|
|
|
+ 'min_amount' => $targetOutput->min_amount,
|
|
|
+ 'max_amount' => $targetOutput->max_amount,
|
|
|
];
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
@@ -1175,4 +1178,5 @@ class CropLogic
|
|
|
return $crop->seed->min_output ?? 1;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
}
|