PetRewardProcessor.php 3.7 KB

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