在宠物自动收获技能中增加自动铲除枯萎作物的功能,收获后自动清理枯萎作物使土地变为空闲状态
在前面的修复中,我们让作物收获后进入枯萎状态而不是直接消失。但这对宠物的自动收获技能产生了影响:
// 调用收获服务
$result = CropService::harvestCrop($userId, $land->id);
if ($result instanceof Res && !$result->error) {
$harvestCount++;
$harvestResults[] = [
'land_id' => $land->id,
'success' => true,
'auto_cleared' => false
];
Log::info('自动收菜成功', [
'user_id' => $userId,
'pet_id' => $pet->id,
'land_id' => $land->id
]);
// 收获后自动铲除枯萎的作物
$clearResult = $this->autoClearWitheredCrop($userId, $land->id);
if ($clearResult) {
$harvestResults[count($harvestResults) - 1]['auto_cleared'] = true;
Log::info('自动铲除枯萎作物成功', [
'user_id' => $userId,
'pet_id' => $pet->id,
'land_id' => $land->id
]);
}
}
// 统计自动铲除的数量
$autoClearedCount = array_sum(array_column($harvestResults, 'auto_cleared'));
// 记录统计信息
$this->recordSkillStatistics($activeSkill, 'auto_harvest', [
'harvest_count' => $harvestCount,
'auto_cleared_count' => $autoClearedCount,
'total_lands_checked' => $harvestableLands->count(),
'harvest_results' => $harvestResults
]);
protected function autoClearWitheredCrop(int $userId, int $landId): bool
{
try {
// 获取土地信息
$land = \App\Module\Farm\Models\FarmLand::where('id', $landId)
->where('user_id', $userId)
->first();
if (!$land) {
return false;
}
// 检查土地状态是否为枯萎状态
if ($land->status !== \App\Module\Farm\Enums\LAND_STATUS::WITHERED->value) {
return false;
}
// 获取土地上的作物
$crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $landId)->first();
if (!$crop) {
// 如果没有作物但土地状态是枯萎,修正土地状态为空闲
$land->status = \App\Module\Farm\Enums\LAND_STATUS::IDLE->value;
$land->save();
return true;
}
// 检查作物是否为枯萎状态
if ($crop->growth_stage !== \App\Module\Farm\Enums\GROWTH_STAGE::WITHERED->value) {
return false;
}
// 调用农场服务铲除作物
$result = \App\Module\Farm\Services\CropService::removeCrop($userId, $landId);
if ($result) {
Log::info('宠物自动铲除枯萎作物成功', [
'user_id' => $userId,
'land_id' => $landId,
'crop_id' => $crop->id
]);
return true;
}
return false;
} catch (\Exception $e) {
Log::warning('宠物自动铲除枯萎作物失败', [
'user_id' => $userId,
'land_id' => $landId,
'error' => $e->getMessage()
]);
return false;
}
}
app/Module/Pet/Logic/PetAutoSkillLogic.phpforeach ($harvestableLands as $land) {
try {
DB::beginTransaction();
// 调用收获服务
$result = CropService::harvestCrop($userId, $land->id);
if ($result instanceof Res && !$result->error) {
$harvestCount++;
$harvestResults[] = [
'land_id' => $land->id,
'success' => true,
'auto_cleared' => false
];
Log::info('自动收菜成功', [
'user_id' => $userId,
'pet_id' => $pet->id,
'land_id' => $land->id
]);
// 收获后自动铲除枯萎的作物
$clearResult = $this->autoClearWitheredCrop($userId, $land->id);
if ($clearResult) {
$harvestResults[count($harvestResults) - 1]['auto_cleared'] = true;
Log::info('自动铲除枯萎作物成功', [
'user_id' => $userId,
'pet_id' => $pet->id,
'land_id' => $land->id
]);
}
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
Log::warning('自动收菜失败', [
'user_id' => $userId,
'pet_id' => $pet->id,
'land_id' => $land->id,
'error' => $e->getMessage()
]);
}
}
// 统计自动铲除的数量
$autoClearedCount = array_sum(array_column($harvestResults, 'auto_cleared'));
// 记录统计信息
$this->recordSkillStatistics($activeSkill, 'auto_harvest', [
'harvest_count' => $harvestCount,
'auto_cleared_count' => $autoClearedCount,
'total_lands_checked' => $harvestableLands->count(),
'harvest_results' => $harvestResults
]);
Log::info('自动收菜技能处理完成', [
'active_skill_id' => $activeSkill->id,
'pet_id' => $pet->id,
'user_id' => $userId,
'harvest_count' => $harvestCount,
'auto_cleared_count' => $autoClearedCount,
'total_lands' => $harvestableLands->count()
]);
protected function autoClearWitheredCrop(int $userId, int $landId): bool
{
// 完整的自动铲除逻辑
// 包括状态检查、服务调用、错误处理等
}
{
"action_type": "auto_harvest",
"timestamp": "2024-12-30 20:45:00",
"data": {
"harvest_count": 5,
"auto_cleared_count": 5,
"total_lands_checked": 8,
"harvest_results": [
{
"land_id": 1,
"success": true,
"auto_cleared": true
},
{
"land_id": 2,
"success": true,
"auto_cleared": true
}
]
}
}
✅ 已完成
app/Module/Pet/Logic/PetAutoSkillLogic.php - 主要修改文件