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

5.18 Kb · 165 lines
  1package advent
  2
  3import (
  4	"strconv"
  5	"strings"
  6	"time"
  7
  8	"chain/runtime"
  9
 10	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/kit/ui/v0"
 11	"gno.land/p/g1magld5ptgzaktqcexxq0pgq5lmdf0kz6re8hlv/realmpath/v0"
 12)
 13
 14const app = "https://moul.github.io/gno-contracts/advent/"
 15
 16func webLinks() (string, string) {
 17	if runtime.ChainID() == "moulstaging-1" {
 18		return app + "?net=moul-dev", "https://gno-staging.moul.p2p.team/faucet"
 19	}
 20	return app, "https://onyx.testnets.gno.land/faucet"
 21}
 22
 23func Render(path string) string {
 24	req := realmpath.Parse(path)
 25	switch req.PathPart(0) {
 26	case "":
 27		return renderCalendar()
 28	case "scores":
 29		return calendarLink() + "\n\n" + renderScores()
 30	case "door":
 31		day, _ := strconv.Atoi(req.PathPart(1))
 32		return renderDoor(day)
 33	case "seal":
 34		day, _ := strconv.Atoi(req.PathPart(1))
 35		id, _ := strconv.Atoi(req.PathPart(2))
 36		return renderSeal(day, id)
 37	default:
 38		return "## Lost in the snow\n\n" + calendarLink()
 39	}
 40}
 41
 42func renderCalendar() string {
 43	play, faucet := webLinks()
 44	var out strings.Builder
 45	out.WriteString("# ✷ Gnoël\n\n")
 46	out.WriteString("**The North Pole, one realm over.**\n\n")
 47	out.WriteString("![A snowbound Gnome outpost](" + app + "outpost.webp)\n\n")
 48	out.WriteString("The Gno-mes hid 24 riddles in the VM. Open a door, follow the clues, and send a sealed guess. Every opened door stays playable.\n\n")
 49	out.WriteString("[Play Gnoël](" + play + ") · [Get test GNOT](" + faucet + ") · [🏆 Leaderboard](" + route("scores") + ")\n\n")
 50	out.WriteString("## Advent calendar\n\n")
 51	table := ui.NewTable("First door", "Second door", "Third door", "Fourth door", "Fifth door", "Sixth door")
 52	open, solves := 0, 0
 53	for week := 0; week < 4; week++ {
 54		row := make([]string, 0, 6)
 55		for j := 1; j <= 6; j++ {
 56			day := week*6 + j
 57			n := dayNumber(day)
 58			d := doors(day)
 59			if d == nil {
 60				row = append(row, "🔒 **"+n+"**")
 61				continue
 62			}
 63			open++
 64			solves += d.solves
 65			row = append(row, "**["+n+"]("+doorURL(day)+")** · "+solveCount(d.solves))
 66		}
 67		table.Row(row...)
 68	}
 69	out.WriteString("**" + strconv.Itoa(open) + " / 24 doors open** · **" + solveCount(solves) + "**\n\n")
 70	out.WriteString(table.String() + "\n")
 71	out.WriteString("A right guess earns one point after the oracle checks the seals. The public ledger shows each commitment and verdict.\n\n")
 72	out.WriteString(renderScores())
 73	return out.String()
 74}
 75
 76func renderDoor(day int) string {
 77	play, faucet := webLinks()
 78	d := doors(day)
 79	if d == nil {
 80		return "## 🔒 Still snowed in\n\nThis door opens when the organizer posts its riddle.\n\n" + calendarLink()
 81	}
 82	var out strings.Builder
 83	out.WriteString(calendarLink() + "\n\n")
 84	out.WriteString("# Door " + dayNumber(day) + " · " + ui.Inline(d.title) + "\n\n")
 85	for _, line := range strings.Split(d.clue, "\n") {
 86		out.WriteString("> " + ui.Inline(line) + "\n")
 87	}
 88	out.WriteString("\n**" + solveCount(d.solves) + "** · " + strconv.Itoa(d.submitted) + " sealed guesses · " + strconv.Itoa(d.submitted-d.reviewed) + " awaiting review\n\n")
 89	out.WriteString("First review from **" + time.Unix(d.reviewFrom, 0).UTC().Format("2 Jan 2006, 15:04 UTC") + "**. This door stays open for late arrivals.\n\n")
 90	out.WriteString("[Seal a guess](" + play + "#door-" + strconv.Itoa(day) + ") · [Get test GNOT](" + faucet + ")\n\n")
 91	out.WriteString("## Public seals\n\n")
 92	if d.submitted == 0 {
 93		return out.String() + "_The snow is still fresh here. Send the first guess._\n"
 94	}
 95	table := ui.NewTable("Seal", "Gnome", "Commitment", "Verdict")
 96	for id := d.submitted; id > 0 && id > d.submitted-8; id-- {
 97		a := d.attempt(id)
 98		n := strconv.Itoa(id)
 99		table.Row("[#"+n+"]("+route("seal/"+strconv.Itoa(day)+"/"+n)+")", ui.AddrOf(a.player), "`"+ui.ShortN(a.commitment, 10, 4)+"`", verdict(a.status))
100	}
101	out.WriteString(table.String())
102	return out.String()
103}
104
105func renderSeal(day, id int) string {
106	d := doors(day)
107	if d == nil || id < 1 || id > d.submitted {
108		return "## Seal not found\n\n" + calendarLink()
109	}
110	a := d.attempt(id)
111	return "# Seal #" + strconv.Itoa(id) + "\n\n[Back to door " + dayNumber(day) + "](" + doorURL(day) + ")\n\n" +
112		"**Gnome** " + a.player + "\n\n**Verdict** " + verdict(a.status) +
113		"\n\n**Public SHA-256 commitment**\n\n`" + a.commitment + "`\n"
114}
115
116func renderScores() string {
117	const heading = "## 🏆 The Nice List\n\n"
118	rows := leaders()
119	if len(rows) == 0 {
120		return heading + "_Nobody has thawed the scoreboard yet._\n"
121	}
122	table := ui.NewTable("Rank", "Gnome", "Solves")
123	for i, e := range rows {
124		table.Row(strings.TrimSpace(strconv.Itoa(i+1)+" "+ui.Podium(i)), "`"+e.Key+"`", strconv.FormatInt(e.Score, 10))
125	}
126	return heading + table.String()
127}
128
129func verdict(status int) string {
130	switch status {
131	case solved:
132		return "✅ Solved"
133	case missed:
134		return "↩ Try again"
135	default:
136		return "✉️ Sealed"
137	}
138}
139
140func dayNumber(day int) string {
141	n := strconv.Itoa(day)
142	if day < 10 {
143		return "0" + n
144	}
145	return n
146}
147
148func solveCount(n int) string {
149	if n == 1 {
150		return "1 solve"
151	}
152	return strconv.Itoa(n) + " solves"
153}
154
155func route(path string) string {
156	return strings.TrimPrefix(self, "gno.land") + ":" + path
157}
158
159func doorURL(day int) string {
160	return route("door/" + strconv.Itoa(day))
161}
162
163func calendarLink() string {
164	return "[Gnoël calendar](" + strings.TrimPrefix(self, "gno.land") + ")"
165}