cp等级问题处理

(cherry picked from commit 7ef1783996fd4202e2e9b688c5721d6e20fd3866)
This commit is contained in:
tianfeng 2026-04-29 21:42:07 +08:00
parent 1bd5ece8fe
commit 37d953f5ba

View File

@ -11,13 +11,14 @@ import java.util.List;
* CP等级配置.
* <p>
* 字段说明
* - expThreshold : 达到该等级所需的累计CP值对应 user_cp_value.cp_val
* - loveHeartLevel : 爱心样式等级0=, 1-3=对应样式展示在资料卡和用户头像旁
* - micEffectLevel : 邻麦动效等级0=, 1-3仅在左右相邻麦位时触发
* - profileCardLevel: 资料卡动态背景等级0=, 1-3展示在CP资料卡内层元素
* - giftWindowLevel : 赠礼飘窗等级0=, 1-3双方赠送CP礼物时触发
* - hasNonMicEffect : 是否解锁非邻麦动效true=解锁非相邻时也可展示动效
* - hasCpHeadwear : 是否解锁CP头饰true=解锁展示在双方头像周期性飞出效果
* - expThreshold : 升入下一等级所需超过的累计CP值门槛
* 超过500000升LV2超过1000000升LV3以此类推
* - loveHeartLevel : 爱心样式等级1-3
* - micEffectLevel : 邻麦动效等级0=, 1-3
* - profileCardLevel: 资料卡动态背景等级0=, 1-3
* - giftWindowLevel : 赠礼飘窗等级0=, 1-3
* - hasNonMicEffect : 是否解锁非邻麦动效
* - hasCpHeadwear : 是否解锁CP头饰
*
* @author tf
*/
@ -36,6 +37,7 @@ public enum CpLevelConfig {
LV10(10, 200000000L, 3, 3, 3, 3, true, true );
private final int level;
/** 升入下一等级所需超过的门槛值LV10为满级门槛. */
private final long expThreshold;
private final int loveHeartLevel;
private final int micEffectLevel;
@ -58,32 +60,50 @@ public enum CpLevelConfig {
this.hasCpHeadwear = hasCpHeadwear;
}
/** 按 expThreshold 降序,用于 of() 从高到低匹配. */
private static final List<CpLevelConfig> DESC = Arrays.stream(values())
.sorted(Comparator.comparingLong(CpLevelConfig::getExpThreshold).reversed())
/** 按 expThreshold 升序,用于 of() 逐级匹配. */
private static final List<CpLevelConfig> ASC = Arrays.stream(values())
.sorted(Comparator.comparingLong(CpLevelConfig::getExpThreshold))
.toList();
/**
* 根据累计CP值推算当前等级未达到Lv1门槛返回 null.
* 根据累计CP值推算当前等级.
* 超过LV(n-1).expThreshold即为LVn不足LV1.expThreshold默认LV1超过LV10.expThreshold返回LV10.
* cpVal=500001 超过LV1门槛500000返回LV2.
*/
public static CpLevelConfig of(BigDecimal cpVal) {
if (cpVal == null) {
return null;
if (cpVal == null || cpVal.compareTo(BigDecimal.ZERO) <= 0) {
return LV1;
}
long val = cpVal.longValue();
return DESC.stream()
.filter(lv -> val >= lv.expThreshold)
.findFirst()
.orElse(null);
// 找第一个未超过其门槛的等级返回上一级全部超过则返回LV10
for (int i = 0; i < ASC.size(); i++) {
if (cpVal.compareTo(BigDecimal.valueOf(ASC.get(i).expThreshold)) <= 0) {
return i == 0 ? LV1 : ASC.get(i);
}
}
return LV10;
}
/**
* 获取下一等级经验门槛满级返回 null.
* 是否已超过LV1门槛即真正升过LV1.
*/
public static boolean hasReachedLevel(BigDecimal cpVal) {
return cpVal != null
&& cpVal.compareTo(BigDecimal.valueOf(LV1.expThreshold)) > 0;
}
/**
* 升入下一级需要超过的门槛值即当前等级的expThreshold满级返回自身门槛.
* 当前LV2需超过1000000升LV3返回1000000.
*/
public Long nextExpThreshold() {
int nextOrdinal = this.ordinal() + 1;
CpLevelConfig[] all = values();
return nextOrdinal < all.length ? all[nextOrdinal].expThreshold : null;
return this.expThreshold;
}
/**
* 根据累计CP值获取升入下一级需要超过的门槛值满级返回自身门槛.
*/
public static Long nextExpThreshold(BigDecimal cpVal) {
return of(cpVal).nextExpThreshold();
}
}