render.gno
2.81 Kb · 81 lines
1package mybots
2
3import (
4 "strconv"
5 "strings"
6
7 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v2"
8)
9
10var classNames = [5]string{"Hunter", "Brawler", "Coward", "Trickster", "Reader"}
11
12func short(addr string) string {
13 if len(addr) < 12 {
14 return addr
15 }
16 return addr[:6] + "…" + addr[len(addr)-4:]
17}
18
19func pct(v int) string { return strconv.Itoa((v*111+5)/10) + "%" }
20
21// Describe says what a profile does, one plain line per trait.
22func Describe(profile string) []string {
23 p := rules.MustProfile(profile)
24 keep := "never runs away"
25 if p.Keep > 0 {
26 keep = "when ahead, runs from a rival closer than " + strconv.Itoa(p.Keep) + " squares"
27 }
28 return []string{
29 "plays " + classNames[p.Class],
30 "changes its plan every " + strconv.Itoa(p.Think/10) + "." + strconv.Itoa(p.Think%10) + "s",
31 "stands still " + strconv.Itoa(p.Idle*5) + "% of the time",
32 "chases a rival who carries gold " + pct(p.Chase) + " of the time",
33 "goes for the jackpot " + pct(p.Jackpot) + " of the time",
34 "looks for gold up to " + strconv.Itoa(p.Reach) + " squares away",
35 "swings at a rival in reach: " + pct(p.Attack) + " a tick",
36 "blocks a rival in reach: " + pct(p.Block) + " a tick",
37 "fires its special when it pays " + pct(p.Special) + " of the time",
38 "dodges right after being robbed " + pct(p.Dodge) + " of the time",
39 keep,
40 }
41}
42
43// Render lists the bots, or shows one at "bot/<address>".
44func Render(path string) string {
45 var b strings.Builder
46 if strings.HasPrefix(path, "bot/") {
47 addr := path[4:]
48 name, p, ok := record(addr)
49 if !ok {
50 return "No bot for " + short(addr) + "."
51 }
52 b.WriteString("# " + name + "\n\n")
53 b.WriteString("The bot " + short(addr) + " taught by playing. It:\n\n")
54 for _, l := range Describe(p) {
55 b.WriteString("- " + l + "\n")
56 }
57 b.WriteString("\nProfile: `" + p + "`\n")
58 return b.String()
59 }
60 b.WriteString("# Player bots\n\n")
61 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. ")
62 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")
63 if bots.Size() == 0 {
64 b.WriteString("No bots yet.\n")
65 return b.String()
66 }
67 b.WriteString("| Bot | Class | Chases | Owner |\n|---|---|---|---|\n")
68 bots.IterateByOffset(0, 50, func(k string, v any) bool {
69 name, p, _ := record(k)
70 pr := rules.MustProfile(p)
71 b.WriteString("| [" + name + "](:bot/" + k + ") | " + classNames[pr.Class] + " | " + pct(pr.Chase) + " | " + short(k) + " |\n")
72 return false
73 })
74 if n := bots.Size(); n == 1 {
75 b.WriteString("\n1 bot.\n")
76 } else {
77 b.WriteString("\n" + strconv.Itoa(n) + " bots.\n")
78 }
79 b.WriteString("\nNeed testnet GNOT to play? Get it free at [faucet.gno.land](https://faucet.gno.land).\n")
80 return b.String()
81}