fix: 修复空提交问题

This commit is contained in:
1
2025-11-23 09:59:34 +00:00
parent 41315ac884
commit aa4574b5eb

View File

@@ -117,62 +117,69 @@ func (pet *PetInfo) AddEV(evadd []uint32) (bool, error) {
return false, errors.New("pet.Ev未初始化或长度不为6")
}
// 2. 预计算当前EV总和 + 增量后的临时值(先不修改原数据,避免边改边算
var (
totalCurrent uint32 // 当前EV总和
tempEV [evFieldCount]uint32 // 增量后的临时EV值避免修改原数组导致计算错误
)
// 一次性遍历:计算当前总和 + 初始化临时EV减少遍历次数
// 2. 第一步:直接添加并限制单项最大值(按索引顺序处理
var tempEV [evFieldCount]uint32
for i := 0; i < evFieldCount; i++ {
totalCurrent += pet.Ev[i]
// 先计算增量后的值同时限制单个不超过maxSingleEV
// 直接累加增量
tempEV[i] = pet.Ev[i] + evadd[i]
// 单项不超过255
if tempEV[i] > maxSingleEV {
tempEV[i] = maxSingleEV
}
}
// 3. 计算增量后的临时总和,检查是否超过maxTotalEV
// 3. 计算增量后的总和,检查是否超过510
totalTemp := uint32(0)
for _, v := range tempEV {
totalTemp += v
}
// 4. 若总和超额,执行削减逻辑(优先削减数值大的字段,保证公平性
// 4. 若总和超额,按索引顺序0→5削减优先削减前面的字段
hasCut := false
if totalTemp > maxTotalEV {
overTotal := totalTemp - maxTotalEV // 需要削减的总量
hasCut = true
// 循环削减直到超额量为0优先削减数值大的字段避免小值被过度削减
for overTotal > 0 {
// 找到当前最大的EV索引
maxIdx := 0
for i := 1; i < evFieldCount; i++ {
if tempEV[i] > tempEV[maxIdx] {
maxIdx = i
}
// 按索引顺序遍历削减从第0个字段开始依次处理
for i := 0; i < evFieldCount && overTotal > 0; i++ {
// 可削减的最大值最多削减到原始值不触碰添加前的基础EV
cutAble := tempEV[i] - pet.Ev[i]
if cutAble <= 0 {
continue // 该字段无增量可削减,跳过
}
// 计算该字段可削减的最大值至少留到原数值或削减到能覆盖overTotal
cutAble := tempEV[maxIdx] - pet.Ev[maxIdx] // 最多削减增量部分,不低于原值
if cutAble == 0 {
cutAble = tempEV[maxIdx] // 若增量为0可削减当前值根据业务调整也可panic
}
// 实际削减量:取可削减量和剩余超额量的较小值
// 实际削减量:取"可削减量"和"剩余需削减量"的较小值
cut := cutAble
if cut > overTotal {
cut = overTotal
}
// 执行削减
tempEV[maxIdx] -= cut
tempEV[i] -= cut
overTotal -= cut
}
// 极端情况即使削减所有增量后仍超额如原始EV总和已超510继续按顺序削减原始值
if overTotal > 0 {
for i := 0; i < evFieldCount && overTotal > 0; i++ {
// 此时可削减到0根据业务需求调整也可返回错误
cutAble := tempEV[i]
if cutAble <= 0 {
continue
}
cut := cutAble
if cut > overTotal {
cut = overTotal
}
tempEV[i] -= cut
overTotal -= cut
}
}
}
// 5. 将处理后的临时值赋值给原EV(批量赋值,减少零散操作)
// 5. 将处理后的结果赋值给原EV数组
copy(pet.Ev[:], tempEV[:])
return hasCut, nil