火箭发送跨级别升级情况处理
This commit is contained in:
parent
30404d97ae
commit
2e9ec14176
@ -17,6 +17,7 @@ import com.red.circle.other.domain.gateway.RocketHistoryGateway;
|
|||||||
import com.red.circle.other.domain.gateway.RocketStatusGateway;
|
import com.red.circle.other.domain.gateway.RocketStatusGateway;
|
||||||
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
|
import com.red.circle.other.domain.gateway.user.UserProfileGateway;
|
||||||
import com.red.circle.other.domain.rocket.RocketHistory;
|
import com.red.circle.other.domain.rocket.RocketHistory;
|
||||||
|
import com.red.circle.other.domain.rocket.RocketLevel;
|
||||||
import com.red.circle.other.domain.rocket.RocketStatus;
|
import com.red.circle.other.domain.rocket.RocketStatus;
|
||||||
import com.red.circle.other.domain.rocket.RocketStatusEnum;
|
import com.red.circle.other.domain.rocket.RocketStatusEnum;
|
||||||
import com.red.circle.other.infra.database.cache.key.RocketKeys;
|
import com.red.circle.other.infra.database.cache.key.RocketKeys;
|
||||||
@ -100,18 +101,31 @@ public class RocketLaunchCmdExe {
|
|||||||
long finalEnergy = rocketStatus.getCurrentEnergy();
|
long finalEnergy = rocketStatus.getCurrentEnergy();
|
||||||
int contributorCount = rocketStatus.getContributors() != null ? rocketStatus.getContributors().size() : 0;
|
int contributorCount = rocketStatus.getContributors() != null ? rocketStatus.getContributors().size() : 0;
|
||||||
|
|
||||||
// 5. 保存历史记录(发射前保存,保留贡献者信息)
|
// 5. 发射并自动升级到下一级
|
||||||
|
// 1. 先计算出能发射的等级
|
||||||
|
RocketLevel launchLevel = rocketStatus.calculateLaunchLevel(rocketConfigGateway);
|
||||||
|
if (launchLevel == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. 标记为已发射状态
|
||||||
|
rocketStatus.markAsLaunched();
|
||||||
|
|
||||||
|
// 3. 保存发射时的历史记录(此时还是发射等级)
|
||||||
try {
|
try {
|
||||||
rocketStatus.setLaunchTime(LocalDateTime.now());
|
rocketStatus.setLevel(launchLevel);
|
||||||
RocketHistory history = RocketHistory.fromRocketStatus(rocketStatus);
|
RocketHistory history = RocketHistory.fromRocketStatus(rocketStatus);
|
||||||
String historyId = rocketHistoryGateway.save(history);
|
String historyId = rocketHistoryGateway.save(history);
|
||||||
log.info("保存火箭历史记录成功: historyId={}, roomId={}, level={}", historyId, roomId, launchedLevel);
|
log.info("保存火箭历史记录成功: historyId={}, roomId={}, level={}",
|
||||||
|
historyId, roomId, launchLevel.getLevel());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("保存火箭历史记录失败: roomId={}", roomId, e);
|
log.error("保存火箭历史记录失败: roomId={}", roomId, e);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 7. 发射并自动升级到下一级
|
// 4. 找到下一个等级
|
||||||
rocketStatus.launchAndUpgrade(rocketConfigGateway);
|
RocketLevel nextLevel = launchLevel.getNextLevel();
|
||||||
|
// 5. 升级到下一个等级
|
||||||
|
rocketStatus.upgradeToLevel(rocketConfigGateway, nextLevel);
|
||||||
|
|
||||||
// 8. 保存新状态
|
// 8. 保存新状态
|
||||||
rocketStatusGateway.save(rocketStatus);
|
rocketStatusGateway.save(rocketStatus);
|
||||||
|
|||||||
@ -127,14 +127,6 @@ public class RoomEnterCmdExe {
|
|||||||
log.error("进房间添加足迹异常:{}", e.getMessage());
|
log.error("进房间添加足迹异常:{}", e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 自动领取火箭奖励
|
|
||||||
try {
|
|
||||||
UserProfile userProfile = userProfileGateway.getByUserId(cmd.getReqUserId());
|
|
||||||
rocketRewardClaimCmdExe.executeAsync(cmd.getRoomId(), manager.getRoomAccount(), cmd.getReqUserId(), userProfile.getUserNickname());
|
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("触发自动领取火箭奖励失败: roomId={}, userId={}", cmd.getRoomId(), cmd.getReqUserId(), e);
|
|
||||||
}
|
|
||||||
|
|
||||||
return new EntryRoomResponseCO()
|
return new EntryRoomResponseCO()
|
||||||
.setRoomProfile(
|
.setRoomProfile(
|
||||||
new EntryRoomProfileCO()
|
new EntryRoomProfileCO()
|
||||||
|
|||||||
@ -60,7 +60,7 @@ public class RocketConvertor {
|
|||||||
|
|
||||||
co.setTotalContributors(rocketStatus.getContributors() != null
|
co.setTotalContributors(rocketStatus.getContributors() != null
|
||||||
? rocketStatus.getContributors().size() : 0);
|
? rocketStatus.getContributors().size() : 0);
|
||||||
co.setLaunchTime(rocketStatus.getLaunchTime());
|
// co.setLaunchTime(rocketStatus.getLaunchTime());
|
||||||
co.setClaimExpireTime(rocketStatus.getClaimExpireTime());
|
co.setClaimExpireTime(rocketStatus.getClaimExpireTime());
|
||||||
co.setClaimCount(rocketStatus.getClaimCount());
|
co.setClaimCount(rocketStatus.getClaimCount());
|
||||||
|
|
||||||
|
|||||||
@ -73,7 +73,7 @@ public class RocketStatusCO {
|
|||||||
/**
|
/**
|
||||||
* 发射时间
|
* 发射时间
|
||||||
*/
|
*/
|
||||||
private LocalDateTime launchTime;
|
// private LocalDateTime launchTime;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 领取过期时间
|
* 领取过期时间
|
||||||
|
|||||||
@ -63,7 +63,7 @@ public enum RocketLevel {
|
|||||||
return switch (this) {
|
return switch (this) {
|
||||||
case LEVEL_1 -> LEVEL_2;
|
case LEVEL_1 -> LEVEL_2;
|
||||||
case LEVEL_2 -> LEVEL_3;
|
case LEVEL_2 -> LEVEL_3;
|
||||||
case LEVEL_3 -> null; // 三级是最高级,返回null表示需要重置
|
case LEVEL_3 -> LEVEL_3; // 三级是最高级,返回null表示需要重置
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -128,32 +128,68 @@ public class RocketStatus {
|
|||||||
|
|
||||||
// RocketStatus.java
|
// RocketStatus.java
|
||||||
|
|
||||||
/**
|
|
||||||
* 升级到下一级火箭(使用配置)
|
|
||||||
*/
|
|
||||||
private void upgradeToNextLevel(RocketConfigGateway configGateway) {
|
|
||||||
RocketLevel nextLevel = this.level.getNextLevel();
|
|
||||||
|
|
||||||
if (nextLevel != null) {
|
/**
|
||||||
// 查询下一级配置
|
* 计算能发射的最高等级
|
||||||
RocketConfig nextConfig = configGateway.findByLevel(nextLevel.getLevel());
|
* @return 能发射的等级,如果能量不足返回null
|
||||||
if (nextConfig == null || nextConfig.getStatus() != 1) {
|
*/
|
||||||
log.warn("下一级火箭配置不存在或未启用, level={}", nextLevel.getLevel());
|
public RocketLevel calculateLaunchLevel(RocketConfigGateway configGateway) {
|
||||||
|
long currentEnergy = this.currentEnergy;
|
||||||
|
|
||||||
|
// 如果当前已经是最高级,不能再发射
|
||||||
|
if (this.level.isMaxLevel()) {
|
||||||
|
return this.level;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 查询所有启用的配置
|
||||||
|
List<RocketConfig> configs = configGateway.findAllEnabled();
|
||||||
|
if (configs == null || configs.isEmpty()) {
|
||||||
|
log.warn("未找到启用的火箭配置");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 按等级从高到低排序
|
||||||
|
configs.sort((a, b) -> b.getLevel().compareTo(a.getLevel()));
|
||||||
|
|
||||||
|
// 从最高等级开始查找,找到能量满足发射条件的最高等级
|
||||||
|
for (RocketConfig config : configs) {
|
||||||
|
if (config.getLevel() > this.level.getLevel()
|
||||||
|
&& currentEnergy >= config.getMaxEnergy()) {
|
||||||
|
return RocketLevel.of(config.getLevel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 标记为已发射状态
|
||||||
|
*/
|
||||||
|
public void markAsLaunched() {
|
||||||
|
this.status = RocketStatusEnum.LAUNCHED;
|
||||||
|
this.launchTime = LocalDateTime.now();
|
||||||
|
this.updatedAt = LocalDateTime.now();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 升级到指定等级
|
||||||
|
* @param targetLevel 目标等级
|
||||||
|
*/
|
||||||
|
public void upgradeToLevel(RocketConfigGateway configGateway, RocketLevel targetLevel) {
|
||||||
|
// 查询目标等级的配置
|
||||||
|
RocketConfig targetConfig = configGateway.findByLevel(targetLevel.getLevel());
|
||||||
|
if (targetConfig == null || targetConfig.getStatus() != 1) {
|
||||||
|
log.warn("目标等级火箭配置不存在或未启用, level={}", targetLevel.getLevel());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 计算溢出能量(当前能量 - 当前等级最大能量)
|
// 保留当前能量值
|
||||||
long overflowEnergy = this.currentEnergy - this.maxEnergy;
|
|
||||||
if (overflowEnergy < 0) {
|
|
||||||
overflowEnergy = 0L;
|
|
||||||
}
|
|
||||||
|
|
||||||
// 保留当前能量值,不清空
|
|
||||||
long preservedEnergy = this.currentEnergy;
|
long preservedEnergy = this.currentEnergy;
|
||||||
|
|
||||||
// 升级到下一级
|
// 升级到目标等级
|
||||||
this.level = nextLevel;
|
RocketLevel oldLevel = this.level;
|
||||||
this.maxEnergy = nextConfig.getMaxEnergy();
|
this.level = targetLevel;
|
||||||
|
this.maxEnergy = targetConfig.getMaxEnergy();
|
||||||
this.currentEnergy = preservedEnergy; // 完全保留当前能量
|
this.currentEnergy = preservedEnergy; // 完全保留当前能量
|
||||||
this.status = RocketStatusEnum.CHARGING;
|
this.status = RocketStatusEnum.CHARGING;
|
||||||
|
|
||||||
@ -162,12 +198,8 @@ public class RocketStatus {
|
|||||||
this.launchTime = null;
|
this.launchTime = null;
|
||||||
this.claimExpireTime = null;
|
this.claimExpireTime = null;
|
||||||
|
|
||||||
log.info("火箭升级成功, 升至{}级, 溢出能量={}, 新目标={}",
|
log.info("火箭升级成功, {}级 -> {}级, 保留能量={}, 新目标={}",
|
||||||
nextLevel.getLevel(), overflowEnergy, this.maxEnergy);
|
oldLevel.getLevel(), targetLevel.getLevel(), preservedEnergy, this.maxEnergy);
|
||||||
} else {
|
|
||||||
// 已经是最高级,重置为一级
|
|
||||||
// reset(configGateway);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -179,7 +211,7 @@ public class RocketStatus {
|
|||||||
this.updatedAt = LocalDateTime.now();
|
this.updatedAt = LocalDateTime.now();
|
||||||
|
|
||||||
// 升级到下一级
|
// 升级到下一级
|
||||||
upgradeToNextLevel(configGateway);
|
// upgradeToNextLevel(configGateway);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user