PetAutoSkillLogic.php 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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. /**
  718. * 处理自动施肥技能
  719. *
  720. * @param PetActiveSkill $activeSkill 激活的技能
  721. * @return void
  722. */
  723. public function processAutoFertilizing(PetActiveSkill $activeSkill): void
  724. {
  725. try {
  726. $pet = $activeSkill->pet;
  727. $userId = $pet->user_id;
  728. Log::info('开始处理自动施肥技能', [
  729. 'active_skill_id' => $activeSkill->id,
  730. 'pet_id' => $pet->id,
  731. 'user_id' => $userId
  732. ]);
  733. // 获取用户所有有作物的土地
  734. $landsWithCrops = LandService::getLandsWithCrops($userId);
  735. if ($landsWithCrops->isEmpty()) {
  736. Log::info('没有种植作物的土地', [
  737. 'user_id' => $userId,
  738. 'pet_id' => $pet->id
  739. ]);
  740. return;
  741. }
  742. $fertilizingCount = 0;
  743. $fertilizingResults = [];
  744. foreach ($landsWithCrops as $land) {
  745. try {
  746. // 检查土地是否可以施肥
  747. $canFertilize = $this->checkCanFertilize($land);
  748. if ($canFertilize) {
  749. // 自动施肥
  750. $fertilized = $this->autoFertilizeCrop($userId, $land);
  751. if ($fertilized) {
  752. $fertilizingCount++;
  753. $fertilizingResults[] = [
  754. 'land_id' => $land->id,
  755. 'success' => true
  756. ];
  757. Log::info('自动施肥成功', [
  758. 'user_id' => $userId,
  759. 'pet_id' => $pet->id,
  760. 'land_id' => $land->id
  761. ]);
  762. }
  763. }
  764. } catch (\Exception $e) {
  765. Log::warning('自动施肥处理失败', [
  766. 'user_id' => $userId,
  767. 'pet_id' => $pet->id,
  768. 'land_id' => $land->id,
  769. 'error' => $e->getMessage()
  770. ]);
  771. }
  772. }
  773. // 记录技能统计信息
  774. $this->recordSkillStatistics($activeSkill, 'auto_fertilizing', [
  775. 'fertilizing_count' => $fertilizingCount,
  776. 'total_lands_checked' => $landsWithCrops->count(),
  777. 'fertilizing_results' => $fertilizingResults
  778. ]);
  779. Log::info('自动施肥技能处理完成', [
  780. 'active_skill_id' => $activeSkill->id,
  781. 'pet_id' => $pet->id,
  782. 'user_id' => $userId,
  783. 'fertilizing_count' => $fertilizingCount,
  784. 'total_lands_checked' => $landsWithCrops->count()
  785. ]);
  786. } catch (\Exception $e) {
  787. Log::error('自动施肥技能处理异常', [
  788. 'active_skill_id' => $activeSkill->id,
  789. 'error' => $e->getMessage(),
  790. 'trace' => $e->getTraceAsString()
  791. ]);
  792. }
  793. }
  794. /**
  795. * 检查作物是否可以施肥
  796. *
  797. * @param mixed $land 土地对象
  798. * @return bool
  799. */
  800. protected function checkCanFertilize($land): bool
  801. {
  802. try {
  803. // 获取土地上的作物
  804. $crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $land->id)->first();
  805. if (!$crop) {
  806. return false;
  807. }
  808. // 检查作物是否已经施肥
  809. if ($crop->fertilized) {
  810. return false;
  811. }
  812. // 检查作物生长阶段是否允许施肥(种子期、发芽期、生长期可以施肥)
  813. $growthStage = is_object($crop->growth_stage) ? $crop->growth_stage->value : $crop->growth_stage;
  814. $allowedStages = [
  815. \App\Module\Farm\Enums\GROWTH_STAGE::SEED->value,
  816. \App\Module\Farm\Enums\GROWTH_STAGE::SPROUT->value,
  817. \App\Module\Farm\Enums\GROWTH_STAGE::GROWTH->value
  818. ];
  819. return in_array($growthStage, $allowedStages);
  820. } catch (\Exception $e) {
  821. Log::warning('检查施肥条件失败', [
  822. 'land_id' => $land->id,
  823. 'error' => $e->getMessage()
  824. ]);
  825. return false;
  826. }
  827. }
  828. /**
  829. * 自动为作物施肥
  830. *
  831. * @param int $userId 用户ID
  832. * @param mixed $land 土地对象
  833. * @return bool
  834. */
  835. protected function autoFertilizeCrop(int $userId, $land): bool
  836. {
  837. try {
  838. // 获取用户的肥料道具
  839. $fertilizerItem = $this->getFertilizerItem($userId);
  840. if (!$fertilizerItem) {
  841. Log::debug('没有找到肥料道具', [
  842. 'user_id' => $userId,
  843. 'land_id' => $land->id
  844. ]);
  845. return false;
  846. }
  847. // 获取作物信息
  848. $crop = \App\Module\Farm\Models\FarmCrop::where('land_id', $land->id)->first();
  849. if (!$crop) {
  850. return false;
  851. }
  852. // 开启事务
  853. DB::beginTransaction();
  854. // 先消耗肥料道具
  855. ItemService::consumeItem(
  856. $userId,
  857. $fertilizerItem['item_id'],
  858. null,
  859. 1,
  860. ['source' => 'pet_auto_fertilizing', 'land_id' => $land->id]
  861. );
  862. // 使用肥料
  863. $result = CropService::useFertilizer($crop->id, $fertilizerItem['crop_growth_time']);
  864. if ($result instanceof Res && !$result->error) {
  865. DB::commit();
  866. return true;
  867. } else {
  868. DB::rollBack();
  869. return false;
  870. }
  871. } catch (\Exception $e) {
  872. DB::rollBack();
  873. Log::warning('自动施肥失败', [
  874. 'user_id' => $userId,
  875. 'land_id' => $land->id,
  876. 'error' => $e->getMessage()
  877. ]);
  878. return false;
  879. }
  880. }
  881. /**
  882. * 获取用户的肥料道具
  883. *
  884. * @param int $userId 用户ID
  885. * @return array|null
  886. */
  887. protected function getFertilizerItem(int $userId): ?array
  888. {
  889. try {
  890. // 获取用户所有物品
  891. $userItems = ItemService::getUserItems($userId);
  892. foreach ($userItems as $userItem) {
  893. // 检查物品是否为肥料类型
  894. $cropGrowthTime = ItemService::getItemNumericAttribute($userItem->itemId, 'crop_growth_time', 0);
  895. if ($cropGrowthTime > 0 && $userItem->quantity > 0) {
  896. return [
  897. 'item_id' => $userItem->itemId,
  898. 'crop_growth_time' => $cropGrowthTime
  899. ];
  900. }
  901. }
  902. return null;
  903. } catch (\Exception $e) {
  904. Log::warning('获取肥料道具失败', [
  905. 'user_id' => $userId,
  906. 'error' => $e->getMessage()
  907. ]);
  908. return null;
  909. }
  910. }
  911. }