|
| 1 | +# Reproducible Test for https://github.com/gnolang/gno/issues/1167 |
| 2 | + |
| 3 | +gnoland start |
| 4 | + |
| 5 | +# add contract |
| 6 | +gnokey maketx addpkg -pkgdir $WORK -pkgpath gno.land/r/demo/xx -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 |
| 7 | +stdout OK! |
| 8 | + |
| 9 | +# execute New |
| 10 | +gnokey maketx call -pkgpath gno.land/r/demo/xx -func New -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 |
| 11 | +stdout OK! |
| 12 | + |
| 13 | +# execute Delta for the first time |
| 14 | +gnokey maketx call -pkgpath gno.land/r/demo/xx -func Delta -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 |
| 15 | +stdout OK! |
| 16 | +stdout '"1,1,1;" string' |
| 17 | + |
| 18 | +# execute Delta for the second time |
| 19 | +gnokey maketx call -pkgpath gno.land/r/demo/xx -func Delta -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 |
| 20 | +stdout OK! |
| 21 | +stdout '1,1,1;2,2,2;" string' |
| 22 | + |
| 23 | +# execute Delta for the third time |
| 24 | +gnokey maketx call -pkgpath gno.land/r/demo/xx -func Delta -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 |
| 25 | +stdout OK! |
| 26 | +stdout '1,1,1;2,2,2;3,3,3;" string' |
| 27 | + |
| 28 | +# execute Render |
| 29 | +gnokey maketx call -pkgpath gno.land/r/demo/xx -func Render -args X -gas-fee 1000000ugnot -gas-wanted 2000000 -broadcast -chainid=tendermint_test test1 |
| 30 | +stdout OK! |
| 31 | +stdout '1,1,1;2,2,2;3,3,3;" string' |
| 32 | + |
| 33 | +-- gno.mod -- |
| 34 | +module gno.land/r/demo/xx |
| 35 | + |
| 36 | +require ( |
| 37 | + gno.land/p/demo/avl v0.0.0-latest |
| 38 | +) |
| 39 | + |
| 40 | +-- realm.gno -- |
| 41 | +package xx |
| 42 | + |
| 43 | +import ( |
| 44 | + "strconv" |
| 45 | + |
| 46 | + "gno.land/p/demo/avl" |
| 47 | +) |
| 48 | + |
| 49 | +type Move struct { |
| 50 | + N1, N2, N3 byte |
| 51 | +} |
| 52 | + |
| 53 | +type Position struct { |
| 54 | + Moves []Move |
| 55 | +} |
| 56 | + |
| 57 | +func (p Position) clone() Position { |
| 58 | + mv := p.Moves |
| 59 | + return Position{Moves: mv} |
| 60 | +} |
| 61 | + |
| 62 | +func (oldp Position) update() Position { |
| 63 | + p := oldp.clone() |
| 64 | + |
| 65 | + counter++ |
| 66 | + // This is a workaround for the wrong behaviour (ie. uncomment this line): |
| 67 | + // p.Moves = append([]Move{}, p.Moves...) |
| 68 | + p.Moves = append(p.Moves, Move{counter, counter, counter}) |
| 69 | + return p |
| 70 | +} |
| 71 | + |
| 72 | +type Game struct { |
| 73 | + Position Position |
| 74 | +} |
| 75 | + |
| 76 | +var games avl.Tree // id -> *Game |
| 77 | + |
| 78 | +var counter byte |
| 79 | + |
| 80 | +func New(s string) string { |
| 81 | + // Bug shows if Moves has a cap > 0 when initialised. |
| 82 | + el := &Game{Position: Position{Moves: make([]Move, 0, 2)}} |
| 83 | + games.Set(s, el) |
| 84 | + return values(el.Position) |
| 85 | +} |
| 86 | + |
| 87 | +func Delta(s string) string { |
| 88 | + v, _ := games.Get(s) |
| 89 | + g, ok := v.(*Game) |
| 90 | + if !ok { |
| 91 | + panic("invalid game") |
| 92 | + } |
| 93 | + n := g.Position.update() |
| 94 | + g.Position = n |
| 95 | + ret := values(n) |
| 96 | + return ret |
| 97 | +} |
| 98 | + |
| 99 | +func Render(s string) string { |
| 100 | + v, _ := games.Get(s) |
| 101 | + g, ok := v.(*Game) |
| 102 | + if !ok { |
| 103 | + panic("invalid game") |
| 104 | + } |
| 105 | + return values(g.Position) |
| 106 | +} |
| 107 | + |
| 108 | +func values(x Position) string { |
| 109 | + s := "" |
| 110 | + for _, val := range x.Moves { |
| 111 | + s += strconv.Itoa(int(val.N1)) + "," + strconv.Itoa(int(val.N2)) + "," + strconv.Itoa(int(val.N3)) + ";" |
| 112 | + } |
| 113 | + return s |
| 114 | +} |
0 commit comments