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.

api.gno

4.06 Kb · 120 lines
  1package gauntlet
  2
  3import (
  4	"strconv"
  5	"strings"
  6	"time"
  7
  8	"gno.land/p/g188c5k6v0tmfyevfzj962jkajjqt4ddtml8zw0x/gauntlet"
  9)
 10
 11// The functions below return JSON for the web app (vm/qeval). They only read.
 12
 13// StatsJSON returns board totals.
 14func StatsJSON() string {
 15	open, escrow := 0, int64(0)
 16	board.Iterate(func(c *gauntlet.Challenge) bool {
 17		if c.Status == gauntlet.Open {
 18			open++
 19			escrow += c.Pot()
 20		}
 21		return false
 22	})
 23	return `{"challenges":` + strconv.Itoa(board.Size()) + `,"open":` + strconv.Itoa(open) +
 24		`,"escrow":` + i64toa(escrow) + `,"paid":` + i64toa(totalPaid) + `,"players":` + strconv.Itoa(profiles.Size()) +
 25		`,"judges":` + strconv.Itoa(judges.Size()) + `,"now":` + i64toa(time.Now().Unix()) + `}`
 26}
 27
 28// ListJSON returns up to limit challenges, newest first, skipping offset.
 29func ListJSON(offset, limit int) string {
 30	if limit <= 0 || limit > 50 {
 31		limit = 50
 32	}
 33	var parts []string
 34	i := 0
 35	board.Iterate(func(c *gauntlet.Challenge) bool {
 36		if i >= offset {
 37			parts = append(parts, summaryJSON(c))
 38		}
 39		i++
 40		return len(parts) >= limit
 41	})
 42	return "[" + strings.Join(parts, ",") + "]"
 43}
 44
 45func summaryJSON(c *gauntlet.Challenge) string {
 46	return `{"id":` + itoa(c.ID) + `,"title":` + jstr(c.Title) + `,"status":` + jstr(c.Status.String()) +
 47		`,"pot":` + i64toa(c.Pot()) + `,"bond":` + i64toa(c.Bond) + `,"deadline":` + i64toa(c.Deadline.Unix()) +
 48		`,"attempts":` + strconv.Itoa(len(c.Attempts)) + `,"judged":` + strconv.FormatBool(c.Judge != nil) +
 49		`,"cases":` + strconv.Itoa(len(c.Cases)) + `}`
 50}
 51
 52// ChallengeJSON returns one challenge in full, or "null".
 53func ChallengeJSON(id uint64) string {
 54	c := board.Get(id)
 55	if c == nil {
 56		return "null"
 57	}
 58	var b strings.Builder
 59	b.WriteString(`{"id":` + itoa(c.ID) + `,"title":` + jstr(c.Title) + `,"spec":` + jstr(c.Spec) +
 60		`,"status":` + jstr(c.Status.String()) + `,"poster":` + jstr(c.Poster.String()) +
 61		`,"reward":` + i64toa(c.Reward) + `,"pot":` + i64toa(c.Pot()) + `,"bond":` + i64toa(c.Bond) +
 62		`,"boosts":` + i64toa(c.Boosts) + `,"forfeited":` + i64toa(c.Forfeited) +
 63		`,"created":` + i64toa(c.Created.Unix()) + `,"deadline":` + i64toa(c.Deadline.Unix()) +
 64		`,"window":` + i64toa(int64(c.Window/time.Second)) + `,"judge":` + jstr(c.JudgePath) + `,"cases":[`)
 65	for i, tc := range c.Cases {
 66		if i > 0 {
 67			b.WriteString(",")
 68		}
 69		b.WriteString(`{"in":` + jstr(tc.In) + `,"want":` + jstr(tc.Want) + `}`)
 70	}
 71	b.WriteString(`],"attempts":[`)
 72	for i, a := range c.Attempts {
 73		if i > 0 {
 74			b.WriteString(",")
 75		}
 76		b.WriteString(`{"seq":` + itoa(a.Seq) + `,"solver":` + jstr(a.Solver.String()) + `,"hash":` + jstr(a.Hash) +
 77			`,"committed":` + i64toa(a.CommittedAt.Unix()) + `,"state":` + jstr(a.State.String()) +
 78			`,"pkgpath":` + jstr(a.PkgPath) + `,"passed":` + strconv.Itoa(a.Report.Passed) +
 79			`,"total":` + strconv.Itoa(a.Report.Total) + `,"failIn":` + jstr(a.Report.FailIn) +
 80			`,"failGot":` + jstr(a.Report.FailGot) + `,"failWant":` + jstr(a.Report.FailWant) +
 81			`,"winner":` + strconv.FormatBool(c.Winner == a) + `}`)
 82	}
 83	b.WriteString(`]}`)
 84	return b.String()
 85}
 86
 87// ProfileJSON returns an address's stats, or "null".
 88func ProfileJSON(addr address) string {
 89	v := profiles.Get(addr.String())
 90	if v == nil {
 91		return "null"
 92	}
 93	return profileJSON(v.(*Profile))
 94}
 95
 96func profileJSON(p *Profile) string {
 97	return `{"addr":` + jstr(p.Addr.String()) + `,"attempts":` + strconv.Itoa(p.Attempts) + `,"solved":` +
 98		strconv.Itoa(p.Solved) + `,"earned":` + i64toa(p.Earned) + `,"posted":` + strconv.Itoa(p.Posted) +
 99		`,"funded":` + i64toa(p.Funded) + `}`
100}
101
102// LeaderboardJSON returns the top solvers by earnings.
103func LeaderboardJSON() string {
104	var parts []string
105	for _, p := range topSolvers(25) {
106		parts = append(parts, profileJSON(p))
107	}
108	return "[" + strings.Join(parts, ",") + "]"
109}
110
111// JudgesJSON lists registered judges.
112func JudgesJSON() string {
113	var parts []string
114	judges.Iterate("", "", func(_ string, v any) bool {
115		j := v.(*JudgeInfo)
116		parts = append(parts, `{"path":`+jstr(j.Path)+`,"name":`+jstr(j.Name)+`,"about":`+jstr(j.About)+`,"uses":`+strconv.Itoa(j.Uses)+`}`)
117		return false
118	})
119	return "[" + strings.Join(parts, ",") + "]"
120}