| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847 |
- <?php
- 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;
- use App\Module\GameItems\Services\ItemService;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use UCore\Dto\Res;
- /**
- * 宠物自动技能处理逻辑
- *
- * 处理宠物激活技能的自动执行逻辑
- */
- class PetAutoSkillLogic
- {
- /**
- * 处理自动收菜技能
- *
- * @param PetActiveSkill $activeSkill 激活的技能
- * @return void
- */
- public function processAutoHarvest(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
- ]);
- // 首先清理所有枯萎的作物(不使用道具)
- $witheredClearCount = $this->clearAllWitheredCrops($userId);
- // 获取用户所有可收获的土地
- $harvestableLands = LandService::getHarvestableLands($userId);
- $harvestCount = 0;
- $harvestResults = [];
- if (!$harvestableLands->isEmpty()) {
- foreach ($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,
- 'withered_cleared_count' => $witheredClearCount,
- '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,
- 'withered_cleared_count' => $witheredClearCount,
- 'total_lands' => $harvestableLands->count()
- ]);
- } catch (\Exception $e) {
- Log::error('处理自动收菜技能失败', [
- 'active_skill_id' => $activeSkill->id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- }
- /**
- * 处理自动播种技能
- *
- * @param PetActiveSkill $activeSkill 激活的技能
- * @return void
- */
- public function processAutoPlant(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
- ]);
- // 获取用户所有空闲的土地
- $idleLands = LandService::getIdleLands($userId);
- if ($idleLands->isEmpty()) {
- Log::info('没有空闲的土地', [
- 'user_id' => $userId,
- 'pet_id' => $pet->id
- ]);
- return;
- }
- // 获取优先使用的种子列表
- $preferredSeeds = $activeSkill->getConfigValue('preferred_seeds', []);
- // 获取用户拥有的种子物品
- $availableSeeds = $this->getAvailableSeeds($userId, $preferredSeeds);
- if (empty($availableSeeds)) {
- Log::info('没有可用的种子', [
- 'user_id' => $userId,
- 'pet_id' => $pet->id
- ]);
- return;
- }
- $plantCount = 0;
- $plantResults = [];
- foreach ($idleLands as $land) {
- if (empty($availableSeeds)) {
- break; // 种子用完了
- }
- try {
- // 选择种子(优先使用配置的种子)
- $seedItemId = array_shift($availableSeeds);
- // 开启事务处理单个土地的播种
- DB::beginTransaction();
- // 先消耗种子物品
- ItemService::consumeItem($userId, $seedItemId, null, 1, [
- 'source' => 'pet_auto_plant'
- ]);
- // 调用种植服务
- $result = CropService::plantCrop($userId, $land->id, $seedItemId);
- if ($result) {
- $plantCount++;
- $plantResults[] = [
- 'land_id' => $land->id,
- 'seed_item_id' => $seedItemId,
- 'result' => $result
- ];
- Log::info('自动播种成功', [
- 'user_id' => $userId,
- 'pet_id' => $pet->id,
- 'land_id' => $land->id,
- 'seed_item_id' => $seedItemId
- ]);
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::warning('自动播种失败', [
- 'user_id' => $userId,
- 'pet_id' => $pet->id,
- 'land_id' => $land->id,
- 'seed_item_id' => $seedItemId ?? null,
- 'error' => $e->getMessage()
- ]);
- }
- }
- // 记录统计信息
- $this->recordSkillStatistics($activeSkill, 'auto_plant', [
- 'plant_count' => $plantCount,
- 'total_lands_checked' => $idleLands->count(),
- 'plant_results' => $plantResults
- ]);
- Log::info('自动播种技能处理完成', [
- 'active_skill_id' => $activeSkill->id,
- 'pet_id' => $pet->id,
- 'user_id' => $userId,
- 'plant_count' => $plantCount,
- 'total_lands' => $idleLands->count()
- ]);
- } catch (\Exception $e) {
- Log::error('处理自动播种技能失败', [
- 'active_skill_id' => $activeSkill->id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- }
- }
- /**
- * 获取用户可用的种子
- *
- * @param int $userId 用户ID
- * @param array $preferredSeeds 优先种子列表
- * @return array 可用种子ID列表
- */
- protected function getAvailableSeeds(int $userId, array $preferredSeeds = []): array
- {
- // 调用物品服务获取用户拥有的种子
- $allSeeds = ItemService::getUserSeedItems($userId);
- $availableSeeds = [];
- // 优先添加配置的种子
- foreach ($preferredSeeds as $seedId) {
- if (isset($allSeeds[$seedId]) && $allSeeds[$seedId] > 0) {
- for ($i = 0; $i < $allSeeds[$seedId]; $i++) {
- $availableSeeds[] = $seedId;
- }
- }
- }
- // 添加其他种子
- foreach ($allSeeds as $seedId => $quantity) {
- if (!in_array($seedId, $preferredSeeds) && $quantity > 0) {
- for ($i = 0; $i < $quantity; $i++) {
- $availableSeeds[] = $seedId;
- }
- }
- }
- return $availableSeeds;
- }
- /**
- * 获取清除灾害的道具
- *
- * @param int $userId 用户ID
- * @param int $disasterType 灾害类型
- * @return array|null 道具信息
- */
- protected function getDisasterClearItem(int $userId, int $disasterType): ?array
- {
- // 根据灾害类型获取对应的清除道具ID
- $clearItemMap = [
- \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;
- if (!$itemId) {
- return null;
- }
- // 检查用户是否拥有该道具
- $checkResult = \App\Module\GameItems\Services\ItemService::checkItemQuantity($userId, $itemId, 1);
- if ($checkResult->error) {
- return null;
- }
- return [
- 'item_id' => $itemId,
- 'disaster_type' => $disasterType
- ];
- }
- /**
- * 处理自动除草技能
- *
- * @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;
- }
- Log::info('开始处理自动杀虫技能', [
- 'land——number' => $landsWithCrops->count()
- ]);
- $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(LandInfoDto $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) {
- 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) {
- DB::commit();
- Log::info('宠物自动清除特定灾害成功', [
- 'user_id' => $userId,
- 'land_id' => $land->id,
- 'disaster_type' => $disasterType,
- 'item_id' => $clearItem['item_id']
- ]);
- return true;
- } else {
- DB::rollback();
- return false;
- }
- } catch (\Exception $e) {
- DB::rollback();
- Log::warning('宠物自动清除特定灾害失败', [
- 'user_id' => $userId,
- 'land_id' => $land->id,
- 'disaster_type' => $disasterType,
- 'error' => $e->getMessage()
- ]);
- return false;
- }
- }
- /**
- * 记录技能统计信息
- *
- * @param PetActiveSkill $activeSkill 激活的技能
- * @param string $actionType 操作类型
- * @param array $statistics 统计数据
- * @return void
- */
- protected function recordSkillStatistics(PetActiveSkill $activeSkill, string $actionType, array $statistics): void
- {
- $config = $activeSkill->config;
- // 确保config是数组类型
- if (!is_array($config)) {
- // 如果是字符串,尝试解析JSON
- if (is_string($config)) {
- $config = json_decode($config, true);
- if (json_last_error() !== JSON_ERROR_NONE) {
- $config = [];
- }
- } else {
- $config = [];
- }
- }
- if (!isset($config['statistics'])) {
- $config['statistics'] = [];
- }
- $config['statistics'][] = [
- 'action_type' => $actionType,
- 'timestamp' => now()->toDateTimeString(),
- 'data' => $statistics
- ];
- // 只保留最近10条统计记录
- if (count($config['statistics']) > 10) {
- $config['statistics'] = array_slice($config['statistics'], -10);
- }
- $activeSkill->config = $config;
- $activeSkill->save();
- }
- /**
- * 清理所有枯萎的作物(不使用道具)
- *
- * @param int $userId 用户ID
- * @return int 清理的数量
- */
- protected function clearAllWitheredCrops(int $userId): int
- {
- try {
- // 获取所有枯萎状态的土地
- $witheredLands = \App\Module\Farm\Models\FarmLand::where('user_id', $userId)
- ->where('status', \App\Module\Farm\Enums\LAND_STATUS::WITHERED->value)
- ->get();
- $clearedCount = 0;
- foreach ($witheredLands as $land) {
- try {
- // 开启事务处理单个土地的清理
- DB::beginTransaction();
- $cleared = $this->autoClearWitheredCrop($userId, $land->id);
- if ($cleared) {
- $clearedCount++;
- Log::info('自动清理枯萎作物成功', [
- 'user_id' => $userId,
- 'land_id' => $land->id
- ]);
- }
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::warning('自动清理枯萎作物失败', [
- 'user_id' => $userId,
- 'land_id' => $land->id,
- 'error' => $e->getMessage()
- ]);
- }
- }
- if ($clearedCount > 0) {
- Log::info('批量清理枯萎作物完成', [
- 'user_id' => $userId,
- 'cleared_count' => $clearedCount,
- 'total_withered_lands' => $witheredLands->count()
- ]);
- }
- return $clearedCount;
- } catch (\Exception $e) {
- Log::error('批量清理枯萎作物失败', [
- 'user_id' => $userId,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- return 0;
- }
- }
- /**
- * 自动铲除枯萎的作物
- *
- * @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;
- }
- // 检查作物是否为枯萎状态
- $cropStageValue = is_object($crop->growth_stage) ? $crop->growth_stage->value : $crop->growth_stage;
- if ($cropStageValue !== \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(),
- 'trace' => $e->getTraceAsString()
- ]);
- return false;
- }
- }
- }
|