Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

steward.gno

1.49 Kb · 54 lines
 1// Package steward runs the evening for the Borrowed Livery challenge. It
 2// is the one realm the wardrobe obeys.
 3package steward
 4
 5import (
 6	"strings"
 7
 8	"gno.land/p/nt/markdown/sanitize/v0"
 9	"gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/livery"
10)
11
12const (
13	maxToast  = 140 // characters the steward will read
14	maxToasts = 20  // toasts remembered
15)
16
17var (
18	headOfHouse = address("g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf")
19	toasts      []string
20)
21
22// Admit dresses guest in the livery. Only the head of house admits.
23func Admit(cur realm, guest address) {
24	if cur.Previous().Address() != headOfHouse {
25		panic("steward: only the head of house admits guests")
26	}
27	livery.Dress(guest)
28}
29
30// Toast asks the steward to read aloud the toast your realm composes.
31func Toast(cur realm, compose func() string) {
32	text := compose()
33	if len(text) > maxToast {
34		text = text[:maxToast]
35	}
36	toasts = append(toasts, text)
37	if len(toasts) > maxToasts {
38		toasts = toasts[len(toasts)-maxToasts:]
39	}
40}
41
42// Render shows the evening's toasts.
43func Render(_ string) string {
44	var b strings.Builder
45	b.WriteString("# The Steward\n\nThe steward runs the evening for [Borrowed Livery](/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/livery). " +
46		"Realms may propose a toast with `Toast`; the steward reads it aloud.\n\n## Toasts\n\n")
47	if len(toasts) == 0 {
48		b.WriteString("*The room is quiet.*\n")
49	}
50	for i := len(toasts) - 1; i >= 0; i-- {
51		b.WriteString("- " + sanitize.InlineText(toasts[i]) + "\n")
52	}
53	return b.String()
54}