// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z6 // Smart plans (rules v2): goto, collect, chase, flee, if near and auto play // in a room, a bad step is refused with an error naming it, and the recorded // inputs replay to the same score. package z6 import ( "strings" "testing" rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v4" "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/rooms/v4" ) var ( alice = address("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") bob = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") ) func as(cur realm, who address) { testing.SetRealm(testing.NewUserRealm(who)) } func field(js, name string) string { i := strings.Index(js, `"`+name+`":`) if i < 0 { return "?" } rest := js[i+len(name)+3:] if strings.HasPrefix(rest, "[") { return rest[:strings.Index(rest, "]")+1] } end := strings.IndexAny(rest, ",}") return rest[:end] } func atoi(s string) int { n := 0 for _, c := range s { n = n*10 + int(c-'0') } return n } func main(cur realm) { as(cur, alice) id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 3, 72, 40) as(cur, bob) rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Normal) pv := rooms.PlanPreview(id, 0, "goto 7,7; collect 200 || chase 100; if near 1.4: attack; auto 60 || fly 3") println("previews:", strings.Count(pv, `"gain"`), "ok,", strings.Count(pv, `"error"`), "error") println(pv[strings.Index(pv, `"error":`):]) plans := [3][2]string{ {"goto 7,7; collect 200", "chase 120; if near 1.4: attack; auto 60; collect 60"}, {"collect 150; special; flee 60", "goto 3,3 80; collect 160"}, {"auto 60; collect 180", "chase 60; if near 1.4: special; collect 180"}, } for _, p := range plans { as(cur, alice) rooms.Seal(cross(cur), id, rooms.PlanHash(p[0], "sa")) as(cur, bob) rooms.Seal(cross(cur), id, rooms.PlanHash(p[1], "sb")) as(cur, alice) rooms.Reveal(cross(cur), id, p[0], "sa", "A") as(cur, bob) rooms.Reveal(cross(cur), id, p[1], "sb", "B") last := rooms.LastResult(id) println("round gold", field(last, "goldAfter"), "bot?", strings.Count(last, `"bot":true`)) } st := rooms.StateJSON(id) println(field(st, "phase"), field(st, "gold")) // the recorded inputs replay to the same score in := rooms.InputsJSON(id) n := 3 * atoi(field(st, "roundTicks")) g := rules.NewLength(uint32(atoi(field(in, "seed"))), rules.Reader, rules.Brawler, n) a := in[strings.Index(in, `"a":"`)+5:][:n] b := in[strings.Index(in, `"b":"`)+5:][:n] for t := 0; t < n; t++ { m0, x0, _ := rules.Decode(a[t]) m1, x1, _ := rules.Decode(b[t]) g.Step(m0, x0, m1, x1) } println("replay:", "[", g.F[0].Gold, g.F[1].Gold, "]") } // Output: // previews: 2 ok, 1 error // "error":"step 1 \"fly 3\": unknown word \"fly\""}] // round gold 15 bot? 0 // round gold 24 bot? 0 // round gold 40 bot? 0 // "done" [40,27] // replay: [ 40 27 ]