mybots.gno
3.57 Kb · 129 lines
1// Package mybots keeps each player's own brawl bot: the bot the game learned
2// from how they play. A bot is not code, just a profile of eleven whole-number
3// traits (see profile.gno in the brawl package), so each player's bot is one
4// short string, "name/profile", like "Sly Fox/c1t3i1f6j8r6a7b2s5d4k0".
5//
6// Anyone can fight anyone's bot: brawl.VersusProfile replays the run, and
7// agent rooms can use a saved bot as a player's fallback.
8package mybots
9
10import (
11 "chain"
12 "strconv"
13 "strings"
14
15 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3"
16 "gno.land/p/nt/avl/v0"
17)
18
19const MaxName = 16
20
21var bots = avl.NewTree() // address -> "name/profile"
22
23// check returns why a bot can't be saved, or "".
24func check(name, profile string) string {
25 if len(name) == 0 || len(name) > MaxName || name[0] == ' ' || name[len(name)-1] == ' ' {
26 return "name is 1 to 16 characters, no space at either end"
27 }
28 for i := 0; i < len(name); i++ {
29 c := name[i]
30 if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == ' ' || c == '-' || c == '\'') {
31 return "name uses letters, digits, spaces, - and ' only"
32 }
33 }
34 if _, err := rules.ParseProfile(profile); err != nil {
35 return err.Error()
36 }
37 return ""
38}
39
40// Check reports whether a bot can be saved: "ok" or the reason it is refused.
41func Check(name, profile string) string {
42 if why := check(name, profile); why != "" {
43 return why
44 }
45 return "ok"
46}
47
48// Save stores the caller's bot, replacing the one they had.
49func Save(cur realm, name, profile string) {
50 if why := check(name, profile); why != "" {
51 panic(why)
52 }
53 p := rules.MustProfile(profile)
54 addr := cur.Previous().Address().String()
55 bots.Set(addr, name+"/"+p.String())
56 chain.Emit("Save", "owner", addr, "name", name, "profile", p.String())
57}
58
59// Remove deletes the caller's bot.
60func Remove(cur realm) {
61 addr := cur.Previous().Address().String()
62 if _, ok := bots.Remove(addr); !ok {
63 panic("you have no bot")
64 }
65 chain.Emit("Remove", "owner", addr)
66}
67
68func record(addr string) (string, string, bool) {
69 v := bots.Get(addr)
70 if v == nil {
71 return "", "", false
72 }
73 s := v.(string)
74 i := strings.LastIndexByte(s, '/')
75 return s[:i], s[i+1:], true
76}
77
78// Profile returns an address's bot profile, or "" if it has none.
79func Profile(addr string) string {
80 _, p, _ := record(addr)
81 return p
82}
83
84// Count is how many players have a bot.
85func Count() int { return bots.Size() }
86
87func botJSON(addr, name, profile string) string {
88 p := rules.MustProfile(profile)
89 return `{"owner":"` + addr + `","name":` + strconv.Quote(name) + `,"profile":"` + profile + `","class":` + strconv.Itoa(p.Class) + `}`
90}
91
92// Get describes an address's bot as JSON, or "null".
93func Get(addr string) string {
94 name, p, ok := record(addr)
95 if !ok {
96 return "null"
97 }
98 return botJSON(addr, name, p)
99}
100
101// List returns up to n bots (at most 50) as a JSON array, by address,
102// skipping the first skip.
103func List(skip, n int) string {
104 if n > 50 {
105 n = 50
106 }
107 parts := []string{}
108 if skip < 0 {
109 skip = 0
110 }
111 bots.IterateByOffset(skip, n, func(k string, v any) bool {
112 name, p, _ := record(k)
113 parts = append(parts, botJSON(k, name, p))
114 return false
115 })
116 return "[" + strings.Join(parts, ",") + "]"
117}
118
119// Versus replays a run (your inputs, as class) against someone's bot and
120// returns "yourGold-botGold". Use it as a read to check a run before telling
121// anyone about it.
122func Versus(owner string, seed int, class int, inputs string) string {
123 p := Profile(owner)
124 if p == "" {
125 panic("no bot for " + owner)
126 }
127 a, b := rules.VersusProfile(uint32(seed), class, p, inputs)
128 return strconv.Itoa(a) + "-" + strconv.Itoa(b)
129}