cagematch.gno
4.53 Kb · 186 lines
1// Package cagematch is the Cage Match arena: it runs deterministic fights
2// between roster fighters, stores the full recap of every fight on-chain,
3// and keeps the ladder. Fights are resolved synchronously inside one
4// transaction with bounded compute (9x9 grid, 60 rounds).
5package cagematch
6
7import (
8 "strconv"
9 "strings"
10
11 "chain"
12
13 "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/engine"
14 roster "gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/roster/v0"
15)
16
17// Fight is one recorded battle.
18type Fight struct {
19 ID int
20 A, B string
21 Winner int // 0 = A, 1 = B, -1 = draw
22 Rounds int
23 Recap engine.Recap
24 Challenger address
25}
26
27// Entry is one fighter's ladder record. Points: win 3, draw 1, loss 0.
28type Entry struct {
29 Name string
30 Points int
31 Wins int
32 Draws int
33 Losses int
34}
35
36var (
37 fights []*Fight
38 ladder = map[string]*Entry{}
39 ladderOrder []string
40)
41
42func assert(cond bool, msg string) {
43 if !cond {
44 panic(msg)
45 }
46}
47
48// Challenge runs a fight between two roster fighters and records it.
49// Anyone may call it; the fight is deterministic and its recap public.
50// Returns the fight id.
51func Challenge(cur realm, a, b string) int {
52 fa, ok := roster.ByName(a)
53 assert(ok, "unknown fighter: "+a)
54 fb, ok := roster.ByName(b)
55 assert(ok, "unknown fighter: "+b)
56 assert(a != b, "a fighter cannot fight itself")
57
58 recap := engine.Run(fa, fb)
59 f := &Fight{
60 ID: len(fights),
61 A: a,
62 B: b,
63 Winner: recap.Winner,
64 Rounds: recap.Rounds,
65 Recap: recap,
66 Challenger: cur.Previous().Address(),
67 }
68 fights = append(fights, f)
69 record(recap)
70 chain.Emit("Fight",
71 "id", strconv.Itoa(f.ID),
72 "a", a,
73 "b", b,
74 "winner", winnerName(f),
75 "rounds", strconv.Itoa(recap.Rounds),
76 )
77 return f.ID
78}
79
80func record(r engine.Recap) {
81 ea := entry(r.A)
82 eb := entry(r.B)
83 switch r.Winner {
84 case 0:
85 ea.Points += 3
86 ea.Wins++
87 eb.Losses++
88 case 1:
89 eb.Points += 3
90 eb.Wins++
91 ea.Losses++
92 default:
93 ea.Points++
94 ea.Draws++
95 eb.Points++
96 eb.Draws++
97 }
98}
99
100func entry(name string) *Entry {
101 e, ok := ladder[name]
102 if !ok {
103 e = &Entry{Name: name}
104 ladder[name] = e
105 ladderOrder = append(ladderOrder, name)
106 }
107 return e
108}
109
110func winnerName(f *Fight) string {
111 switch f.Winner {
112 case 0:
113 return f.A
114 case 1:
115 return f.B
116 default:
117 return "draw"
118 }
119}
120
121// ---- read-only API ----
122
123// FightCount returns how many fights have been recorded.
124func FightCount() int { return len(fights) }
125
126// GetFight returns a copy of a recorded fight. Panics if unknown.
127func GetFight(id int) Fight {
128 assert(id >= 0 && id < len(fights), "unknown fight id: "+strconv.Itoa(id))
129 return *fights[id]
130}
131
132// Leaderboard returns ladder entries sorted by points, then name.
133func Leaderboard() []Entry {
134 out := make([]Entry, 0, len(ladderOrder))
135 for _, n := range ladderOrder {
136 out = append(out, *ladder[n])
137 }
138 for i := 1; i < len(out); i++ {
139 for j := i; j > 0 && less(out[j], out[j-1]); j-- {
140 out[j], out[j-1] = out[j-1], out[j]
141 }
142 }
143 return out
144}
145
146func less(a, b Entry) bool {
147 if a.Points != b.Points {
148 return a.Points > b.Points
149 }
150 return a.Name < b.Name
151}
152
153// RosterNames lists the fighters available to challenge with.
154func RosterNames() []string { return roster.Names() }
155
156// ---- recap JSON for the viewer ----
157
158// FightRecapJSON renders a fight recap as compact JSON. Frames are flat
159// arrays: [round, ax, ay, bx, by, ahp, bhp, aact, adir, bact, bdir, admg, bdmg].
160func FightRecapJSON(id int) string {
161 f := GetFight(id)
162 var sb strings.Builder
163 sb.WriteString(`{"id":` + strconv.Itoa(f.ID))
164 sb.WriteString(`,"a":` + strconv.Quote(f.A))
165 sb.WriteString(`,"b":` + strconv.Quote(f.B))
166 sb.WriteString(`,"winner":` + strconv.Itoa(f.Winner))
167 sb.WriteString(`,"rounds":` + strconv.Itoa(f.Rounds))
168 sb.WriteString(`,"grid":` + strconv.Itoa(engine.GridSize))
169 sb.WriteString(`,"maxhp":` + strconv.Itoa(engine.MaxHP))
170 sb.WriteString(`,"frames":[`)
171 for i, fr := range f.Recap.Frames {
172 if i > 0 {
173 sb.WriteByte(',')
174 }
175 sb.WriteString("[" + strconv.Itoa(fr.Round))
176 sb.WriteString("," + strconv.Itoa(fr.APos.X) + "," + strconv.Itoa(fr.APos.Y))
177 sb.WriteString("," + strconv.Itoa(fr.BPos.X) + "," + strconv.Itoa(fr.BPos.Y))
178 sb.WriteString("," + strconv.Itoa(fr.AHP) + "," + strconv.Itoa(fr.BHP))
179 sb.WriteString("," + strconv.Itoa(int(fr.AAct.Kind)) + "," + strconv.Itoa(int(fr.AAct.Dir)))
180 sb.WriteString("," + strconv.Itoa(int(fr.BAct.Kind)) + "," + strconv.Itoa(int(fr.BAct.Dir)))
181 sb.WriteString("," + strconv.Itoa(fr.ADmg) + "," + strconv.Itoa(fr.BDmg))
182 sb.WriteString("]")
183 }
184 sb.WriteString("]}")
185 return sb.String()
186}