PetExpRewardProcessor.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace App\Module\Game\Logics\RewardProcessors;
  3. use App\Module\Game\Dtos\RewardItemDto;
  4. use Exception;
  5. use Illuminate\Support\Facades\Log;
  6. /**
  7. * 宠物经验奖励处理器
  8. *
  9. * 处理宠物经验(PET_EXP)类型的奖励发放
  10. */
  11. class PetExpRewardProcessor
  12. {
  13. /**
  14. * 处理宠物经验奖励
  15. *
  16. * @param int $userId 用户ID
  17. * @param RewardItemDto $item 奖励项
  18. * @param string $sourceType 来源类型
  19. * @param int $sourceId 来源ID
  20. * @return void
  21. * @throws Exception
  22. */
  23. public static function process(int $userId, RewardItemDto $item, string $sourceType, int $sourceId): void
  24. {
  25. try {
  26. // 获取用户的宠物
  27. $pet = \App\Module\Pet\Models\PetUser::where('user_id', $userId)
  28. ->where('id', $item->targetId)
  29. ->first();
  30. if (!$pet) {
  31. throw new Exception("找不到ID为 {$item->targetId} 的宠物");
  32. }
  33. // 使用宠物逻辑类来增加经验
  34. $petLogic = new \App\Module\Pet\Logic\PetLogic();
  35. $levelUpOccurred = $petLogic->addExperienceReward($item->targetId, $item->quantity, $sourceType, $sourceId);
  36. Log::info("宠物经验奖励发放成功", [
  37. 'userId' => $userId,
  38. 'petId' => $item->targetId,
  39. 'expAmount' => $item->quantity,
  40. 'levelUp' => $levelUpOccurred,
  41. 'sourceType' => $sourceType,
  42. 'sourceId' => $sourceId
  43. ]);
  44. } catch (Exception $e) {
  45. Log::error("宠物经验奖励发放失败", [
  46. 'userId' => $userId,
  47. 'petId' => $item->targetId,
  48. 'expAmount' => $item->quantity,
  49. 'sourceType' => $sourceType,
  50. 'sourceId' => $sourceId,
  51. 'error' => $e->getMessage()
  52. ]);
  53. throw new Exception("宠物经验奖励发放失败: " . $e->getMessage());
  54. }
  55. }
  56. }