PetExpRewardProcessor.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. use ReflectionClass;
  7. /**
  8. * 宠物经验奖励处理器
  9. *
  10. * 处理宠物经验(PET_EXP)类型的奖励发放
  11. */
  12. class PetExpRewardProcessor
  13. {
  14. /**
  15. * 处理宠物经验奖励
  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. $pet = \App\Module\Pet\Models\PetUser::where('user_id', $userId)
  29. ->where('id', $item->targetId)
  30. ->first();
  31. if (!$pet) {
  32. throw new Exception("找不到ID为 {$item->targetId} 的宠物");
  33. }
  34. // 使用宠物逻辑类来增加经验
  35. $petLogic = new \App\Module\Pet\Logic\PetLogic();
  36. $reflection = new ReflectionClass($petLogic);
  37. $method = $reflection->getMethod('addExperience');
  38. $method->setAccessible(true);
  39. $levelUpOccurred = $method->invoke($petLogic, $item->targetId, $item->quantity);
  40. Log::info("宠物经验奖励发放成功", [
  41. 'userId' => $userId,
  42. 'petId' => $item->targetId,
  43. 'expAmount' => $item->quantity,
  44. 'levelUp' => $levelUpOccurred,
  45. 'sourceType' => $sourceType,
  46. 'sourceId' => $sourceId
  47. ]);
  48. } catch (Exception $e) {
  49. Log::error("宠物经验奖励发放失败", [
  50. 'userId' => $userId,
  51. 'petId' => $item->targetId,
  52. 'expAmount' => $item->quantity,
  53. 'sourceType' => $sourceType,
  54. 'sourceId' => $sourceId,
  55. 'error' => $e->getMessage()
  56. ]);
  57. throw new Exception("宠物经验奖励发放失败: " . $e->getMessage());
  58. }
  59. }
  60. }