Playground
gnomod.toml
module = "gno.land/r/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/x/daily/moodstone/v1" gno = "0.9" private = true [addpkg] creator = "g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv" height = 914308 max_deposit = "5000000ugnot"
moodstone.gno
package moodstone
import (
"chain/runtime"
"chain/runtime/unsafe"
"strconv"
"strings"
"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
)
// Entry holds a single mood log.
type Entry struct {
Author address
Mood string
Note string
Block int64
}
var entries []Entry
var validMoods = []string{"happy", "sad", "excited", "calm", "anxious", "angry", "neutral"}
func isValid(mood string) bool {
for _, m := range validMoods {
if m == mood {
return true
}
}
return false
}
func emoji(mood string) string {
switch mood {
case "happy":
return "😊"
case "sad":
return "😢"
case "excited":
return "🎉"
case "calm":
return "😌"
case "anxious":
return "😰"
case "angry":
return "😠"
case "neutral":
return "😐"
}
return "❓"
}
func bar(count, total int) string {
if total == 0 {
return strings.Repeat("░", 20)
}
filled := (count * 20) / total
return strings.Repeat("▓", filled) + strings.Repeat("░", 20-filled)
}
// LogMood records the caller's current mood with an optional note.
// mood must be one of: happy sad excited calm anxious angry neutral
func LogMood(_ realm, mood, note string) {
mood = strings.ToLower(strings.TrimSpace(mood))
if !isValid(mood) {
panic("invalid mood; choose from: " + strings.Join(validMoods, ", "))
}
entries = append(entries, Entry{
Author: unsafe.OriginCaller(),
Mood: mood,
Note: strings.TrimSpace(note),
Block: runtime.ChainHeight(),
})
}
// TotalEntries returns the total number of mood logs on record.
func TotalEntries() int {
return len(entries)
}
// MoodCount returns how many times the given mood has been logged.
func MoodCount(mood string) int {
mood = strings.ToLower(strings.TrimSpace(mood))
n := 0
for _, e := range entries {
if e.Mood == mood {
n++
}
}
return n
}
// DominantMood returns the most-logged mood, or "none" if no entries.
func DominantMood() string {
if len(entries) == 0 {
return "none"
}
counts := make(map[string]int)
for _, e := range entries {
counts[e.Mood]++
}
best := ""
bestN := 0
for _, m := range validMoods {
if counts[m] > bestN {
bestN = counts[m]
best = m
}
}
return best
}
func Render(path string) string {
out := "# 🪨 Moodstone — On-chain Mood Tracker\n\n"
out += "> Log how you feel. Watch how the community feels. Immutably, on Gno.\n\n"
total := len(entries)
// Community stats table
out += "## 📊 Community Vibes\n\n"
if total == 0 {
out += "*No moods logged yet — be the first!*\n\n"
} else {
counts := make(map[string]int)
for _, e := range entries {
counts[e.Mood]++
}
out += "| Mood | Count | Distribution |\n"
out += "|------|-------|--------------|\n"
for _, m := range validMoods {
c := counts[m]
pct := 0
if total > 0 {
pct = (c * 100) / total
}
out += "| " + emoji(m) + " " + m + " | " + strconv.Itoa(c) +
" | `" + bar(c, total) + "` " + strconv.Itoa(pct) + "% |\n"
}
dom := DominantMood()
out += "\n**Total entries:** " + strconv.Itoa(total) +
" · **Dominant mood:** " + emoji(dom) + " " + dom + "\n\n"
}
// Recent entries (newest first, max 10)
out += "## 📝 Recent Entries\n\n"
if total == 0 {
out += "*Nothing here yet.*\n\n"
} else {
start := total - 10
if start < 0 {
start = 0
}
for i := total - 1; i >= start; i-- {
e := entries[i]
line := "- " + emoji(e.Mood) + " **" + e.Mood + "**"
if e.Note != "" {
line += ": *" + e.Note + "*"
}
line += " — " + ui.Addr(e.Author) + " at block " +
strconv.FormatInt(e.Block, 10)
out += line + "\n"
}
out += "\n"
}
// How to use
out += "## 🚀 How to Use\n\n"
out += "```bash\n"
out += "gnokey maketx call \\\n"
out += " -pkgpath gno.land/r/g12cs4cehujpffpjpywmkqj43m6u5ya53nj69sjz/moodstone \\\n"
out += " -func LogMood \\\n"
out += " -args 'happy' -args 'Just shipped something awesome!'\n"
out += "```\n\n"
out += "**Valid moods:** " + strings.Join(validMoods, " · ") + "\n"
return out
}
moodstone_test.gno
package moodstone
import (
"testing"
"gno.land/p/nt/testutils/v0"
)
var (
alice = testutils.TestAddress("alice")
bob = testutils.TestAddress("bob")
)
func TestLogMoodBasic(cur realm, t *testing.T) {
entries = nil
testing.SetOriginCaller(alice)
LogMood(cross(cur),"happy", "great day!")
LogMood(cross(cur),"calm", "")
if len(entries) != 2 {
t.Fatalf("expected 2 entries, got %d", len(entries))
}
if entries[0].Mood != "happy" {
t.Errorf("expected happy, got %s", entries[0].Mood)
}
if entries[0].Note != "great day!" {
t.Errorf("expected 'great day!', got %s", entries[0].Note)
}
if entries[1].Mood != "calm" {
t.Errorf("expected calm, got %s", entries[1].Mood)
}
}
func TestLogMoodCaseInsensitive(cur realm, t *testing.T) {
entries = nil
testing.SetOriginCaller(alice)
LogMood(cross(cur),"HAPPY", "uppercase input")
if entries[0].Mood != "happy" {
t.Errorf("expected normalized to happy, got %s", entries[0].Mood)
}
}
func TestIsValid(t *testing.T) {
for _, m := range validMoods {
if !isValid(m) {
t.Errorf("%s should be valid", m)
}
}
if isValid("euphoric") {
t.Error("euphoric should not be valid")
}
if isValid("") {
t.Error("empty string should not be valid")
}
}
func TestMoodCount(cur realm, t *testing.T) {
entries = nil
testing.SetOriginCaller(alice)
LogMood(cross(cur),"happy", "")
LogMood(cross(cur),"happy", "")
LogMood(cross(cur),"sad", "")
if MoodCount("happy") != 2 {
t.Errorf("expected 2 happy, got %d", MoodCount("happy"))
}
if MoodCount("sad") != 1 {
t.Errorf("expected 1 sad, got %d", MoodCount("sad"))
}
if MoodCount("angry") != 0 {
t.Errorf("expected 0 angry, got %d", MoodCount("angry"))
}
}
func TestTotalEntries(cur realm, t *testing.T) {
entries = nil
if TotalEntries() != 0 {
t.Errorf("expected 0, got %d", TotalEntries())
}
testing.SetOriginCaller(alice)
LogMood(cross(cur),"excited", "testing!")
if TotalEntries() != 1 {
t.Errorf("expected 1, got %d", TotalEntries())
}
}
func TestDominantMood(cur realm, t *testing.T) {
entries = nil
if DominantMood() != "none" {
t.Errorf("expected none on empty, got %s", DominantMood())
}
testing.SetOriginCaller(alice)
LogMood(cross(cur),"happy", "")
LogMood(cross(cur),"happy", "")
testing.SetOriginCaller(bob)
LogMood(cross(cur),"sad", "")
if DominantMood() != "happy" {
t.Errorf("expected happy to dominate, got %s", DominantMood())
}
}
func TestRender(cur realm, t *testing.T) {
entries = nil
out := Render("")
if len(out) == 0 {
t.Error("Render returned empty string")
}
testing.SetOriginCaller(alice)
LogMood(cross(cur),"neutral", "just checking the render")
out = Render("")
if len(out) == 0 {
t.Error("Render returned empty string after entries")
}
}