claim.gno
1.73 Kb · 80 lines
1package hall
2
3import (
4 "chain"
5 "chain/runtime"
6 "strconv"
7
8 "gno.land/p/g15c02s5hhdny9uvr9za3xsyn9sw8p0klptrh6kf/cellar/flag"
9)
10
11// Solve is one claimed capture.
12type Solve struct {
13 Player address
14 Slug string
15 Height int64
16 Season int
17 FirstBlood bool
18}
19
20// Claim records that the caller broke the challenge slug and returns the
21// points it is now worth. The challenge realm is asked whether the player
22// captured it; the hall never takes the player's word for it.
23//
24// Call it from your wallet, or from your solution realm right after the
25// exploit: a realm at gno.land/r/<your-address>/... is credited to you.
26func Claim(cur realm, slug string) int {
27 player := flag.MustPlayer(cur.Previous())
28 e := mustEntry(slug)
29 if e.status != Listed {
30 panic(ErrNotListed)
31 }
32 p := profileOf(player)
33 if p.solved(slug) {
34 panic(ErrAlreadyClaimed)
35 }
36 if !e.ch.Captured(player) {
37 panic(ErrNotCaptured)
38 }
39
40 s := Solve{
41 Player: player,
42 Slug: slug,
43 Height: runtime.ChainHeight(),
44 Season: currentSeason().Number,
45 FirstBlood: len(e.solvers) == 0,
46 }
47 e.solvers = append(e.solvers, player)
48 p.solves = append(p.solves, s)
49 currentSeason().Claims++
50 pushFeed(s)
51 awardSolve(p, s)
52
53 chain.Emit("Claimed",
54 "slug", slug,
55 "player", player.String(),
56 "firstBlood", strconv.FormatBool(s.FirstBlood),
57 "season", strconv.Itoa(s.Season),
58 )
59 return e.value()
60}
61
62func pushFeed(s Solve) {
63 feed = append(feed, s)
64 if len(feed) > feedSize {
65 feed = feed[len(feed)-feedSize:]
66 }
67}
68
69// solvePoints is what one solve is worth today, first blood included.
70func solvePoints(s Solve) int {
71 e, ok := getEntry(s.Slug)
72 if !ok {
73 return 0
74 }
75 pts := e.value()
76 if s.FirstBlood {
77 pts += e.info.Difficulty.FirstBloodBonus()
78 }
79 return pts
80}