|
|
@@ -39,13 +39,14 @@ class PetAutoSkillLogic
|
|
|
'user_id' => $userId
|
|
|
]);
|
|
|
|
|
|
- // 首先清理所有枯萎的作物(不使用道具)
|
|
|
+ // 首先清理所有已存在的枯萎作物(不使用道具)
|
|
|
$witheredClearCount = $this->clearAllWitheredCrops($userId);
|
|
|
|
|
|
// 获取用户所有可收获的土地
|
|
|
$harvestableLands = LandService::getHarvestableLands($userId);
|
|
|
|
|
|
$harvestCount = 0;
|
|
|
+ $autoClearedCount = 0;
|
|
|
$harvestResults = [];
|
|
|
|
|
|
if (!$harvestableLands->isEmpty()) {
|
|
|
@@ -67,15 +68,24 @@ class PetAutoSkillLogic
|
|
|
'land_id' => $land->id
|
|
|
]);
|
|
|
|
|
|
- // 收获后自动铲除枯萎的作物
|
|
|
- $clearResult = $this->autoClearWitheredCrop($userId, $land->id);
|
|
|
- if ($clearResult) {
|
|
|
+ // 收获后该土地的作物会自动变成枯萎状态,立即铲除
|
|
|
+ $clearResult = $this->autoClearWitheredCropAfterHarvest($userId, $land->id);
|
|
|
+ if ($clearResult['success']) {
|
|
|
$harvestResults[count($harvestResults) - 1]['auto_cleared'] = true;
|
|
|
+ $autoClearedCount++;
|
|
|
|
|
|
- Log::info('自动铲除枯萎作物成功', [
|
|
|
+ Log::info('自动铲除收获后枯萎作物成功', [
|
|
|
'user_id' => $userId,
|
|
|
'pet_id' => $pet->id,
|
|
|
- 'land_id' => $land->id
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'crop_id' => $clearResult['crop_id']
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ Log::warning('自动铲除收获后枯萎作物失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'reason' => $clearResult['reason']
|
|
|
]);
|
|
|
}
|
|
|
} else {
|
|
|
@@ -89,9 +99,6 @@ class PetAutoSkillLogic
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 统计自动铲除的数量
|
|
|
- $autoClearedCount = array_sum(array_column($harvestResults, 'auto_cleared'));
|
|
|
-
|
|
|
// 记录统计信息
|
|
|
$this->recordSkillStatistics($activeSkill, 'auto_harvest', [
|
|
|
'harvest_count' => $harvestCount,
|
|
|
@@ -784,6 +791,8 @@ class PetAutoSkillLogic
|
|
|
/**
|
|
|
* 清理所有枯萎的作物(不使用道具)
|
|
|
*
|
|
|
+ * 直接查找枯萎状态的作物,而不是通过土地状态
|
|
|
+ *
|
|
|
* @param int $userId 用户ID
|
|
|
* @return int 清理的数量
|
|
|
* @throws \Exception
|
|
|
@@ -793,25 +802,29 @@ class PetAutoSkillLogic
|
|
|
// 检查事务是否已开启
|
|
|
\UCore\Db\Helper::check_tr();
|
|
|
|
|
|
- // 获取所有枯萎状态的土地
|
|
|
- $witheredLands = \App\Module\Farm\Models\FarmLand::where('user_id', $userId)
|
|
|
- ->where('status', \App\Module\Farm\Enums\LAND_STATUS::WITHERED->value)
|
|
|
+ // 直接查找所有枯萎状态的作物
|
|
|
+ $witheredCrops = \App\Module\Farm\Models\FarmCrop::whereHas('land', function($query) use ($userId) {
|
|
|
+ $query->where('user_id', $userId);
|
|
|
+ })
|
|
|
+ ->where('growth_stage', \App\Module\Farm\Enums\GROWTH_STAGE::WITHERED->value)
|
|
|
->get();
|
|
|
|
|
|
$clearedCount = 0;
|
|
|
|
|
|
- foreach ($witheredLands as $land) {
|
|
|
- $cleared = $this->autoClearWitheredCrop($userId, $land->id);
|
|
|
- if ($cleared) {
|
|
|
+ foreach ($witheredCrops as $crop) {
|
|
|
+ $clearResult = $this->autoClearWitheredCrop($userId, $crop->land_id);
|
|
|
+ if ($clearResult) {
|
|
|
$clearedCount++;
|
|
|
Log::info('自动清理枯萎作物成功', [
|
|
|
'user_id' => $userId,
|
|
|
- 'land_id' => $land->id
|
|
|
+ 'land_id' => $crop->land_id,
|
|
|
+ 'crop_id' => $crop->id
|
|
|
]);
|
|
|
} else {
|
|
|
Log::warning('自动清理枯萎作物失败', [
|
|
|
'user_id' => $userId,
|
|
|
- 'land_id' => $land->id,
|
|
|
+ 'land_id' => $crop->land_id,
|
|
|
+ 'crop_id' => $crop->id,
|
|
|
'error' => '清理操作返回失败'
|
|
|
]);
|
|
|
}
|
|
|
@@ -821,7 +834,7 @@ class PetAutoSkillLogic
|
|
|
Log::info('批量清理枯萎作物完成', [
|
|
|
'user_id' => $userId,
|
|
|
'cleared_count' => $clearedCount,
|
|
|
- 'total_withered_lands' => $witheredLands->count()
|
|
|
+ 'total_withered_crops' => $witheredCrops->count()
|
|
|
]);
|
|
|
}
|
|
|
|
|
|
@@ -1080,4 +1093,71 @@ class PetAutoSkillLogic
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 收获后自动铲除枯萎的作物
|
|
|
+ *
|
|
|
+ * 收获后作物会自动变成枯萎状态,此方法专门处理收获后的枯萎作物铲除
|
|
|
+ * 直接检查作物状态,而不是土地状态
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int $landId 土地ID
|
|
|
+ * @return array 返回操作结果 ['success' => bool, 'reason' => string, 'crop_id' => int|null]
|
|
|
+ */
|
|
|
+ protected function autoClearWitheredCropAfterHarvest(int $userId, int $landId): array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 获取土地信息
|
|
|
+ $land = \App\Module\Farm\Models\FarmLand::where('id', $landId)
|
|
|
+ ->where('user_id', $userId)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (!$land) {
|
|
|
+ return ['success' => false, 'reason' => '土地不存在', 'crop_id' => null];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取土地上的作物(直接检查作物状态,不依赖土地状态)
|
|
|
+ $crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $landId)->first();
|
|
|
+ if (!$crop) {
|
|
|
+ return ['success' => false, 'reason' => '土地上没有作物', 'crop_id' => null];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查作物是否为枯萎状态
|
|
|
+ $cropStageValue = is_object($crop->growth_stage) ? $crop->growth_stage->value : $crop->growth_stage;
|
|
|
+ if ($cropStageValue !== \App\Module\Farm\Enums\GROWTH_STAGE::WITHERED->value) {
|
|
|
+ return ['success' => false, 'reason' => '作物不是枯萎状态,当前状态: ' . $cropStageValue, 'crop_id' => $crop->id];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 调用农场服务铲除作物(宠物自动铲除,工具ID为0)
|
|
|
+ $result = \App\Module\Farm\Services\CropService::removeCrop($userId, $landId, 0);
|
|
|
+
|
|
|
+ if ($result['success']) {
|
|
|
+ return [
|
|
|
+ 'success' => true,
|
|
|
+ 'reason' => '宠物自动铲除枯萎作物成功',
|
|
|
+ 'crop_id' => $crop->id
|
|
|
+ ];
|
|
|
+ } else {
|
|
|
+ return [
|
|
|
+ 'success' => false,
|
|
|
+ 'reason' => '铲除作物失败',
|
|
|
+ 'crop_id' => $crop->id
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('宠物自动铲除收获后枯萎作物失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $landId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'success' => false,
|
|
|
+ 'reason' => '铲除作物异常: ' . $e->getMessage(),
|
|
|
+ 'crop_id' => null
|
|
|
+ ];
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|