Playground
cagematch.gno
// Package cagematch is the Cage Match arena: it runs deterministic fights
// between roster fighters, stores the full recap of every fight on-chain,
// and keeps the ladder. Fights are resolved synchronously inside one
// transaction with bounded compute (9x9 grid, 60 rounds).
package cagematch
import (
"strconv"
"strings"
"chain"
"gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/engine"
roster "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/roster/v0"
)
// Fight is one recorded battle.
type Fight struct {
ID int
A, B string
Winner int // 0 = A, 1 = B, -1 = draw
Rounds int
Recap engine.Recap
Challenger address
}
// Entry is one fighter's ladder record. Points: win 3, draw 1, loss 0.
type Entry struct {
Name string
Points int
Wins int
Draws int
Losses int
}
var (
fights []*Fight
ladder = map[string]*Entry{}
ladderOrder []string
)
func assert(cond bool, msg string) {
if !cond {
panic(msg)
}
}
// Challenge runs a fight between two roster fighters and records it.
// Anyone may call it; the fight is deterministic and its recap public.
// Returns the fight id.
func Challenge(cur realm, a, b string) int {
fa, ok := roster.ByName(a)
assert(ok, "unknown fighter: "+a)
fb, ok := roster.ByName(b)
assert(ok, "unknown fighter: "+b)
assert(a != b, "a fighter cannot fight itself")
recap := engine.Run(fa, fb)
f := &Fight{
ID: len(fights),
A: a,
B: b,
Winner: recap.Winner,
Rounds: recap.Rounds,
Recap: recap,
Challenger: cur.Previous().Address(),
}
fights = append(fights, f)
record(recap)
chain.Emit("Fight",
"id", strconv.Itoa(f.ID),
"a", a,
"b", b,
"winner", winnerName(f),
"rounds", strconv.Itoa(recap.Rounds),
)
return f.ID
}
func record(r engine.Recap) {
ea := entry(r.A)
eb := entry(r.B)
switch r.Winner {
case 0:
ea.Points += 3
ea.Wins++
eb.Losses++
case 1:
eb.Points += 3
eb.Wins++
ea.Losses++
default:
ea.Points++
ea.Draws++
eb.Points++
eb.Draws++
}
}
func entry(name string) *Entry {
e, ok := ladder[name]
if !ok {
e = &Entry{Name: name}
ladder[name] = e
ladderOrder = append(ladderOrder, name)
}
return e
}
func winnerName(f *Fight) string {
switch f.Winner {
case 0:
return f.A
case 1:
return f.B
default:
return "draw"
}
}
// ---- read-only API ----
// FightCount returns how many fights have been recorded.
func FightCount() int { return len(fights) }
// GetFight returns a copy of a recorded fight. Panics if unknown.
func GetFight(id int) Fight {
assert(id >= 0 && id < len(fights), "unknown fight id: "+strconv.Itoa(id))
return *fights[id]
}
// Leaderboard returns ladder entries sorted by points, then name.
func Leaderboard() []Entry {
out := make([]Entry, 0, len(ladderOrder))
for _, n := range ladderOrder {
out = append(out, *ladder[n])
}
for i := 1; i < len(out); i++ {
for j := i; j > 0 && less(out[j], out[j-1]); j-- {
out[j], out[j-1] = out[j-1], out[j]
}
}
return out
}
func less(a, b Entry) bool {
if a.Points != b.Points {
return a.Points > b.Points
}
return a.Name < b.Name
}
// RosterNames lists the fighters available to challenge with.
func RosterNames() []string { return roster.Names() }
// ---- recap JSON for the viewer ----
// FightRecapJSON renders a fight recap as compact JSON. Frames are flat
// arrays: [round, ax, ay, bx, by, ahp, bhp, aact, adir, bact, bdir, admg, bdmg].
func FightRecapJSON(id int) string {
f := GetFight(id)
var sb strings.Builder
sb.WriteString(`{"id":` + strconv.Itoa(f.ID))
sb.WriteString(`,"a":` + strconv.Quote(f.A))
sb.WriteString(`,"b":` + strconv.Quote(f.B))
sb.WriteString(`,"winner":` + strconv.Itoa(f.Winner))
sb.WriteString(`,"rounds":` + strconv.Itoa(f.Rounds))
sb.WriteString(`,"grid":` + strconv.Itoa(engine.GridSize))
sb.WriteString(`,"maxhp":` + strconv.Itoa(engine.MaxHP))
sb.WriteString(`,"frames":[`)
for i, fr := range f.Recap.Frames {
if i > 0 {
sb.WriteByte(',')
}
sb.WriteString("[" + strconv.Itoa(fr.Round))
sb.WriteString("," + strconv.Itoa(fr.APos.X) + "," + strconv.Itoa(fr.APos.Y))
sb.WriteString("," + strconv.Itoa(fr.BPos.X) + "," + strconv.Itoa(fr.BPos.Y))
sb.WriteString("," + strconv.Itoa(fr.AHP) + "," + strconv.Itoa(fr.BHP))
sb.WriteString("," + strconv.Itoa(int(fr.AAct.Kind)) + "," + strconv.Itoa(int(fr.AAct.Dir)))
sb.WriteString("," + strconv.Itoa(int(fr.BAct.Kind)) + "," + strconv.Itoa(int(fr.BAct.Dir)))
sb.WriteString("," + strconv.Itoa(fr.ADmg) + "," + strconv.Itoa(fr.BDmg))
sb.WriteString("]")
}
sb.WriteString("]}")
return sb.String()
}
gnomod.toml
module = "gno.land/r/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch" gno = "0.9" [addpkg] creator = "g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v" height = 1534438 max_deposit = "100000000ugnot"
render.gno
package cagematch
import (
"strconv"
"strings"
"gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/engine"
)
// Render is the gnoweb entry point. Routes: "" home, "f/<id>" a fight.
func Render(path string) string {
if path == "" {
return renderHome()
}
if strings.HasPrefix(path, "f/") {
id, err := strconv.Atoi(strings.TrimPrefix(path, "f/"))
if err == nil && id >= 0 && id < len(fights) {
return renderFight(fights[id])
}
}
return "Unknown page. [Cage Match home](:)"
}
func renderHome() string {
var b strings.Builder
b.WriteString("# Cage Match\n\n")
b.WriteString("**AI fighters battle on-chain.** Every fighter is a small Gno package. ")
b.WriteString("Every fight is resolved by this realm, deterministically: the same two fighters always produce the same result, and anyone can replay a fight from its on-chain record.\n\n")
b.WriteString("Rules: 9x9 grid, 100 HP, at most 60 rounds. Each round a fighter moves, attacks (12 damage, adjacent tile), charges (next attack 24), or defends (damage halved). Points: win 3, draw 1, loss 0.\n\n")
b.WriteString("## Ladder\n\n")
if len(ladderOrder) == 0 {
b.WriteString("No fights yet. Start one from the Actions tab with `Challenge`.\n")
} else {
b.WriteString("| Fighter | Points | W | D | L |\n")
b.WriteString("| --- | --- | --- | --- | --- |\n")
for _, e := range Leaderboard() {
b.WriteString("| " + e.Name + " | " + strconv.Itoa(e.Points) + " | " + strconv.Itoa(e.Wins) + " | " + strconv.Itoa(e.Draws) + " | " + strconv.Itoa(e.Losses) + " |\n")
}
}
if len(fights) > 0 {
b.WriteString("\n## Recent fights\n\n")
start := 0
if len(fights) > 10 {
start = len(fights) - 10
}
for i := len(fights) - 1; i >= start; i-- {
f := fights[i]
b.WriteString("- [Fight " + strconv.Itoa(f.ID) + "](:f/" + strconv.Itoa(f.ID) + "): " + f.A + " vs " + f.B + " — **" + winnerName(f) + "** in " + strconv.Itoa(f.Rounds) + " rounds\n")
}
}
return b.String()
}
func renderFight(f *Fight) string {
var b strings.Builder
b.WriteString("# Fight " + strconv.Itoa(f.ID) + ": " + f.A + " vs " + f.B + "\n\n")
if f.Winner < 0 {
b.WriteString("**Draw** after " + strconv.Itoa(f.Rounds) + " rounds.\n\n")
} else {
b.WriteString("**" + winnerName(f) + " wins** in " + strconv.Itoa(f.Rounds) + " rounds.\n\n")
}
b.WriteString("Challenged by `" + f.Challenger.String() + "`.\n\n")
b.WriteString("| Round | A | B | A HP | B HP | A dmg | B dmg |\n")
b.WriteString("| --- | --- | --- | --- | --- | --- | --- |\n")
for _, fr := range f.Recap.Frames {
b.WriteString("| " + strconv.Itoa(fr.Round) +
" | " + pos(fr.APos) + " | " + pos(fr.BPos) +
" | " + strconv.Itoa(fr.AHP) + " | " + strconv.Itoa(fr.BHP) +
" | " + strconv.Itoa(fr.ADmg) + " | " + strconv.Itoa(fr.BDmg) + " |\n")
}
b.WriteString("\nThis table is the fight. Replay it with the same two fighters and you get exactly these rows.\n")
return b.String()
}
func pos(p engine.Pos) string {
return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")"
}