| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Game\Dtos\RewardGroupDto;
- use App\Module\Game\Dtos\RewardResultDto;
- use App\Module\Game\Logics\RewardLogic;
- /**
- * 奖励服务类
- *
- * 提供奖励相关的服务,对外接口
- */
- class RewardService
- {
- /**
- * 获取奖励组信息
- *
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @return RewardGroupDto|null 奖励组DTO,不存在时返回null
- */
- public static function getRewardGroup($groupIdOrCode): ?RewardGroupDto
- {
- $logic = new RewardLogic();
- return $logic->getRewardGroup($groupIdOrCode);
- }
- /**
- * 发放奖励
- *
- * @param int $userId 用户ID
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @param string $sourceType 来源类型(任务、活动、签到等)
- * @param int $sourceId 来源ID
- * @return RewardResultDto 奖励结果
- */
- public static function grantReward(int $userId, $groupIdOrCode, string $sourceType, int $sourceId): RewardResultDto
- {
- $logic = new RewardLogic();
- return $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
- }
- /**
- * 批量发放奖励
- *
- * @param array $userIds 用户ID数组
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @param string $sourceType 来源类型
- * @param int $sourceId 来源ID
- * @return array 奖励结果数组,键为用户ID,值为RewardResultDto
- */
- public static function batchGrantReward(array $userIds, $groupIdOrCode, string $sourceType, int $sourceId): array
- {
- $results = [];
- $logic = new RewardLogic();
- foreach ($userIds as $userId) {
- $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
- }
- return $results;
- }
- /**
- * 检查奖励组是否存在
- *
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @return bool 是否存在
- */
- public static function rewardGroupExists($groupIdOrCode): bool
- {
- return self::getRewardGroup($groupIdOrCode) !== null;
- }
- /**
- * 发放奖励(支持保底机制)
- *
- * @param int $userId 用户ID
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @param string $sourceType 来源类型(任务、活动、签到等)
- * @param int $sourceId 来源ID
- * @param bool $enablePity 是否启用保底机制
- * @return RewardResultDto 奖励结果
- */
- public static function grantRewardWithPity(int $userId, $groupIdOrCode, string $sourceType, int $sourceId, bool $enablePity = true): RewardResultDto
- {
- $logic = new RewardLogic();
- return $logic->grantRewardWithPity($userId, $groupIdOrCode, $sourceType, $sourceId, $enablePity);
- }
- /**
- * 获取用户保底状态
- *
- * @param int $userId 用户ID
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @return array 保底状态信息
- */
- public static function getUserPityStatus(int $userId, $groupIdOrCode): array
- {
- $rewardGroup = self::getRewardGroup($groupIdOrCode);
- if (!$rewardGroup) {
- return [];
- }
- return PityService::getPityStatus($userId, $rewardGroup->id);
- }
- /**
- * 重置用户保底计数
- *
- * @param int $userId 用户ID
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @return bool 是否成功
- */
- public static function resetUserPity(int $userId, $groupIdOrCode): bool
- {
- $rewardGroup = self::getRewardGroup($groupIdOrCode);
- if (!$rewardGroup) {
- return false;
- }
- PityService::resetAllPityCounts($userId, $rewardGroup->id);
- return true;
- }
- }
|