basic item filter wip

This commit is contained in:
Héctor Giménez
2023-03-12 19:32:50 +09:00
parent 306150e873
commit 66dd506ba7
12 changed files with 273 additions and 26 deletions

45
pkg/itemfilter/chain.go Normal file
View File

@@ -0,0 +1,45 @@
package itemfilter
import "github.com/hectorgimenez/d2go/pkg/nip"
type evaluationChain struct {
links []link
}
type link struct {
Result bool
Operand nip.Operand
}
func (ch *evaluationChain) Add(result bool, operand nip.Operand) {
ch.links = append(ch.links, link{
Result: result,
Operand: operand,
})
}
func (ch *evaluationChain) Evaluate() bool {
if len(ch.links) == 1 {
return ch.links[0].Result
}
result := ch.links[0].Result
operand := ch.links[0].Operand
for i, eval := range ch.links {
if i == 0 {
continue
}
if i < len(ch.links) {
switch operand {
case nip.OperandAnd:
result = result && eval.Result
case nip.OperandOr:
result = result || eval.Result
}
}
operand = eval.Operand
}
return result
}