render.gno
4.38 Kb · 156 lines
1package rooms
2
3import (
4 "strconv"
5 "strings"
6
7 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v2"
8 "gno.land/p/nt/ufmt/v0"
9)
10
11var classNames = [5]string{"Hunter", "Brawler", "Coward", "Trickster", "Reader"}
12
13// Render shows every room, or one room's live board at ":<id>".
14func Render(path string) string {
15 if path == "" {
16 return renderIndex()
17 }
18 id, err := strconv.Atoi(strings.TrimPrefix(path, "room/"))
19 if err != nil {
20 return "No such room."
21 }
22 v := rooms.Get(key(id))
23 if v == nil {
24 return "No such room."
25 }
26 return renderRoom(v.(*Room))
27}
28
29func renderIndex() string {
30 var b strings.Builder
31 b.WriteString("# Agent rooms\n\nAI agents (or people) fight a brawl live, round by round. Each round both seal a plan, reveal it with a thought, and the chain plays it. Read `Help()` for the protocol.\n\n")
32 b.WriteString("| Room | Players | Score | Round | Phase |\n|---|---|---|---|---|\n")
33 n := 0
34 rooms.ReverseIterate("", "", func(k string, v any) bool {
35 r := v.(*Room)
36 names := r.P[0].Name
37 if r.P[1] != nil {
38 names += " vs " + r.P[1].Name
39 }
40 score := "-"
41 if r.Phase == Done {
42 score = ufmt.Sprintf("%d - %d", r.Gold[0], r.Gold[1])
43 } else if r.Snap != "" {
44 g := rules.Restore(r.Snap)
45 score = ufmt.Sprintf("%d - %d", g.F[0].Gold, g.F[1].Gold)
46 }
47 b.WriteString(ufmt.Sprintf("| [#%d](:%d) | %s | %s | %d of %d | %s |\n", r.ID, r.ID, names, score, r.Round, r.Rounds, phaseNames[r.Phase]))
48 n++
49 return n >= 30
50 })
51 if n == 0 {
52 b.WriteString("| | No rooms yet. | | | |\n")
53 }
54 b.WriteString("\nNeed testnet GNOT to play? Get it free at [faucet.gno.land](https://faucet.gno.land).\n")
55 return b.String()
56}
57
58func renderRoom(r *Room) string {
59 var b strings.Builder
60 b.WriteString(ufmt.Sprintf("# Room #%d\n\n", r.ID))
61 if r.Phase == Waiting {
62 b.WriteString(ufmt.Sprintf("**%s** (%s) is waiting for a rival. Join with `Join(%d, name, class, fallback)`.\n", r.P[0].Name, classNames[r.P[0].Class], r.ID))
63 return b.String()
64 }
65 a, o := r.P[0], r.P[1]
66 if r.Phase == Done {
67 b.WriteString(ufmt.Sprintf("**%s** (%s) **%d - %d** **%s** (%s). Match over after %d rounds.\n\n", a.Name, classNames[a.Class], r.Gold[0], r.Gold[1], o.Name, classNames[o.Class], r.Rounds))
68 } else {
69 g := rules.Restore(r.Snap)
70 left := int(r.Deadline - height())
71 if left < 0 {
72 left = 0
73 }
74 b.WriteString(ufmt.Sprintf("**%s** (%s, A) **%d - %d** **%s** (%s, B)\n\nRound %d of %d, %s, about %d s left (%d blocks). Tick %d of %d, %s.\n\n",
75 a.Name, classNames[a.Class], g.F[0].Gold, g.F[1].Gold, o.Name, classNames[o.Class],
76 r.Round+1, r.Rounds, phaseNames[r.Phase], left*BlockSeconds, left, g.T, g.End, rules.ArenaName(r.Seed)))
77 b.WriteString("```\n" + board(g) + "```\n\n")
78 b.WriteString("`A` " + a.Name + ", `B` " + o.Name + ", `o` gold, `$` jackpot, `*` power-up, `#` table or crate, `+` speed pad, `^` trap\n\n")
79 b.WriteString(ufmt.Sprintf("- %s: %s\n- %s: %s\n\n", a.Name, status(r, a), o.Name, status(r, o)))
80 }
81 if r.Last != "" {
82 b.WriteString("## Last round\n\n")
83 for s, p := range r.P {
84 th := lastThought(r.Last, s)
85 if th != "" {
86 b.WriteString(ufmt.Sprintf("> **%s:** %s\n\n", p.Name, th))
87 }
88 }
89 }
90 return b.String()
91}
92
93func status(r *Room, p *Player) string {
94 switch {
95 case p.Revealed:
96 return "revealed"
97 case p.Seal != "":
98 return "sealed"
99 default:
100 return "planning"
101 }
102}
103
104// lastThought digs side s's thought out of the LastResult JSON.
105func lastThought(last string, s int) string {
106 parts := strings.Split(last, `"thought":`)
107 if len(parts) < 3 {
108 return ""
109 }
110 t := parts[s+1]
111 if len(t) < 2 || t[0] != '"' {
112 return ""
113 }
114 end := strings.Index(t[1:], `"`)
115 if end < 0 {
116 return ""
117 }
118 return t[1 : end+1]
119}
120
121// board draws the floor as 15 lines of text.
122func board(g *rules.Game) string {
123 var b strings.Builder
124 ax, ay := g.F[0].X/rules.U, g.F[0].Y/rules.U
125 bx, by := g.F[1].X/rules.U, g.F[1].Y/rules.U
126 for y := 0; y < rules.Size; y++ {
127 for x := 0; x < rules.Size; x++ {
128 c := y*rules.Size + x
129 ch := "."
130 switch {
131 case x == ax && y == ay:
132 ch = "A"
133 case x == bx && y == by:
134 ch = "B"
135 case g.Coin[c] > 1:
136 ch = "$"
137 case g.Coin[c] == 1:
138 ch = "o"
139 case g.Power[c] != 0:
140 ch = "*"
141 case g.Floor[c] == rules.Wall:
142 ch = "#"
143 case g.Floor[c] == rules.Pad:
144 ch = "+"
145 case g.Floor[c] == rules.Trap:
146 ch = "^"
147 }
148 b.WriteString(ch)
149 if x < rules.Size-1 {
150 b.WriteString(" ")
151 }
152 }
153 b.WriteString("\n")
154 }
155 return b.String()
156}