arena.gno
4.54 Kb · 161 lines
1// Package arena holds Gnomepit's online matches. A challenger posts a sealed deck and
2// round-1 plan; an acceptor answers face up; the challenger reveals each round with the
3// seal of the next, and the shared duel engine checks and resolves every move. A session
4// key is bound to the match of the first call it signs: it then acts in no other match and
5// posts or accepts nothing more. Calls the wallet signs are never bound.
6package arena
7
8import (
9 "encoding/base64"
10 "strconv"
11 "strings"
12 "time"
13
14 "gno.land/p/nym-gfanton001/gnomepit/engine/v0"
15)
16
17const (
18 moveWindow = 24 * 60 * 60 // seconds a player has for each move
19 openWindow = 7 * 24 * 60 * 60 // seconds an open challenge stays on the board
20 maxBox = 104 // bytes
21 maxPage = 50
22 maxCleared = 8 // expired challenges one Challenge deletes at most
23 idDigits = 10
24)
25
26const (
27 phaseOpen = "open"
28 phaseReveal = "reveal"
29 phasePlay = "play"
30 phaseEnded = "ended"
31 phaseExpired = "expired" // reported for an open match past its expiry, never stored
32)
33
34var lastID int
35
36// OpenChallenge is an open challenge as the board lists it.
37type OpenChallenge struct {
38 ID, Challenger, Clan, Opponent string
39 Posted, Expires int64
40}
41
42// Challenge posts a challenge for clan with the seals of the challenger's deck and
43// round-1 plan, the box holding both, and an opponent who alone may accept, or none.
44// It returns the new match's id. It first deletes up to 8 expired challenges, whoever
45// posted them.
46func Challenge(cur realm, clan, deckSeal, planSeal, box string, opponent address) string {
47 mustBeOpen()
48 rules := engine.ProofOfConcept()
49 c := clanNumber(rules, clan)
50 if c == 0 {
51 panic("unknown clan")
52 }
53 if !validSeal(deckSeal) || !validSeal(planSeal) {
54 panic("invalid seal")
55 }
56 raw := boxBytes(box)
57 if opponent != "" && !canonical(opponent) {
58 panic("invalid opponent")
59 }
60 now := time.Now().Unix()
61 for _, id := range openClear(now, maxCleared) {
62 if m, ok := loadMatch(rules, id); ok {
63 forget(id, m.challenger)
64 }
65 }
66
67 lastID++
68 m := record{
69 phase: phaseOpen,
70 deadline: now + openWindow,
71 challenger: cur.Previous().Address(),
72 opponent: opponent,
73 deckSeal: deckSeal,
74 seal: planSeal,
75 box: raw,
76 }
77 m.clans[0] = c
78 join(cur.Previous().IsUser(), m.challenger, lastID)
79 saveMatch(rules, lastID, m)
80 openAdd(lastID, m.deadline)
81 tellBoard(lastID, clan+" posted a challenge.")
82 return strconv.Itoa(lastID)
83}
84
85// Open lists open, unexpired challenges, newest first: limit of them after skipping
86// offset. limit is 1 to 50.
87func Open(offset, limit int) []OpenChallenge {
88 checkPage(offset, limit)
89 rules := engine.ProofOfConcept()
90 list := []OpenChallenge{}
91 for _, id := range openNewest(time.Now().Unix(), offset, limit) {
92 m, ok := loadMatch(rules, id)
93 if !ok {
94 continue
95 }
96 list = append(list, OpenChallenge{
97 ID: strconv.Itoa(id),
98 Challenger: m.challenger.String(),
99 Clan: clanName(rules, m.clans[0]),
100 Opponent: m.opponent.String(),
101 Posted: m.deadline - openWindow,
102 Expires: m.deadline,
103 })
104 }
105 return list
106}
107
108// forget deletes open challenge id and its place among its challenger's matches. Its
109// entry on the open list is the caller's to remove.
110func forget(id int, challenger address) {
111 dropMatch(id)
112 p := loadPlayer(challenger)
113 p.remove(id)
114 savePlayer(challenger, p)
115}
116
117func (m record) expired(now int64) bool {
118 return m.phase == phaseOpen && now > m.deadline
119}
120
121func checkPage(offset, limit int) {
122 if offset < 0 {
123 panic("offset must not be negative")
124 }
125 if limit < 1 || limit > maxPage {
126 panic("limit must be 1 to 50")
127 }
128}
129
130// boxBytes decodes a box argument: the standard base64 of 1 to maxBox bytes, in the one
131// form that encodes them, so Match returns every box exactly as it was sent.
132func boxBytes(text string) string {
133 if len(text) == 0 || len(text) > base64.StdEncoding.EncodedLen(maxBox) {
134 panic("invalid box")
135 }
136 raw, err := base64.StdEncoding.DecodeString(text)
137 if err != nil || len(raw) > maxBox || base64.StdEncoding.EncodeToString(raw) != text {
138 panic("invalid box")
139 }
140 return string(raw)
141}
142
143// canonical accepts a valid address in the lowercase form callers carry. bech32 also
144// allows uppercase, which would never equal a caller's address.
145func canonical(a address) bool {
146 return a.IsValid() && string(a) == strings.ToLower(string(a))
147}
148
149// validSeal accepts a sha256 sum in lowercase hex.
150func validSeal(s string) bool {
151 if len(s) != 64 {
152 return false
153 }
154 for i := 0; i < len(s); i++ {
155 c := s[i]
156 if !(c >= '0' && c <= '9' || c >= 'a' && c <= 'f') {
157 return false
158 }
159 }
160 return true
161}