|
|
@@ -4,6 +4,7 @@ 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;
|
|
|
|
|
|
/**
|
|
|
@@ -47,14 +48,20 @@ class RewardService
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int|string $groupIdOrCode 奖励组ID或编码
|
|
|
- * @param string $sourceType 来源类型(任务、活动、签到等)
|
|
|
+ * @param REWARD_SOURCE_TYPE $sourceType 来源类型枚举
|
|
|
* @param int $sourceId 来源ID
|
|
|
+ * @param int $multiplier 倍率
|
|
|
* @return RewardResultDto 奖励结果
|
|
|
*/
|
|
|
- public static function grantReward(int $userId, $groupIdOrCode, string $sourceType, int $sourceId ,int $multiplier = 1): 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, $sourceId,$multiplier);
|
|
|
+ return $logic->grantReward($userId, $groupIdOrCode, $sourceType->value, $sourceId, $multiplier);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -62,17 +69,23 @@ class RewardService
|
|
|
*
|
|
|
* @param array $userIds 用户ID数组
|
|
|
* @param int|string $groupIdOrCode 奖励组ID或编码
|
|
|
- * @param string $sourceType 来源类型
|
|
|
+ * @param REWARD_SOURCE_TYPE $sourceType 来源类型枚举
|
|
|
* @param int $sourceId 来源ID
|
|
|
* @return array 奖励结果数组,键为用户ID,值为RewardResultDto
|
|
|
*/
|
|
|
- public static function batchGrantReward(array $userIds, $groupIdOrCode, string $sourceType, int $sourceId): array
|
|
|
+ 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, $sourceId);
|
|
|
+ $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType->value, $sourceId);
|
|
|
}
|
|
|
|
|
|
return $results;
|
|
|
@@ -94,15 +107,20 @@ class RewardService
|
|
|
*
|
|
|
* @param int $userId 用户ID
|
|
|
* @param int|string $groupIdOrCode 奖励组ID或编码
|
|
|
- * @param string $sourceType 来源类型(任务、活动、签到等)
|
|
|
+ * @param REWARD_SOURCE_TYPE $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
|
|
|
+ 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, $sourceId, $enablePity);
|
|
|
+ return $logic->grantRewardWithPity($userId, $groupIdOrCode, $sourceType->value, $sourceId, $enablePity);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -170,4 +188,93 @@ class RewardService
|
|
|
PityService::resetAllPityCounts($userId, $rewardGroup->id);
|
|
|
return true;
|
|
|
}
|
|
|
+
|
|
|
+ // ==================== 兼容性方法(已废弃,建议使用枚举版本) ====================
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发放奖励(兼容性方法,已废弃)
|
|
|
+ *
|
|
|
+ * @deprecated 请使用 grantReward(int $userId, $groupIdOrCode, REWARD_SOURCE_TYPE $sourceType, int $sourceId, int $multiplier = 1) 方法
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int|string $groupIdOrCode 奖励组ID或编码
|
|
|
+ * @param string $sourceType 来源类型字符串
|
|
|
+ * @param int $sourceId 来源ID
|
|
|
+ * @param int $multiplier 倍率
|
|
|
+ * @return RewardResultDto 奖励结果
|
|
|
+ */
|
|
|
+ public static function grantRewardLegacy(int $userId, $groupIdOrCode, string $sourceType, int $sourceId, int $multiplier = 1): RewardResultDto
|
|
|
+ {
|
|
|
+ // 验证来源类型是否有效
|
|
|
+ if (!REWARD_SOURCE_TYPE::isValid($sourceType)) {
|
|
|
+ return RewardResultDto::fail("无效的奖励来源类型: {$sourceType}");
|
|
|
+ }
|
|
|
+
|
|
|
+ $logic = new RewardLogic();
|
|
|
+ return $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId, $multiplier);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 批量发放奖励(兼容性方法,已废弃)
|
|
|
+ *
|
|
|
+ * @deprecated 请使用 batchGrantReward(array $userIds, $groupIdOrCode, REWARD_SOURCE_TYPE $sourceType, int $sourceId) 方法
|
|
|
+ * @param array $userIds 用户ID数组
|
|
|
+ * @param int|string $groupIdOrCode 奖励组ID或编码
|
|
|
+ * @param string $sourceType 来源类型字符串
|
|
|
+ * @param int $sourceId 来源ID
|
|
|
+ * @return array 奖励结果数组,键为用户ID,值为RewardResultDto
|
|
|
+ */
|
|
|
+ public static function batchGrantRewardLegacy(array $userIds, $groupIdOrCode, string $sourceType, int $sourceId): array
|
|
|
+ {
|
|
|
+ // 验证来源类型是否有效
|
|
|
+ if (!REWARD_SOURCE_TYPE::isValid($sourceType)) {
|
|
|
+ $failResult = RewardResultDto::fail("无效的奖励来源类型: {$sourceType}");
|
|
|
+ return array_fill_keys($userIds, $failResult);
|
|
|
+ }
|
|
|
+
|
|
|
+ $results = [];
|
|
|
+ $logic = new RewardLogic();
|
|
|
+
|
|
|
+ foreach ($userIds as $userId) {
|
|
|
+ $results[$userId] = $logic->grantReward($userId, $groupIdOrCode, $sourceType, $sourceId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return $results;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发放奖励(支持保底机制,兼容性方法,已废弃)
|
|
|
+ *
|
|
|
+ * @deprecated 请使用 grantRewardWithPity(int $userId, $groupIdOrCode, REWARD_SOURCE_TYPE $sourceType, int $sourceId, bool $enablePity = true) 方法
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int|string $groupIdOrCode 奖励组ID或编码
|
|
|
+ * @param string $sourceType 来源类型字符串
|
|
|
+ * @param int $sourceId 来源ID
|
|
|
+ * @param bool $enablePity 是否启用保底机制
|
|
|
+ * @return RewardResultDto 奖励结果
|
|
|
+ */
|
|
|
+ public static function grantRewardWithPityLegacy(int $userId, $groupIdOrCode, string $sourceType, int $sourceId, bool $enablePity = true): RewardResultDto
|
|
|
+ {
|
|
|
+ // 验证来源类型是否有效
|
|
|
+ if (!REWARD_SOURCE_TYPE::isValid($sourceType)) {
|
|
|
+ return RewardResultDto::fail("无效的奖励来源类型: {$sourceType}");
|
|
|
+ }
|
|
|
+
|
|
|
+ $logic = new RewardLogic();
|
|
|
+ return $logic->grantRewardWithPity($userId, $groupIdOrCode, $sourceType, $sourceId, $enablePity);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据字符串创建枚举实例的辅助方法
|
|
|
+ *
|
|
|
+ * @param string $sourceType 来源类型字符串
|
|
|
+ * @return REWARD_SOURCE_TYPE|null 枚举实例,无效时返回null
|
|
|
+ */
|
|
|
+ public static function createSourceTypeEnum(string $sourceType): ?REWARD_SOURCE_TYPE
|
|
|
+ {
|
|
|
+ if (!REWARD_SOURCE_TYPE::isValid($sourceType)) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return REWARD_SOURCE_TYPE::from($sourceType);
|
|
|
+ }
|
|
|
}
|