// 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() }