gitee.com/zhongguo168a/gocodes@v0.0.0-20230609140523-e1828349603f/datax/modifyx2/pool-list.go (about)

     1  package modifyx
     2  
     3  func NewPoolList() (obj *PoolList) {
     4  	obj = &PoolList{
     5  		versions: map[string]int{},
     6  	}
     7  	return
     8  }
     9  
    10  type PoolList struct {
    11  	root *Pool
    12  
    13  	// 版本信息,
    14  	// 保证一个root对象唯一, 需要传递给新的对象
    15  	// 记录已经更新到的版本号,用于与修正池中的版本号进行对比。
    16  	// 如果当前对象的版本号低于修正池中的版本号,则更新当前的对象到最新的版本
    17  	versions map[string]int
    18  	//
    19  	newestVersionCreator func() int
    20  }
    21  
    22  func (p *PoolList) SetRoot(pool *Pool) {
    23  	p.root = pool
    24  }
    25  
    26  func (p *PoolList) SetHost(pool *Pool) {
    27  	if p.root == nil {
    28  		p.root = pool
    29  	} else {
    30  		p.root.Parent = pool
    31  	}
    32  }
    33  
    34  func (p *PoolList) Dispose() {
    35  	p.root = nil
    36  	p.versions = nil
    37  	p.newestVersionCreator = nil
    38  }
    39  
    40  func (p *PoolList) SetNewestVersionCreator(val func() int) {
    41  	p.newestVersionCreator = val
    42  }
    43  
    44  func (p *PoolList) GetNewestVersion() int {
    45  	if p.newestVersionCreator != nil {
    46  		return p.newestVersionCreator()
    47  	}
    48  
    49  	return 0
    50  }
    51  
    52  func (p *PoolList) IsPropChanged(key string) bool {
    53  	lastVersion := p.versions[key]
    54  	if p.root == nil {
    55  		return false
    56  	}
    57  	cur := p.root
    58  	for {
    59  		if cur.IsPropChanged(key, lastVersion) {
    60  			return true
    61  		}
    62  
    63  		cur = cur.Parent
    64  		if cur == nil {
    65  			return false
    66  		}
    67  	}
    68  }
    69  
    70  func (p *PoolList) GetNewPropInt(key string, val int) (r int) {
    71  	var last = p.GetNewPropFloat(key, float64(val))
    72  	return int(last)
    73  }
    74  
    75  func (p *PoolList) GetNewPropFloat(key string, val float64) (r float64) {
    76  	last := val
    77  	var changed = false
    78  	// 第一步,计算修正
    79  	{
    80  		cur := p.root
    81  		for {
    82  			arr, has := cur.ModifyList[key]
    83  			if has && arr.Length() > 0 {
    84  				for _, imodify := range arr.Slice() {
    85  					modify := imodify.(IModify)
    86  					if !modify.Valid() {
    87  						continue
    88  					}
    89  					changed = true
    90  					// 计算修正
    91  					last = modify.Value(val).(float64)
    92  				}
    93  			}
    94  			cur = cur.Parent
    95  			if cur == nil {
    96  				break
    97  			}
    98  		}
    99  	}
   100  
   101  	// 第二步,计算加成
   102  	{
   103  		cur := p.root
   104  		for {
   105  			arr, has := cur.BonusList[key]
   106  			if has && arr.Length() > 0 {
   107  				var lastBonus *Bonus
   108  				for _, ibonus := range arr.Slice() {
   109  					bonus := ibonus.(IBonus)
   110  					if !bonus.Valid() {
   111  						continue
   112  					}
   113  					if lastBonus == nil {
   114  						lastBonus = NewBonus("")
   115  					}
   116  					changed = true
   117  					lastBonus.Combine(bonus)
   118  				}
   119  
   120  				if changed {
   121  					last = CalculateBonusFactor(float64(val), lastBonus, true, true, true)
   122  				}
   123  			}
   124  			cur = cur.Parent
   125  			if cur == nil {
   126  				break
   127  			}
   128  		}
   129  	}
   130  
   131  	r = last
   132  	if changed {
   133  		p.versions[key] = p.GetNewestVersion()
   134  	}
   135  	return
   136  }
   137  
   138  func (p *PoolList) GetNewPropBool(key string, val bool) (r bool) {
   139  	last := val
   140  	var changed = false
   141  	// 第一步,计算修正
   142  	{
   143  		cur := p.root
   144  		for {
   145  			arr, has := cur.ModifyList[key]
   146  			if has && arr.Length() > 0 {
   147  				for _, imodify := range arr.Slice() {
   148  					modify := imodify.(IModify)
   149  					if !modify.Valid() {
   150  						continue
   151  					}
   152  					changed = true
   153  					// 计算修正
   154  					//switch modify.(type) {
   155  					//}
   156  				}
   157  			}
   158  			cur = cur.Parent
   159  			if cur == nil {
   160  				break
   161  			}
   162  		}
   163  	}
   164  
   165  	r = last
   166  	if changed {
   167  		p.versions[key] = p.GetNewestVersion()
   168  	}
   169  	return
   170  }