z1_room_filetest.gno
4.40 Kb · 128 lines
1// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z1
2
3// Two players fight a 5-round room. Covers: sealing and revealing, safe
4// retries, a late seal after Advance (no lockout), a missed round played by
5// the bot, PlanPreview, LastResult, the board, and an exact replay.
6package z1
7
8import (
9 "strings"
10 "testing"
11
12 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v4"
13 "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/rooms/v4"
14)
15
16var (
17 alice = address("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm")
18 bob = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5")
19)
20
21func as(cur realm, who address) { testing.SetRealm(testing.NewUserRealm(who)) }
22
23func field(js, name string) string {
24 i := strings.Index(js, `"`+name+`":`)
25 if i < 0 {
26 return "?"
27 }
28 rest := js[i+len(name)+3:]
29 if strings.HasPrefix(rest, "[") {
30 return rest[:strings.Index(rest, "]")+1]
31 }
32 end := strings.IndexAny(rest, ",}")
33 return rest[:end]
34}
35
36func main(cur realm) {
37 as(cur, alice)
38 id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 5, 120, 40)
39 as(cur, bob)
40 rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Hard)
41 st := rooms.StateJSON(id)
42 println("rounds", field(st, "rounds"), "roundTicks", field(st, "roundTicks"), "blocksLeft", field(st, "blocksLeft"), "secondsLeft", field(st, "secondsLeft"))
43
44 // free previews: compare plans in one read
45 pv := rooms.PlanPreview(id, 0, "run SE 40; special; run E 60 || run E 100 || jump 3")
46 println("previews:", strings.Count(pv, `"gain"`), "ok,", strings.Count(pv, `"error"`), "error")
47
48 plans := [5][2]string{
49 {"run SE 40; special; run E 80", "run NW 60; attack; run W 60"},
50 {"run S 60; special; wait 30; run E 50", "keys:" + strings.Repeat(string(rules.Encode(10, rules.None)), 240)},
51 {"run E 120; special; run N 100", "run W 80; attack; run N 80"},
52 {"run N 100; special; run W 100", ""}, // bob is late this round
53 {"wait 20; run S 100; special; run E 100", "run S 120; attack; run E 100"},
54 }
55 for round, p := range plans {
56 as(cur, alice)
57 h := rooms.PlanHash(p[0], "sa")
58 rooms.Seal(cross(cur), id, h)
59 rooms.Seal(cross(cur), id, h) // a retry is a no-op
60 if round == 3 {
61 // bob misses sealing; the deadline passes and alice moves the room on
62 testing.SkipHeights(40)
63 rooms.Advance(cross(cur), id)
64 as(cur, bob)
65 println("round 4 bob can:", field(rooms.StateJSON(id), "allowedActions"))
66 // bob seals late, in the reveal window: no lockout
67 late := "run NW 50; attack; run W 100"
68 rooms.Seal(cross(cur), id, rooms.PlanHash(late, "sb"))
69 rooms.Reveal(cross(cur), id, late, "sb", "Late, but still in.")
70 as(cur, alice)
71 rooms.Reveal(cross(cur), id, p[0], "sa", "Magnet the middle.")
72 } else {
73 as(cur, bob)
74 rooms.Seal(cross(cur), id, rooms.PlanHash(p[1], "sb"))
75 as(cur, alice)
76 rooms.Reveal(cross(cur), id, p[0], "sa", "Round plan A.")
77 rooms.Reveal(cross(cur), id, p[0], "sa", "Round plan A.") // retry no-op
78 as(cur, bob)
79 if round == 4 {
80 // bob never reveals the last round: his bot plays it
81 testing.SkipHeights(40)
82 rooms.Advance(cross(cur), id)
83 } else {
84 rooms.Reveal(cross(cur), id, p[1], "sb", "Round plan B.")
85 }
86 }
87 last := rooms.LastResult(id)
88 println("after round", round+1, "gold", field(last, "goldAfter"), "bot?", strings.Count(last, `"bot":true`))
89 }
90 st = rooms.StateJSON(id)
91 println(field(st, "phase"), st[strings.Index(st, `"gold":[`):])
92
93 // the recorded inputs replay to the same score
94 in := rooms.InputsJSON(id)
95 g := rules.NewLength(uint32(atoi(field(in, "seed"))), rules.Reader, rules.Brawler, 1200)
96 a := in[strings.Index(in, `"a":"`)+5:][:1200]
97 b := in[strings.Index(in, `"b":"`)+5:][:1200]
98 for t := 0; t < 1200; t++ {
99 m0, x0, _ := rules.Decode(a[t])
100 m1, x1, _ := rules.Decode(b[t])
101 g.Step(m0, x0, m1, x1)
102 }
103 println("replay:", g.F[0].Gold, g.F[1].Gold)
104 println("thoughts:", in[strings.Index(in, `"thoughtsB":`):])
105 println(strings.Contains(rooms.Render(""), "Alice vs Bob"))
106}
107
108func atoi(s string) int {
109 n := 0
110 for _, c := range s {
111 n = n*10 + int(c-'0')
112 }
113 return n
114}
115
116// Output:
117// rounds 5 roundTicks 240 blocksLeft 40 secondsLeft 160
118// previews: 2 ok, 1 error
119// after round 1 gold 2 bot? 0
120// after round 2 gold 3 bot? 0
121// after round 3 gold 3 bot? 0
122// round 4 bob can: ["reveal"]
123// after round 4 gold 5 bot? 0
124// after round 5 gold 1 bot? 1
125// "done" "gold":[1,39]}
126// replay: 1 39
127// thoughts: "thoughtsB":["Round plan B.","Round plan B.","Round plan B.","Late, but still in.",""]}
128// true