livery.gno
1.58 Kb · 53 lines
1// Package livery is a Cellar challenge about the difference between the
2// realm that is acting and the code that is running.
3package livery
4
5import (
6 "chain/runtime/unsafe"
7 "strings"
8
9 "gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
10 "gno.land/p/nt/avl/v0"
11)
12
13// StewardPath is the only realm allowed to hand out the livery.
14const StewardPath = "gno.land/r/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/steward"
15
16var dressed = avl.NewTree() // guest address -> true
17
18// Dress puts guest in the house livery. The wardrobe only obeys the
19// steward: it checks that the steward is the realm currently acting.
20func Dress(guest address) {
21 if unsafe.CurrentRealm().PkgPath() != StewardPath {
22 panic("livery: only the steward hands out the livery")
23 }
24 dressed.Set(guest.String(), true)
25}
26
27// Enter lets a guest in livery into the private cellar, where the flag is.
28func Enter(cur realm) {
29 player := flag.MustPlayer(cur.Previous())
30 if !dressed.Has(player.String()) {
31 panic("livery: guests in livery only")
32 }
33 captures.Record(player)
34}
35
36// InLivery reports whether addr wears the livery.
37func InLivery(addr address) bool { return dressed.Has(addr.String()) }
38
39// Render shows who wears the livery.
40func Render(_ string) string {
41 var b strings.Builder
42 b.WriteString(flag.Card(info, captures))
43 b.WriteString("\n## In livery\n\n")
44 if dressed.Size() == 0 {
45 b.WriteString("*Nobody yet.*\n")
46 }
47 dressed.Iterate("", "", func(addr string, _ any) bool {
48 b.WriteString("- `" + addr + "`\n")
49 return false
50 })
51 b.WriteString("\n## Captured by\n\n" + flag.Captures(captures, 5))
52 return b.String()
53}