puzzlerounds.gno
3.76 Kb · 107 lines
1// Package puzzlerounds is the reusable, stateless puzzle-round library
2// behind Riddle Tavern (gno.land/r/g1j7l6ekxy82r9tav7d8jv3gp0h3xaunw2m2k3lx/riddletavern).
3//
4// It holds the commitment scheme, the round record, streak math and
5// leaderboard ordering, so any other realm or package can import it and run
6// the same twice-daily puzzle game without copying the logic.
7//
8// Answer commitment scheme (public, part of the game rules):
9// - An answer is an integer. It is encoded as its decimal string
10// (strconv.Itoa) and committed with sha256.
11// - Only the hash is ever stored on-chain; the answer itself is never
12// stored. Anyone can recompute HashAnswer to verify a round.
13// - Note the honest limitation, shared with the BSC original this ports
14// (Puzzle Arena, which used keccak256 the same way): small-integer
15// answers are brute-forceable from the hash in principle. The hash is
16// a commitment that the round cannot be changed after creation, not
17// a vault. The game is the puzzle, the live window and one entry per
18// player — not the secrecy of the answer.
19package puzzlerounds
20
21import (
22 "crypto/sha256"
23 "strconv"
24)
25
26// AnswerHash is a sha256 commitment to a puzzle answer.
27type AnswerHash [32]byte
28
29// HashAnswer computes the commitment for an answer.
30//
31// The answer is encoded as its decimal string and hashed with sha256.
32// The encoding is part of the public commitment scheme: anyone can
33// recompute it to verify a round, and the answer itself is never stored.
34func HashAnswer(answer int) AnswerHash {
35 return sha256.Sum256([]byte(strconv.Itoa(answer)))
36}
37
38// VerifyAnswer reports whether answer matches the stored commitment.
39func VerifyAnswer(hash AnswerHash, answer int) bool {
40 return HashAnswer(answer) == hash
41}
42
43// Round is the public record of one puzzle round. It carries no behaviour
44// of its own; the realm that owns the round holds the state.
45type Round struct {
46 Question string // puzzle text, shown to players
47 AnswerHash AnswerHash // sha256(decimal answer); the answer is never stored
48 Start int64 // unix seconds, inclusive
49 End int64 // unix seconds, inclusive
50 Solves int // correct solves so far
51}
52
53// IsOpen reports whether unix time now falls inside the round window.
54func (r Round) IsOpen(now int64) bool {
55 return now >= r.Start && now <= r.End
56}
57
58// DayOf returns the UTC day number for a unix timestamp.
59func DayOf(unix int64) int64 {
60 return unix / 86400
61}
62
63// NextStreak computes the new streak after a solve on day today,
64// given the day of the previous solve (lastDay, 0 if never) and the
65// current streak. Ported from Puzzle Arena (BSC):
66// - a second solve on the same day leaves the streak unchanged
67// (two rounds run per day, so a same-day solve must not reset it),
68// - a solve the day after the last one extends the streak by one,
69// - anything else restarts the streak at 1.
70func NextStreak(lastDay int64, streak int, today int64) int {
71 if today > 0 && lastDay == today {
72 return streak
73 }
74 if today > 0 && lastDay == today-1 {
75 return streak + 1
76 }
77 return 1
78}
79
80// Leader is one row of the leaderboard.
81type Leader struct {
82 Solver address
83 Solves int
84 Streak int
85}
86
87// SortLeaders sorts in place: most solves first, then highest streak,
88// then lowest address (deterministic tie-break).
89func SortLeaders(leaders []Leader) {
90 // Insertion sort: leaderboards are small, and sort.Slice is not
91 // available in Gno.
92 for i := 1; i < len(leaders); i++ {
93 for j := i; j > 0 && lessLeader(leaders[j], leaders[j-1]); j-- {
94 leaders[j], leaders[j-1] = leaders[j-1], leaders[j]
95 }
96 }
97}
98
99func lessLeader(a, b Leader) bool {
100 if a.Solves != b.Solves {
101 return a.Solves > b.Solves
102 }
103 if a.Streak != b.Streak {
104 return a.Streak > b.Streak
105 }
106 return a.Solver.String() < b.Solver.String()
107}