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_more.gno

4.84 Kb · 142 lines
  1package arena
  2
  3import (
  4	"strconv"
  5	"strings"
  6
  7	"gno.land/p/nt/ufmt/v0"
  8)
  9
 10// blockSeconds is Onyx's approximate block time, for "about N days" hints.
 11const blockSeconds = 3.6
 12
 13// seasonLine describes the current season's timing.
 14func seasonLine() string {
 15	if seasonStart == 0 {
 16		return ufmt.Sprintf("Season %d starts with the first match. [Past seasons](%s)", season, link("seasons"))
 17	}
 18	end := seasonStart + SeasonLength
 19	left := end - height()
 20	if left < 0 {
 21		left = 0
 22	}
 23	days := int(float64(left) * blockSeconds / 86400)
 24	return ufmt.Sprintf("Ends at block %d (about %d days). Ratings reset each season; the top 10 is kept. [Past seasons](%s)", end, days, link("seasons"))
 25}
 26
 27// activeLeaderboard is the leaderboard without retired bots.
 28func activeLeaderboard() []*Bot {
 29	var out []*Bot
 30	for _, b := range leaderboard() {
 31		if !b.Retired {
 32			out = append(out, b)
 33		}
 34	}
 35	return out
 36}
 37
 38func writeTournamentList(sb *strings.Builder, limit int) {
 39	if lastTID == 0 {
 40		sb.WriteString("No tournaments yet.\n\n")
 41		return
 42	}
 43	n := 0
 44	tournaments.ReverseIterate("", "", func(_ string, v any) bool {
 45		t := v.(*Tournament)
 46		extra := ""
 47		if t.Status == Done && t.Winner != "" {
 48			extra = ", won by " + t.Winner
 49		}
 50		sb.WriteString(ufmt.Sprintf("- [#%d %s](%s): %d/%d bots, %s%s\n",
 51			t.ID, t.Name, link("tournament/"+strconv.Itoa(t.ID)), len(t.Entrants), t.Capacity, t.Status.String(), extra))
 52		n++
 53		return limit > 0 && n >= limit
 54	})
 55	sb.WriteString("\n")
 56}
 57
 58func renderTournaments() string {
 59	var sb strings.Builder
 60	sb.WriteString("# Tournaments\n\n")
 61	sb.WriteString("Round-robins of 2 to 8 bots: everyone plays everyone once. 3 points for a win, 1 for a draw; ties go to gold difference.\n\n")
 62	sb.WriteString(ufmt.Sprintf("[Open a tournament](%s)\n\n", tx.Call("CreateTournament", "name", "", "capacity", "8")))
 63	writeTournamentList(&sb, 0)
 64	sb.WriteString("[Back to the arena](" + link("") + ")\n")
 65	return sb.String()
 66}
 67
 68func renderTournament(t *Tournament) string {
 69	var sb strings.Builder
 70	id := strconv.Itoa(t.ID)
 71	sb.WriteString(ufmt.Sprintf("# Tournament #%d: %s\n\n", t.ID, t.Name))
 72	sb.WriteString(ufmt.Sprintf("**%s** · %d/%d bots · created by `%s`\n\n", t.Status.String(), len(t.Entrants), t.Capacity, t.Creator.String()))
 73	if t.Status == Done && t.Winner != "" {
 74		sb.WriteString(ufmt.Sprintf("**Winner: [%s](%s)**\n\n", t.Winner, link("bot/"+t.Winner)))
 75	}
 76
 77	switch t.Status {
 78	case Open:
 79		sb.WriteString(ufmt.Sprintf("[Enter your bot](%s) · [Start (creator only)](%s)\n\n",
 80			tx.Call("JoinTournament", "id", id, "botName", ""), tx.Call("StartTournament", "id", id)))
 81	case Running:
 82		idx, inProgress := t.next()
 83		switch {
 84		case inProgress:
 85			mid := t.MatchIDs[idx]
 86			sb.WriteString(ufmt.Sprintf("Match [#%d](%s) is being played: finish it from its page.\n\n", mid, matchLink(mid)))
 87		case idx < len(t.Pairs):
 88			sb.WriteString(ufmt.Sprintf("Next: %s. [Play it](%s) · if a bot keeps that failing, [create it](%s) and use step mode from its page.\n\n",
 89				strings.Replace(t.Pairs[idx], "|", " vs ", 1), tx.Call("PlayTournament", "id", id), tx.Call("NextTournamentMatch", "id", id)))
 90		default:
 91			sb.WriteString(ufmt.Sprintf("All matches are played: [close the tournament](%s).\n\n", tx.Call("CloseTournament", "id", id)))
 92		}
 93	}
 94
 95	if len(t.Entrants) > 0 {
 96		sb.WriteString("## Table\n\n| # | Bot | P | W | D | L | Gold | Pts |\n|---|---|---|---|---|---|---|---|\n")
 97		for i, r := range t.Standings() {
 98			sb.WriteString(ufmt.Sprintf("| %d | [%s](%s) | %d | %d | %d | %d | %d:%d | **%d** |\n",
 99				i+1, r.Bot, link("bot/"+r.Bot), r.Played, r.Wins, r.Draws, r.Losses, r.GoldFor, r.GoldAgainst, r.Points))
100		}
101		sb.WriteString("\n")
102	}
103
104	if len(t.Pairs) > 0 {
105		sb.WriteString("## Schedule\n\n")
106		for i, pair := range t.Pairs {
107			vs := strings.Replace(pair, "|", " vs ", 1)
108			if mid := t.MatchIDs[i]; mid != 0 {
109				sb.WriteString(ufmt.Sprintf("%d. [%s](%s)\n", i+1, mustMatch(mid).Summary(), matchLink(mid)))
110			} else {
111				sb.WriteString(ufmt.Sprintf("%d. %s\n", i+1, vs))
112			}
113		}
114		sb.WriteString("\n")
115	}
116	sb.WriteString("[All tournaments](" + link("tournaments") + ") · [Back to the arena](" + link("") + ")\n")
117	return sb.String()
118}
119
120func renderSeasons() string {
121	var sb strings.Builder
122	sb.WriteString("# Seasons\n\n")
123	sb.WriteString(ufmt.Sprintf("Season %d is running. %s\n\n", season, seasonLine()))
124	if season == 1 {
125		sb.WriteString("No finished seasons yet.\n\n")
126	}
127	for n := season - 1; n >= 1; n-- {
128		sb.WriteString(ufmt.Sprintf("## Season %d\n\n", n))
129		top := PastSeason(n)
130		if len(top) == 0 {
131			sb.WriteString("Nobody played.\n\n")
132			continue
133		}
134		sb.WriteString("| # | Bot | Elo | W-L-D |\n|---|---|---|---|\n")
135		for i, s := range top {
136			sb.WriteString(ufmt.Sprintf("| %d | [%s](%s) | %d | %d-%d-%d |\n", i+1, s.Bot, link("bot/"+s.Bot), s.Rating, s.Wins, s.Losses, s.Draws))
137		}
138		sb.WriteString("\n")
139	}
140	sb.WriteString("[Back to the arena](" + link("") + ")\n")
141	return sb.String()
142}