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.

owner.gno

3.40 Kb · 127 lines
  1package arena
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
  8)
  9
 10// Owner tools. A bot's owner is the account that sent the transaction which
 11// registered it. Only the owner can describe, retire or upgrade a bot.
 12
 13const maxDescription = 200
 14
 15func mustOwn(cur realm, name string) *Bot {
 16	b := mustBot(name)
 17	if cur.Previous().Address() != b.Owner {
 18		panic("only the owner of " + name + " can do this")
 19	}
 20	return b
 21}
 22
 23// cleanText keeps a description on one line and drops characters that would
 24// turn it into markdown links, images, tables or HTML on the bot's page.
 25func cleanText(s string) string {
 26	var sb strings.Builder
 27	for _, c := range s {
 28		switch {
 29		case c == '\n' || c == '\r' || c == '\t':
 30			sb.WriteRune(' ')
 31		case strings.ContainsRune("<>[]()|*_`#!\\", c):
 32			// dropped
 33		default:
 34			sb.WriteRune(c)
 35		}
 36	}
 37	out := strings.TrimSpace(sb.String())
 38	if len(out) > maxDescription {
 39		out = out[:maxDescription]
 40	}
 41	return out
 42}
 43
 44// SetDescription sets the one-line description shown on the bot's page.
 45func SetDescription(cur realm, name, text string) {
 46	b := mustOwn(cur, name)
 47	b.Description = cleanText(text)
 48	emit("BotDescribed", "name", name)
 49}
 50
 51// Retire takes a bot off the leaderboard and stops new challenges against
 52// it. Its history stays. Matches already created can still be finished.
 53func Retire(cur realm, name string) {
 54	b := mustOwn(cur, name)
 55	if b.Retired {
 56		panic(name + " is already retired")
 57	}
 58	b.Retired = true
 59	emit("BotRetired", "name", name)
 60}
 61
 62// Unretire brings a retired bot back.
 63func Unretire(cur realm, name string) {
 64	b := mustOwn(cur, name)
 65	if !b.Retired {
 66		panic(name + " is not retired")
 67	}
 68	b.Retired = false
 69	emit("BotUnretired", "name", name)
 70}
 71
 72// UpgradeBot points an existing bot at new code, keeping its name, rating
 73// and record. Call it from the new bot realm's init, sent by the bot's owner:
 74//
 75//	func init(cur realm) { arena.UpgradeBot(cross(cur), "my-bot", &myBotV2{}) }
 76func UpgradeBot(cur realm, name string, b bot.Bot) {
 77	if b == nil {
 78		panic("bot is nil")
 79	}
 80	upgrade(cur, name, b, nil)
 81}
 82
 83// UpgradeStrategy is UpgradeBot for a bot written as a plain function.
 84func UpgradeStrategy(cur realm, name string, s bot.Strategy) {
 85	if s == nil {
 86		panic("strategy is nil")
 87	}
 88	upgrade(cur, name, nil, s)
 89}
 90
 91func upgrade(cur realm, name string, impl bot.Bot, fn bot.Strategy) {
 92	caller := cur.Previous()
 93	if !caller.IsCode() || caller.IsEphemeral() {
 94		panic("upgrades must come from a deployed realm, so the new source is public")
 95	}
 96	b := mustBot(name)
 97	if currentOrigin() != b.Owner {
 98		panic("only the owner of " + name + " can upgrade it")
 99	}
100	newRealm := caller.PkgPath()
101	if newRealm == b.Realm {
102		panic("the new version must live in a new realm")
103	}
104	reserveRealmSlot(newRealm)
105	b.History = appendHistory(b.History, b.Realm)
106	b.Realm = newRealm
107	b.Version++
108	b.impl, b.fn = impl, fn
109	emit("BotUpgraded", "name", name, "realm", newRealm, "version", strconv.Itoa(b.Version))
110}
111
112// appendHistory adds a realm to a bot's "realm@block," history.
113func appendHistory(h, realmPath string) string {
114	return h + realmPath + "@" + strconv.FormatInt(height(), 10) + ","
115}
116
117// PreviousVersions lists the realms a bot used before its current one, oldest
118// first.
119func (b *Bot) PreviousVersions() []string {
120	var out []string
121	for _, e := range strings.Split(b.History, ",") {
122		if at := strings.LastIndex(e, "@"); at > 0 {
123			out = append(out, e[:at])
124		}
125	}
126	return out
127}