spins task处理

任务已完成  无需增加进度值
领取礼物修改
This commit is contained in:
tianfeng 2025-10-21 12:31:08 +08:00
parent 83f79ce3d8
commit f23433b4d7
9 changed files with 193 additions and 21 deletions

View File

@ -48,7 +48,7 @@ public class SpinsTaskRestController {
*/ */
@PostMapping("/receive/reward") @PostMapping("/receive/reward")
public SpinsTaskRewardCO receiveReward(@Validated @RequestBody SpinsTaskReceiveRewardCmd cmd) { public SpinsTaskRewardCO receiveReward(@Validated @RequestBody SpinsTaskReceiveRewardCmd cmd) {
log.info("领取任务奖励, userId={}, taskCode={}", cmd.getUserId(), cmd.getTaskCode()); log.info("领取任务奖励, userId={}, taskCode={}", cmd.getReqUserId(), cmd.getTaskCode());
SpinsTaskRewardCO reward = spinsTaskService.receiveTaskReward(cmd); SpinsTaskRewardCO reward = spinsTaskService.receiveTaskReward(cmd);
return reward; return reward;
} }

View File

@ -59,15 +59,22 @@ public class SpinsTaskProgressIncrementExe {
String cycleKey = getCurrentCycleKey(taskConfig.getCycleType()); String cycleKey = getCurrentCycleKey(taskConfig.getCycleType());
SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey); SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey);
// 3. 增加进度值 // 3. 校验任务状态如果已完成或已领取则不更新
if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) {
log.info("任务已完成,无需增加进度, userId={}, taskCode={}, taskStatus={}",
userId, taskCode, progress.getTaskStatus());
return;
}
// 4. 增加进度值
int newValue = progress.getCurrentValue() + incrementValue; int newValue = progress.getCurrentValue() + incrementValue;
progress.setCurrentValue(newValue); progress.setCurrentValue(newValue);
// 4. 计算进度百分比 // 5. 计算进度百分比
BigDecimal progressRate = calculateProgressRate(newValue, taskConfig.getTargetValue()); BigDecimal progressRate = calculateProgressRate(newValue, taskConfig.getTargetValue());
progress.setProgressRate(progressRate); progress.setProgressRate(progressRate);
// 5. 检查是否完成 // 6. 检查是否完成
if (newValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) { if (newValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) {
progress.setTaskStatus(1); progress.setTaskStatus(1);
progress.setCompleteTime(new Timestamp(System.currentTimeMillis())); progress.setCompleteTime(new Timestamp(System.currentTimeMillis()));

View File

@ -61,14 +61,21 @@ public class SpinsTaskProgressUpdateExe {
String cycleKey = getCurrentCycleKey(taskConfig.getCycleType()); String cycleKey = getCurrentCycleKey(taskConfig.getCycleType());
SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey); SpinsUserTaskProgress progress = getOrCreateProgress(userId, taskConfig, cycleKey);
// 3. 更新进度值 // 3. 校验任务状态如果已完成或已领取则不更新
if (progress.getTaskStatus() != null && progress.getTaskStatus() >= 1) {
log.info("任务已完成,无需更新进度, userId={}, taskCode={}, taskStatus={}",
userId, taskCode, progress.getTaskStatus());
return;
}
// 4. 更新进度值
progress.setCurrentValue(progressValue); progress.setCurrentValue(progressValue);
// 4. 计算进度百分比 // 5. 计算进度百分比
BigDecimal progressRate = calculateProgressRate(progressValue, taskConfig.getTargetValue()); BigDecimal progressRate = calculateProgressRate(progressValue, taskConfig.getTargetValue());
progress.setProgressRate(progressRate); progress.setProgressRate(progressRate);
// 5. 检查是否完成 // 6. 检查是否完成
if (progressValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) { if (progressValue >= taskConfig.getTargetValue() && progress.getTaskStatus() == 0) {
progress.setTaskStatus(1); progress.setTaskStatus(1);
progress.setCompleteTime(new Timestamp(System.currentTimeMillis())); progress.setCompleteTime(new Timestamp(System.currentTimeMillis()));

View File

@ -45,7 +45,7 @@ public class SpinsTaskReceiveRewardExe {
*/ */
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public SpinsTaskRewardCO execute(SpinsTaskReceiveRewardCmd cmd) { public SpinsTaskRewardCO execute(SpinsTaskReceiveRewardCmd cmd) {
Long userId = cmd.getUserId(); Long userId = cmd.getReqUserId();
String taskCode = cmd.getTaskCode(); String taskCode = cmd.getTaskCode();
// 1. 查询任务配置 // 1. 查询任务配置
@ -62,16 +62,17 @@ public class SpinsTaskReceiveRewardExe {
// 2. 查询用户任务进度 // 2. 查询用户任务进度
String cycleKey = getCurrentCycleKey(); String cycleKey = getCurrentCycleKey();
SpinsUserTaskProgress progress = spinsUserTaskProgressDAO.selectById( List<SpinsUserTaskProgress> progressList = spinsUserTaskProgressDAO.selectList(
new LambdaQueryWrapper<SpinsUserTaskProgress>() new LambdaQueryWrapper<SpinsUserTaskProgress>()
.eq(SpinsUserTaskProgress::getUserId, userId) .eq(SpinsUserTaskProgress::getUserId, userId)
.eq(SpinsUserTaskProgress::getTaskCode, taskCode) .eq(SpinsUserTaskProgress::getTaskCode, taskCode)
.eq(SpinsUserTaskProgress::getCycleKey, cycleKey) .eq(SpinsUserTaskProgress::getCycleKey, cycleKey)
); );
if (progress == null) { if (progressList.isEmpty()) {
throw new RuntimeException("任务未开始"); throw new RuntimeException("任务未开始");
} }
SpinsUserTaskProgress progress = progressList.get(0);
// 3. 校验任务状态 // 3. 校验任务状态
if (progress.getTaskStatus() == 0) { if (progress.getTaskStatus() == 0) {

View File

@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.red.circle.common.business.core.enums.GameStateEnum; import com.red.circle.common.business.core.enums.GameStateEnum;
import com.red.circle.common.business.core.enums.SysOriginPlatformEnum; import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
import com.red.circle.component.redis.service.RedisService;
import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient; import com.red.circle.external.inner.endpoint.message.OfficialNoticeClient;
import com.red.circle.external.inner.model.cmd.message.notice.official.NoticeExtTemplateTypeCmd; import com.red.circle.external.inner.model.cmd.message.notice.official.NoticeExtTemplateTypeCmd;
import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum; import com.red.circle.external.inner.model.enums.message.OfficialNoticeTypeEnum;
@ -14,6 +15,8 @@ import com.red.circle.mq.rocket.business.producer.UserMqMessageService;
import com.red.circle.other.app.common.game.GameIndoorTeamPkCommon; import com.red.circle.other.app.common.game.GameIndoorTeamPkCommon;
import com.red.circle.other.app.common.gift.GameLuckyGiftCommon; import com.red.circle.other.app.common.gift.GameLuckyGiftCommon;
import com.red.circle.other.app.dto.clientobject.game.GameRoomPkUserCO; import com.red.circle.other.app.dto.clientobject.game.GameRoomPkUserCO;
import com.red.circle.other.app.dto.cmd.SpinsTaskProgressUpdateCmd;
import com.red.circle.other.app.service.SpinsUserTaskProgressService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.domain.gateway.user.UserProfileGateway;
import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway; import com.red.circle.other.domain.gateway.user.ability.UserRegionGateway;
import com.red.circle.other.domain.model.user.UserProfile; import com.red.circle.other.domain.model.user.UserProfile;
@ -77,6 +80,7 @@ import java.math.RoundingMode;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.ZonedDateTime; import java.time.ZonedDateTime;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -124,6 +128,8 @@ public class GiftCountStrategy implements GiftStrategy {
private final UserProfileGateway userProfileRepository; private final UserProfileGateway userProfileRepository;
private final OfficialNoticeClient officialService; private final OfficialNoticeClient officialService;
private final TaskMqMessage taskMqMessage; private final TaskMqMessage taskMqMessage;
private final SpinsUserTaskProgressService spinsUserTaskProgressService;
private final RedisService redisService;
private static final List<String> inRegionCodes = List.of("IN", "PK", "BD"); private static final List<String> inRegionCodes = List.of("IN", "PK", "BD");
private final UserRegionCacheService userRegionCacheService; private final UserRegionCacheService userRegionCacheService;
@ -260,6 +266,9 @@ public class GiftCountStrategy implements GiftStrategy {
// 累计活动 // 累计活动
// consumeActivity(runningWater, isLuckyGift, luckyGiftRatio); // consumeActivity(runningWater, isLuckyGift, luckyGiftRatio);
// Spins 送礼物任务
handleSpinsGiftTask(runningWater);
giftGiveRunningWaterService.addLog(runningWater.getId(), "结束:礼物统计相关"); giftGiveRunningWaterService.addLog(runningWater.getId(), "结束:礼物统计相关");
} }
@ -725,7 +734,94 @@ public class GiftCountStrategy implements GiftStrategy {
return amount.multiply(luckyGiftRatio).setScale(0, RoundingMode.DOWN).longValue(); return amount.multiply(luckyGiftRatio).setScale(0, RoundingMode.DOWN).longValue();
} }
return amount.longValue(); return amount.longValue();
}
/**
* 处理 Spins 送礼物任务
*/
private void handleSpinsGiftTask(GiftGiveRunningWater runningWater) {
try {
Long userId = runningWater.getUserId();
// 增加用户每日送礼物数量
int giftCount = incrementDailyGiftCount(userId);
log.info("Spins送礼物任务, userId={}, giftCount={}", userId, giftCount);
// 处理送礁1个礼物任务
if (giftCount >= 1) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_SEND_GIFT_1")
.setProgressValue(giftCount)
);
}
// 处理送出3个礼物任务
if (giftCount >= 3) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_SEND_GIFT_3")
.setProgressValue(giftCount)
);
}
// 处理送出10个礼物任务
if (giftCount >= 10) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_SEND_GIFT_10")
.setProgressValue(giftCount)
);
}
// 处理送出15个礼物任务
if (giftCount >= 15) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_SEND_GIFT_15")
.setProgressValue(giftCount)
);
}
log.info("Spins送礼物任务处理成功, userId={}, giftCount={}", userId, giftCount);
} catch (Exception e) {
log.error("处理Spins送礼物任务失败", e);
}
}
/**
* 增加用户每日送礼物数量
* @param userId 用户ID
* @return 当前送礼物数量
*/
private int incrementDailyGiftCount(Long userId) {
String redisKey = "spins:daily:gift:count:" + userId;
String countStr = redisService.getString(redisKey);
if (StringUtils.isBlank(countStr)) {
// 计算到凌晨的过期时间
long expireSeconds = getSecondsUntilMidnight();
redisService.setString(redisKey, "1", expireSeconds, TimeUnit.SECONDS);
return 1;
} else {
Long newCount = redisService.increment(redisKey, 1);
return newCount.intValue();
}
}
/**
* 获取当前时间到凌晨的秒数
* @return 秒数
*/
private long getSecondsUntilMidnight() {
ZonedDateTime now = ZonedDateTimeAsiaRiyadhUtils.now();
ZonedDateTime midnight = now.toLocalDate().plusDays(1).atStartOfDay(now.getZone());
return java.time.Duration.between(now, midnight).getSeconds();
} }
} }

View File

@ -18,6 +18,8 @@ import com.red.circle.external.inner.model.enums.message.GroupMessageTypeEnum;
import com.red.circle.mq.business.model.event.task.TaskApprovalEvent; import com.red.circle.mq.business.model.event.task.TaskApprovalEvent;
import com.red.circle.mq.rocket.business.streams.TaskSink; import com.red.circle.mq.rocket.business.streams.TaskSink;
import com.red.circle.other.app.dto.clientobject.game.GameRoomMessageCO; import com.red.circle.other.app.dto.clientobject.game.GameRoomMessageCO;
import com.red.circle.other.app.dto.cmd.SpinsTaskProgressUpdateCmd;
import com.red.circle.other.app.service.SpinsUserTaskProgressService;
import com.red.circle.other.app.service.room.RoomProfileService; import com.red.circle.other.app.service.room.RoomProfileService;
import com.red.circle.other.app.service.task.TaskService; import com.red.circle.other.app.service.task.TaskService;
import com.red.circle.other.domain.gateway.user.UserProfileGateway; import com.red.circle.other.domain.gateway.user.UserProfileGateway;
@ -84,6 +86,7 @@ public class TaskListener implements MessageListener {
private final ImGroupClient imGroupClient; private final ImGroupClient imGroupClient;
private final GameListConfigService gameListConfigService; private final GameListConfigService gameListConfigService;
private final InviteUserGateway inviteUserGateway; private final InviteUserGateway inviteUserGateway;
private final SpinsUserTaskProgressService spinsUserTaskProgressService;
@Override @Override
public Action consume(ConsumerMessage message) { public Action consume(ConsumerMessage message) {
@ -184,11 +187,71 @@ public class TaskListener implements MessageListener {
if ("10".equals(inc) || Integer.parseInt(inc) >= 10) { if ("10".equals(inc) || Integer.parseInt(inc) >= 10) {
// 更新任务状态的逻辑 直接更新 // 更新任务状态的逻辑 直接更新
updateTaskStatus(eventBody); updateTaskStatus(eventBody);
} else {
redisService.increment(redisKey, 1);
} }
redisService.increment(redisKey, 1);
} }
// 处理 Spins 上麦任务进度
handleSpinsMicTask(eventBody.getUserId(), inc);
}
/**
* 处理 Spins 上麦任务进度
* @param userId 用户ID
* @param micTimeStr 上麦时长分钟字符串
*/
private void handleSpinsMicTask(Long userId, String micTimeStr) {
if (StringUtils.isEmpty(micTimeStr)) {
return;
}
try {
int micTime = Integer.parseInt(micTimeStr);
// 处理上麦15分钟任务
if (micTime >= 15) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_MIC_15_MIN")
.setProgressValue(micTime)
);
}
// 处理上麦30分钟任务
if (micTime >= 30) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_MIC_30_MIN")
.setProgressValue(micTime)
);
}
// 处理上麦1小时任务
if (micTime >= 60) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_MIC_1_HOUR")
.setProgressValue(micTime)
);
}
// 处理上麦2小时任务
if (micTime >= 120) {
spinsUserTaskProgressService.updateTaskProgress(
new SpinsTaskProgressUpdateCmd()
.setUserId(userId)
.setTaskCode("SPINS_MIC_2_HOUR")
.setProgressValue(micTime)
);
}
log.info("Spins上麦任务进度更新成功, userId={}, micTime={}", userId, micTime);
} catch (Exception e) {
log.error("处理Spins上麦任务进度失败, userId={}, micTimeStr={}", userId, micTimeStr, e);
}
} }
private void handleTask99(TaskApprovalEvent eventBody) { private void handleTask99(TaskApprovalEvent eventBody) {

View File

@ -36,7 +36,7 @@ public class SpinsTaskServiceImpl implements SpinsTaskService {
@Override @Override
public SpinsTaskRewardCO receiveTaskReward(SpinsTaskReceiveRewardCmd cmd) { public SpinsTaskRewardCO receiveTaskReward(SpinsTaskReceiveRewardCmd cmd) {
log.info("执行领取任务奖励, userId={}, taskCode={}", cmd.getUserId(), cmd.getTaskCode()); log.info("执行领取任务奖励, userId={}, taskCode={}", cmd.getReqUserId(), cmd.getTaskCode());
return spinsTaskReceiveRewardExe.execute(cmd); return spinsTaskReceiveRewardExe.execute(cmd);
} }
} }

View File

@ -3,6 +3,7 @@ package com.red.circle.other.app.dto.cmd;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import lombok.experimental.Accessors;
import java.io.Serializable; import java.io.Serializable;
@ -13,6 +14,7 @@ import java.io.Serializable;
* @date 2025-01-21 * @date 2025-01-21
*/ */
@Data @Data
@Accessors(chain = true)
public class SpinsTaskProgressUpdateCmd implements Serializable { public class SpinsTaskProgressUpdateCmd implements Serializable {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;

View File

@ -1,10 +1,10 @@
package com.red.circle.other.app.dto.cmd; package com.red.circle.other.app.dto.cmd;
import com.red.circle.common.business.dto.cmd.AppExtCommand;
import jakarta.validation.constraints.NotBlank; import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/** /**
* 领取任务奖励命令 * 领取任务奖励命令
@ -12,13 +12,9 @@ import java.io.Serializable;
* @author system * @author system
* @date 2025-01-21 * @date 2025-01-21
*/ */
@EqualsAndHashCode(callSuper = true)
@Data @Data
public class SpinsTaskReceiveRewardCmd implements Serializable { public class SpinsTaskReceiveRewardCmd extends AppExtCommand {
private static final long serialVersionUID = 1L;
@NotNull(message = "用户ID不能为空")
private Long userId;
@NotBlank(message = "任务编码不能为空") @NotBlank(message = "任务编码不能为空")
private String taskCode; private String taskCode;