RewardService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace App\Module\Game\Services;
  3. use App\Module\Game\Dtos\RewardGroupDto;
  4. use App\Module\Game\Dtos\RewardResultDto;
  5. use App\Module\Game\Logics\RewardLogic;
  6. /**
  7. * 奖励服务类
  8. *
  9. * 提供奖励相关的服务,对外接口
  10. */
  11. class RewardService
  12. {
  13. /**
  14. * 获取奖励组信息
  15. *
  16. * @param int|string $groupIdOrCode 奖励组ID或编码
  17. * @return RewardGroupDto|null 奖励组DTO,不存在时返回null
  18. */
  19. public static function getRewardGroup($groupIdOrCode): ?RewardGroupDto
  20. {
  21. $logic = new RewardLogic();
  22. return $logic->getRewardGroup($groupIdOrCode);
  23. }
  24. /**
  25. * 获取奖励组并转换为Protobuf Reward对象
  26. *
  27. * @param int|string $groupIdOrCode 奖励组ID或编码
  28. * @return \Uraus\Kku\Common\Reward|null Protobuf Reward对象,不存在时返回null
  29. */
  30. public static function getRewardGroupAsReward($groupIdOrCode): ?\Uraus\Kku\Common\Reward
  31. {
  32. $rewardGroupDto = self::getRewardGroup($groupIdOrCode);
  33. if (!$rewardGroupDto) {
  34. return null;
  35. }
  36. return ProtobufConverter::convertRewardGroupToReward($rewardGroupDto);
  37. }
  38. /**
  39. * 发放奖励
  40. *
  41. * @param int $userId 用户ID
  42. * @param int|string $groupIdOrCode 奖励组ID或编码
  43. * @param string $sourceType 来源类型(任务、活动、签到等)
  44. * @param int $sourceId 来源ID
  45. * @return RewardResultDto 奖励结果
  46. */
  47. public static function grantReward(int $userId, $groupIdOrCode, string $sourceType, int $sourceId ,int $multiplier = 1): RewardResultDto
  48. {
  49. $logic = new RewardLogic();
  50. return $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId,$multiplier);
  51. }
  52. /**
  53. * 批量发放奖励
  54. *
  55. * @param array $userIds 用户ID数组
  56. * @param int|string $groupIdOrCode 奖励组ID或编码
  57. * @param string $sourceType 来源类型
  58. * @param int $sourceId 来源ID
  59. * @return array 奖励结果数组,键为用户ID,值为RewardResultDto
  60. */
  61. public static function batchGrantReward(array $userIds, $groupIdOrCode, string $sourceType, int $sourceId): array
  62. {
  63. $results = [];
  64. $logic = new RewardLogic();
  65. foreach ($userIds as $userId) {
  66. $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
  67. }
  68. return $results;
  69. }
  70. /**
  71. * 检查奖励组是否存在
  72. *
  73. * @param int|string $groupIdOrCode 奖励组ID或编码
  74. * @return bool 是否存在
  75. */
  76. public static function rewardGroupExists($groupIdOrCode): bool
  77. {
  78. return self::getRewardGroup($groupIdOrCode) !== null;
  79. }
  80. /**
  81. * 发放奖励(支持保底机制)
  82. *
  83. * @param int $userId 用户ID
  84. * @param int|string $groupIdOrCode 奖励组ID或编码
  85. * @param string $sourceType 来源类型(任务、活动、签到等)
  86. * @param int $sourceId 来源ID
  87. * @param bool $enablePity 是否启用保底机制
  88. * @return RewardResultDto 奖励结果
  89. */
  90. public static function grantRewardWithPity(int $userId, $groupIdOrCode, string $sourceType, int $sourceId, bool $enablePity = true): RewardResultDto
  91. {
  92. $logic = new RewardLogic();
  93. return $logic->grantRewardWithPity($userId, $groupIdOrCode, $sourceType, $sourceId, $enablePity);
  94. }
  95. /**
  96. * 模拟奖励发放(不实际发放,仅返回奖励结果)
  97. *
  98. * @param int|string $groupIdOrCode 奖励组ID或编码
  99. * @return RewardResultDto 奖励结果
  100. */
  101. public static function simulateReward($groupIdOrCode): RewardResultDto
  102. {
  103. $logic = new RewardLogic();
  104. return $logic->simulateReward($groupIdOrCode);
  105. }
  106. /**
  107. * 批量模拟奖励发放(不实际发放,仅返回奖励结果)
  108. *
  109. * @param int|string $groupIdOrCode 奖励组ID或编码
  110. * @param int $count 模拟次数
  111. * @return array 奖励结果数组
  112. */
  113. public static function batchSimulateReward($groupIdOrCode, int $count): array
  114. {
  115. $results = [];
  116. $logic = new RewardLogic();
  117. for ($i = 0; $i < $count; $i++) {
  118. $results[] = $logic->simulateReward($groupIdOrCode);
  119. }
  120. return $results;
  121. }
  122. /**
  123. * 获取用户保底状态
  124. *
  125. * @param int $userId 用户ID
  126. * @param int|string $groupIdOrCode 奖励组ID或编码
  127. * @return array 保底状态信息
  128. */
  129. public static function getUserPityStatus(int $userId, $groupIdOrCode): array
  130. {
  131. $rewardGroup = self::getRewardGroup($groupIdOrCode);
  132. if (!$rewardGroup) {
  133. return [];
  134. }
  135. return PityService::getPityStatus($userId, $rewardGroup->id);
  136. }
  137. /**
  138. * 重置用户保底计数
  139. *
  140. * @param int $userId 用户ID
  141. * @param int|string $groupIdOrCode 奖励组ID或编码
  142. * @return bool 是否成功
  143. */
  144. public static function resetUserPity(int $userId, $groupIdOrCode): bool
  145. {
  146. $rewardGroup = self::getRewardGroup($groupIdOrCode);
  147. if (!$rewardGroup) {
  148. return false;
  149. }
  150. PityService::resetAllPityCounts($userId, $rewardGroup->id);
  151. return true;
  152. }
  153. }