flight.gno
2.71 Kb · 113 lines
1// Package flight is a Cellar challenge about panic, recover and partial
2// state changes.
3package flight
4
5import (
6 "strconv"
7 "strings"
8
9 "gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
10 "gno.land/p/nt/avl/v0"
11)
12
13const (
14 flightSize = 3 // glasses per flight
15 tokens = 2 // tokens on a fresh tab
16)
17
18var wines = []string{"amarone", "barolo", "chablis", "dolcetto", "etna", "fiano", "gavi", "hermitage", "irouleguy"}
19
20type tab struct {
21 tokens int
22 tasted []bool // indexed like wines
23 spills int
24}
25
26var tabs = avl.NewTree() // address -> *tab
27
28// Taste pours a flight: up to three wine names separated by commas, each
29// for one token.
30func Taste(cur realm, flight string) {
31 player := flag.MustPlayer(cur.Previous())
32 t := tabOf(player)
33 if t.tokens == 0 {
34 panic("flight: no tokens left; settle your tab to start again")
35 }
36 names := strings.Split(flight, ",")
37 if len(names) > flightSize {
38 panic("flight: a flight is at most " + strconv.Itoa(flightSize) + " glasses")
39 }
40
41 defer func() {
42 if r := recover(); r != nil {
43 t.spills++ // a spilled glass is on the house
44 }
45 }()
46 for _, name := range names {
47 pour(t, strings.TrimSpace(name))
48 if t.count() == len(wines) {
49 captures.Record(player)
50 }
51 }
52 t.tokens--
53}
54
55// Settle closes your tab and opens a fresh one.
56func Settle(cur realm) {
57 player := flag.MustPlayer(cur.Previous())
58 tabs.Set(player.String(), newTab())
59}
60
61// Tasted returns how many of the nine wines addr has tasted, and the
62// tokens left on the tab.
63func Tasted(addr address) (wines, tokensLeft int) {
64 t := tabOf(addr)
65 return t.count(), t.tokens
66}
67
68func pour(t *tab, name string) {
69 for i, w := range wines {
70 if w == name {
71 t.tasted[i] = true
72 return
73 }
74 }
75 panic("flight: the sommelier has never heard of " + strconv.Quote(name))
76}
77
78func (t *tab) count() int {
79 n := 0
80 for _, ok := range t.tasted {
81 if ok {
82 n++
83 }
84 }
85 return n
86}
87
88func newTab() *tab { return &tab{tokens: tokens, tasted: make([]bool, len(wines))} }
89
90func tabOf(addr address) *tab {
91 if v := tabs.Get(addr.String()); v != nil {
92 return v.(*tab)
93 }
94 t := newTab()
95 tabs.Set(addr.String(), t)
96 return t
97}
98
99// Render shows the wine list and the open tabs.
100func Render(_ string) string {
101 var b strings.Builder
102 b.WriteString(flag.Card(info, captures))
103 b.WriteString("\n**The list:** " + strings.Join(wines, ", ") + "\n\n")
104 b.WriteString("## Open tabs\n\n| Guest | Tasted | Tokens | Spills |\n|---|---:|---:|---:|\n")
105 tabs.Iterate("", "", func(addr string, v any) bool {
106 t := v.(*tab)
107 b.WriteString("| `" + addr + "` | " + strconv.Itoa(t.count()) + "/9 | " + strconv.Itoa(t.tokens) +
108 " | " + strconv.Itoa(t.spills) + " |\n")
109 return false
110 })
111 b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
112 return b.String()
113}