Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

render.gno

3.88 Kb · 105 lines
  1package game
  2
  3import "strings"
  4
  5// Render returns the state of the game as markdown, for gnoweb:
  6//
  7//	""               overview
  8//	"leaderboard"    the top players by kills
  9//	"params"         the game parameters and their bounds
 10//	"player/<addr>"  one player
 11//
 12// Everything printed here was validated against a narrow charset when it was
 13// written (see validate.gno), and the only caller-supplied value, the address
 14// of a player page, is printed only if it is a valid address. Nothing needs
 15// markdown escaping.
 16func Render(path string) string {
 17	switch {
 18	case path == "":
 19		return renderHome()
 20	case path == "leaderboard":
 21		return renderLeaderboard()
 22	case path == "params":
 23		return renderParams()
 24	case strings.HasPrefix(path, "player/"):
 25		return renderPlayer(address(strings.TrimPrefix(path, "player/")))
 26	}
 27	return "# gnofly\n\nPage not found. Go back to the [overview](" + link("") + ").\n"
 28}
 29
 30// link returns the gnoweb URL of a Render path of this realm.
 31func link(path string) string {
 32	if path == "" {
 33		return webPath
 34	}
 35	return webPath + ":" + path
 36}
 37
 38func renderHome() string {
 39	var b strings.Builder
 40	b.WriteString("# gnofly\n\n")
 41	b.WriteString("A multiplayer plane game. Fly through rings, pop balloons, shoot down the other pilots.\n")
 42	b.WriteString("This realm holds the rules of the game: parameters, roles and the kills that count.\n\n")
 43
 44	b.WriteString("- [Leaderboard](" + link("leaderboard") + "): " + itoa(int64(len(killers.addrs))) + " ranked pilots\n")
 45	b.WriteString("- [Parameters](" + link("params") + "): " + itoa(int64(len(params))) + "\n\n")
 46
 47	b.WriteString("| | |\n|---|---|\n")
 48	b.WriteString("| Version | " + itoa(version) + " |\n")
 49	b.WriteString("| Owner | " + owner.Owner().String() + " |\n")
 50	b.WriteString("| Treasury | " + treasury.String() + " |\n")
 51	b.WriteString("| Admins | " + itoa(int64(admins.Size())) + " besides the owner |\n")
 52	b.WriteString("| Referees | " + itoa(int64(referees.Size())) + " |\n")
 53	b.WriteString("| Pilots on record | " + itoa(int64(pilots.Size())) + " |\n")
 54	return b.String()
 55}
 56
 57func renderLeaderboard() string {
 58	var b strings.Builder
 59	b.WriteString("# gnofly leaderboard\n\n")
 60	b.WriteString("## Most kills\n\n")
 61	b.WriteString("Kills recorded on-chain by the game servers. The table keeps its top " +
 62		itoa(int64(leaderboardSize())) + " pilots.\n\n")
 63	if len(killers.addrs) == 0 {
 64		b.WriteString("No kill was recorded yet.\n")
 65	} else {
 66		b.WriteString("| Rank | Pilot | Kills |\n|---|---|---|\n")
 67		for i, a := range killers.addrs {
 68			b.WriteString("| " + itoa(int64(i+1)) + " | " + pilotLink(a) + " | " + itoa(killers.scores[i]) + " |\n")
 69		}
 70	}
 71	b.WriteString("\n[Back to the overview](" + link("") + ")\n")
 72	return b.String()
 73}
 74
 75// pilotLink returns a markdown link to the page of a player.
 76func pilotLink(addr address) string {
 77	a := addr.String()
 78	return "[" + a + "](" + link("player/"+a) + ")"
 79}
 80
 81func renderParams() string {
 82	var b strings.Builder
 83	b.WriteString("# gnofly parameters\n\n")
 84	b.WriteString("Admins change a value with `SetParam`, within its bounds.\n\n")
 85	b.WriteString("| Key | Value | Min | Max | Meaning |\n|---|---|---|---|---|\n")
 86	for _, p := range params {
 87		b.WriteString("| " + p.key + " | " + itoa(p.value) + " | " + itoa(p.min) + " | " + itoa(p.max) + " | " + p.doc + " |\n")
 88	}
 89	b.WriteString("\n[Back to the overview](" + link("") + ")\n")
 90	return b.String()
 91}
 92
 93func renderPlayer(addr address) string {
 94	// The address comes from the URL: never print it unless it is one.
 95	if !validAddress(addr) {
 96		return "# gnofly pilot\n\nThis is not a valid address. Go back to the [overview](" + link("") + ").\n"
 97	}
 98	var b strings.Builder
 99	b.WriteString("# gnofly pilot\n\n")
100	b.WriteString("| | |\n|---|---|\n")
101	b.WriteString("| Address | " + addr.String() + " |\n")
102	b.WriteString("| Kills | " + itoa(recordedKills(addr)) + " |\n")
103	b.WriteString("\n[Leaderboard](" + link("leaderboard") + ") | [Overview](" + link("") + ")\n")
104	return b.String()
105}