DisasterLogic.php 16 KB

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