// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z1 // Two players fight a room round by round: seal, reveal, play. A player who // misses a round is played by their fallback bot. The finished room replays // exactly from its recorded inputs. package z1 import ( "strings" "testing" rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl" "gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/rooms" ) var ( alice = address("g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm") bob = address("g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5") ) func as(cur realm, who address) { testing.SetRealm(testing.NewUserRealm(who)) } func plan(dir, act, n int) string { return string(rules.Encode(dir, act)) + strings.Repeat(string(rules.Encode(dir, rules.None)), n-1) } func main(cur realm) { as(cur, alice) id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 100, 180) println(rooms.Open()) as(cur, bob) rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Hard) for round := 0; round < 18; round++ { pa, pb := plan(round%16, rules.Special, 100), plan((round+8)%16, rules.Attack, 100) as(cur, alice) rooms.Seal(cross(cur), id, rooms.PlanHash(pa, "salt-a")) if round != 3 { // bob misses round 3: his bot plays it as(cur, bob) rooms.Seal(cross(cur), id, rooms.PlanHash(pb, "salt-b")) } else { testing.SkipHeights(rooms.PhaseBlocks) rooms.Advance(cross(cur), id) // alice sealed, so she still gets to reveal } as(cur, alice) rooms.Reveal(cross(cur), id, pa, "salt-a", "Round "+string(rune('a'+round))+": magnet, then run.") if round == 3 { testing.SkipHeights(rooms.PhaseBlocks) rooms.Advance(cross(cur), id) } else { as(cur, bob) rooms.Reveal(cross(cur), id, pb, "salt-b", "Chase and punch.") } } st := rooms.StateJSON(id) println(st) println(rooms.InputsJSON(id)[:60]) // the recorded inputs replay to the same score g := rules.NewLength(1078766, rules.Reader, rules.Brawler, 1800) in := rooms.InputsJSON(id) a := in[strings.Index(in, `"a":"`)+5:] a = a[:1800] b := in[strings.Index(in, `"b":"`)+5:] b = b[:1800] for t := 0; t < 1800; 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: // [{"id":1,"name":"Alice","class":4,"roundTicks":100,"seconds":180}] // {"id":1,"phase":"done","round":18,"roundTicks":100,"deadline":213,"height":183,"seed":1078766,"arena":"Scrapyard","tick":1800,"ticks":1800,"players":[{"player":"g1wymu47drhr0kuq2098m792lytgtj2nyx77yrsm","name":"Alice","class":4,"fallback":1,"sealed":false,"revealed":false,"missed":0},{"player":"g1jg8mtutu9khhfwc4nxmuhcpftf0pajdhfvsqf5","name":"Bob","class":1,"fallback":2,"sealed":false,"revealed":false,"missed":1}],"gold":[9,18]} // {"seed":1078766,"ticks":1800,"classA":4,"classB":1,"a":"V### // replay: 9 18