// 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/v2" "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) }