Search Apps Documentation Source Content File Folder Download Copy Actions Download

cond_and.gno

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