z0_render_filetest.gno
13.55 Kb Β· 147 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"))
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"))
36
37 testing.SetRealm(testing.NewUserRealm(bob))
38 testing.SetOriginSend(stake)
39 testing.IssueCoins(c4, stake)
40 connect4.Accept(cross(cur), g1)
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 a 0.1 GNOT fee. 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 | Creator | For | Expires in | |
67// |---|---|---|---|---|---|
68// | [#2](/r/samcrew/connect4:game/2) | 5 GNOT | g1vdshymmvta047h6lta047h6lta047h6l2asz94 | g1vfhkyh6lta047h6lta047h6lta047h6l03vdhu | 5m0s | [Accept](/r/samcrew/connect4$help&func=Accept&.send=5000000ugnot&id=2) |
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)**
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// Pick a passphrase of 4+ random words, fresh for every offer, and keep it: once someone accepts you have 90s to reveal it, or you forfeit. Paste only its sha256 below, never the passphrase itself:
127//
128// printf %s 'your pass phrase' | shasum -a 256
129//
130// <gno-form path="new">
131// <gno-input name="stake" type="text" placeholder="Stake in GNOT, e.g. 10 (min 1)" />
132// <gno-input name="validFor" type="number" placeholder="Minutes the offer stays open, 1-60 (default 15)" />
133// <gno-input name="opponent" type="text" placeholder="Opponent address (optional, leave empty for anyone)" />
134// <gno-input name="commitment" type="text" placeholder="sha256 of your passphrase (64 hex chars)" />
135// </gno-form>
136//
137// ## Ready
138//
139// Stake 12.5 GNOT, open to anyone for 10 min.
140//
141// **[Post offer for 12.5 GNOT](/r/samcrew/connect4$help&func=Offer&.send=12500000ugnot&commitment=5fb8d3d0c4f3b0fe7ab6ab1f7eda42c6e2de0c3c1d4a6c5e3d0f2f5d0e4f5a6b&opponent=&validFor=10)**
142//
143// -----
144// # Not found
145//
146// -----
147// # Not found