PetRewardProcessor.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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)类型的奖励发放
  10. */
  11. class PetRewardProcessor
  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. $petConfigId = $item->targetId; // 宠物配置ID或宠物类型
  28. $petCount = $item->quantity > 0 ? $item->quantity : 1; // 宠物数量,默认1只
  29. $petGrade = $item->param1 > 0 ? $item->param1 : null; // 宠物品阶,为null时随机生成
  30. // 验证宠物配置是否存在
  31. $petConfig = \App\Module\Pet\Models\PetConfig::find($petConfigId);
  32. if (!$petConfig) {
  33. throw new Exception("宠物配置不存在,配置ID: {$petConfigId}");
  34. }
  35. // 循环创建宠物(支持一次奖励多只宠物)
  36. $createdPets = [];
  37. for ($i = 0; $i < $petCount; $i++) {
  38. // 生成宠物名称
  39. $petName = self::generatePetName($petConfig->name, $i + 1, $petCount);
  40. // 创建宠物
  41. $result = \App\Module\Pet\Services\PetService::createPet($userId, $petName, $petGrade, [
  42. 'pet_type' => $petConfig->pet_type,
  43. 'source' => 'reward',
  44. 'source_type' => $sourceType,
  45. 'source_id' => $sourceId
  46. ]);
  47. if (!$result['success']) {
  48. throw new Exception("宠物创建失败: " . ($result['message'] ?? '未知错误'));
  49. }
  50. $createdPets[] = [
  51. 'pet_id' => $result['pet_id'],
  52. 'name' => $petName,
  53. 'grade' => $result['grade']
  54. ];
  55. Log::info("宠物奖励创建成功", [
  56. 'userId' => $userId,
  57. 'petId' => $result['pet_id'],
  58. 'petName' => $petName,
  59. 'petGrade' => $result['grade'],
  60. 'petConfigId' => $petConfigId,
  61. 'petType' => $petConfig->pet_type,
  62. 'index' => $i + 1,
  63. 'totalCount' => $petCount,
  64. 'sourceType' => $sourceType,
  65. 'sourceId' => $sourceId
  66. ]);
  67. }
  68. Log::info("宠物奖励发放完成", [
  69. 'userId' => $userId,
  70. 'petConfigId' => $petConfigId,
  71. 'petType' => $petConfig->pet_type,
  72. 'petCount' => $petCount,
  73. 'petGrade' => $petGrade,
  74. 'createdPets' => $createdPets,
  75. 'sourceType' => $sourceType,
  76. 'sourceId' => $sourceId
  77. ]);
  78. } catch (Exception $e) {
  79. Log::error("宠物奖励发放失败", [
  80. 'userId' => $userId,
  81. 'petConfigId' => $item->targetId,
  82. 'petCount' => $item->quantity,
  83. 'petGrade' => $item->param1,
  84. 'sourceType' => $sourceType,
  85. 'sourceId' => $sourceId,
  86. 'error' => $e->getMessage()
  87. ]);
  88. throw new Exception("宠物奖励发放失败: " . $e->getMessage());
  89. }
  90. }
  91. /**
  92. * 生成宠物名称
  93. *
  94. * @param string $baseName 基础名称
  95. * @param int $index 索引
  96. * @param int $total 总数
  97. * @return string 生成的宠物名称
  98. */
  99. private static function generatePetName(string $baseName, int $index, int $total): string
  100. {
  101. if ($total == 1) {
  102. return $baseName;
  103. }
  104. return $baseName . $index;
  105. }
  106. }