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.

vintage.gno

1.79 Kb · 66 lines
 1// Package vintage is a Cellar challenge about trusting interface values.
 2package vintage
 3
 4import (
 5	"strconv"
 6	"strings"
 7
 8	"gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
 9	"gno.land/p/nt/markdown/sanitize/v0"
10)
11
12const maxLabel = 60
13
14// Bottle is a bottle from this cellar. The seal method is unexported, so
15// no other package can declare a type that implements Bottle.
16type Bottle interface {
17	Vintage() int
18	Label() string
19	seal()
20}
21
22type bottle struct {
23	vintage int
24	label   string
25}
26
27func (b *bottle) Vintage() int  { return b.vintage }
28func (b *bottle) Label() string { return b.label }
29func (b *bottle) seal()         {}
30
31// Uncork pours the house wine.
32func Uncork() Bottle { return &bottle{vintage: 2024, label: "House red"} }
33
34var certified []string // labels of certified bottles, for the window
35
36// Appraise certifies b if it was bottled before 1900.
37func Appraise(cur realm, b Bottle) {
38	player := flag.MustPlayer(cur.Previous())
39	if b == nil {
40		panic("vintage: the appraiser needs a bottle")
41	}
42	if b.Vintage() >= 1900 {
43		panic("vintage: a " + strconv.Itoa(b.Vintage()) + " is young. Come back with something older")
44	}
45	label := b.Label()
46	if len(label) > maxLabel {
47		label = label[:maxLabel]
48	}
49	certified = append(certified, label+" ("+strconv.Itoa(b.Vintage())+")")
50	captures.Record(player)
51}
52
53// Render shows the appraiser's window.
54func Render(_ string) string {
55	var b strings.Builder
56	b.WriteString(flag.Card(info, captures))
57	b.WriteString("\n## In the window\n\n")
58	if len(certified) == 0 {
59		b.WriteString("*No bottle has been certified.*\n")
60	}
61	for i := len(certified) - 1; i >= 0 && i >= len(certified)-10; i-- {
62		b.WriteString("- certified: " + sanitize.InlineText(certified[i]) + "\n")
63	}
64	b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
65	return b.String()
66}