render.gno
3.96 Kb · 151 lines
1package blueprint
2
3import (
4 "net/url"
5 "strconv"
6 "strings"
7)
8
9func Render(iUrl string) string {
10 assertMigrationStateAvailable()
11 u, err := url.Parse(iUrl)
12 if err != nil {
13 return "404\n"
14 }
15
16 query := u.Query()
17 switch u.Path {
18 case "":
19 return renderHome()
20 case "blueprint":
21 idStr := query.Get("id")
22 if idStr == "" {
23 return renderError("Blueprint ID required")
24 }
25 return renderBlueprint(idStr)
26 case "blueprints":
27 owner := query.Get("owner")
28 if owner != "" {
29 return renderBlueprintsByOwner(owner)
30 }
31 biome := query.Get("biome")
32 if biome != "" {
33 return renderBlueprintsByBiome(biome)
34 }
35 return renderBlueprintsList()
36 default:
37 return renderError("Not found")
38 }
39}
40
41func renderHome() string {
42 output := "# Blueprints\n\n"
43 output += "* **Creation Cost**: " + strconv.FormatInt(creationCost, 10) + " ugnot\n"
44 output += "* **List Limit**: " + strconv.Itoa(listLimit) + "\n"
45 output += "* **Batch Limit**: " + strconv.Itoa(batchLimit) + "\n"
46 output += "* **Total Blueprints**: " + strconv.Itoa(blueprintStore.Total()) + "\n\n"
47 output += "* [Browse All Blueprints](./blueprint:blueprints)\n"
48 return output
49}
50
51func renderBlueprint(idStr string) string {
52 idInt, err := strconv.Atoi(idStr)
53 if err != nil || idInt < 1 {
54 return renderError("Invalid blueprint ID")
55 }
56 blueprintID := uint32(idInt)
57 blueprint, found := blueprintStore.Get(blueprintID)
58 if !found {
59 return renderError("Blueprint not found")
60 }
61
62 output := "# " + escapeMarkdown(blueprint["name"]) + "\n\n"
63 output += "* **ID**: " + escapeMarkdown(blueprint["id"]) + "\n"
64 output += "* **Owner**: " + escapeMarkdown(blueprint["owner"]) + "\n"
65 output += "* **Biome**: " + escapeMarkdown(blueprint["biome"]) + "\n"
66 output += "* **Chunk Verifier Count**: " + strconv.Itoa(verifierStore.Size(blueprintID)) + "\n"
67 if description := blueprint["description"]; description != "" {
68 output += "\n" + escapeMarkdown(description) + "\n"
69 }
70 return output
71}
72
73func renderBlueprintsList() string {
74 totalSize := blueprintStore.Total()
75 output := "# All Blueprints\n\n"
76 if totalSize == 0 {
77 output += "*No blueprints created yet.*\n"
78 return output
79 }
80
81 ids := blueprintStore.ListIDs(1, 20)
82 for _, blueprint := range blueprintStore.ListByIDs(ids...) {
83 output += renderBlueprintItem(blueprint)
84 }
85 return output
86}
87
88func renderBlueprintsByOwner(owner string) string {
89 ownerAddr := address(owner)
90 if !ownerAddr.IsValid() {
91 return renderError("Invalid owner address")
92 }
93
94 output := "# Blueprints by " + escapeMarkdown(ownerAddr.String()) + "\n\n"
95 ids := blueprintStore.ListIDsByOwner(ownerAddr, 1, 20)
96 if len(ids) == 0 {
97 output += "*No blueprints found for this owner.*\n"
98 return output
99 }
100
101 for _, blueprintID := range ids {
102 if blueprint, found := blueprintStore.Get(blueprintID); found {
103 output += renderBlueprintItem(blueprint)
104 }
105 }
106 return output
107}
108
109func renderBlueprintsByBiome(biomeName string) string {
110 assertBiomeName(biomeName)
111 assertBiomeExists(biomeName)
112
113 output := "# Blueprints in " + escapeMarkdown(biomeName) + "\n\n"
114 ids := blueprintStore.ListIDsByBiome(biomeName, 1, 20)
115 if len(ids) == 0 {
116 output += "*No blueprints found for this biome.*\n"
117 return output
118 }
119
120 for _, blueprintID := range ids {
121 if blueprint, found := blueprintStore.Get(blueprintID); found {
122 output += renderBlueprintItem(blueprint)
123 }
124 }
125 return output
126}
127
128func renderBlueprintItem(blueprint map[string]string) string {
129 output := "## [" + escapeMarkdown(blueprint["name"]) + "](./blueprint:blueprint?id=" + url.QueryEscape(blueprint["id"]) + ")\n\n"
130 output += "* **Owner**: " + escapeMarkdown(blueprint["owner"]) + "\n"
131 output += "* **Biome**: " + escapeMarkdown(blueprint["biome"]) + "\n\n"
132 return output
133}
134
135func renderError(message string) string {
136 return "# Error\n\n" + escapeMarkdown(message) + "\n"
137}
138
139func escapeMarkdown(input string) string {
140 replacer := strings.NewReplacer(
141 "\\", "\\\\",
142 "*", "\\*",
143 "_", "\\_",
144 "[", "\\[",
145 "]", "\\]",
146 "(", "\\(",
147 ")", "\\)",
148 "#", "\\#",
149 )
150 return replacer.Replace(input)
151}