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

render.gno

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