Files
bl/logic/service/fight/info/BattlePetEntity.go
昔念 7a8be1c23a feat(element): 优化元素计算器并发安全与缓存机制
- 使用 sync.Map 替代 map+锁,提升并发读写性能
- 预加载所有元素组合,避免运行时重复创建
- 攻击系数计算结果加入缓存,提高查询效率
- 完善缓存键命名与错误处理机制
- 调整元素组合字符串展示格式,增强可读性

fix(item): 修复购买物品时价格为0仍扣除金币的问题

- 在购买逻辑中增加对物品价格是否为0的判断
- 防止免费物品被误扣金币
2025-11-02 23:52:06 +08:00

94 lines
2.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package info
import (
element "blazing/common/data/Element"
"blazing/common/data/xmlres"
"blazing/modules/blazing/model"
"math/rand"
"sync"
)
// 战斗属性类型
type EnumAttrType int
// 辅助函数:取整数绝对值(处理负等级)
func abs(x int8) int8 {
if x < 0 {
return -x
}
return x
}
type BattlePetEntity struct {
xmlres.PetInfo
Info *model.PetInfo //通过偏移赋值
//*input.Input
statusConditions sync.Map // key: StatusCondition, value: int (剩余回合)
Skills [4]*SkillEntity // 技能槽最多4个技能
//Status StatusDict //精灵的状态
//能力提升属性
//Prop PropDict
NotAlive bool `struc:"skip"`
//DamageZone map[EnumCategory]map[EnumsZoneType]map[EnumsZoneType][]float64 // 三维map 伤害类型-》增还是减-》加还是乘-》值
}
// 创建精灵实例
func CreateBattlePetEntity(info *model.PetInfo, rand *rand.Rand) *BattlePetEntity {
ret := &BattlePetEntity{}
ret.PetInfo = xmlres.PetMAP[int(info.ID)] //注入精灵信息
ret.Info = info
for i := 0; i < len(info.SkillList); i++ {
//todo 技能信息应该每回合进行深拷贝,保证每次的技能效果都是不一样的
ret.Skills[i] = CreateSkill(&info.SkillList[i], rand, ret)
}
return ret
}
// calculateRealValue 计算实际属性值根据状态变化计算实际值
// value 基础属性值
// stat 状态变化值(可正可负)
// 返回// 返回计算后的实际属性值确保结果至少为1
func CalculateRealValue(value int, stat int) int {
if stat == 0 {
if value <= 0 {
return 1
}
return value
} else if stat > 0 {
r := int(float64(value) * (float64(stat+2) / 2.0))
if r <= 0 {
return 1
}
return r
} else {
r := int(float64(value) * (2.0 / float64(2-stat)))
if r <= 0 {
return 1
}
return r
}
}
func (u *BattlePetEntity) Type() *element.ElementCombination {
var ff *element.ElementCombination
for _, v := range xmlres.PetMAP {
if v.ID == int(u.Info.ID) {
ff, _ = element.NewElementCombination(v.Type)
break
}
}
if ff == nil {
ff, _ = element.NewElementCombination(8)
}
return ff
}