arena.gno
4.48 Kb · 159 lines
1// Package arena runs matches between bots that other people deploy as their
2// own realms. Every turn the arena calls each bot's code, so the chain is the
3// referee and every bot's source is public.
4//
5// Matches have two speeds:
6//
7// - Fast: Play (or Fight) runs a whole match in one transaction.
8// - Step: if a match can't finish in one transaction (a bot crashes or runs
9// out of gas), StartStepMode switches it to one move per SubmitMove
10// transaction. The bot whose move can't be made misses its deadline and
11// ClaimForfeit records the loss. Crashing can't save a bot from a loss.
12package arena
13
14import (
15 "chain/runtime/unsafe"
16 "strings"
17
18 "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
19 "gno.land/p/nt/avl/v0"
20)
21
22const (
23 maxBotsPerRealm = 5
24 startRating = 1200
25)
26
27// Bot is a registered bot.
28type Bot struct {
29 Name string
30 Realm string // realm that registered it; its source is public
31 Owner address // account that sent the registering transaction
32 Registered int64 // block height
33 Rating int
34 Wins int
35 Losses int
36 Draws int
37 Forfeits int // losses by forfeit, also counted in Losses
38
39 impl bot.Bot
40 fn bot.Strategy
41 recentRated string // "opponent@block," for recent rated matches, see claimRatedSlot
42}
43
44// Played is the number of finished matches.
45func (b *Bot) Played() int { return b.Wins + b.Losses + b.Draws }
46
47var (
48 bots = avl.NewTree() // name -> *Bot
49 botsPerRealm = avl.NewTree() // realm path -> int
50)
51
52// RegisterBot enters a bot with a Move method. Call it from your bot realm,
53// usually in init:
54//
55// func init(cur realm) { arena.RegisterBot(cross(cur), "my-bot", &myBot{}) }
56func RegisterBot(cur realm, name string, b bot.Bot) {
57 if b == nil {
58 panic("bot is nil")
59 }
60 register(cur, name, &Bot{impl: b})
61}
62
63// RegisterStrategy enters a bot written as a plain function. A function
64// declared in a /p/ package has one extra guarantee: if it crashes, the arena
65// can catch it straight away and score a forfeit without step mode.
66func RegisterStrategy(cur realm, name string, s bot.Strategy) {
67 if s == nil {
68 panic("strategy is nil")
69 }
70 register(cur, name, &Bot{fn: s})
71}
72
73func register(cur realm, name string, b *Bot) {
74 caller := cur.Previous()
75 if !caller.IsCode() || caller.IsEphemeral() {
76 panic("bots must be registered by a deployed realm, so their source is public")
77 }
78 if err := validName(name); err != "" {
79 panic(err)
80 }
81 if bots.Has(name) {
82 panic("bot name already taken: " + name)
83 }
84 realmPath := caller.PkgPath()
85 count := 0
86 if v := botsPerRealm.Get(realmPath); v != nil {
87 count = v.(int)
88 }
89 if count >= maxBotsPerRealm {
90 panic("this realm already registered the maximum number of bots")
91 }
92 b.Name = name
93 b.Realm = realmPath
94 b.Owner = unsafe.OriginCaller()
95 b.Registered = height()
96 b.Rating = startRating
97 bots.Set(name, b)
98 botsPerRealm.Set(realmPath, count+1)
99 emit("BotRegistered", "name", name, "realm", realmPath)
100}
101
102func validName(name string) string {
103 if len(name) < 3 || len(name) > 24 {
104 return "bot names must be 3 to 24 characters"
105 }
106 for i, c := range name {
107 ok := (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || (c == '-' && i > 0 && i < len(name)-1)
108 if !ok {
109 return "bot names may only use a-z, 0-9 and inner dashes"
110 }
111 }
112 if strings.Contains(name, "--") {
113 return "bot names may not contain --"
114 }
115 return ""
116}
117
118func mustBot(name string) *Bot {
119 v := bots.Get(name)
120 if v == nil {
121 panic("unknown bot: " + name)
122 }
123 return v.(*Bot)
124}
125
126// ask gets one move from b. A crash inside a /p/ function bot is caught and
127// reported; a crash inside realm code aborts the transaction (Gno does not
128// let the caller recover it), which is what step mode is for.
129func (b *Bot) ask(s bot.State) (a bot.Action, crashed bool) {
130 defer func() {
131 if r := recover(); r != nil {
132 crashed = true
133 }
134 }()
135 if b.impl != nil {
136 return b.impl.Move(s), false
137 }
138 return b.fn(s), false
139}
140
141// BotInfo describes a bot for other realms and off-chain tools.
142type BotInfo struct {
143 Name, Realm string
144 Owner address
145 Rating, Wins, Losses, Draws, Forfeits, Played int
146}
147
148// GetBot returns public information about a bot.
149func GetBot(name string) (BotInfo, bool) {
150 v := bots.Get(name)
151 if v == nil {
152 return BotInfo{}, false
153 }
154 b := v.(*Bot)
155 return BotInfo{b.Name, b.Realm, b.Owner, b.Rating, b.Wins, b.Losses, b.Draws, b.Forfeits, b.Played()}, true
156}
157
158// BotCount is the number of registered bots.
159func BotCount() int { return bots.Size() }