gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/modifyx2/pool.go (about) 1 package modifyx 2 3 import ( 4 "gitee.com/zhongguo168a/gocodes/datax/listx" 5 ) 6 7 type IPool interface { 8 // 属性是否发生了变化 9 IsPropChanged(key string, version int) bool 10 GetNewPropInt(key string, val int) (r int, ver int) 11 GetNewPropStruct(key string) (r map[string]interface{}, ver int) 12 GetNewPropFloat(key string, val int) (r map[string]interface{}, ver int) 13 GetNewPropBool(key string, val bool) (r bool, ver int) 14 } 15 16 func NewPool() (obj *Pool) { 17 obj = &Pool{ 18 BonusList: map[string]listx.List{}, 19 ModifyList: map[string]listx.List{}, 20 version: map[string]int{}, 21 } 22 return 23 } 24 25 type Pool struct { 26 // 27 Parent *Pool 28 // key=结构/属性 29 // 例如=CUnit/Vital/Life/Max或 CEffectDamage/Amount 30 BonusList map[string]listx.List 31 // 32 ModifyList map[string]listx.List 33 // 加成或修正发生变化, 版本号+1 34 // 结构/属性 35 version map[string]int 36 } 37 38 // key为需要修正的属性 39 // 例如: key=CEffectDamage/Amount 40 func (p *Pool) AddBonus(val IBonus, version int) { 41 list := p.BonusList[val.Key()] 42 list.Add(val) 43 p.version[val.Key()] = version 44 p.BonusList[val.Key()] = list 45 } 46 47 func (p *Pool) RemoveBonus(val IBonus, version int) { 48 list := p.BonusList[val.Key()] 49 list.Remove(val) 50 p.version[val.Key()] = version 51 p.BonusList[val.Key()] = list 52 } 53 54 // co.CModify 55 func (p *Pool) AddModify(val IModify, version int) { 56 list := p.ModifyList[val.Key()] 57 list.Add(val) 58 p.version[val.Key()] = version 59 p.ModifyList[val.Key()] = list 60 } 61 62 func (p *Pool) RemoveModify(val IModify, version int) { 63 list := p.ModifyList[val.Key()] 64 list.Remove(val) 65 p.version[val.Key()] = version 66 p.ModifyList[val.Key()] = list 67 } 68 69 // 增加指定键值的版本号 70 func (p *Pool) SetVersion(key string, version int) { 71 p.version[key] = version 72 } 73 74 func (p *Pool) IsPropChanged(key string, version int) bool { 75 if p.version[key] > version { 76 return true 77 } 78 return false 79 }