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

2.97 Kb · 77 lines
 1package cagematch
 2
 3import (
 4	"strconv"
 5	"strings"
 6
 7	"gno.land/p/g1ejwp09wwnu6psem2sdwrh7l7xcldqm6zz3vu2v/cagematch/engine"
 8)
 9
10// Render is the gnoweb entry point. Routes: "" home, "f/<id>" a fight.
11func Render(path string) string {
12	if path == "" {
13		return renderHome()
14	}
15	if strings.HasPrefix(path, "f/") {
16		id, err := strconv.Atoi(strings.TrimPrefix(path, "f/"))
17		if err == nil && id >= 0 && id < len(fights) {
18			return renderFight(fights[id])
19		}
20	}
21	return "Unknown page. [Cage Match home](:)"
22}
23
24func renderHome() string {
25	var b strings.Builder
26	b.WriteString("# Cage Match\n\n")
27	b.WriteString("**AI fighters battle on-chain.** Every fighter is a small Gno package. ")
28	b.WriteString("Every fight is resolved by this realm, deterministically: the same two fighters always produce the same result, and anyone can replay a fight from its on-chain record.\n\n")
29	b.WriteString("Rules: 9x9 grid, 100 HP, at most 60 rounds. Each round a fighter moves, attacks (12 damage, adjacent tile), charges (next attack 24), or defends (damage halved). Points: win 3, draw 1, loss 0.\n\n")
30	b.WriteString("## Ladder\n\n")
31	if len(ladderOrder) == 0 {
32		b.WriteString("No fights yet. Start one from the Actions tab with `Challenge`.\n")
33	} else {
34		b.WriteString("| Fighter | Points | W | D | L |\n")
35		b.WriteString("| --- | --- | --- | --- | --- |\n")
36		for _, e := range Leaderboard() {
37			b.WriteString("| " + e.Name + " | " + strconv.Itoa(e.Points) + " | " + strconv.Itoa(e.Wins) + " | " + strconv.Itoa(e.Draws) + " | " + strconv.Itoa(e.Losses) + " |\n")
38		}
39	}
40	if len(fights) > 0 {
41		b.WriteString("\n## Recent fights\n\n")
42		start := 0
43		if len(fights) > 10 {
44			start = len(fights) - 10
45		}
46		for i := len(fights) - 1; i >= start; i-- {
47			f := fights[i]
48			b.WriteString("- [Fight " + strconv.Itoa(f.ID) + "](:f/" + strconv.Itoa(f.ID) + "): " + f.A + " vs " + f.B + " — **" + winnerName(f) + "** in " + strconv.Itoa(f.Rounds) + " rounds\n")
49		}
50	}
51	return b.String()
52}
53
54func renderFight(f *Fight) string {
55	var b strings.Builder
56	b.WriteString("# Fight " + strconv.Itoa(f.ID) + ": " + f.A + " vs " + f.B + "\n\n")
57	if f.Winner < 0 {
58		b.WriteString("**Draw** after " + strconv.Itoa(f.Rounds) + " rounds.\n\n")
59	} else {
60		b.WriteString("**" + winnerName(f) + " wins** in " + strconv.Itoa(f.Rounds) + " rounds.\n\n")
61	}
62	b.WriteString("Challenged by `" + f.Challenger.String() + "`.\n\n")
63	b.WriteString("| Round | A | B | A HP | B HP | A dmg | B dmg |\n")
64	b.WriteString("| --- | --- | --- | --- | --- | --- | --- |\n")
65	for _, fr := range f.Recap.Frames {
66		b.WriteString("| " + strconv.Itoa(fr.Round) +
67			" | " + pos(fr.APos) + " | " + pos(fr.BPos) +
68			" | " + strconv.Itoa(fr.AHP) + " | " + strconv.Itoa(fr.BHP) +
69			" | " + strconv.Itoa(fr.ADmg) + " | " + strconv.Itoa(fr.BDmg) + " |\n")
70	}
71	b.WriteString("\nThis table is the fight. Replay it with the same two fighters and you get exactly these rows.\n")
72	return b.String()
73}
74
75func pos(p engine.Pos) string {
76	return "(" + strconv.Itoa(p.X) + "," + strconv.Itoa(p.Y) + ")"
77}