FarmShrineRewardProcessor.php 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace App\Module\Game\Logics\RewardProcessors;
  3. use App\Module\Game\Dtos\RewardItemDto;
  4. use App\Module\Game\Services\RewardCollectorService;
  5. use Exception;
  6. use Illuminate\Support\Facades\Log;
  7. /**
  8. * 神像奖励处理器
  9. *
  10. * 处理神像(FARM_SHRINE)类型的奖励发放
  11. */
  12. class FarmShrineRewardProcessor
  13. {
  14. /**
  15. * 处理神像奖励(农场buff)
  16. *
  17. * @param int $userId 用户ID
  18. * @param RewardItemDto $item 奖励项
  19. * @param string $sourceType 来源类型
  20. * @param int $sourceId 来源ID
  21. * @return void
  22. * @throws Exception
  23. */
  24. public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
  25. {
  26. try {
  27. // 获取神像类型和持续时间
  28. $shrineType = $item->targetId; // 神像类型(1=丰收之神,2=雨露之神,3=屠草之神,4=拭虫之神)
  29. $durationHours = $item->param2 > 0 ? $item->param2 : 24; // 持续时间(小时),默认24小时
  30. $activationCount = $item->quantity > 0 ? $item->quantity : 1; // 激活次数,默认1次
  31. // 验证神像类型是否有效
  32. if (!in_array($shrineType, [1, 2, 3, 4])) {
  33. throw new Exception("无效的神像类型: {$shrineType}");
  34. }
  35. // 验证用户是否存在
  36. $farmUser = \App\Module\Farm\Models\FarmUser::where('user_id', $userId)->first();
  37. if (!$farmUser) {
  38. throw new Exception("用户农场数据不存在,用户ID: {$userId}");
  39. }
  40. // 循环激活神像(支持多次激活)
  41. for ($i = 0; $i < $activationCount; $i++) {
  42. // 使用BuffService激活神像
  43. $buff = \App\Module\Farm\Services\BuffService::activateBuff($userId, $shrineType, $durationHours);
  44. if (!$buff) {
  45. throw new Exception("神像激活失败,神像类型: {$shrineType},用户ID: {$userId}");
  46. }
  47. Log::info("神像奖励激活成功", [
  48. 'userId' => $userId,
  49. 'shrineType' => $shrineType,
  50. 'shrineName' => \App\Module\Farm\Enums\BUFF_TYPE::getName($shrineType),
  51. 'durationHours' => $durationHours,
  52. 'activationIndex' => $i + 1,
  53. 'totalActivations' => $activationCount,
  54. 'expireTime' => $buff->expire_time ? $buff->expire_time->toDateTimeString() : null,
  55. 'sourceType' => $sourceType,
  56. 'sourceId' => $sourceId
  57. ]);
  58. }
  59. // 收集神像奖励到Response.Reward中
  60. RewardCollectorService::addGodReward(
  61. $userId,
  62. $shrineType,
  63. $durationHours * 3600, // 转换为秒
  64. $activationCount
  65. );
  66. Log::info("神像奖励发放完成", [
  67. 'userId' => $userId,
  68. 'shrineType' => $shrineType,
  69. 'shrineName' => \App\Module\Farm\Enums\BUFF_TYPE::getName($shrineType),
  70. 'durationHours' => $durationHours,
  71. 'totalActivations' => $activationCount,
  72. 'sourceType' => $sourceType,
  73. 'sourceId' => $sourceId
  74. ]);
  75. } catch (Exception $e) {
  76. Log::error("神像奖励发放失败", [
  77. 'userId' => $userId,
  78. 'shrineType' => $item->targetId,
  79. 'durationHours' => $item->param2,
  80. 'quantity' => $item->quantity,
  81. 'sourceType' => $sourceType,
  82. 'sourceId' => $sourceId,
  83. 'error' => $e->getMessage()
  84. ]);
  85. throw new Exception("神像奖励发放失败: " . $e->getMessage());
  86. }
  87. }
  88. }