PetAutoSkillLogic.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. <?php
  2. namespace App\Module\Pet\Logic;
  3. use App\Module\Farm\Dtos\LandInfoDto;
  4. use App\Module\Pet\Models\PetActiveSkill;
  5. use App\Module\Farm\Services\CropService;
  6. use App\Module\Farm\Services\LandService;
  7. use App\Module\GameItems\Services\ItemService;
  8. use Illuminate\Support\Facades\DB;
  9. use Illuminate\Support\Facades\Log;
  10. use UCore\Dto\Res;
  11. /**
  12. * 宠物自动技能处理逻辑
  13. *
  14. * 处理宠物激活技能的自动执行逻辑
  15. */
  16. class PetAutoSkillLogic
  17. {
  18. /**
  19. * 处理自动收菜技能
  20. *
  21. * @param PetActiveSkill $activeSkill 激活的技能
  22. * @return void
  23. */
  24. public function processAutoHarvest(PetActiveSkill $activeSkill): void
  25. {
  26. try {
  27. $pet = $activeSkill->pet;
  28. $userId = $pet->user_id;
  29. Log::info('开始处理自动收菜技能', [
  30. 'active_skill_id' => $activeSkill->id,
  31. 'pet_id' => $pet->id,
  32. 'user_id' => $userId
  33. ]);
  34. // 首先清理所有枯萎的作物(不使用道具)
  35. $witheredClearCount = $this->clearAllWitheredCrops($userId);
  36. // 获取用户所有可收获的土地
  37. $harvestableLands = LandService::getHarvestableLands($userId);
  38. $harvestCount = 0;
  39. $harvestResults = [];
  40. if (!$harvestableLands->isEmpty()) {
  41. foreach ($harvestableLands as $land) {
  42. try {
  43. // 开启事务处理单个土地的收获
  44. DB::beginTransaction();
  45. // 调用收获服务
  46. $result = CropService::harvestCrop($userId, $land->id);
  47. if ($result instanceof Res && !$result->error) {
  48. $harvestCount++;
  49. $harvestResults[] = [
  50. 'land_id' => $land->id,
  51. 'success' => true,
  52. 'auto_cleared' => false
  53. ];
  54. Log::info('自动收菜成功', [
  55. 'user_id' => $userId,
  56. 'pet_id' => $pet->id,
  57. 'land_id' => $land->id
  58. ]);
  59. // 收获后自动铲除枯萎的作物
  60. $clearResult = $this->autoClearWitheredCrop($userId, $land->id);
  61. if ($clearResult) {
  62. $harvestResults[count($harvestResults) - 1]['auto_cleared'] = true;
  63. Log::info('自动铲除枯萎作物成功', [
  64. 'user_id' => $userId,
  65. 'pet_id' => $pet->id,
  66. 'land_id' => $land->id
  67. ]);
  68. }
  69. }
  70. DB::commit();
  71. } catch (\Exception $e) {
  72. DB::rollBack();
  73. Log::warning('自动收菜失败', [
  74. 'user_id' => $userId,
  75. 'pet_id' => $pet->id,
  76. 'land_id' => $land->id,
  77. 'error' => $e->getMessage()
  78. ]);
  79. }
  80. }
  81. }
  82. // 统计自动铲除的数量
  83. $autoClearedCount = array_sum(array_column($harvestResults, 'auto_cleared'));
  84. // 记录统计信息
  85. $this->recordSkillStatistics($activeSkill, 'auto_harvest', [
  86. 'harvest_count' => $harvestCount,
  87. 'auto_cleared_count' => $autoClearedCount,
  88. 'withered_cleared_count' => $witheredClearCount,
  89. 'total_lands_checked' => $harvestableLands->count(),
  90. 'harvest_results' => $harvestResults
  91. ]);
  92. Log::info('自动收菜技能处理完成', [
  93. 'active_skill_id' => $activeSkill->id,
  94. 'pet_id' => $pet->id,
  95. 'user_id' => $userId,
  96. 'harvest_count' => $harvestCount,
  97. 'auto_cleared_count' => $autoClearedCount,
  98. 'withered_cleared_count' => $witheredClearCount,
  99. 'total_lands' => $harvestableLands->count()
  100. ]);
  101. } catch (\Exception $e) {
  102. Log::error('处理自动收菜技能失败', [
  103. 'active_skill_id' => $activeSkill->id,
  104. 'error' => $e->getMessage(),
  105. 'trace' => $e->getTraceAsString()
  106. ]);
  107. }
  108. }
  109. /**
  110. * 处理自动播种技能
  111. *
  112. * @param PetActiveSkill $activeSkill 激活的技能
  113. * @return void
  114. */
  115. public function processAutoPlant(PetActiveSkill $activeSkill): void
  116. {
  117. try {
  118. $pet = $activeSkill->pet;
  119. $userId = $pet->user_id;
  120. Log::info('开始处理自动播种技能', [
  121. 'active_skill_id' => $activeSkill->id,
  122. 'pet_id' => $pet->id,
  123. 'user_id' => $userId
  124. ]);
  125. // 获取用户所有空闲的土地
  126. $idleLands = LandService::getIdleLands($userId);
  127. if ($idleLands->isEmpty()) {
  128. Log::info('没有空闲的土地', [
  129. 'user_id' => $userId,
  130. 'pet_id' => $pet->id
  131. ]);
  132. return;
  133. }
  134. // 获取优先使用的种子列表
  135. $preferredSeeds = $activeSkill->getConfigValue('preferred_seeds', []);
  136. // 获取用户拥有的种子物品
  137. $availableSeeds = $this->getAvailableSeeds($userId, $preferredSeeds);
  138. if (empty($availableSeeds)) {
  139. Log::info('没有可用的种子', [
  140. 'user_id' => $userId,
  141. 'pet_id' => $pet->id
  142. ]);
  143. return;
  144. }
  145. $plantCount = 0;
  146. $plantResults = [];
  147. foreach ($idleLands as $land) {
  148. if (empty($availableSeeds)) {
  149. break; // 种子用完了
  150. }
  151. try {
  152. // 选择种子(优先使用配置的种子)
  153. $seedItemId = array_shift($availableSeeds);
  154. // 开启事务处理单个土地的播种
  155. DB::beginTransaction();
  156. // 先消耗种子物品
  157. ItemService::consumeItem($userId, $seedItemId, null, 1, [
  158. 'source' => 'pet_auto_plant'
  159. ]);
  160. // 调用种植服务
  161. $result = CropService::plantCrop($userId, $land->id, $seedItemId);
  162. if ($result) {
  163. $plantCount++;
  164. $plantResults[] = [
  165. 'land_id' => $land->id,
  166. 'seed_item_id' => $seedItemId,
  167. 'result' => $result
  168. ];
  169. Log::info('自动播种成功', [
  170. 'user_id' => $userId,
  171. 'pet_id' => $pet->id,
  172. 'land_id' => $land->id,
  173. 'seed_item_id' => $seedItemId
  174. ]);
  175. }
  176. DB::commit();
  177. } catch (\Exception $e) {
  178. DB::rollBack();
  179. Log::warning('自动播种失败', [
  180. 'user_id' => $userId,
  181. 'pet_id' => $pet->id,
  182. 'land_id' => $land->id,
  183. 'seed_item_id' => $seedItemId ?? null,
  184. 'error' => $e->getMessage()
  185. ]);
  186. }
  187. }
  188. // 记录统计信息
  189. $this->recordSkillStatistics($activeSkill, 'auto_plant', [
  190. 'plant_count' => $plantCount,
  191. 'total_lands_checked' => $idleLands->count(),
  192. 'plant_results' => $plantResults
  193. ]);
  194. Log::info('自动播种技能处理完成', [
  195. 'active_skill_id' => $activeSkill->id,
  196. 'pet_id' => $pet->id,
  197. 'user_id' => $userId,
  198. 'plant_count' => $plantCount,
  199. 'total_lands' => $idleLands->count()
  200. ]);
  201. } catch (\Exception $e) {
  202. Log::error('处理自动播种技能失败', [
  203. 'active_skill_id' => $activeSkill->id,
  204. 'error' => $e->getMessage(),
  205. 'trace' => $e->getTraceAsString()
  206. ]);
  207. }
  208. }
  209. /**
  210. * 获取用户可用的种子
  211. *
  212. * @param int $userId 用户ID
  213. * @param array $preferredSeeds 优先种子列表
  214. * @return array 可用种子ID列表
  215. */
  216. protected function getAvailableSeeds(int $userId, array $preferredSeeds = []): array
  217. {
  218. // 调用物品服务获取用户拥有的种子
  219. $allSeeds = ItemService::getUserSeedItems($userId);
  220. $availableSeeds = [];
  221. // 优先添加配置的种子
  222. foreach ($preferredSeeds as $seedId) {
  223. if (isset($allSeeds[$seedId]) && $allSeeds[$seedId] > 0) {
  224. for ($i = 0; $i < $allSeeds[$seedId]; $i++) {
  225. $availableSeeds[] = $seedId;
  226. }
  227. }
  228. }
  229. // 添加其他种子
  230. foreach ($allSeeds as $seedId => $quantity) {
  231. if (!in_array($seedId, $preferredSeeds) && $quantity > 0) {
  232. for ($i = 0; $i < $quantity; $i++) {
  233. $availableSeeds[] = $seedId;
  234. }
  235. }
  236. }
  237. return $availableSeeds;
  238. }
  239. /**
  240. * 获取清除灾害的道具
  241. *
  242. * @param int $userId 用户ID
  243. * @param int $disasterType 灾害类型
  244. * @return array|null 道具信息
  245. */
  246. protected function getDisasterClearItem(int $userId, int $disasterType): ?array
  247. {
  248. // 根据灾害类型获取对应的清除道具ID
  249. $clearItemMap = [
  250. \App\Module\Farm\Enums\DISASTER_TYPE::DROUGHT->value => 24, // 干旱清除道具ID(洒水壶)
  251. \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value => 23, // 虫害清除道具ID(杀虫剂)
  252. \App\Module\Farm\Enums\DISASTER_TYPE::WEED->value => 22, // 杂草清除道具ID(除草剂)
  253. ];
  254. $itemId = $clearItemMap[$disasterType] ?? null;
  255. if (!$itemId) {
  256. return null;
  257. }
  258. // 检查用户是否拥有该道具
  259. $checkResult = \App\Module\GameItems\Services\ItemService::checkItemQuantity($userId, $itemId, 1);
  260. if ($checkResult->error) {
  261. return null;
  262. }
  263. return [
  264. 'item_id' => $itemId,
  265. 'disaster_type' => $disasterType
  266. ];
  267. }
  268. /**
  269. * 处理自动除草技能
  270. *
  271. * @param PetActiveSkill $activeSkill 激活的技能
  272. * @return void
  273. */
  274. public function processAutoWeeding(PetActiveSkill $activeSkill): void
  275. {
  276. try {
  277. $pet = $activeSkill->pet;
  278. $userId = $pet->user_id;
  279. Log::info('开始处理自动除草技能', [
  280. 'active_skill_id' => $activeSkill->id,
  281. 'pet_id' => $pet->id,
  282. 'user_id' => $userId
  283. ]);
  284. // 获取用户所有有作物的土地
  285. $landsWithCrops = LandService::getLandsWithCrops($userId);
  286. if ($landsWithCrops->isEmpty()) {
  287. Log::info('没有种植作物的土地', [
  288. 'user_id' => $userId,
  289. 'pet_id' => $pet->id
  290. ]);
  291. return;
  292. }
  293. $weedingCount = 0;
  294. $disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::WEED->value;
  295. foreach ($landsWithCrops as $land) {
  296. try {
  297. // 检查土地是否有杂草灾害
  298. $hasWeedDisaster = $this->checkSpecificDisaster($land, $disasterType);
  299. if ($hasWeedDisaster) {
  300. // 自动清除杂草灾害
  301. $cleared = $this->autoClearSpecificDisaster($userId, $land, $disasterType);
  302. if ($cleared) {
  303. $weedingCount++;
  304. Log::info('自动除草成功', [
  305. 'user_id' => $userId,
  306. 'pet_id' => $pet->id,
  307. 'land_id' => $land->id
  308. ]);
  309. }
  310. }
  311. } catch (\Exception $e) {
  312. Log::warning('自动除草处理失败', [
  313. 'user_id' => $userId,
  314. 'pet_id' => $pet->id,
  315. 'land_id' => $land->id,
  316. 'error' => $e->getMessage()
  317. ]);
  318. }
  319. }
  320. // 记录统计信息
  321. $this->recordSkillStatistics($activeSkill, 'auto_weeding', [
  322. 'weeding_count' => $weedingCount,
  323. 'total_lands_checked' => $landsWithCrops->count()
  324. ]);
  325. Log::info('自动除草技能处理完成', [
  326. 'active_skill_id' => $activeSkill->id,
  327. 'pet_id' => $pet->id,
  328. 'user_id' => $userId,
  329. 'weeding_count' => $weedingCount,
  330. 'total_lands' => $landsWithCrops->count()
  331. ]);
  332. } catch (\Exception $e) {
  333. Log::error('处理自动除草技能失败', [
  334. 'active_skill_id' => $activeSkill->id,
  335. 'error' => $e->getMessage(),
  336. 'trace' => $e->getTraceAsString()
  337. ]);
  338. }
  339. }
  340. /**
  341. * 处理自动浇水技能
  342. *
  343. * @param PetActiveSkill $activeSkill 激活的技能
  344. * @return void
  345. */
  346. public function processAutoWatering(PetActiveSkill $activeSkill): void
  347. {
  348. try {
  349. $pet = $activeSkill->pet;
  350. $userId = $pet->user_id;
  351. Log::info('开始处理自动浇水技能', [
  352. 'active_skill_id' => $activeSkill->id,
  353. 'pet_id' => $pet->id,
  354. 'user_id' => $userId
  355. ]);
  356. // 获取用户所有有作物的土地
  357. $landsWithCrops = LandService::getLandsWithCrops($userId);
  358. if ($landsWithCrops->isEmpty()) {
  359. Log::info('没有种植作物的土地', [
  360. 'user_id' => $userId,
  361. 'pet_id' => $pet->id
  362. ]);
  363. return;
  364. }
  365. $wateringCount = 0;
  366. $disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::DROUGHT->value;
  367. foreach ($landsWithCrops as $land) {
  368. try {
  369. // 检查土地是否有干旱灾害
  370. $hasDroughtDisaster = $this->checkSpecificDisaster($land, $disasterType);
  371. if ($hasDroughtDisaster) {
  372. // 自动清除干旱灾害
  373. $cleared = $this->autoClearSpecificDisaster($userId, $land, $disasterType);
  374. if ($cleared) {
  375. $wateringCount++;
  376. Log::info('自动浇水成功', [
  377. 'user_id' => $userId,
  378. 'pet_id' => $pet->id,
  379. 'land_id' => $land->id
  380. ]);
  381. }
  382. }
  383. } catch (\Exception $e) {
  384. Log::warning('自动浇水处理失败', [
  385. 'user_id' => $userId,
  386. 'pet_id' => $pet->id,
  387. 'land_id' => $land->id,
  388. 'error' => $e->getMessage()
  389. ]);
  390. }
  391. }
  392. // 记录统计信息
  393. $this->recordSkillStatistics($activeSkill, 'auto_watering', [
  394. 'watering_count' => $wateringCount,
  395. 'total_lands_checked' => $landsWithCrops->count()
  396. ]);
  397. Log::info('自动浇水技能处理完成', [
  398. 'active_skill_id' => $activeSkill->id,
  399. 'pet_id' => $pet->id,
  400. 'user_id' => $userId,
  401. 'watering_count' => $wateringCount,
  402. 'total_lands' => $landsWithCrops->count()
  403. ]);
  404. } catch (\Exception $e) {
  405. Log::error('处理自动浇水技能失败', [
  406. 'active_skill_id' => $activeSkill->id,
  407. 'error' => $e->getMessage(),
  408. 'trace' => $e->getTraceAsString()
  409. ]);
  410. }
  411. }
  412. /**
  413. * 处理自动杀虫技能
  414. *
  415. * @param PetActiveSkill $activeSkill 激活的技能
  416. * @return void
  417. */
  418. public function processAutoPestControl(PetActiveSkill $activeSkill): void
  419. {
  420. try {
  421. $pet = $activeSkill->pet;
  422. $userId = $pet->user_id;
  423. Log::info('开始处理自动杀虫技能', [
  424. 'active_skill_id' => $activeSkill->id,
  425. 'pet_id' => $pet->id,
  426. 'user_id' => $userId
  427. ]);
  428. // 获取用户所有有作物的土地
  429. $landsWithCrops = LandService::getLandsWithCrops($userId);
  430. if ($landsWithCrops->isEmpty()) {
  431. Log::info('没有种植作物的土地', [
  432. 'user_id' => $userId,
  433. 'pet_id' => $pet->id
  434. ]);
  435. return;
  436. }
  437. Log::info('开始处理自动杀虫技能', [
  438. 'land——number' => $landsWithCrops->count()
  439. ]);
  440. $pestControlCount = 0;
  441. $disasterType = \App\Module\Farm\Enums\DISASTER_TYPE::PEST->value;
  442. foreach ($landsWithCrops as $land) {
  443. try {
  444. // 检查土地是否有虫害灾害
  445. $hasPestDisaster = $this->checkSpecificDisaster($land, $disasterType);
  446. if ($hasPestDisaster) {
  447. // 自动清除虫害灾害
  448. $cleared = $this->autoClearSpecificDisaster($userId, $land, $disasterType);
  449. if ($cleared) {
  450. $pestControlCount++;
  451. Log::info('自动杀虫成功', [
  452. 'user_id' => $userId,
  453. 'pet_id' => $pet->id,
  454. 'land_id' => $land->id
  455. ]);
  456. }
  457. }
  458. } catch (\Exception $e) {
  459. Log::warning('自动杀虫处理失败', [
  460. 'user_id' => $userId,
  461. 'pet_id' => $pet->id,
  462. 'land_id' => $land->id,
  463. 'error' => $e->getMessage()
  464. ]);
  465. }
  466. }
  467. // 记录统计信息
  468. $this->recordSkillStatistics($activeSkill, 'auto_pest_control', [
  469. 'pest_control_count' => $pestControlCount,
  470. 'total_lands_checked' => $landsWithCrops->count()
  471. ]);
  472. Log::info('自动杀虫技能处理完成', [
  473. 'active_skill_id' => $activeSkill->id,
  474. 'pet_id' => $pet->id,
  475. 'user_id' => $userId,
  476. 'pest_control_count' => $pestControlCount,
  477. 'total_lands' => $landsWithCrops->count()
  478. ]);
  479. } catch (\Exception $e) {
  480. Log::error('处理自动杀虫技能失败', [
  481. 'active_skill_id' => $activeSkill->id,
  482. 'error' => $e->getMessage(),
  483. 'trace' => $e->getTraceAsString()
  484. ]);
  485. }
  486. }
  487. /**
  488. * 检查特定类型的灾害
  489. *
  490. * @param mixed $land 土地对象
  491. * @param int $disasterType 灾害类型
  492. * @return bool
  493. */
  494. protected function checkSpecificDisaster(LandInfoDto $land, int $disasterType): bool
  495. {
  496. // 检查土地状态是否为灾害状态
  497. if ($land->status !== \App\Module\Farm\Enums\LAND_STATUS::DISASTER->value) {
  498. return false;
  499. }
  500. // 获取土地上的作物
  501. $crop = $land->crop;
  502. if (!$crop) {
  503. return false;
  504. }
  505. // 检查作物是否有指定类型的活跃灾害
  506. $disasters = $crop->disasters ?? [];
  507. foreach ($disasters as $disaster) {
  508. if (($disaster['status'] ?? '') === 'active' && ($disaster['type'] ?? 0) == $disasterType) {
  509. return true;
  510. }
  511. }
  512. return false;
  513. }
  514. /**
  515. * 自动清除特定类型的灾害
  516. *
  517. * @param int $userId 用户ID
  518. * @param mixed $land 土地对象
  519. * @param int $disasterType 灾害类型
  520. * @return bool
  521. */
  522. protected function autoClearSpecificDisaster(int $userId, $land, int $disasterType): bool
  523. {
  524. try {
  525. // 获取对应的清除道具
  526. $clearItem = $this->getDisasterClearItem($userId, $disasterType);
  527. if (!$clearItem) {
  528. Log::debug('没有找到清除道具', [
  529. 'user_id' => $userId,
  530. 'land_id' => $land->id,
  531. 'disaster_type' => $disasterType
  532. ]);
  533. return false;
  534. }
  535. // 开启事务
  536. DB::beginTransaction();
  537. // 先消耗道具
  538. \App\Module\GameItems\Services\ItemService::consumeItem(
  539. $userId,
  540. $clearItem['item_id'],
  541. null,
  542. 1,
  543. ['source' => 'pet_auto_specific_disaster_clear']
  544. );
  545. // 调用农场服务清除灾害
  546. $result = \App\Module\Farm\Services\CropService::clearDisaster($userId, $land->id, $disasterType);
  547. if ($result) {
  548. DB::commit();
  549. Log::info('宠物自动清除特定灾害成功', [
  550. 'user_id' => $userId,
  551. 'land_id' => $land->id,
  552. 'disaster_type' => $disasterType,
  553. 'item_id' => $clearItem['item_id']
  554. ]);
  555. return true;
  556. } else {
  557. DB::rollback();
  558. return false;
  559. }
  560. } catch (\Exception $e) {
  561. DB::rollback();
  562. Log::warning('宠物自动清除特定灾害失败', [
  563. 'user_id' => $userId,
  564. 'land_id' => $land->id,
  565. 'disaster_type' => $disasterType,
  566. 'error' => $e->getMessage()
  567. ]);
  568. return false;
  569. }
  570. }
  571. /**
  572. * 记录技能统计信息
  573. *
  574. * @param PetActiveSkill $activeSkill 激活的技能
  575. * @param string $actionType 操作类型
  576. * @param array $statistics 统计数据
  577. * @return void
  578. */
  579. protected function recordSkillStatistics(PetActiveSkill $activeSkill, string $actionType, array $statistics): void
  580. {
  581. $config = $activeSkill->config;
  582. // 确保config是数组类型
  583. if (!is_array($config)) {
  584. // 如果是字符串,尝试解析JSON
  585. if (is_string($config)) {
  586. $config = json_decode($config, true);
  587. if (json_last_error() !== JSON_ERROR_NONE) {
  588. $config = [];
  589. }
  590. } else {
  591. $config = [];
  592. }
  593. }
  594. if (!isset($config['statistics'])) {
  595. $config['statistics'] = [];
  596. }
  597. $config['statistics'][] = [
  598. 'action_type' => $actionType,
  599. 'timestamp' => now()->toDateTimeString(),
  600. 'data' => $statistics
  601. ];
  602. // 只保留最近10条统计记录
  603. if (count($config['statistics']) > 10) {
  604. $config['statistics'] = array_slice($config['statistics'], -10);
  605. }
  606. $activeSkill->config = $config;
  607. $activeSkill->save();
  608. }
  609. /**
  610. * 清理所有枯萎的作物(不使用道具)
  611. *
  612. * @param int $userId 用户ID
  613. * @return int 清理的数量
  614. */
  615. protected function clearAllWitheredCrops(int $userId): int
  616. {
  617. try {
  618. // 获取所有枯萎状态的土地
  619. $witheredLands = \App\Module\Farm\Models\FarmLand::where('user_id', $userId)
  620. ->where('status', \App\Module\Farm\Enums\LAND_STATUS::WITHERED->value)
  621. ->get();
  622. $clearedCount = 0;
  623. foreach ($witheredLands as $land) {
  624. try {
  625. // 开启事务处理单个土地的清理
  626. DB::beginTransaction();
  627. $cleared = $this->autoClearWitheredCrop($userId, $land->id);
  628. if ($cleared) {
  629. $clearedCount++;
  630. Log::info('自动清理枯萎作物成功', [
  631. 'user_id' => $userId,
  632. 'land_id' => $land->id
  633. ]);
  634. }
  635. DB::commit();
  636. } catch (\Exception $e) {
  637. DB::rollBack();
  638. Log::warning('自动清理枯萎作物失败', [
  639. 'user_id' => $userId,
  640. 'land_id' => $land->id,
  641. 'error' => $e->getMessage()
  642. ]);
  643. }
  644. }
  645. if ($clearedCount > 0) {
  646. Log::info('批量清理枯萎作物完成', [
  647. 'user_id' => $userId,
  648. 'cleared_count' => $clearedCount,
  649. 'total_withered_lands' => $witheredLands->count()
  650. ]);
  651. }
  652. return $clearedCount;
  653. } catch (\Exception $e) {
  654. Log::error('批量清理枯萎作物失败', [
  655. 'user_id' => $userId,
  656. 'error' => $e->getMessage(),
  657. 'trace' => $e->getTraceAsString()
  658. ]);
  659. return 0;
  660. }
  661. }
  662. /**
  663. * 自动铲除枯萎的作物
  664. *
  665. * @param int $userId 用户ID
  666. * @param int $landId 土地ID
  667. * @return bool 是否成功铲除
  668. */
  669. protected function autoClearWitheredCrop(int $userId, int $landId): bool
  670. {
  671. try {
  672. // 获取土地信息
  673. $land = \App\Module\Farm\Models\FarmLand::where('id', $landId)
  674. ->where('user_id', $userId)
  675. ->first();
  676. if (!$land) {
  677. return false;
  678. }
  679. // 检查土地状态是否为枯萎状态
  680. if ($land->status !== \App\Module\Farm\Enums\LAND_STATUS::WITHERED->value) {
  681. return false;
  682. }
  683. // 获取土地上的作物
  684. $crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $landId)->first();
  685. if (!$crop) {
  686. // 如果没有作物但土地状态是枯萎,修正土地状态为空闲
  687. $land->status = \App\Module\Farm\Enums\LAND_STATUS::IDLE->value;
  688. $land->save();
  689. return true;
  690. }
  691. // 检查作物是否为枯萎状态
  692. $cropStageValue = is_object($crop->growth_stage) ? $crop->growth_stage->value : $crop->growth_stage;
  693. if ($cropStageValue !== \App\Module\Farm\Enums\GROWTH_STAGE::WITHERED->value) {
  694. return false;
  695. }
  696. // 调用农场服务铲除作物
  697. $result = \App\Module\Farm\Services\CropService::removeCrop($userId, $landId);
  698. if ($result) {
  699. Log::info('宠物自动铲除枯萎作物成功', [
  700. 'user_id' => $userId,
  701. 'land_id' => $landId,
  702. 'crop_id' => $crop->id
  703. ]);
  704. return true;
  705. }
  706. return false;
  707. } catch (\Exception $e) {
  708. Log::warning('宠物自动铲除枯萎作物失败', [
  709. 'user_id' => $userId,
  710. 'land_id' => $landId,
  711. 'error' => $e->getMessage(),
  712. 'trace' => $e->getTraceAsString()
  713. ]);
  714. return false;
  715. }
  716. }
  717. }