// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z1 // Two players fight a 5-round room. Covers: sealing and revealing, safe // retries, a late seal after Advance (no lockout), a missed round played by // the bot, PlanPreview, LastResult, the board, and an exact replay. package z1 import ( "strings" "testing" rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl/v3" "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/rooms/v3" ) 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 main(cur realm) { as(cur, alice) id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 5, 120, 40) as(cur, bob) rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Hard) st := rooms.StateJSON(id) println("rounds", field(st, "rounds"), "roundTicks", field(st, "roundTicks"), "blocksLeft", field(st, "blocksLeft"), "secondsLeft", field(st, "secondsLeft")) // free previews: compare plans in one read pv := rooms.PlanPreview(id, 0, "run SE 40; special; run E 60 || run E 100 || jump 3") println("previews:", strings.Count(pv, `"gain"`), "ok,", strings.Count(pv, `"error"`), "error") plans := [5][2]string{ {"run SE 40; special; run E 80", "run NW 60; attack; run W 60"}, {"run S 60; special; wait 30; run E 50", "keys:" + strings.Repeat(string(rules.Encode(10, rules.None)), 240)}, {"run E 120; special; run N 100", "run W 80; attack; run N 80"}, {"run N 100; special; run W 100", ""}, // bob is late this round {"wait 20; run S 100; special; run E 100", "run S 120; attack; run E 100"}, } for round, p := range plans { as(cur, alice) h := rooms.PlanHash(p[0], "sa") rooms.Seal(cross(cur), id, h) rooms.Seal(cross(cur), id, h) // a retry is a no-op if round == 3 { // bob misses sealing; the deadline passes and alice moves the room on testing.SkipHeights(40) rooms.Advance(cross(cur), id) as(cur, bob) println("round 4 bob can:", field(rooms.StateJSON(id), "allowedActions")) // bob seals late, in the reveal window: no lockout late := "run NW 50; attack; run W 100" rooms.Seal(cross(cur), id, rooms.PlanHash(late, "sb")) rooms.Reveal(cross(cur), id, late, "sb", "Late, but still in.") as(cur, alice) rooms.Reveal(cross(cur), id, p[0], "sa", "Magnet the middle.") } else { as(cur, bob) rooms.Seal(cross(cur), id, rooms.PlanHash(p[1], "sb")) as(cur, alice) rooms.Reveal(cross(cur), id, p[0], "sa", "Round plan A.") rooms.Reveal(cross(cur), id, p[0], "sa", "Round plan A.") // retry no-op as(cur, bob) if round == 4 { // bob never reveals the last round: his bot plays it testing.SkipHeights(40) rooms.Advance(cross(cur), id) } else { rooms.Reveal(cross(cur), id, p[1], "sb", "Round plan B.") } } last := rooms.LastResult(id) println("after round", round+1, "gold", field(last, "goldAfter"), "bot?", strings.Count(last, `"bot":true`)) } st = rooms.StateJSON(id) println(field(st, "phase"), st[strings.Index(st, `"gold":[`):]) // the recorded inputs replay to the same score in := rooms.InputsJSON(id) g := rules.NewLength(uint32(atoi(field(in, "seed"))), rules.Reader, rules.Brawler, 1200) a := in[strings.Index(in, `"a":"`)+5:][:1200] b := in[strings.Index(in, `"b":"`)+5:][:1200] for t := 0; t < 1200; 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) println("thoughts:", in[strings.Index(in, `"thoughtsB":`):]) println(strings.Contains(rooms.Render(""), "Alice vs Bob")) } func atoi(s string) int { n := 0 for _, c := range s { n = n*10 + int(c-'0') } return n } // Output: // rounds 5 roundTicks 240 blocksLeft 40 secondsLeft 160 // previews: 2 ok, 1 error // after round 1 gold 2 bot? 0 // after round 2 gold 3 bot? 0 // after round 3 gold 3 bot? 0 // round 4 bob can: ["reveal"] // after round 4 gold 5 bot? 0 // after round 5 gold 1 bot? 1 // "done" "gold":[1,39]} // replay: 1 39 // thoughts: "thoughtsB":["Round plan B.","Round plan B.","Round plan B.","Late, but still in.",""]} // true