Skip to content

Commit

Permalink
Clean up formatting.
Browse files Browse the repository at this point in the history
  • Loading branch information
wyhaines committed Feb 20, 2025
1 parent e6dfb43 commit 4fa9786
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
11 changes: 7 additions & 4 deletions examples/gno.land/p/demo/poll/poll.gno
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ import (
type Poll struct {
Title string
Description string
Deadline time.Time // now a standard time.Time
Deadline time.Time // now a standard time.Time
Creator std.Address
Options []string // e.g. ["Red", "Green", "Blue"]
MaxChoices int // how many options a voter can select
Votes *avl.Tree // key: voter address, value: []int (indices of Options)
Options []string // e.g. ["Red", "Green", "Blue"]
MaxChoices int // how many options a voter can select
VoteTotal int // total number of votes cast
Votes *avl.Tree // key: voter address, value: []int (indices of Options)
}

// NewPoll constructs a Poll with the desired configuration.
Expand All @@ -41,6 +42,7 @@ func NewPoll(
Creator: creator,
Options: opts,
MaxChoices: maxChoices,
VoteTotal: 0,
Votes: avl.NewTree(),
}
return p
Expand Down Expand Up @@ -71,6 +73,7 @@ func (p *Poll) Vote(voter std.Address, choices []int) {
panic("invalid option index")
}
}
p.VoteTotal = p.VoteTotal + len(choices)
p.Votes.Set(voter.String(), choices)
}

Expand Down
13 changes: 9 additions & 4 deletions examples/gno.land/r/demo/polls/polls.gno
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func renderSinglePoll(b *bytes.Buffer, id string, p *poll.Poll) {
tally := p.Tally()
b.WriteString("\n## Options:\n")
for i, opt := range p.Options {
b.WriteString(ufmt.Sprintf(" - (%d) %s — Votes: %d\n", i, opt, tally[i]))
b.WriteString(ufmt.Sprintf(" - (%d) %s — Votes: %d -- %f%%\n", i, opt, tally[i], 100*float64(tally[i])/float64(p.VoteTotal)))
}

b.WriteString(ufmt.Sprintf("\n**Distinct Voters:** %d\n", p.VoterCount()))
Expand All @@ -172,13 +172,18 @@ func renderSinglePoll(b *bytes.Buffer, id string, p *poll.Poll) {
sumVotes += count
}
if sumVotes > p.VoterCount() && p.MaxChoices > 1 {
b.WriteString("*Multiple selections are allowed; total votes can exceed distinct voters.*\n\n")
b.WriteString(
ufmt.Sprintf("*Multiple selections (%d) are allowed; total votes can exceed distinct voters.*\n\n", p.MaxChoices))
}

b.WriteString("\n## Detailed Votes:\n")
p.Votes.Iterate("", "", func(addr string, val interface{}) bool {
indexes := val.([]int)
b.WriteString(ufmt.Sprintf("- %s => %v\n", addr, indexes))
strIndexes := make([]string, len(indexes))
for i, num := range indexes {
strIndexes[i] = strconv.Itoa(num)
}
b.WriteString(ufmt.Sprintf("- %s => %s\n", addr, strings.Join(strIndexes, ", ")))
return false
})
}
Expand Down Expand Up @@ -244,7 +249,7 @@ func renderPollList(b *bytes.Buffer, path string) string {
func getPoll(id string) *poll.Poll {
val, exists := polls.Get(id)
if !exists {
panic("poll not found")
panic(ufmt.Sprintf("poll not found: %s", id))
}
return val.(*poll.Poll)
}
Expand Down

0 comments on commit 4fa9786

Please sign in to comment.