render.gno
7.35 Kb · 225 lines
1package hall
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/moul/txlink/v0"
8 "gno.land/p/nt/markdown/sanitize/v0"
9 "gno.land/p/nt/mux/v0"
10)
11
12// boardSize is how many rows the lobby leaderboard shows.
13const boardSize = 10
14
15var router = newRouter()
16
17func newRouter() *mux.Router {
18 r := mux.NewRouter()
19 r.HandleFunc("", renderLobby)
20 r.HandleFunc("c/{slug}", renderChallenge)
21 r.HandleFunc("p/{who}", renderPlayer)
22 r.HandleFunc("board", renderBoard)
23 r.HandleFunc("seasons", renderSeasons)
24 r.HandleFunc("community", renderCommunity)
25 r.HandleFunc("api/{what}", renderAPI)
26 r.SetNotFoundHandler(func(res *mux.ResponseWriter, _ *mux.Request) {
27 res.Write("# Nothing on this shelf\n\n[Back to the hall](" + selfPath + ")\n")
28 })
29 return r
30}
31
32// Render serves the hall's pages to gnoweb, and JSON under api/ for the
33// Cellar web client.
34func Render(path string) string { return router.Render(path) }
35
36func renderLobby(res *mux.ResponseWriter, _ *mux.Request) {
37 s := currentSeason()
38 res.Write("# The Cellar\n\n")
39 res.Write("*A capture-the-flag wargame for Gno, played on-chain. Every challenge is a live realm with a flaw in it. Break it, then claim it here.*\n\n")
40 res.Write("**Season " + strconv.Itoa(s.Number) + ": " + s.Name + "** · opened at block " +
41 strconv.FormatInt(s.OpenedAt, 10) + " · " + strconv.Itoa(s.Claims) + " claims\n\n")
42
43 res.Write("## The racks\n\n")
44 res.Write(challengeTable(func(e *entry) bool { return e.status == Listed }))
45
46 res.Write("\n## Standings this season\n\n")
47 res.Write(boardTable(standings(s.Number), boardSize))
48
49 res.Write("\n## Last pours\n\n")
50 if len(feed) == 0 {
51 res.Write("*Nobody has claimed a flag yet. The first one draws first blood.*\n")
52 }
53 for i := len(feed) - 1; i >= 0 && i >= len(feed)-10; i-- {
54 res.Write("- " + feedLine(feed[i]) + "\n")
55 }
56
57 res.Write("\n---\n\n" + link("All-time board", "board") + " · " + link("Seasons", "seasons") +
58 " · " + link("Community cellar", "community") + "\n")
59}
60
61func renderChallenge(res *mux.ResponseWriter, req *mux.Request) {
62 e, ok := getEntry(req.GetVar("slug"))
63 if !ok {
64 res.Write("# No such challenge\n\n" + link("Back to the hall", "") + "\n")
65 return
66 }
67 info := e.info
68 res.Write("# " + sanitize.InlineText(info.Title) + "\n\n")
69 res.Write("**" + info.Difficulty.String() + "** · " + sanitize.InlineText(info.Category) +
70 " · " + e.status.String() + " · worth **" + strconv.Itoa(e.value()) + "** points to each of its " +
71 strconv.Itoa(len(e.solvers)) + " solvers\n\n")
72 res.Write(info.Brief + "\n\n")
73
74 web := strings.TrimPrefix(e.path, "gno.land")
75 res.Write("**Realm:** [" + e.path + "](" + web + ") · [read the source](" + web + "$source)\n\n")
76 if e.status == Listed {
77 res.Write("When you have broken it, [claim the flag](" + txlink.Call("Claim", "slug", info.Slug) + ").\n\n")
78 }
79
80 res.Write("## Solvers\n\n")
81 if len(e.solvers) == 0 {
82 res.Write("*Untouched. First blood is worth +" + strconv.Itoa(info.Difficulty.FirstBloodBonus()) + ".*\n")
83 }
84 for i, addr := range e.solvers {
85 line := strconv.Itoa(i+1) + ". " + playerLink(addr)
86 if i == 0 {
87 line += " · first blood"
88 }
89 res.Write(line + "\n")
90 }
91}
92
93func renderPlayer(res *mux.ResponseWriter, req *mux.Request) {
94 addr, ok := resolvePlayer(req.GetVar("who"))
95 p, found := findProfile(addr)
96 if !ok || !found {
97 res.Write("# Unknown player\n\nNobody by that name has claimed a flag yet.\n\n" + link("Back to the hall", "") + "\n")
98 return
99 }
100 all, n, _ := p.score(0)
101 season, _, _ := p.score(currentSeason().Number)
102 res.Write("# " + DisplayName(addr) + "\n\n`" + addr.String() + "`\n\n")
103 res.Write("**" + strconv.Itoa(all) + "** points all time · **" + strconv.Itoa(season) +
104 "** this season · " + strconv.Itoa(n) + " flags\n\n")
105
106 res.Write("## Flags\n\n")
107 if len(p.solves) == 0 {
108 res.Write("*No flags yet.*\n")
109 }
110 for _, s := range p.solves {
111 e, _ := getEntry(s.Slug)
112 line := "- " + link(sanitize.InlineText(e.info.Title), "c/"+s.Slug) + " · block " +
113 strconv.FormatInt(s.Height, 10) + " · season " + strconv.Itoa(s.Season) + " · " +
114 strconv.Itoa(solvePoints(s)) + " pts"
115 if s.FirstBlood {
116 line += " · first blood"
117 }
118 res.Write(line + "\n")
119 }
120
121 res.Write("\n## Badges\n\n")
122 if len(p.badges) == 0 {
123 res.Write("*No badges yet.*\n")
124 }
125 for _, tid := range p.badges {
126 res.Write("- `" + tid + "`\n")
127 }
128}
129
130func renderBoard(res *mux.ResponseWriter, _ *mux.Request) {
131 res.Write("# All-time board\n\n")
132 rows := standings(0)
133 res.Write(boardTable(rows, len(rows)))
134 res.Write("\n" + link("Back to the hall", "") + "\n")
135}
136
137func renderSeasons(res *mux.ResponseWriter, _ *mux.Request) {
138 res.Write("# Seasons\n\n")
139 for i := len(seasons) - 1; i >= 0; i-- {
140 s := seasons[i]
141 res.Write("## Season " + strconv.Itoa(s.Number) + ": " + s.Name + "\n\n")
142 span := "opened at block " + strconv.FormatInt(s.OpenedAt, 10)
143 if s.ClosedAt != 0 {
144 span += ", closed at block " + strconv.FormatInt(s.ClosedAt, 10)
145 } else {
146 span += ", still open"
147 }
148 res.Write(span + " · " + strconv.Itoa(s.Claims) + " claims\n\n")
149 for rank, addr := range s.Podium {
150 res.Write(strconv.Itoa(rank+1) + ". " + playerLink(addr) + "\n")
151 }
152 if s.ClosedAt == 0 {
153 res.Write(boardTable(standings(s.Number), boardSize))
154 }
155 res.Write("\n")
156 }
157}
158
159func renderCommunity(res *mux.ResponseWriter, _ *mux.Request) {
160 res.Write("# Community cellar\n\n")
161 res.Write("Anyone can build a challenge: import `gno.land/p/" + strings.TrimPrefix(officialPrefix, "gno.land/r/") +
162 "flag`, keep a `flag.Ledger`, and call `hall.Enlist(cross(cur), challenge{})` from your realm's `init`. " +
163 "The curator reviews it, then it opens for claims.\n\n")
164 res.Write("## Listed\n\n")
165 res.Write(challengeTable(func(e *entry) bool { return !e.official && e.status == Listed }))
166 res.Write("\n## Awaiting review\n\n")
167 res.Write(challengeTable(func(e *entry) bool { return e.status == Pending }))
168}
169
170func challengeTable(keep func(*entry) bool) string {
171 var b strings.Builder
172 b.WriteString("| Challenge | Grade | Teaches | Solves | Worth |\n|---|---|---|---:|---:|\n")
173 rows := 0
174 eachEntry(func(slug string, e *entry) {
175 if !keep(e) {
176 return
177 }
178 rows++
179 b.WriteString("| " + link(sanitize.TableCell(e.info.Title), "c/"+slug) + " | " + e.info.Difficulty.String() +
180 " | " + sanitize.TableCell(e.info.Category) + " | " + strconv.Itoa(len(e.solvers)) +
181 " | " + strconv.Itoa(e.value()) + " |\n")
182 })
183 if rows == 0 {
184 return "*Empty rack.*\n"
185 }
186 return b.String()
187}
188
189func boardTable(rows []Standing, limit int) string {
190 if len(rows) == 0 {
191 return "*No points on the board yet.*\n"
192 }
193 var b strings.Builder
194 b.WriteString("| # | Player | Points | Flags |\n|---:|---|---:|---:|\n")
195 for i, r := range rows {
196 if i == limit {
197 break
198 }
199 b.WriteString("| " + strconv.Itoa(i+1) + " | " + playerLink(r.Player) + " | " +
200 strconv.Itoa(r.Points) + " | " + strconv.Itoa(r.Solves) + " |\n")
201 }
202 return b.String()
203}
204
205func feedLine(s Solve) string {
206 title := s.Slug
207 if e, ok := getEntry(s.Slug); ok {
208 title = sanitize.InlineText(e.info.Title)
209 }
210 line := "block " + strconv.FormatInt(s.Height, 10) + ": " + playerLink(s.Player) + " broke " + link(title, "c/"+s.Slug)
211 if s.FirstBlood {
212 line += " (first blood)"
213 }
214 return line
215}
216
217func playerLink(addr address) string { return link(DisplayName(addr), "p/"+addr.String()) }
218
219// link points at a page of this realm.
220func link(text, page string) string {
221 if page == "" {
222 return "[" + text + "](" + selfPath + ")"
223 }
224 return "[" + text + "](" + selfPath + ":" + page + ")"
225}