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

2.53 Kb · 82 lines
 1package moves
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl"
 8)
 9
10func short(addr string) string {
11	if len(addr) < 12 {
12		return addr
13	}
14	return addr[:6] + "…" + addr[len(addr)-4:]
15}
16
17func secs(ticks int) string {
18	s := strconv.Itoa(ticks / rules.TPS)
19	if ticks%rules.TPS != 0 {
20		s += "." + strconv.Itoa(ticks%rules.TPS)
21	}
22	return s + "s"
23}
24
25func row(b *strings.Builder, id int) {
26	a, s := mustRecord(id)
27	r := rules.MustRecipe(s)
28	b.WriteString("| [#" + strconv.Itoa(id) + "](:move/" + strconv.Itoa(id) + ") | " + r.Name + " | " + secs(r.CD) +
29		" | " + r.Describe() + " | [" + short(a) + "](:by/" + a + ") |\n")
30}
31
32const header = "| # | Move | Recharge | What it does | Author |\n|---|---|---|---|---|\n"
33
34// Render shows the newest moves, one move at "move/<id>", or an author's
35// moves at "by/<address>".
36func Render(path string) string {
37	var b strings.Builder
38	switch {
39	case strings.HasPrefix(path, "move/"):
40		id, err := strconv.Atoi(path[5:])
41		if err != nil || record(id) == "" {
42			return "No such move."
43		}
44		a, s := mustRecord(id)
45		r := rules.MustRecipe(s)
46		b.WriteString("# " + r.Name + "\n\n")
47		b.WriteString("Move #" + strconv.Itoa(id) + " by [" + short(a) + "](:by/" + a + "). Recharges in " + secs(r.CD) +
48			" (its effects need at least " + secs(r.Cost()) + ").\n\n")
49		for _, e := range r.Fx {
50			b.WriteString("- " + e.Describe() + "\n")
51		}
52		b.WriteString("\nRecipe: `" + s + "`\n")
53		return b.String()
54	case strings.HasPrefix(path, "by/"):
55		addr := path[3:]
56		ids := authorIDs(addr)
57		b.WriteString("# Moves by " + short(addr) + "\n\n")
58		if len(ids) == 0 {
59			b.WriteString("None yet.\n")
60			return b.String()
61		}
62		b.WriteString(header)
63		for _, id := range ids {
64			row(&b, id)
65		}
66		return b.String()
67	}
68	b.WriteString("# Move recipes\n\n")
69	b.WriteString("Special moves anyone can design, written as data. Equip one in the brawler to use it in place of your class special; the chain runs it with the same rules as the browser.\n\n")
70	b.WriteString("A recipe is `name/cooldown/effects`, for example `Blink Thief/84/D82S11`: dash 4 squares to the nearest gold, then steal 1 gold in melee. ")
71	b.WriteString("Stronger effects need longer cooldowns. Check one with `Check(recipe)` and publish it with `Publish(recipe)`.\n\n")
72	if count == 0 {
73		b.WriteString("No moves published yet.\n")
74		return b.String()
75	}
76	b.WriteString("## Newest\n\n" + header)
77	for id := count; id >= 1 && id > count-20; id-- {
78		row(&b, id)
79	}
80	b.WriteString("\n" + strconv.Itoa(count) + " moves published.\n")
81	return b.String()
82}