|
|
@@ -8,6 +8,7 @@ use App\Module\Farm\Services\LandService;
|
|
|
use App\Module\GameItems\Services\ItemService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use UCore\Dto\Res;
|
|
|
|
|
|
/**
|
|
|
* 宠物自动技能处理逻辑
|
|
|
@@ -55,11 +56,12 @@ class PetAutoSkillLogic
|
|
|
// 调用收获服务
|
|
|
$result = CropService::harvestCrop($userId, $land->id);
|
|
|
|
|
|
- if ($result && !$result->error) {
|
|
|
+ if ($result instanceof Res && !$result->error) {
|
|
|
$harvestCount++;
|
|
|
$harvestResults[] = [
|
|
|
'land_id' => $land->id,
|
|
|
- 'result' => $result->data
|
|
|
+ 'success' => true,
|
|
|
+ 'auto_cleared' => false
|
|
|
];
|
|
|
|
|
|
Log::info('自动收菜成功', [
|
|
|
@@ -67,6 +69,18 @@ class PetAutoSkillLogic
|
|
|
'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();
|
|
|
@@ -82,9 +96,13 @@ class PetAutoSkillLogic
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ // 统计自动铲除的数量
|
|
|
+ $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
|
|
|
]);
|
|
|
@@ -94,6 +112,7 @@ class PetAutoSkillLogic
|
|
|
'pet_id' => $pet->id,
|
|
|
'user_id' => $userId,
|
|
|
'harvest_count' => $harvestCount,
|
|
|
+ 'auto_cleared_count' => $autoClearedCount,
|
|
|
'total_lands' => $harvestableLands->count()
|
|
|
]);
|
|
|
|
|
|
@@ -486,6 +505,344 @@ class PetAutoSkillLogic
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 处理自动除草技能
|
|
|
+ *
|
|
|
+ * @param PetActiveSkill $activeSkill 激活的技能
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function processAutoWeeding(PetActiveSkill $activeSkill): void
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pet = $activeSkill->pet;
|
|
|
+ $userId = $pet->user_id;
|
|
|
+
|
|
|
+ Log::info('开始处理自动除草技能', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'user_id' => $userId
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 获取用户所有有作物的土地
|
|
|
+ $landsWithCrops = LandService::getLandsWithCrops($userId);
|
|
|
+
|
|
|
+ if ($landsWithCrops->isEmpty()) {
|
|
|
+ Log::info('没有种植作物的土地', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id
|
|
|
+ ]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $weedingCount = 0;
|
|
|
+ $disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::WEED->value;
|
|
|
+
|
|
|
+ foreach ($landsWithCrops as $land) {
|
|
|
+ try {
|
|
|
+ // 检查土地是否有杂草灾害
|
|
|
+ $hasWeedDisaster = $this->checkSpecificDisaster($land, $disasterType);
|
|
|
+
|
|
|
+ if ($hasWeedDisaster) {
|
|
|
+ // 自动清除杂草灾害
|
|
|
+ $cleared = $this->autoClearSpecificDisaster($userId, $land, $disasterType);
|
|
|
+
|
|
|
+ if ($cleared) {
|
|
|
+ $weedingCount++;
|
|
|
+ Log::info('自动除草成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::warning('自动除草处理失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录统计信息
|
|
|
+ $this->recordSkillStatistics($activeSkill, 'auto_weeding', [
|
|
|
+ 'weeding_count' => $weedingCount,
|
|
|
+ 'total_lands_checked' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ Log::info('自动除草技能处理完成', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'weeding_count' => $weedingCount,
|
|
|
+ 'total_lands' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('处理自动除草技能失败', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理自动浇水技能
|
|
|
+ *
|
|
|
+ * @param PetActiveSkill $activeSkill 激活的技能
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function processAutoWatering(PetActiveSkill $activeSkill): void
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pet = $activeSkill->pet;
|
|
|
+ $userId = $pet->user_id;
|
|
|
+
|
|
|
+ Log::info('开始处理自动浇水技能', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'user_id' => $userId
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 获取用户所有有作物的土地
|
|
|
+ $landsWithCrops = LandService::getLandsWithCrops($userId);
|
|
|
+
|
|
|
+ if ($landsWithCrops->isEmpty()) {
|
|
|
+ Log::info('没有种植作物的土地', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id
|
|
|
+ ]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $wateringCount = 0;
|
|
|
+ $disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::DROUGHT->value;
|
|
|
+
|
|
|
+ foreach ($landsWithCrops as $land) {
|
|
|
+ try {
|
|
|
+ // 检查土地是否有干旱灾害
|
|
|
+ $hasDroughtDisaster = $this->checkSpecificDisaster($land, $disasterType);
|
|
|
+
|
|
|
+ if ($hasDroughtDisaster) {
|
|
|
+ // 自动清除干旱灾害
|
|
|
+ $cleared = $this->autoClearSpecificDisaster($userId, $land, $disasterType);
|
|
|
+
|
|
|
+ if ($cleared) {
|
|
|
+ $wateringCount++;
|
|
|
+ Log::info('自动浇水成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::warning('自动浇水处理失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录统计信息
|
|
|
+ $this->recordSkillStatistics($activeSkill, 'auto_watering', [
|
|
|
+ 'watering_count' => $wateringCount,
|
|
|
+ 'total_lands_checked' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ Log::info('自动浇水技能处理完成', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'watering_count' => $wateringCount,
|
|
|
+ 'total_lands' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('处理自动浇水技能失败', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 处理自动杀虫技能
|
|
|
+ *
|
|
|
+ * @param PetActiveSkill $activeSkill 激活的技能
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function processAutoPestControl(PetActiveSkill $activeSkill): void
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $pet = $activeSkill->pet;
|
|
|
+ $userId = $pet->user_id;
|
|
|
+
|
|
|
+ Log::info('开始处理自动杀虫技能', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'user_id' => $userId
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 获取用户所有有作物的土地
|
|
|
+ $landsWithCrops = LandService::getLandsWithCrops($userId);
|
|
|
+
|
|
|
+ if ($landsWithCrops->isEmpty()) {
|
|
|
+ Log::info('没有种植作物的土地', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id
|
|
|
+ ]);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ $pestControlCount = 0;
|
|
|
+ $disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value;
|
|
|
+
|
|
|
+ foreach ($landsWithCrops as $land) {
|
|
|
+ try {
|
|
|
+ // 检查土地是否有虫害灾害
|
|
|
+ $hasPestDisaster = $this->checkSpecificDisaster($land, $disasterType);
|
|
|
+
|
|
|
+ if ($hasPestDisaster) {
|
|
|
+ // 自动清除虫害灾害
|
|
|
+ $cleared = $this->autoClearSpecificDisaster($userId, $land, $disasterType);
|
|
|
+
|
|
|
+ if ($cleared) {
|
|
|
+ $pestControlCount++;
|
|
|
+ Log::info('自动杀虫成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::warning('自动杀虫处理失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录统计信息
|
|
|
+ $this->recordSkillStatistics($activeSkill, 'auto_pest_control', [
|
|
|
+ 'pest_control_count' => $pestControlCount,
|
|
|
+ 'total_lands_checked' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ Log::info('自动杀虫技能处理完成', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'pet_id' => $pet->id,
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'pest_control_count' => $pestControlCount,
|
|
|
+ 'total_lands' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('处理自动杀虫技能失败', [
|
|
|
+ 'active_skill_id' => $activeSkill->id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString()
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查特定类型的灾害
|
|
|
+ *
|
|
|
+ * @param mixed $land 土地对象
|
|
|
+ * @param int $disasterType 灾害类型
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ protected function checkSpecificDisaster($land, int $disasterType): bool
|
|
|
+ {
|
|
|
+ // 检查土地状态是否为灾害状态
|
|
|
+ if ($land->status !== \App\Module\Farm\Enums\LAND_STATUS::DISASTER->value) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取土地上的作物
|
|
|
+ $crop = $land->crop;
|
|
|
+ if (!$crop) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查作物是否有指定类型的活跃灾害
|
|
|
+ $disasters = $crop->disasters ?? [];
|
|
|
+
|
|
|
+ foreach ($disasters as $disaster) {
|
|
|
+ if (($disaster['status'] ?? '') === 'active' && ($disaster['type'] ?? 0) == $disasterType) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自动清除特定类型的灾害
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param mixed $land 土地对象
|
|
|
+ * @param int $disasterType 灾害类型
|
|
|
+ * @return bool
|
|
|
+ */
|
|
|
+ protected function autoClearSpecificDisaster(int $userId, $land, int $disasterType): bool
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ // 获取对应的清除道具
|
|
|
+ $clearItem = $this->getDisasterClearItem($userId, $disasterType);
|
|
|
+
|
|
|
+ if (!$clearItem) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 调用农场服务清除灾害
|
|
|
+ $result = \App\Module\Farm\Services\CropService::clearDisaster($userId, $land->id, $disasterType);
|
|
|
+
|
|
|
+ if ($result) {
|
|
|
+ // 消耗道具
|
|
|
+ \App\Module\GameItems\Services\ItemService::consumeItem(
|
|
|
+ $userId,
|
|
|
+ $clearItem['item_id'],
|
|
|
+ null,
|
|
|
+ 1,
|
|
|
+ ['source' => 'pet_auto_specific_disaster_clear']
|
|
|
+ );
|
|
|
+
|
|
|
+ Log::info('宠物自动清除特定灾害成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'disaster_type' => $disasterType,
|
|
|
+ 'item_id' => $clearItem['item_id']
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::warning('宠物自动清除特定灾害失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'disaster_type' => $disasterType,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 记录技能统计信息
|
|
|
*
|
|
|
@@ -516,4 +873,66 @@ class PetAutoSkillLogic
|
|
|
$activeSkill->config = $config;
|
|
|
$activeSkill->save();
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自动铲除枯萎的作物
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int $landId 土地ID
|
|
|
+ * @return bool 是否成功铲除
|
|
|
+ */
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|