Skip to content Search Apps Documentation Source Content Run Reset Eval Playground File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

z6_smart_plan_filetest.gno

2.81 Kb · 95 lines
 1// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z6
 2
 3// Smart plans (rules v2): goto, collect, chase, flee, if near and auto play
 4// in a room, a bad step is refused with an error naming it, and the recorded
 5// inputs replay to the same score.
 6package z6
 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
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 atoi(s string) int {
37	n := 0
38	for _, c := range s {
39		n = n*10 + int(c-'0')
40	}
41	return n
42}
43
44func main(cur realm) {
45	as(cur, alice)
46	id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 3, 72, 40)
47	as(cur, bob)
48	rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Normal)
49
50	pv := rooms.PlanPreview(id, 0, "goto 7,7; collect 200 || chase 100; if near 1.4: attack; auto 100 || fly 3")
51	println("previews:", strings.Count(pv, `"gain"`), "ok,", strings.Count(pv, `"error"`), "error")
52	println(pv[strings.Index(pv, `"error":`):])
53
54	plans := [3][2]string{
55		{"goto 7,7; collect 200", "chase 120; if near 1.4: attack; auto 120"},
56		{"collect 150; special; flee 60", "goto 3,3 80; collect 160"},
57		{"auto 240", "chase 60; if near 1.4: special; collect 180"},
58	}
59	for _, p := range plans {
60		as(cur, alice)
61		rooms.Seal(cross(cur), id, rooms.PlanHash(p[0], "sa"))
62		as(cur, bob)
63		rooms.Seal(cross(cur), id, rooms.PlanHash(p[1], "sb"))
64		as(cur, alice)
65		rooms.Reveal(cross(cur), id, p[0], "sa", "A")
66		as(cur, bob)
67		rooms.Reveal(cross(cur), id, p[1], "sb", "B")
68		last := rooms.LastResult(id)
69		println("round gold", field(last, "goldAfter"), "bot?", strings.Count(last, `"bot":true`))
70	}
71	st := rooms.StateJSON(id)
72	println(field(st, "phase"), field(st, "gold"))
73
74	// the recorded inputs replay to the same score
75	in := rooms.InputsJSON(id)
76	n := 3 * atoi(field(st, "roundTicks"))
77	g := rules.NewLength(uint32(atoi(field(in, "seed"))), rules.Reader, rules.Brawler, n)
78	a := in[strings.Index(in, `"a":"`)+5:][:n]
79	b := in[strings.Index(in, `"b":"`)+5:][:n]
80	for t := 0; t < n; t++ {
81		m0, x0, _ := rules.Decode(a[t])
82		m1, x1, _ := rules.Decode(b[t])
83		g.Step(m0, x0, m1, x1)
84	}
85	println("replay:", "[", g.F[0].Gold, g.F[1].Gold, "]")
86}
87
88// Output:
89// previews: 2 ok, 1 error
90// "error":"step 1 \"fly 3\": unknown word \"fly\""}]
91// round gold 13 bot? 0
92// round gold 19 bot? 0
93// round gold 47 bot? 0
94// "done" [47,21]
95// replay: [ 47 21 ]