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.

z1_room_filetest.gno

2.82 Kb · 77 lines
 1// PKGPATH: gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/z1
 2
 3// Two players fight a room round by round: seal, reveal, play. A player who
 4// misses a round is played by their fallback bot. The finished room replays
 5// exactly from its recorded inputs.
 6package z1
 7
 8import (
 9	"strings"
10	"testing"
11
12	rules "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/brawl"
13	"gno.land/r/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/rooms"
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 plan(dir, act, n int) string {
24	return string(rules.Encode(dir, act)) + strings.Repeat(string(rules.Encode(dir, rules.None)), n-1)
25}
26
27func main(cur realm) {
28	as(cur, alice)
29	id := rooms.Create(cross(cur), "Alice", rules.Reader, rules.Normal, 100, 180)
30	println(rooms.Open())
31	as(cur, bob)
32	rooms.Join(cross(cur), id, "Bob", rules.Brawler, rules.Hard)
33
34	for round := 0; round < 18; round++ {
35		pa, pb := plan(round%16, rules.Special, 100), plan((round+8)%16, rules.Attack, 100)
36		as(cur, alice)
37		rooms.Seal(cross(cur), id, rooms.PlanHash(pa, "salt-a"))
38		if round != 3 { // bob misses round 3: his bot plays it
39			as(cur, bob)
40			rooms.Seal(cross(cur), id, rooms.PlanHash(pb, "salt-b"))
41		} else {
42			testing.SkipHeights(rooms.PhaseBlocks)
43			rooms.Advance(cross(cur), id) // alice sealed, so she still gets to reveal
44		}
45		as(cur, alice)
46		rooms.Reveal(cross(cur), id, pa, "salt-a", "Round "+string(rune('a'+round))+": magnet, then run.")
47		if round == 3 {
48			testing.SkipHeights(rooms.PhaseBlocks)
49			rooms.Advance(cross(cur), id)
50		} else {
51			as(cur, bob)
52			rooms.Reveal(cross(cur), id, pb, "salt-b", "Chase and punch.")
53		}
54	}
55	st := rooms.StateJSON(id)
56	println(st)
57	println(rooms.InputsJSON(id)[:60])
58	// the recorded inputs replay to the same score
59	g := rules.NewLength(1078766, rules.Reader, rules.Brawler, 1800)
60	in := rooms.InputsJSON(id)
61	a := in[strings.Index(in, `"a":"`)+5:]
62	a = a[:1800]
63	b := in[strings.Index(in, `"b":"`)+5:]
64	b = b[:1800]
65	for t := 0; t < 1800; t++ {
66		m0, x0, _ := rules.Decode(a[t])
67		m1, x1, _ := rules.Decode(b[t])
68		g.Step(m0, x0, m1, x1)
69	}
70	println("replay:", g.F[0].Gold, g.F[1].Gold)
71}
72
73// Output:
74// [{"id":1,"name":"Alice","class":4,"roundTicks":100,"seconds":180}]
75// {"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]}
76// {"seed":1078766,"ticks":1800,"classA":4,"classB":1,"a":"V###
77// replay: 9 18