|
|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Module\Pet\Logic;
|
|
|
|
|
|
+use App\Module\Farm\Dtos\LandInfoDto;
|
|
|
use App\Module\Pet\Models\PetActiveSkill;
|
|
|
use App\Module\Farm\Services\CropService;
|
|
|
use App\Module\Farm\Services\LandService;
|
|
|
@@ -246,92 +247,7 @@ class PetAutoSkillLogic
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 处理灾害防护技能
|
|
|
- *
|
|
|
- * @param PetActiveSkill $activeSkill 激活的技能
|
|
|
- * @return void
|
|
|
- */
|
|
|
- public function processDisasterProtection(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
|
|
|
- ]);
|
|
|
-
|
|
|
- // 获取防护的灾害类型
|
|
|
- $protectedTypes = $activeSkill->getConfigValue('protected_types', ['all']);
|
|
|
-
|
|
|
- // 获取用户所有有作物的土地
|
|
|
- $landsWithCrops = LandService::getLandsWithCrops($userId);
|
|
|
-
|
|
|
- if ($landsWithCrops->isEmpty()) {
|
|
|
- Log::info('没有种植作物的土地', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'pet_id' => $pet->id
|
|
|
- ]);
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- $protectionCount = 0;
|
|
|
-
|
|
|
- foreach ($landsWithCrops as $land) {
|
|
|
- try {
|
|
|
- // 检查土地是否有灾害
|
|
|
- $hasDisaster = $this->checkLandDisaster($land, $protectedTypes);
|
|
|
|
|
|
- if ($hasDisaster) {
|
|
|
- // 自动清除灾害(需要消耗相应道具)
|
|
|
- $cleared = $this->autoClearDisaster($userId, $land, $protectedTypes);
|
|
|
-
|
|
|
- if ($cleared) {
|
|
|
- $protectionCount++;
|
|
|
- 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, 'disaster_protection', [
|
|
|
- 'protection_count' => $protectionCount,
|
|
|
- 'total_lands_checked' => $landsWithCrops->count(),
|
|
|
- 'protected_types' => $protectedTypes
|
|
|
- ]);
|
|
|
-
|
|
|
- Log::info('灾害防护技能处理完成', [
|
|
|
- 'active_skill_id' => $activeSkill->id,
|
|
|
- 'pet_id' => $pet->id,
|
|
|
- 'user_id' => $userId,
|
|
|
- 'protection_count' => $protectionCount,
|
|
|
- 'total_lands' => $landsWithCrops->count()
|
|
|
- ]);
|
|
|
-
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('处理灾害防护技能失败', [
|
|
|
- 'active_skill_id' => $activeSkill->id,
|
|
|
- 'error' => $e->getMessage(),
|
|
|
- 'trace' => $e->getTraceAsString()
|
|
|
- ]);
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 获取用户可用的种子
|
|
|
@@ -368,111 +284,9 @@ class PetAutoSkillLogic
|
|
|
return $availableSeeds;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 检查土地是否有灾害
|
|
|
- *
|
|
|
- * @param mixed $land 土地对象
|
|
|
- * @param array $protectedTypes 防护的灾害类型
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- protected function checkLandDisaster($land, array $protectedTypes): 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') {
|
|
|
- $disasterType = $disaster['type'] ?? 0;
|
|
|
|
|
|
- // 如果防护类型包含'all'或包含特定灾害类型
|
|
|
- if (in_array('all', $protectedTypes) || in_array($disasterType, $protectedTypes)) {
|
|
|
- return true;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 自动清除灾害
|
|
|
- *
|
|
|
- * @param int $userId 用户ID
|
|
|
- * @param mixed $land 土地对象
|
|
|
- * @param array $protectedTypes 防护的灾害类型
|
|
|
- * @return bool
|
|
|
- */
|
|
|
- protected function autoClearDisaster(int $userId, $land, array $protectedTypes): bool
|
|
|
- {
|
|
|
- // 获取土地上的作物
|
|
|
- $crop = $land->crop;
|
|
|
- if (!$crop) {
|
|
|
- return false;
|
|
|
- }
|
|
|
-
|
|
|
- // 检查作物是否有活跃的灾害
|
|
|
- $disasters = $crop->disasters ?? [];
|
|
|
- $clearedCount = 0;
|
|
|
-
|
|
|
- foreach ($disasters as $disaster) {
|
|
|
- if (($disaster['status'] ?? '') === 'active') {
|
|
|
- $disasterType = $disaster['type'] ?? 0;
|
|
|
-
|
|
|
- // 检查是否需要清除这种类型的灾害
|
|
|
- if (in_array('all', $protectedTypes) || in_array($disasterType, $protectedTypes)) {
|
|
|
- try {
|
|
|
- // 获取对应的清除道具
|
|
|
- $clearItem = $this->getDisasterClearItem($userId, $disasterType);
|
|
|
-
|
|
|
- if ($clearItem) {
|
|
|
- // 调用农场服务清除灾害
|
|
|
- $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_disaster_protection']
|
|
|
- );
|
|
|
-
|
|
|
- $clearedCount++;
|
|
|
-
|
|
|
- Log::info('宠物自动清除灾害成功', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $land->id,
|
|
|
- 'disaster_type' => $disasterType,
|
|
|
- 'item_id' => $clearItem['item_id']
|
|
|
- ]);
|
|
|
- }
|
|
|
- }
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::warning('宠物自动清除灾害失败', [
|
|
|
- 'user_id' => $userId,
|
|
|
- 'land_id' => $land->id,
|
|
|
- 'disaster_type' => $disasterType,
|
|
|
- 'error' => $e->getMessage()
|
|
|
- ]);
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return $clearedCount > 0;
|
|
|
- }
|
|
|
|
|
|
/**
|
|
|
* 获取清除灾害的道具
|
|
|
@@ -485,9 +299,9 @@ class PetAutoSkillLogic
|
|
|
{
|
|
|
// 根据灾害类型获取对应的清除道具ID
|
|
|
$clearItemMap = [
|
|
|
- \App\Module\Farm\Enums\DISASTER_TYPE::DROUGHT->value => 1001, // 干旱清除道具ID
|
|
|
- \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value => 1002, // 虫害清除道具ID
|
|
|
- \App\Module\Farm\Enums\DISASTER_TYPE::WEED->value => 1003, // 杂草清除道具ID
|
|
|
+ \App\Module\Farm\Enums\DISASTER_TYPE::DROUGHT->value => 24, // 干旱清除道具ID(洒水壶)
|
|
|
+ \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value => 23, // 虫害清除道具ID(杀虫剂)
|
|
|
+ \App\Module\Farm\Enums\DISASTER_TYPE::WEED->value => 22, // 杂草清除道具ID(除草剂)
|
|
|
];
|
|
|
|
|
|
$itemId = $clearItemMap[$disasterType] ?? null;
|
|
|
@@ -703,7 +517,9 @@ class PetAutoSkillLogic
|
|
|
]);
|
|
|
return;
|
|
|
}
|
|
|
-
|
|
|
+ Log::info('开始处理自动杀虫技能', [
|
|
|
+ 'land——number' => $landsWithCrops->count()
|
|
|
+ ]);
|
|
|
$pestControlCount = 0;
|
|
|
$disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value;
|
|
|
|
|
|
@@ -766,7 +582,7 @@ class PetAutoSkillLogic
|
|
|
* @param int $disasterType 灾害类型
|
|
|
* @return bool
|
|
|
*/
|
|
|
- protected function checkSpecificDisaster($land, int $disasterType): bool
|
|
|
+ protected function checkSpecificDisaster(LandInfoDto $land, int $disasterType): bool
|
|
|
{
|
|
|
// 检查土地状态是否为灾害状态
|
|
|
if ($land->status !== \App\Module\Farm\Enums\LAND_STATUS::DISASTER->value) {
|
|
|
@@ -806,21 +622,31 @@ class PetAutoSkillLogic
|
|
|
$clearItem = $this->getDisasterClearItem($userId, $disasterType);
|
|
|
|
|
|
if (!$clearItem) {
|
|
|
+ Log::debug('没有找到清除道具', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'land_id' => $land->id,
|
|
|
+ 'disaster_type' => $disasterType
|
|
|
+ ]);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
+ // 开启事务
|
|
|
+ DB::beginTransaction();
|
|
|
+
|
|
|
+ // 先消耗道具
|
|
|
+ \App\Module\GameItems\Services\ItemService::consumeItem(
|
|
|
+ $userId,
|
|
|
+ $clearItem['item_id'],
|
|
|
+ null,
|
|
|
+ 1,
|
|
|
+ ['source' => 'pet_auto_specific_disaster_clear']
|
|
|
+ );
|
|
|
+
|
|
|
// 调用农场服务清除灾害
|
|
|
$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']
|
|
|
- );
|
|
|
+ DB::commit();
|
|
|
|
|
|
Log::info('宠物自动清除特定灾害成功', [
|
|
|
'user_id' => $userId,
|
|
|
@@ -830,10 +656,14 @@ class PetAutoSkillLogic
|
|
|
]);
|
|
|
|
|
|
return true;
|
|
|
+ } else {
|
|
|
+ DB::rollback();
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
- return false;
|
|
|
} catch (\Exception $e) {
|
|
|
+ DB::rollback();
|
|
|
+
|
|
|
Log::warning('宠物自动清除特定灾害失败', [
|
|
|
'user_id' => $userId,
|
|
|
'land_id' => $land->id,
|