RewardService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 $userId 用户ID
  85. * @param int|string $groupIdOrCode 奖励组ID或编码
  86. * @return array 保底状态信息
  87. */
  88. public static function getUserPityStatus(int $userId, $groupIdOrCode): array
  89. {
  90. $rewardGroup = self::getRewardGroup($groupIdOrCode);
  91. if (!$rewardGroup) {
  92. return [];
  93. }
  94. return PityService::getPityStatus($userId, $rewardGroup->id);
  95. }
  96. /**
  97. * 重置用户保底计数
  98. *
  99. * @param int $userId 用户ID
  100. * @param int|string $groupIdOrCode 奖励组ID或编码
  101. * @return bool 是否成功
  102. */
  103. public static function resetUserPity(int $userId, $groupIdOrCode): bool
  104. {
  105. $rewardGroup = self::getRewardGroup($groupIdOrCode);
  106. if (!$rewardGroup) {
  107. return false;
  108. }
  109. PityService::resetAllPityCounts($userId, $rewardGroup->id);
  110. return true;
  111. }
  112. }