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.

Playground

gnomod.toml

module = "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/mybots"
gno = "0.9"

[addpkg]
  creator = "g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28"
  height = 1523197
  max_deposit = "50000000ugnot"

mybots.gno

// Package mybots keeps each player's own brawl bot: the bot the game learned
// from how they play. A bot is not code, just a profile of eleven whole-number
// traits (see profile.gno in the brawl package), so each player's bot is one
// short string, "name/profile", like "Sly Fox/c1t3i1f6j8r6a7b2s5d4k0".
//
// Anyone can fight anyone's bot: brawl.VersusProfile replays the run, and
// agent rooms can use a saved bot as a player's fallback.
package mybots

import (
	"chain"
	"strconv"
	"strings"

	rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl"
	"gno.land/p/nt/avl/v0"
)

const MaxName = 16

var bots = avl.NewTree() // address -> "name/profile"

// check returns why a bot can't be saved, or "".
func check(name, profile string) string {
	if len(name) == 0 || len(name) > MaxName || name[0] == ' ' || name[len(name)-1] == ' ' {
		return "name is 1 to 16 characters, no space at either end"
	}
	for i := 0; i < len(name); i++ {
		c := name[i]
		if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == ' ' || c == '-' || c == '\'') {
			return "name uses letters, digits, spaces, - and ' only"
		}
	}
	if _, err := rules.ParseProfile(profile); err != nil {
		return err.Error()
	}
	return ""
}

// Check reports whether a bot can be saved: "ok" or the reason it is refused.
func Check(name, profile string) string {
	if why := check(name, profile); why != "" {
		return why
	}
	return "ok"
}

// Save stores the caller's bot, replacing the one they had.
func Save(cur realm, name, profile string) {
	if why := check(name, profile); why != "" {
		panic(why)
	}
	p := rules.MustProfile(profile)
	addr := cur.Previous().Address().String()
	bots.Set(addr, name+"/"+p.String())
	chain.Emit("Save", "owner", addr, "name", name, "profile", p.String())
}

// Remove deletes the caller's bot.
func Remove(cur realm) {
	addr := cur.Previous().Address().String()
	if _, ok := bots.Remove(addr); !ok {
		panic("you have no bot")
	}
	chain.Emit("Remove", "owner", addr)
}

func record(addr string) (string, string, bool) {
	v := bots.Get(addr)
	if v == nil {
		return "", "", false
	}
	s := v.(string)
	i := strings.LastIndexByte(s, '/')
	return s[:i], s[i+1:], true
}

// Profile returns an address's bot profile, or "" if it has none.
func Profile(addr string) string {
	_, p, _ := record(addr)
	return p
}

// Count is how many players have a bot.
func Count() int { return bots.Size() }

func botJSON(addr, name, profile string) string {
	p := rules.MustProfile(profile)
	return `{"owner":"` + addr + `","name":` + strconv.Quote(name) + `,"profile":"` + profile + `","class":` + strconv.Itoa(p.Class) + `}`
}

// Get describes an address's bot as JSON, or "null".
func Get(addr string) string {
	name, p, ok := record(addr)
	if !ok {
		return "null"
	}
	return botJSON(addr, name, p)
}

// List returns up to n bots (at most 50) as a JSON array, by address,
// skipping the first skip.
func List(skip, n int) string {
	if n > 50 {
		n = 50
	}
	parts := []string{}
	if skip < 0 {
		skip = 0
	}
	bots.IterateByOffset(skip, n, func(k string, v any) bool {
		name, p, _ := record(k)
		parts = append(parts, botJSON(k, name, p))
		return false
	})
	return "[" + strings.Join(parts, ",") + "]"
}

// Versus replays a run (your inputs, as class) against someone's bot and
// returns "yourGold-botGold". Use it as a read to check a run before telling
// anyone about it.
func Versus(owner string, seed int, class int, inputs string) string {
	p := Profile(owner)
	if p == "" {
		panic("no bot for " + owner)
	}
	a, b := rules.VersusProfile(uint32(seed), class, p, inputs)
	return strconv.Itoa(a) + "-" + strconv.Itoa(b)
}

render.gno

package mybots

import (
	"strconv"
	"strings"

	rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl"
)

var classNames = [5]string{"Hunter", "Brawler", "Coward", "Trickster", "Reader"}

func short(addr string) string {
	if len(addr) < 12 {
		return addr
	}
	return addr[:6] + "…" + addr[len(addr)-4:]
}

func pct(v int) string { return strconv.Itoa((v*111+5)/10) + "%" }

// Describe says what a profile does, one plain line per trait.
func Describe(profile string) []string {
	p := rules.MustProfile(profile)
	keep := "never runs away"
	if p.Keep > 0 {
		keep = "when ahead, runs from a rival closer than " + strconv.Itoa(p.Keep) + " squares"
	}
	return []string{
		"plays " + classNames[p.Class],
		"changes its plan every " + strconv.Itoa(p.Think/10) + "." + strconv.Itoa(p.Think%10) + "s",
		"stands still " + strconv.Itoa(p.Idle*5) + "% of the time",
		"chases a rival who carries gold " + pct(p.Chase) + " of the time",
		"goes for the jackpot " + pct(p.Jackpot) + " of the time",
		"looks for gold up to " + strconv.Itoa(p.Reach) + " squares away",
		"swings at a rival in reach: " + pct(p.Attack) + " a tick",
		"blocks a rival in reach: " + pct(p.Block) + " a tick",
		"fires its special when it pays " + pct(p.Special) + " of the time",
		"dodges right after being robbed " + pct(p.Dodge) + " of the time",
		keep,
	}
}

// Render lists the bots, or shows one at "bot/<address>".
func Render(path string) string {
	var b strings.Builder
	if strings.HasPrefix(path, "bot/") {
		addr := path[4:]
		name, p, ok := record(addr)
		if !ok {
			return "No bot for " + short(addr) + "."
		}
		b.WriteString("# " + name + "\n\n")
		b.WriteString("The bot " + short(addr) + " taught by playing. It:\n\n")
		for _, l := range Describe(p) {
			b.WriteString("- " + l + "\n")
		}
		b.WriteString("\nProfile: `" + p + "`\n")
		return b.String()
	}
	b.WriteString("# Player bots\n\n")
	b.WriteString("Bots the brawler learned from how people play. Each one is a profile of eleven numbers, not code, stored as one short string. ")
	b.WriteString("Play a few runs, then save yours from the end screen with `Save(name, profile)`; anyone can fight it, and rooms can use it as your fallback.\n\n")
	if bots.Size() == 0 {
		b.WriteString("No bots yet.\n")
		return b.String()
	}
	b.WriteString("| Bot | Class | Chases | Owner |\n|---|---|---|---|\n")
	bots.IterateByOffset(0, 50, func(k string, v any) bool {
		name, p, _ := record(k)
		pr := rules.MustProfile(p)
		b.WriteString("| [" + name + "](:bot/" + k + ") | " + classNames[pr.Class] + " | " + pct(pr.Chase) + " | " + short(k) + " |\n")
		return false
	})
	if n := bots.Size(); n == 1 {
		b.WriteString("\n1 bot.\n")
	} else {
		b.WriteString("\n" + strconv.Itoa(n) + " bots.\n")
	}
	return b.String()
}