z7_preview_vs_filetest.gno
2.63 Kb · 74 lines
1// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z7
2
3// Rules v3 in a room: PlanPreview models the rival by the plan they revealed
4// last round (their bot before that), PlanPreviewVs picks the model, guard
5// plays, and Compile points smart plans at Seal.
6package z7
7
8import (
9 "strings"
10 "testing"
11
12 rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3"
13 "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/rooms/v3"
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
23// models lists the "rival" field of each preview.
24func models(pv string) string {
25 out := []string{}
26 for _, part := range strings.Split(pv, `"rival":"`)[1:] {
27 out = append(out, part[:strings.Index(part, `"`)])
28 }
29 return strings.Join(out, ",")
30}
31
32func round(cur realm, id int, pa, pb string) {
33 as(cur, alice)
34 rooms.Seal(cross(cur), id, rooms.PlanHash(pa, "a"))
35 as(cur, bob)
36 rooms.Seal(cross(cur), id, rooms.PlanHash(pb, "b"))
37 as(cur, alice)
38 rooms.Reveal(cross(cur), id, pa, "a", "A")
39 as(cur, bob)
40 rooms.Reveal(cross(cur), id, pb, "b", "B")
41}
42
43func main(cur realm) {
44 as(cur, alice)
45 id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 3, 72, 40)
46 as(cur, bob)
47 rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Hard)
48
49 // round 1: Bob has revealed nothing yet, so "last" falls back to his bot
50 println("round 1:", models(rooms.PlanPreview(id, 0, "guard on; auto 240 || auto 240")))
51 round(cur, id, "guard on; auto 240", "chase 120; if near 1.45: attack; auto 120")
52
53 // round 2: "last" replays Bob's round-1 plan; "bot" and a profile are chosen
54 println("round 2:", models(rooms.PlanPreview(id, 0, "guard on; auto 240")))
55 println("vs bot:", models(rooms.PlanPreviewVs(id, 0, "guard on; auto 240", "bot")))
56 style := rooms.LearnStyle(id, 1)
57 println("vs profile:", models(rooms.PlanPreviewVs(id, 0, "guard on; auto 240", style)))
58 println(revive(func() { rooms.PlanPreviewVs(id, 0, "auto 240", "nonsense") }) != nil)
59 round(cur, id, "guard on; collect 200; guard off; auto 40", "auto 240")
60 round(cur, id, "guard on; auto 240", "guard on; auto 240")
61
62 st := rooms.StateJSON(id)
63 println(st[strings.Index(st, `"phase"`):][:15], st[strings.Index(st, `"gold":[`):])
64 println(revive(func() { rooms.Compile("collect 30", 50) }))
65}
66
67// Output:
68// round 1: bot,bot
69// round 2: last
70// vs bot: bot
71// vs profile: profile
72// true
73// "phase":"done", "gold":[19,28]}
74// plan step "collect 30": Compile only turns run, wait and buttons into keys; seal and reveal a smart plan (collect ...) as plain text