BuffService.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Module\Farm\Services;
  3. use App\Module\Farm\Logics\BuffLogic;
  4. use App\Module\Farm\Models\FarmGodBuff;
  5. use Illuminate\Support\Collection;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 神灵加持服务
  9. */
  10. class BuffService
  11. {
  12. /**
  13. * 获取用户的所有神灵加持
  14. *
  15. * @param int $userId
  16. * @return Collection
  17. */
  18. public static function getUserBuffs(int $userId): Collection
  19. {
  20. try {
  21. $buffLogic = new BuffLogic();
  22. return $buffLogic->getUserBuffs($userId);
  23. } catch (\Exception $e) {
  24. Log::error('获取用户神灵加持失败', [
  25. 'user_id' => $userId,
  26. 'error' => $e->getMessage(),
  27. 'trace' => $e->getTraceAsString()
  28. ]);
  29. return collect();
  30. }
  31. }
  32. /**
  33. * 获取用户的有效神灵加持
  34. *
  35. * @param int $userId
  36. * @return Collection
  37. */
  38. public static function getActiveBuffs(int $userId): Collection
  39. {
  40. try {
  41. $buffLogic = new BuffLogic();
  42. return $buffLogic->getActiveBuffs($userId);
  43. } catch (\Exception $e) {
  44. Log::error('获取用户有效神灵加持失败', [
  45. 'user_id' => $userId,
  46. 'error' => $e->getMessage(),
  47. 'trace' => $e->getTraceAsString()
  48. ]);
  49. return collect();
  50. }
  51. }
  52. /**
  53. * 获取用户指定类型的神灵加持
  54. *
  55. * @param int $userId
  56. * @param int $buffType
  57. * @return FarmGodBuff|null
  58. */
  59. public static function getUserBuff(int $userId, int $buffType): ?FarmGodBuff
  60. {
  61. try {
  62. $buffLogic = new BuffLogic();
  63. return $buffLogic->getUserBuff($userId, $buffType);
  64. } catch (\Exception $e) {
  65. Log::error('获取用户指定类型神灵加持失败', [
  66. 'user_id' => $userId,
  67. 'buff_type' => $buffType,
  68. 'error' => $e->getMessage(),
  69. 'trace' => $e->getTraceAsString()
  70. ]);
  71. return null;
  72. }
  73. }
  74. /**
  75. * 获取用户指定类型的有效神灵加持
  76. *
  77. * @param int $userId
  78. * @param int $buffType
  79. * @return FarmGodBuff|null
  80. */
  81. public static function getActiveUserBuff(int $userId, int $buffType): ?FarmGodBuff
  82. {
  83. try {
  84. $buffLogic = new BuffLogic();
  85. return $buffLogic->getActiveUserBuff($userId, $buffType);
  86. } catch (\Exception $e) {
  87. Log::error('获取用户指定类型有效神灵加持失败', [
  88. 'user_id' => $userId,
  89. 'buff_type' => $buffType,
  90. 'error' => $e->getMessage(),
  91. 'trace' => $e->getTraceAsString()
  92. ]);
  93. return null;
  94. }
  95. }
  96. /**
  97. * 激活神灵加持
  98. *
  99. * @param int $userId
  100. * @param int $buffType
  101. * @param int $durationHours
  102. * @return FarmGodBuff|null
  103. */
  104. public static function activateBuff(int $userId, int $buffType, int $durationHours): ?FarmGodBuff
  105. {
  106. try {
  107. $buffLogic = new BuffLogic();
  108. return $buffLogic->activateBuff($userId, $buffType, $durationHours);
  109. } catch (\Exception $e) {
  110. Log::error('激活神灵加持失败', [
  111. 'user_id' => $userId,
  112. 'buff_type' => $buffType,
  113. 'duration_hours' => $durationHours,
  114. 'error' => $e->getMessage(),
  115. 'trace' => $e->getTraceAsString()
  116. ]);
  117. return null;
  118. }
  119. }
  120. }