moodstone.gno
3.88 Kb Β· 177 lines
1package moodstone
2
3import (
4 "chain/runtime"
5 "chain/runtime/unsafe"
6 "strconv"
7 "strings"
8
9 "gno.land/p/moul/kit/ui/v0"
10)
11
12// Entry holds a single mood log.
13type Entry struct {
14 Author address
15 Mood string
16 Note string
17 Block int64
18}
19
20var entries []Entry
21
22var validMoods = []string{"happy", "sad", "excited", "calm", "anxious", "angry", "neutral"}
23
24func isValid(mood string) bool {
25 for _, m := range validMoods {
26 if m == mood {
27 return true
28 }
29 }
30 return false
31}
32
33func emoji(mood string) string {
34 switch mood {
35 case "happy":
36 return "π"
37 case "sad":
38 return "π’"
39 case "excited":
40 return "π"
41 case "calm":
42 return "π"
43 case "anxious":
44 return "π°"
45 case "angry":
46 return "π "
47 case "neutral":
48 return "π"
49 }
50 return "β"
51}
52
53func bar(count, total int) string {
54 if total == 0 {
55 return strings.Repeat("β", 20)
56 }
57 filled := (count * 20) / total
58 return strings.Repeat("β", filled) + strings.Repeat("β", 20-filled)
59}
60
61// LogMood records the caller's current mood with an optional note.
62// mood must be one of: happy sad excited calm anxious angry neutral
63func LogMood(_ realm, mood, note string) {
64 mood = strings.ToLower(strings.TrimSpace(mood))
65 if !isValid(mood) {
66 panic("invalid mood; choose from: " + strings.Join(validMoods, ", "))
67 }
68 entries = append(entries, Entry{
69 Author: unsafe.OriginCaller(),
70 Mood: mood,
71 Note: strings.TrimSpace(note),
72 Block: runtime.ChainHeight(),
73 })
74}
75
76// TotalEntries returns the total number of mood logs on record.
77func TotalEntries() int {
78 return len(entries)
79}
80
81// MoodCount returns how many times the given mood has been logged.
82func MoodCount(mood string) int {
83 mood = strings.ToLower(strings.TrimSpace(mood))
84 n := 0
85 for _, e := range entries {
86 if e.Mood == mood {
87 n++
88 }
89 }
90 return n
91}
92
93// DominantMood returns the most-logged mood, or "none" if no entries.
94func DominantMood() string {
95 if len(entries) == 0 {
96 return "none"
97 }
98 counts := make(map[string]int)
99 for _, e := range entries {
100 counts[e.Mood]++
101 }
102 best := ""
103 bestN := 0
104 for _, m := range validMoods {
105 if counts[m] > bestN {
106 bestN = counts[m]
107 best = m
108 }
109 }
110 return best
111}
112
113func Render(path string) string {
114 out := "# πͺ¨ Moodstone β On-chain Mood Tracker\n\n"
115 out += "> Log how you feel. Watch how the community feels. Immutably, on Gno.\n\n"
116
117 total := len(entries)
118
119 // Community stats table
120 out += "## π Community Vibes\n\n"
121 if total == 0 {
122 out += "*No moods logged yet β be the first!*\n\n"
123 } else {
124 counts := make(map[string]int)
125 for _, e := range entries {
126 counts[e.Mood]++
127 }
128 out += "| Mood | Count | Distribution |\n"
129 out += "|------|-------|--------------|\n"
130 for _, m := range validMoods {
131 c := counts[m]
132 pct := 0
133 if total > 0 {
134 pct = (c * 100) / total
135 }
136 out += "| " + emoji(m) + " " + m + " | " + strconv.Itoa(c) +
137 " | `" + bar(c, total) + "` " + strconv.Itoa(pct) + "% |\n"
138 }
139 dom := DominantMood()
140 out += "\n**Total entries:** " + strconv.Itoa(total) +
141 " Β· **Dominant mood:** " + emoji(dom) + " " + dom + "\n\n"
142 }
143
144 // Recent entries (newest first, max 10)
145 out += "## π Recent Entries\n\n"
146 if total == 0 {
147 out += "*Nothing here yet.*\n\n"
148 } else {
149 start := total - 10
150 if start < 0 {
151 start = 0
152 }
153 for i := total - 1; i >= start; i-- {
154 e := entries[i]
155 line := "- " + emoji(e.Mood) + " **" + e.Mood + "**"
156 if e.Note != "" {
157 line += ": *" + e.Note + "*"
158 }
159 line += " β " + ui.Addr(e.Author) + " at block " +
160 strconv.FormatInt(e.Block, 10)
161 out += line + "\n"
162 }
163 out += "\n"
164 }
165
166 // How to use
167 out += "## π How to Use\n\n"
168 out += "```bash\n"
169 out += "gnokey maketx call \\\n"
170 out += " -pkgpath gno.land/r/g12cs4cehujpffpjpywmkqj43m6u5ya53nj69sjz/moodstone \\\n"
171 out += " -func LogMood \\\n"
172 out += " -args 'happy' -args 'Just shipped something awesome!'\n"
173 out += "```\n\n"
174 out += "**Valid moods:** " + strings.Join(validMoods, " Β· ") + "\n"
175
176 return out
177}