RewardService.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * 发放奖励
  26. *
  27. * @param int $userId 用户ID
  28. * @param int|string $groupIdOrCode 奖励组ID或编码
  29. * @param string $sourceType 来源类型(任务、活动、签到等)
  30. * @param int $sourceId 来源ID
  31. * @return RewardResultDto 奖励结果
  32. */
  33. public static function grantReward(int $userId, $groupIdOrCode, string $sourceType, int $sourceId): RewardResultDto
  34. {
  35. $logic = new RewardLogic();
  36. return $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
  37. }
  38. /**
  39. * 批量发放奖励
  40. *
  41. * @param array $userIds 用户ID数组
  42. * @param int|string $groupIdOrCode 奖励组ID或编码
  43. * @param string $sourceType 来源类型
  44. * @param int $sourceId 来源ID
  45. * @return array 奖励结果数组,键为用户ID,值为RewardResultDto
  46. */
  47. public static function batchGrantReward(array $userIds, $groupIdOrCode, string $sourceType, int $sourceId): array
  48. {
  49. $results = [];
  50. $logic = new RewardLogic();
  51. foreach ($userIds as $userId) {
  52. $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
  53. }
  54. return $results;
  55. }
  56. /**
  57. * 检查奖励组是否存在
  58. *
  59. * @param int|string $groupIdOrCode 奖励组ID或编码
  60. * @return bool 是否存在
  61. */
  62. public static function rewardGroupExists($groupIdOrCode): bool
  63. {
  64. return self::getRewardGroup($groupIdOrCode) !== null;
  65. }
  66. /**
  67. * 发放奖励(支持保底机制)
  68. *
  69. * @param int $userId 用户ID
  70. * @param int|string $groupIdOrCode 奖励组ID或编码
  71. * @param string $sourceType 来源类型(任务、活动、签到等)
  72. * @param int $sourceId 来源ID
  73. * @param bool $enablePity 是否启用保底机制
  74. * @return RewardResultDto 奖励结果
  75. */
  76. public static function grantRewardWithPity(int $userId, $groupIdOrCode, string $sourceType, int $sourceId, bool $enablePity = true): RewardResultDto
  77. {
  78. $logic = new RewardLogic();
  79. return $logic->grantRewardWithPity($userId, $groupIdOrCode, $sourceType, $sourceId, $enablePity);
  80. }
  81. /**
  82. * 模拟奖励发放(不实际发放,仅返回奖励结果)
  83. *
  84. * @param int|string $groupIdOrCode 奖励组ID或编码
  85. * @return RewardResultDto 奖励结果
  86. */
  87. public static function simulateReward($groupIdOrCode): RewardResultDto
  88. {
  89. $logic = new RewardLogic();
  90. return $logic->simulateReward($groupIdOrCode);
  91. }
  92. /**
  93. * 批量模拟奖励发放(不实际发放,仅返回奖励结果)
  94. *
  95. * @param int|string $groupIdOrCode 奖励组ID或编码
  96. * @param int $count 模拟次数
  97. * @return array 奖励结果数组
  98. */
  99. public static function batchSimulateReward($groupIdOrCode, int $count): array
  100. {
  101. $results = [];
  102. $logic = new RewardLogic();
  103. for ($i = 0; $i < $count; $i++) {
  104. $results[] = $logic->simulateReward($groupIdOrCode);
  105. }
  106. return $results;
  107. }
  108. /**
  109. * 获取用户保底状态
  110. *
  111. * @param int $userId 用户ID
  112. * @param int|string $groupIdOrCode 奖励组ID或编码
  113. * @return array 保底状态信息
  114. */
  115. public static function getUserPityStatus(int $userId, $groupIdOrCode): array
  116. {
  117. $rewardGroup = self::getRewardGroup($groupIdOrCode);
  118. if (!$rewardGroup) {
  119. return [];
  120. }
  121. return PityService::getPityStatus($userId, $rewardGroup->id);
  122. }
  123. /**
  124. * 重置用户保底计数
  125. *
  126. * @param int $userId 用户ID
  127. * @param int|string $groupIdOrCode 奖励组ID或编码
  128. * @return bool 是否成功
  129. */
  130. public static function resetUserPity(int $userId, $groupIdOrCode): bool
  131. {
  132. $rewardGroup = self::getRewardGroup($groupIdOrCode);
  133. if (!$rewardGroup) {
  134. return false;
  135. }
  136. PityService::resetAllPityCounts($userId, $rewardGroup->id);
  137. return true;
  138. }
  139. }