Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

render.gno

1.94 Kb · 58 lines
 1package brawl
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3"
 8	"gno.land/p/nt/ufmt/v0"
 9)
10
11var classNames = [5]string{"Hunter", "Brawler", "Coward", "Trickster", "Reader"}
12
13func who(g *Ghost) string {
14	if g.Name != "" {
15		return g.Name
16	}
17	s := g.Player.String()
18	if len(s) < 12 {
19		return s
20	}
21	return s[:6] + "…" + s[len(s)-4:]
22}
23
24// Render shows the leaderboard, or one ghost at "ghost/<id>".
25func Render(path string) string {
26	if strings.HasPrefix(path, "ghost/") {
27		id, err := strconv.Atoi(path[6:])
28		if err != nil {
29			return "Bad ghost number."
30		}
31		g := findGhost(id)
32		if g == nil {
33			return "No such ghost: it may have dropped off the top 10."
34		}
35		return ufmt.Sprintf("# Ghost #%d\n\n**%s** (%s) scored **%d - %d** against the Hard bot in the %s, block %d. The chain replayed the run before accepting it. You can fight this run as a ghost in the game.\n",
36			g.ID, who(g), classNames[g.Class], g.Gold, g.BotGold, rules.ArenaName(g.Seed), int(g.Height))
37	}
38	var b strings.Builder
39	b.WriteString("# Code Arena brawl\n\n")
40	b.WriteString("Ranked runs are against the Hard bot. Every score on this board was replayed by the chain before it counted, and each one can be fought as a ghost.\n\n")
41	b.WriteString("## Top 10\n\n")
42	if board[0] == "" {
43		b.WriteString("Nobody yet. Play a ranked run against the Hard bot to get on the board.\n")
44	} else {
45		b.WriteString("| # | Player | Class | Gold | Hard bot | Arena | Ghost |\n|---|---|---|---|---|---|---|\n")
46		for i, enc := range board {
47			if enc == "" {
48				break
49			}
50			g := decode(enc)
51			b.WriteString(ufmt.Sprintf("| %d | %s | %s | %d | %d | %s | [#%d](:ghost/%d) |\n",
52				i+1, who(g), classNames[g.Class], g.Gold, g.BotGold, rules.ArenaName(g.Seed), g.ID, g.ID))
53		}
54	}
55	b.WriteString(ufmt.Sprintf("\n%d runs so far.\n", runs))
56	b.WriteString("\nNeed testnet GNOT to play? Get it free at [faucet.gno.land](https://faucet.gno.land).\n")
57	return b.String()
58}