package rooms import ( "strconv" "strings" rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3" "gno.land/p/nt/ufmt/v0" ) var classNames = [5]string{"Hunter", "Brawler", "Coward", "Trickster", "Reader"} // Render shows every room, or one room's live board at ":". func Render(path string) string { if path == "" { return renderIndex() } id, err := strconv.Atoi(strings.TrimPrefix(path, "room/")) if err != nil { return "No such room." } v := rooms.Get(key(id)) if v == nil { return "No such room." } return renderRoom(v.(*Room)) } func renderIndex() string { var b strings.Builder 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") b.WriteString("| Room | Players | Score | Round | Phase |\n|---|---|---|---|---|\n") n := 0 rooms.ReverseIterate("", "", func(k string, v any) bool { r := v.(*Room) names := r.P[0].Name if r.P[1] != nil { names += " vs " + r.P[1].Name } score := "-" if r.Phase == Done { score = ufmt.Sprintf("%d - %d", r.Gold[0], r.Gold[1]) } else if r.Snap != "" { g := rules.Restore(r.Snap) score = ufmt.Sprintf("%d - %d", g.F[0].Gold, g.F[1].Gold) } 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])) n++ return n >= 30 }) if n == 0 { b.WriteString("| | No rooms yet. | | | |\n") } b.WriteString("\nNeed testnet GNOT to play? Get it free at [faucet.gno.land](https://faucet.gno.land).\n") return b.String() } func renderRoom(r *Room) string { var b strings.Builder b.WriteString(ufmt.Sprintf("# Room #%d\n\n", r.ID)) if r.Phase == Waiting { 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)) return b.String() } a, o := r.P[0], r.P[1] if r.Phase == Done { 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)) } else { g := rules.Restore(r.Snap) left := int(r.Deadline - height()) if left < 0 { left = 0 } 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", a.Name, classNames[a.Class], g.F[0].Gold, g.F[1].Gold, o.Name, classNames[o.Class], r.Round+1, r.Rounds, phaseNames[r.Phase], left*BlockSeconds, left, g.T, g.End, rules.ArenaName(r.Seed))) b.WriteString("```\n" + board(g) + "```\n\n") b.WriteString("`A` " + a.Name + ", `B` " + o.Name + ", `o` gold, `$` jackpot, `*` power-up, `#` table or crate, `+` speed pad, `^` trap\n\n") b.WriteString(ufmt.Sprintf("- %s: %s\n- %s: %s\n\n", a.Name, status(r, a), o.Name, status(r, o))) } if r.Last != "" { b.WriteString("## Last round\n\n") for s, p := range r.P { th := lastThought(r.Last, s) if th != "" { b.WriteString(ufmt.Sprintf("> **%s:** %s\n\n", p.Name, th)) } } } return b.String() } func status(r *Room, p *Player) string { switch { case p.Revealed: return "revealed" case p.Seal != "": return "sealed" default: return "planning" } } // lastThought digs side s's thought out of the LastResult JSON. func lastThought(last string, s int) string { parts := strings.Split(last, `"thought":`) if len(parts) < 3 { return "" } t := parts[s+1] if len(t) < 2 || t[0] != '"' { return "" } end := strings.Index(t[1:], `"`) if end < 0 { return "" } return t[1 : end+1] } // board draws the floor as 15 lines of text. func board(g *rules.Game) string { var b strings.Builder ax, ay := g.F[0].X/rules.U, g.F[0].Y/rules.U bx, by := g.F[1].X/rules.U, g.F[1].Y/rules.U for y := 0; y < rules.Size; y++ { for x := 0; x < rules.Size; x++ { c := y*rules.Size + x ch := "." switch { case x == ax && y == ay: ch = "A" case x == bx && y == by: ch = "B" case g.Coin[c] > 1: ch = "$" case g.Coin[c] == 1: ch = "o" case g.Power[c] != 0: ch = "*" case g.Floor[c] == rules.Wall: ch = "#" case g.Floor[c] == rules.Pad: ch = "+" case g.Floor[c] == rules.Trap: ch = "^" } b.WriteString(ch) if x < rules.Size-1 { b.WriteString(" ") } } b.WriteString("\n") } return b.String() }