Search Apps Documentation Source Content File Folder Download Copy Actions Download

cond_or.gno

1.57 Kb ยท 62 lines
 1package daocond
 2
 3import (
 4	"math"
 5	"strings"
 6)
 7
 8type orCond struct {
 9	conditions []Condition
10}
11
12// Creates a new condition from multiple conditions where any one must be true.
13//
14// Example: Or(MembersThreshold(0.6), RoleCount(2, "admin"))
15// Requires either 60% member approval OR 2 admin votes.
16func Or(conditions ...Condition) Condition {
17	if len(conditions) < 2 {
18		panic("at least two conditions are required")
19	}
20	return &orCond{conditions: conditions}
21}
22
23// Checks if any condition is true.
24func (c *orCond) Eval(ballot Ballot) bool {
25	for _, condition := range c.conditions {
26		if condition.Eval(ballot) {
27			return true
28		}
29	}
30	return false
31}
32
33// Returns highest signal among all conditions between 0.0 and 1.0.
34func (c *orCond) Signal(ballot Ballot) float64 {
35	maxSignal := 0.0
36	for _, condition := range c.conditions {
37		maxSignal = math.Max(maxSignal, condition.Signal(ballot))
38	}
39	return maxSignal
40}
41
42// Displays the condition as text.
43// Example output: "[ 60% Members OR 2 admin ]"
44func (c *orCond) Render() string {
45	renders := []string{}
46	for _, condition := range c.conditions {
47		renders = append(renders, condition.Render())
48	}
49	return "**[** " + strings.Join(renders, " **OR** ") + " **]**"
50}
51
52// Displays the condition with current vote counts.
53// Example output: "[ 3/5 Members OR 1/2 admin ]"
54func (c *orCond) RenderWithVotes(ballot Ballot) string {
55	renders := []string{}
56	for _, condition := range c.conditions {
57		renders = append(renders, condition.RenderWithVotes(ballot))
58	}
59	return "**[** " + strings.Join(renders, " **OR** ") + " **]**"
60}
61
62var _ Condition = (*orCond)(nil)