badge.gno
1.81 Kb · 70 lines
1package hall
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/nt/grc721/v0"
8)
9
10// badgeArt is where the badge artwork lives. The artwork of a badge is
11// derived from its token id, so nothing extra is stored per token.
12const badgeArt = "https://akinbola247.github.io/cellar-ctf/badges/"
13
14var (
15 badges *grc721.Token
16 badgeMint *grc721.PrivateLedger
17)
18
19func initBadges(cur realm) {
20 badges, badgeMint = grc721.NewToken("Cellar Badges", "CELLAR", 0, cur)
21}
22
23// Badges returns a read-only view of the badge collection (GRC721).
24// Badges cannot be transferred: the hall exposes no transfer.
25//
26// Token ids:
27//
28// solve:<slug>:<address> broke a challenge
29// blood:<slug> broke it first (replaces the solve badge)
30// season:<n>:<rank> finished a season on the podium
31func Badges() grc721.Teller { return badges.ReadonlyTeller() }
32
33// BadgeURI returns the artwork URI of badge tid, or "" if it was never
34// minted.
35func BadgeURI(tid string) string {
36 if _, err := badges.OwnerOf(grc721.TokenID(tid)); err != nil {
37 return ""
38 }
39 parts := strings.Split(tid, ":")
40 switch parts[0] {
41 case "solve":
42 return badgeArt + parts[1] + ".svg"
43 case "blood":
44 return badgeArt + "blood-" + parts[1] + ".svg"
45 case "season":
46 return badgeArt + "season-" + parts[2] + ".svg"
47 }
48 return ""
49}
50
51// awardSolve mints the badge for a solve: first blood, or a plain solve.
52func awardSolve(p *profile, s Solve) {
53 if s.FirstBlood {
54 mint(p, "blood:"+s.Slug)
55 return
56 }
57 mint(p, "solve:"+s.Slug+":"+s.Player.String())
58}
59
60// awardSeason mints a season podium badge.
61func awardSeason(addr address, season, rank int) {
62 mint(profileOf(addr), "season:"+strconv.Itoa(season)+":"+strconv.Itoa(rank))
63}
64
65func mint(p *profile, tid string) {
66 if err := badgeMint.Mint(p.addr, grc721.TokenID(tid)); err != nil {
67 panic(err)
68 }
69 p.badges = append(p.badges, tid)
70}