z0_render_filetest.gno
14.08 Kb · 148 lines
1// PKGPATH: gno.land/r/demo/main
2
3package main
4
5import (
6 "chain"
7 "crypto/sha256"
8 "encoding/hex"
9 "testing"
10
11 "gno.land/p/nt/testutils/v0"
12 "gno.land/r/samcrew/connect4"
13)
14
15func commit(passphrase string) string {
16 h := sha256.Sum256([]byte(passphrase))
17 return hex.EncodeToString(h[:])
18}
19
20func main(cur realm) {
21 alice := testutils.TestAddress("alice")
22 bob := testutils.TestAddress("bob")
23 carol := testutils.TestAddress("carol")
24 c4 := chain.PackageAddress("gno.land/r/samcrew/connect4")
25 stake := chain.Coins{{"ugnot", 5_000_000}}
26
27 testing.SetRealm(testing.NewUserRealm(alice))
28 testing.SetOriginSend(stake)
29 testing.IssueCoins(c4, stake)
30 g1 := connect4.Offer(cross(cur), "", 15, commit("alice four random words"), 100_000)
31
32 testing.SetRealm(testing.NewUserRealm(carol))
33 testing.SetOriginSend(stake)
34 testing.IssueCoins(c4, stake)
35 connect4.Offer(cross(cur), bob, 5, commit("carol four random words"), 100_000)
36
37 testing.SetRealm(testing.NewUserRealm(bob))
38 testing.SetOriginSend(stake)
39 testing.IssueCoins(c4, stake)
40 connect4.Accept(cross(cur), g1, commit("acceptor seed"))
41
42 // Render runs as the realm itself, as it does under gnoweb.
43 testing.SetRealm(testing.NewCodeRealm("gno.land/r/samcrew/connect4"))
44 println(connect4.Render(""))
45 println("-----")
46 println(connect4.Render("game/1"))
47 println("-----")
48 println(connect4.Render("game/2"))
49 println("-----")
50 println(connect4.Render("new?stake=12.5&validFor=10&opponent=&commitment=5fb8d3d0c4f3b0fe7ab6ab1f7eda42c6e2de0c3c1d4a6c5e3d0f2f5d0e4f5a6b"))
51 println("-----")
52 println(connect4.Render("game/x"))
53 println("-----")
54 println(connect4.Render("game/99999999999"))
55}
56
57// Output:
58// # Connect 4
59//
60// Both players stake the same amount; the winner takes the pot minus the house fee shown on each offer (0.1 GNOT for new offers). 90s per move: run out and you forfeit. If the first mover never moves, both stakes are refunded.
61//
62// **[Post an offer](/r/samcrew/connect4:new)** · open a game below to play it
63//
64// ## Open offers
65//
66// | Game | Stake | Fee | Creator | For | Expires in | |
67// |---|---|---|---|---|---|---|
68// | [#2](/r/samcrew/connect4:game/2) | 5 GNOT | 0.1 GNOT | g1vdshymmvta047h6lta047h6lta047h6l2asz94 | g1vfhkyh6lta047h6lta047h6lta047h6l03vdhu | 5m0s | [Accept](/r/samcrew/connect4$help&func=Accept&.send=5000000ugnot&id=2&seedCommitment=) |
69//
70// ## Live games
71//
72// | Game | Pot | Turn | Time left |
73// |---|---|---|---|
74// | [#1](/r/samcrew/connect4:game/1) | 10 GNOT | g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh (reveal) | 1m30s |
75//
76// ## Most games won
77//
78// No finished games yet.
79//
80// ## Most gnot won
81//
82// No finished games yet.
83//
84//
85// -----
86// # Game #1 — playing
87//
88// [← Lobby](/r/samcrew/connect4)
89//
90// 🔴 g1v9kxjcm9ta047h6lta047h6lta047h6lzd40gh
91// 🟡 g1vfhkyh6lta047h6lta047h6lta047h6l03vdhu
92// Stake: 5 GNOT each · fee 0.1 GNOT
93//
94// 
95//
96// Waiting for the creator to reveal the passphrase.
97//
98// Time left: 1m30s
99//
100// **[Reveal](/r/samcrew/connect4$help&func=Reveal&id=1&passphrase=)** (creator)
101//
102// [Resign](/r/samcrew/connect4$help&func=Resign&id=1)
103//
104// -----
105// # Game #2 — open
106//
107// [← Lobby](/r/samcrew/connect4)
108//
109// 🔴 g1vdshymmvta047h6lta047h6lta047h6l2asz94
110// 🟡 waiting for opponent
111// Stake: 5 GNOT each · fee 0.1 GNOT
112//
113// 
114//
115// Open offer for g1vfhkyh6lta047h6lta047h6lta047h6l03vdhu, expires in 5m0s.
116//
117// **[Accept for 5 GNOT](/r/samcrew/connect4$help&func=Accept&.send=5000000ugnot&id=2&seedCommitment=)** with the sha256 of a fresh random seed (`openssl rand -hex 32`). Keep the seed: you reveal it after the creator, within 90s, or forfeit.
118//
119// [Cancel offer](/r/samcrew/connect4$help&func=Cancel&id=2) (creator)
120//
121// -----
122// # Post an offer
123//
124// [← Lobby](/r/samcrew/connect4)
125//
126// Generate a fresh random passphrase for every offer and keep it: once someone accepts you have 90s to reveal it, or you forfeit. A guessable one lets an opponent work out who moves first. Paste only its sha256 below, never the passphrase itself:
127//
128// pass=$(openssl rand -hex 32); echo "$pass"
129// printf %s "$pass" | shasum -a 256
130//
131// <gno-form path="new">
132// <gno-input name="stake" type="text" placeholder="Stake in GNOT, e.g. 10 (min 1)" />
133// <gno-input name="validFor" type="number" placeholder="Minutes the offer stays open, 1-60 (default 15)" />
134// <gno-input name="opponent" type="text" placeholder="Opponent address (optional, leave empty for anyone)" />
135// <gno-input name="commitment" type="text" placeholder="sha256 of your passphrase (64 hex chars)" />
136// </gno-form>
137//
138// ## Ready
139//
140// Stake 12.5 GNOT, open to anyone for 10 min, house fee at most 0.1 GNOT, commitment `5fb8d3d0c4f3b0fe7ab6ab1f7eda42c6e2de0c3c1d4a6c5e3d0f2f5d0e4f5a6b`. Check it is the sha256 of your own passphrase before signing.
141//
142// **[Post offer for 12.5 GNOT](/r/samcrew/connect4$help&func=Offer&.send=12500000ugnot&commitment=5fb8d3d0c4f3b0fe7ab6ab1f7eda42c6e2de0c3c1d4a6c5e3d0f2f5d0e4f5a6b&maxFee=100000&opponent=&validFor=10)**
143//
144// -----
145// # Not found
146//
147// -----
148// # Not found