| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- namespace App\Module\Game\Services;
- use App\Module\Game\Dtos\RewardGroupDto;
- use App\Module\Game\Dtos\RewardResultDto;
- use App\Module\Game\Enums\REWARD_SOURCE_TYPE;
- 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);
- }
- /**
- * 获取奖励组并转换为Protobuf Reward对象
- *
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @return \Uraus\Kku\Common\Reward|null Protobuf Reward对象,不存在时返回null
- */
- public static function getRewardGroupAsReward($groupIdOrCode): ?\Uraus\Kku\Common\Reward
- {
- $rewardGroupDto = self::getRewardGroup($groupIdOrCode);
- if (!$rewardGroupDto) {
- return null;
- }
- return ProtobufConverter::convertRewardGroupToReward($rewardGroupDto);
- }
- /**
- * 发放奖励
- *
- * @param int $userId 用户ID
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @param REWARD_SOURCE_TYPE $sourceType 来源类型枚举
- * @param int $sourceId 来源ID
- * @param int $multiplier 倍率
- * @return RewardResultDto 奖励结果
- */
- public static function grantReward(int $userId, $groupIdOrCode, REWARD_SOURCE_TYPE $sourceType, int $sourceId, int $multiplier = 1): RewardResultDto
- {
- // 验证来源类型是否有效
- if (!REWARD_SOURCE_TYPE::isValid($sourceType->value)) {
- return RewardResultDto::fail("无效的奖励来源类型: {$sourceType->value}");
- }
- $logic = new RewardLogic();
- return $logic->grantReward($userId, $groupIdOrCode, $sourceType->value, $sourceId, $multiplier);
- }
- /**
- * 批量发放奖励
- *
- * @param array $userIds 用户ID数组
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @param REWARD_SOURCE_TYPE $sourceType 来源类型枚举
- * @param int $sourceId 来源ID
- * @return array 奖励结果数组,键为用户ID,值为RewardResultDto
- */
- public static function batchGrantReward(array $userIds, $groupIdOrCode, REWARD_SOURCE_TYPE $sourceType, int $sourceId): array
- {
- // 验证来源类型是否有效
- if (!REWARD_SOURCE_TYPE::isValid($sourceType->value)) {
- $failResult = RewardResultDto::fail("无效的奖励来源类型: {$sourceType->value}");
- return array_fill_keys($userIds, $failResult);
- }
- $results = [];
- $logic = new RewardLogic();
- foreach ($userIds as $userId) {
- $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType->value, $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 REWARD_SOURCE_TYPE $sourceType 来源类型枚举
- * @param int $sourceId 来源ID
- * @param bool $enablePity 是否启用保底机制
- * @return RewardResultDto 奖励结果
- */
- public static function grantRewardWithPity(int $userId, $groupIdOrCode, REWARD_SOURCE_TYPE $sourceType, int $sourceId, bool $enablePity = true): RewardResultDto
- {
- // 验证来源类型是否有效
- if (!REWARD_SOURCE_TYPE::isValid($sourceType->value)) {
- return RewardResultDto::fail("无效的奖励来源类型: {$sourceType->value}");
- }
- $logic = new RewardLogic();
- return $logic->grantRewardWithPity($userId, $groupIdOrCode, $sourceType->value, $sourceId, $enablePity);
- }
- /**
- * 模拟奖励发放(不实际发放,仅返回奖励结果)
- *
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @return RewardResultDto 奖励结果
- */
- public static function simulateReward($groupIdOrCode): RewardResultDto
- {
- $logic = new RewardLogic();
- return $logic->simulateReward($groupIdOrCode);
- }
- /**
- * 批量模拟奖励发放(不实际发放,仅返回奖励结果)
- *
- * @param int|string $groupIdOrCode 奖励组ID或编码
- * @param int $count 模拟次数
- * @return array 奖励结果数组
- */
- public static function batchSimulateReward($groupIdOrCode, int $count): array
- {
- $results = [];
- $logic = new RewardLogic();
- for ($i = 0; $i < $count; $i++) {
- $results[] = $logic->simulateReward($groupIdOrCode);
- }
- return $results;
- }
- /**
- * 获取用户保底状态
- *
- * @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;
- }
- }
|