DisasterLogic.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Dtos\DisasterInfoDto;
  4. use App\Module\Farm\Enums\DISASTER_TYPE;
  5. use App\Module\Farm\Enums\GROWTH_STAGE;
  6. use App\Module\Farm\Enums\LAND_STATUS;
  7. use App\Module\Farm\Events\DisasterGeneratedEvent;
  8. use App\Module\Farm\Models\FarmCrop;
  9. use App\Module\Farm\Models\FarmGodBuff;
  10. use App\Module\Farm\Models\FarmLand;
  11. use App\Module\Farm\Models\FarmCropLog;
  12. use App\Module\Farm\Services\DisasterService;
  13. use Illuminate\Support\Collection;
  14. use Illuminate\Support\Facades\Log;
  15. /**
  16. * 灾害管理逻辑
  17. */
  18. class DisasterLogic
  19. {
  20. /**
  21. * 获取作物的灾害信息
  22. *
  23. * @param int $cropId
  24. * @return Collection
  25. */
  26. public function getCropDisasters(int $cropId): Collection
  27. {
  28. try {
  29. $crop = FarmCrop::find($cropId);
  30. if (!$crop || empty($crop->disasters)) {
  31. return collect();
  32. }
  33. $disasters = collect($crop->disasters);
  34. return $disasters->map(function ($disaster) {
  35. return DisasterInfoDto::fromArray($disaster);
  36. });
  37. } catch (\Exception $e) {
  38. Log::error('获取作物灾害信息失败', [
  39. 'crop_id' => $cropId,
  40. 'error' => $e->getMessage(),
  41. 'trace' => $e->getTraceAsString()
  42. ]);
  43. return collect();
  44. }
  45. }
  46. /**
  47. * 获取作物的活跃灾害
  48. *
  49. * @param int $cropId
  50. * @return Collection
  51. */
  52. public function getActiveDisasters(int $cropId): Collection
  53. {
  54. try {
  55. $crop = FarmCrop::find($cropId);
  56. if (!$crop || empty($crop->disasters)) {
  57. return collect();
  58. }
  59. $disasters = collect($crop->disasters)->filter(function ($disaster) {
  60. return ($disaster['status'] ?? '') === 'active';
  61. });
  62. return $disasters->map(function ($disaster) {
  63. return DisasterInfoDto::fromArray($disaster);
  64. });
  65. } catch (\Exception $e) {
  66. Log::error('获取作物活跃灾害失败', [
  67. 'crop_id' => $cropId,
  68. 'error' => $e->getMessage(),
  69. 'trace' => $e->getTraceAsString()
  70. ]);
  71. return collect();
  72. }
  73. }
  74. /**
  75. * 生成灾害
  76. *
  77. * @param int $cropId
  78. * @return DisasterInfoDto|null
  79. */
  80. public function generateDisaster(int $cropId): ?DisasterInfoDto
  81. {
  82. try {
  83. $crop = FarmCrop::find($cropId);
  84. if (!$crop) {
  85. throw new \Exception('作物不存在');
  86. }
  87. /**
  88. * @var FarmLand $land
  89. */
  90. $land = $crop->land;
  91. if (!$land) {
  92. throw new \Exception('土地不存在');
  93. }
  94. // 只在发芽期、生长期和果实期生成灾害
  95. if (!in_array($crop->growth_stage, [ GROWTH_STAGE::SPROUT, GROWTH_STAGE::GROWTH, GROWTH_STAGE::FRUIT ])) {
  96. return null;
  97. }
  98. // 跳过已经有灾害的土地
  99. if ($land->status === LAND_STATUS::DISASTER) {
  100. return null;
  101. }
  102. $userId = $crop->user_id;
  103. $seed = $crop->seed;
  104. // 获取种子的灾害抵抗属性
  105. $disasterResistance = $seed->disaster_resistance ?? [];
  106. // 获取土地的灾害抵抗属性(数据库存储百分比,需要除以100转换为小数)
  107. $landDisasterResistance = ($land->landType->disaster_resistance ?? 0) / 100;
  108. // 检查用户是否有有效的神灵加持
  109. $activeBuffs = FarmGodBuff::where('user_id', $userId)
  110. ->where('expire_time', '>', now())
  111. ->pluck('buff_type')
  112. ->toArray();
  113. // 尝试生成灾害(支持多种灾害)
  114. $disasterInfos = $this->tryGenerateDisasterForCrop($crop, $disasterResistance, $landDisasterResistance, $activeBuffs);
  115. if (!empty($disasterInfos)) {
  116. $disasterDtos = $this->applyDisastersToCrop($crop, $disasterInfos);
  117. Log::info('灾害生成成功', [
  118. 'user_id' => $userId,
  119. 'crop_id' => $crop->id,
  120. 'land_id' => $land->id,
  121. 'disaster_count' => count($disasterInfos),
  122. 'disaster_types' => array_column($disasterInfos, 'type')
  123. ]);
  124. return $disasterDtos[0] ?? null; // 返回第一个灾害信息保持兼容性
  125. }
  126. return null;
  127. } catch (\Exception $e) {
  128. \UCore\Helper\Logger::exception('灾害生成失败', $e, [
  129. 'crop_id' => $cropId,
  130. ]);
  131. return null;
  132. }
  133. }
  134. /**
  135. * 尝试为作物生成灾害(支持多种灾害同时发生)
  136. *
  137. * @param FarmCrop $crop
  138. * @param mixed $disasterResistance
  139. * @param float $landDisasterResistance
  140. * @param array $activeBuffs
  141. * @return array 生成的灾害信息数组
  142. */
  143. private function tryGenerateDisasterForCrop(FarmCrop $crop, $disasterResistance, float $landDisasterResistance, array $activeBuffs): array
  144. {
  145. // 灾害类型及其基础概率
  146. $disasterTypes = DisasterService::getRate();
  147. $generatedDisasters = [];
  148. // 对每种灾害类型都进行判定
  149. foreach ($disasterTypes as $disasterType => $baseProb) {
  150. // 计算最终概率,考虑种子抵抗、土地抵抗和神灵加持
  151. $seedResistance = 0;
  152. if ($disasterResistance) {
  153. if (is_object($disasterResistance) && method_exists($disasterResistance, 'getResistance')) {
  154. // 如果是 DisasterResistanceCast 对象(数据库存储百分比,需要除以100转换为小数)
  155. $seedResistance = $disasterResistance->getResistance($disasterType) / 100;
  156. } elseif (is_array($disasterResistance)) {
  157. // 如果是数组格式(数据库存储百分比,需要除以100转换为小数)
  158. $seedResistance = ($disasterResistance[DisasterService::getDisasterKey($disasterType)] ?? 0) / 100;
  159. }
  160. }
  161. $finalProb = $baseProb - $seedResistance - $landDisasterResistance;
  162. // 如果有对应的神灵加持,则不生成该类型的灾害
  163. $buffType = DISASTER_TYPE::getPreventBuffType($disasterType);
  164. $hasGodBuff = $buffType && in_array($buffType, $activeBuffs);
  165. if ($hasGodBuff) {
  166. $finalProb = 0;
  167. }
  168. // 确保概率在有效范围内
  169. $finalProb = max(0, min(1, $finalProb));
  170. // 生成随机数
  171. $randomNumber = mt_rand(1, 100);
  172. $threshold = $finalProb * 100;
  173. // 详细的debug日志
  174. Log::debug('灾害生成详细信息', [
  175. 'crop_id' => $crop->id,
  176. 'user_id' => $crop->user_id,
  177. 'growth_stage' => $crop->growth_stage->valueInt(),
  178. 'disaster_type' => $disasterType,
  179. 'disaster_type_name' => DISASTER_TYPE::getName($disasterType),
  180. 'base_probability' => $baseProb,
  181. 'seed_resistance' => $seedResistance,
  182. 'land_resistance' => $landDisasterResistance,
  183. 'active_buffs' => $activeBuffs,
  184. 'god_buff_type' => $buffType,
  185. 'has_god_buff' => $hasGodBuff,
  186. 'final_probability' => $finalProb,
  187. 'threshold' => $threshold,
  188. 'random_number' => $randomNumber,
  189. 'will_generate' => $randomNumber <= $threshold,
  190. 'disaster_resistance_type' => is_object($disasterResistance) ? get_class($disasterResistance) : gettype($disasterResistance),
  191. 'disaster_resistance_raw' => is_object($disasterResistance) ? 'object' : $disasterResistance
  192. ]);
  193. // 随机决定是否生成该类型灾害
  194. if ($randomNumber <= $threshold) {
  195. $disasterInfo = [
  196. 'type' => $disasterType,
  197. 'generated_ts' => now()->toDateTimeString(),
  198. 'status' => 'active'
  199. ];
  200. $generatedDisasters[] = $disasterInfo;
  201. Log::debug('灾害生成成功', [
  202. 'crop_id' => $crop->id,
  203. 'disaster_type' => $disasterType,
  204. 'disaster_type_name' => DISASTER_TYPE::getName($disasterType),
  205. 'final_probability' => $finalProb
  206. ]);
  207. } else {
  208. Log::info('灾害未生成', [
  209. 'crop_id' => $crop->id,
  210. 'disaster_type' => $disasterType,
  211. 'disaster_type_name' => DISASTER_TYPE::getName($disasterType),
  212. 'reason' => '随机数超过阈值',
  213. 'random_number' => $randomNumber,
  214. 'threshold' => $threshold,
  215. 'final_probability' => $finalProb
  216. ]);
  217. }
  218. }
  219. return $generatedDisasters;
  220. }
  221. /**
  222. * 将多个灾害应用到作物上
  223. *
  224. * @param FarmCrop $crop
  225. * @param array $disasterInfos
  226. * @return array
  227. */
  228. private function applyDisastersToCrop(FarmCrop $crop, array $disasterInfos): array
  229. {
  230. if (empty($disasterInfos)) {
  231. return [];
  232. }
  233. // 更新作物灾害信息
  234. $disasters = $crop->disasters ?? [];
  235. $disasters = array_merge($disasters, $disasterInfos);
  236. $crop->disasters = $disasters;
  237. // 更新土地状态为灾害
  238. $land = $crop->land;
  239. $oldLandStatus = $land->status;
  240. $land->status = LAND_STATUS::DISASTER->value;
  241. // 保存更改
  242. $crop->save();
  243. $land->save();
  244. // 如果土地状态发生了变化,触发土地状态变更事件
  245. if ($oldLandStatus !== $land->status) {
  246. event(new \App\Module\Farm\Events\LandStatusChangedEvent($crop->user_id, $land->id, $oldLandStatus, $land->status));
  247. }
  248. // 触发灾害生成事件
  249. $disasterDtos = [];
  250. foreach ($disasterInfos as $disasterInfo) {
  251. event(new DisasterGeneratedEvent($crop->user_id, $crop, $disasterInfo['type'], $disasterInfo));
  252. $disasterDtos[] = DisasterInfoDto::fromArray($disasterInfo);
  253. // 记录灾害产生事件
  254. FarmCropLog::logDisasterOccurred($crop->user_id, $crop->land_id, $crop->id, $crop->seed_id, [
  255. 'disaster_type' => $disasterInfo['type'],
  256. 'disaster_info' => $disasterInfo,
  257. 'growth_stage' => $crop->growth_stage->value,
  258. 'land_type' => $land->land_type ?? 1,
  259. 'old_land_status' => $oldLandStatus,
  260. 'new_land_status' => $land->status,
  261. 'generated_at' => $disasterInfo['generated_ts']
  262. ]);
  263. }
  264. return $disasterDtos;
  265. }
  266. /**
  267. * 批量获取需要检查灾害的作物并处理
  268. *
  269. * @param int|null $checkIntervalMinutes 检查间隔(分钟)
  270. * @return array 生成结果统计
  271. */
  272. public function generateDisasterBatchs(?int $checkIntervalMinutes = null): array
  273. {
  274. if ($checkIntervalMinutes === null) {
  275. $checkIntervalMinutes = DisasterService::getCheckInterval();
  276. }
  277. $checkTime = now()->subMinutes($checkIntervalMinutes);
  278. // 获取需要检查灾害的作物:
  279. // 1. 必须在发芽期、生长期或果实期
  280. // 2. 当前阶段可以产生灾害 (can_disaster = 1)
  281. // 3. 满足时间检查条件(首次检查或超过检查间隔)
  282. $crops = FarmCrop::whereIn('growth_stage', [GROWTH_STAGE::SPROUT, GROWTH_STAGE::GROWTH, GROWTH_STAGE::FRUIT])
  283. ->where('can_disaster', true)
  284. ->where(function ($query) use ($checkTime) {
  285. $query->whereNull('last_disaster_check_time')
  286. ->orWhere('last_disaster_check_time', '<', $checkTime);
  287. })
  288. ->with(['land.landType', 'seed', 'user.buffs' => function ($query) {
  289. $query->where('expire_time', '>', now());
  290. }])
  291. ->get();
  292. $totalCount = $crops->count();
  293. $generatedCount = 0;
  294. $skippedCount = 0;
  295. foreach ($crops as $crop) {
  296. $result = $this->generateDisasters($crop);
  297. if ($result === 'generated') {
  298. $generatedCount++;
  299. } elseif ($result === 'skipped') {
  300. $skippedCount++;
  301. }
  302. }
  303. return [
  304. 'total' => $totalCount,
  305. 'generated' => $generatedCount,
  306. 'skipped' => $skippedCount
  307. ];
  308. }
  309. /**
  310. * 为单个作物生成灾害
  311. *
  312. * @param FarmCrop $crop
  313. * @return string 处理结果:'generated', 'skipped', 'failed'
  314. */
  315. public function generateDisasters(FarmCrop $crop): string
  316. {
  317. try {
  318. // 更新检查时间
  319. $crop->last_disaster_check_time = now();
  320. // 跳过已经有灾害的土地
  321. if ($crop->land->status === LAND_STATUS::DISASTER) {
  322. $crop->save(); // 仍需更新检查时间
  323. return 'skipped';
  324. }
  325. // 获取相关数据
  326. $disasterResistance = $crop->seed->disaster_resistance ?? null;
  327. $landDisasterResistance = ($crop->land->landType->disaster_resistance ?? 0) / 100; // 数据库存储百分比,需要除以100
  328. $activeBuffs = $crop->user->buffs->pluck('buff_type')->toArray();
  329. // 尝试生成灾害(支持多种灾害)
  330. $disasterInfos = $this->tryGenerateDisasterForCrop($crop, $disasterResistance, $landDisasterResistance, $activeBuffs);
  331. if (!empty($disasterInfos)) {
  332. // 应用灾害到作物
  333. $this->applyDisastersToCrop($crop, $disasterInfos);
  334. // 生成灾害后,设置当前阶段不能再产生灾害
  335. $crop->can_disaster = false;
  336. $crop->save();
  337. Log::info('单个作物灾害生成成功', [
  338. 'crop_id' => $crop->id,
  339. 'user_id' => $crop->user_id,
  340. 'disaster_count' => count($disasterInfos),
  341. 'disaster_types' => array_column($disasterInfos, 'type')
  342. ]);
  343. return 'generated';
  344. } else {
  345. // 没有生成灾害,但需要保存检查时间
  346. $crop->save();
  347. return 'checked';
  348. }
  349. } catch (\Exception $e) {
  350. Log::error('单个作物灾害生成失败', [
  351. 'crop_id' => $crop->id,
  352. 'error' => $e->getMessage(),
  353. 'trace' => $e->getTraceAsString()
  354. ]);
  355. return 'failed';
  356. }
  357. }
  358. /**
  359. * 批量检查并生成灾害(兼容性方法)
  360. *
  361. * @param int|null $checkIntervalMinutes 检查间隔(分钟)
  362. * @return array 生成结果统计
  363. */
  364. public function batchGenerateDisasters(?int $checkIntervalMinutes = null): array
  365. {
  366. return $this->generateDisasterBatchs($checkIntervalMinutes);
  367. }
  368. }