BuffLogic.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. namespace App\Module\Farm\Logics;
  3. use App\Module\Farm\Enums\BUFF_TYPE;
  4. use App\Module\Farm\Events\BuffActivatedEvent;
  5. use App\Module\Farm\Models\FarmGodBuff;
  6. use Illuminate\Support\Collection;
  7. use Illuminate\Support\Facades\Log;
  8. /**
  9. * 神灵加持逻辑
  10. */
  11. class BuffLogic
  12. {
  13. /**
  14. * 获取用户的所有神灵加持
  15. *
  16. * @param int $userId
  17. * @return Collection
  18. */
  19. public function getUserBuffs(int $userId): Collection
  20. {
  21. try {
  22. return FarmGodBuff::where('user_id', $userId)
  23. ->orderBy('buff_type')
  24. ->get();
  25. } catch (\Exception $e) {
  26. Log::error('获取用户神灵加持失败', [
  27. 'user_id' => $userId,
  28. 'error' => $e->getMessage(),
  29. 'trace' => $e->getTraceAsString()
  30. ]);
  31. return collect();
  32. }
  33. }
  34. /**
  35. * 获取用户的有效神灵加持
  36. *
  37. * @param int $userId
  38. * @return Collection
  39. */
  40. public function getActiveBuffs(int $userId): Collection
  41. {
  42. try {
  43. return FarmGodBuff::where('user_id', $userId)
  44. ->where('expire_time', '>', now())
  45. ->orderBy('buff_type')
  46. ->get();
  47. } catch (\Exception $e) {
  48. Log::error('获取用户有效神灵加持失败', [
  49. 'user_id' => $userId,
  50. 'error' => $e->getMessage(),
  51. 'trace' => $e->getTraceAsString()
  52. ]);
  53. return collect();
  54. }
  55. }
  56. /**
  57. * 获取用户指定类型的神灵加持
  58. *
  59. * @param int $userId
  60. * @param int $buffType
  61. * @return FarmGodBuff|null
  62. */
  63. public function getUserBuff(int $userId, int $buffType): ?FarmGodBuff
  64. {
  65. try {
  66. return FarmGodBuff::where('user_id', $userId)
  67. ->where('buff_type', $buffType)
  68. ->first();
  69. } catch (\Exception $e) {
  70. Log::error('获取用户指定类型神灵加持失败', [
  71. 'user_id' => $userId,
  72. 'buff_type' => $buffType,
  73. 'error' => $e->getMessage(),
  74. 'trace' => $e->getTraceAsString()
  75. ]);
  76. return null;
  77. }
  78. }
  79. /**
  80. * 获取用户指定类型的有效神灵加持
  81. *
  82. * @param int $userId
  83. * @param int $buffType
  84. * @return FarmGodBuff|null
  85. */
  86. public function getActiveUserBuff(int $userId, int $buffType): ?FarmGodBuff
  87. {
  88. try {
  89. return FarmGodBuff::where('user_id', $userId)
  90. ->where('buff_type', $buffType)
  91. ->where('expire_time', '>', now())
  92. ->first();
  93. } catch (\Exception $e) {
  94. Log::error('获取用户指定类型有效神灵加持失败', [
  95. 'user_id' => $userId,
  96. 'buff_type' => $buffType,
  97. 'error' => $e->getMessage(),
  98. 'trace' => $e->getTraceAsString()
  99. ]);
  100. return null;
  101. }
  102. }
  103. /**
  104. * 激活神灵加持
  105. *
  106. * @param int $userId
  107. * @param int $buffType
  108. * @param int $durationHours
  109. * @return FarmGodBuff|null
  110. */
  111. public function activateBuff(int $userId, int $buffType, int $durationHours): ?FarmGodBuff
  112. {
  113. try {
  114. // 检查buff类型是否有效
  115. if (!in_array($buffType, [
  116. BUFF_TYPE::HARVEST_GOD,
  117. BUFF_TYPE::RAIN_GOD,
  118. BUFF_TYPE::WEED_KILLER_GOD,
  119. BUFF_TYPE::PEST_CLEANER_GOD
  120. ])) {
  121. throw new \Exception('无效的神灵加持类型');
  122. }
  123. // 获取当前buff
  124. $buff = FarmGodBuff::where('user_id', $userId)
  125. ->where('buff_type', $buffType)
  126. ->first();
  127. $now = now();
  128. $expireTime = $now->copy()->addHours($durationHours);
  129. if ($buff) {
  130. // 如果已有buff,延长时间
  131. if ($buff->expire_time > $now) {
  132. // 如果buff未过期,在当前过期时间基础上延长
  133. $expireTime = $buff->expire_time->addHours($durationHours);
  134. }
  135. $buff->expire_time = $expireTime;
  136. $buff->save();
  137. } else {
  138. // 创建新buff
  139. $buff = new FarmGodBuff();
  140. $buff->user_id = $userId;
  141. $buff->buff_type = $buffType;
  142. $buff->expire_time = $expireTime;
  143. $buff->save();
  144. }
  145. // 触发buff激活事件
  146. event(new BuffActivatedEvent($userId, $buff));
  147. Log::info('神灵加持激活成功', [
  148. 'user_id' => $userId,
  149. 'buff_type' => $buffType,
  150. 'duration_hours' => $durationHours,
  151. 'expire_time' => $expireTime->toDateTimeString()
  152. ]);
  153. return $buff;
  154. } catch (\Exception $e) {
  155. Log::error('神灵加持激活失败', [
  156. 'user_id' => $userId,
  157. 'buff_type' => $buffType,
  158. 'duration_hours' => $durationHours,
  159. 'error' => $e->getMessage(),
  160. 'trace' => $e->getTraceAsString()
  161. ]);
  162. return null;
  163. }
  164. }
  165. /**
  166. * 清理过期的神灵加持
  167. *
  168. * @return int
  169. */
  170. public function clearExpiredBuffs(): int
  171. {
  172. try {
  173. $count = FarmGodBuff::where('expire_time', '<', now())->delete();
  174. Log::info('清理过期神灵加持成功', [
  175. 'count' => $count
  176. ]);
  177. return $count;
  178. } catch (\Exception $e) {
  179. Log::error('清理过期神灵加持失败', [
  180. 'error' => $e->getMessage(),
  181. 'trace' => $e->getTraceAsString()
  182. ]);
  183. return 0;
  184. }
  185. }
  186. }