codec.gno
3.85 Kb · 153 lines
1package arena
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/bot"
8 "gno.land/p/g1ftppd2jpfx0p8cy08th4u9whwvqcpychhlad28/game"
9)
10
11// Matches are stored as one compact string each, not as Go objects. Every
12// stored object carries its own id, hash and type information, so a Match
13// struct with an embedded Game used to cost several KB. A string costs little
14// more than its bytes. On top of that, bucketSize records share one tree node:
15// storage is paid on the bytes a transaction adds, so a new match mostly pays
16// for its own record and only every bucketSize-th match pays for a new node.
17// A Match is decoded when a transaction or page needs it and encoded again
18// when it changes.
19
20const (
21 bucketSize = 16
22 recSep = "\x1e" // record separator inside a bucket
23 sep = "\x1f" // field separator; never appears in names or reasons
24 byteOffset = 40 // coins (0-80) and moves (0-6) are shifted into printable bytes
25 codecV1 = "1"
26)
27
28func shift(raw string) string {
29 b := []byte(raw)
30 for i := range b {
31 b[i] += byteOffset
32 }
33 return string(b)
34}
35
36func unshift(enc string) string {
37 b := []byte(enc)
38 for i := range b {
39 b[i] -= byteOffset
40 }
41 return string(b)
42}
43
44func itoa(n int) string { return strconv.Itoa(n) }
45func i64(n int64) string { return strconv.FormatInt(n, 10) }
46func flag(b bool) string {
47 if b {
48 return "1"
49 }
50 return "0"
51}
52
53func (m *Match) encode() string {
54 g := &m.Game
55 return strings.Join([]string{
56 codecV1,
57 m.BotA, m.BotB, itoa(int(m.Status)),
58 itoa(g.Seed), itoa(g.Turn), itoa(g.CellA), itoa(g.CellB), itoa(g.GoldA), itoa(g.GoldB),
59 shift(g.Coins), shift(g.Moves),
60 m.Challenger.String(), i64(m.Created), flag(m.Rated),
61 itoa(m.Waiting), itoa(int(m.Held)), i64(m.Deadline),
62 itoa(m.Winner), flag(m.Forfeit), m.Reason, itoa(m.Delta), i64(m.Settled),
63 }, sep)
64}
65
66func decode(id int, s string) *Match {
67 f := strings.Split(s, sep)
68 if len(f) != 23 || f[0] != codecV1 {
69 panic("corrupt match record " + itoa(id))
70 }
71 n := func(i int) int {
72 v, err := strconv.Atoi(f[i])
73 if err != nil {
74 panic("corrupt match record " + itoa(id))
75 }
76 return v
77 }
78 n64 := func(i int) int64 { return int64(n(i)) }
79 return &Match{
80 ID: id,
81 BotA: f[1],
82 BotB: f[2],
83 Status: Status(n(3)),
84 Game: game.Game{
85 Seed: n(4), Turn: n(5), CellA: n(6), CellB: n(7), GoldA: n(8), GoldB: n(9),
86 Coins: unshift(f[10]), Moves: unshift(f[11]),
87 },
88 Challenger: address(f[12]),
89 Created: n64(13),
90 Rated: f[14] == "1",
91 Waiting: n(15),
92 Held: bot.Action(n(16)),
93 Deadline: n64(17),
94 Winner: n(18),
95 Forfeit: f[19] == "1",
96 Reason: f[20],
97 Delta: n(21),
98 Settled: n64(22),
99 }
100}
101
102func bucketKey(id int) string { return key((id - 1) / bucketSize) }
103
104func bucket(id int) []string {
105 v := matches.Get(bucketKey(id))
106 if v == nil {
107 return nil
108 }
109 return strings.Split(v.(string), recSep)
110}
111
112// record returns the stored string for match id.
113func record(id int) (string, bool) {
114 if id < 1 || id > lastID {
115 return "", false
116 }
117 recs := bucket(id)
118 i := (id - 1) % bucketSize
119 if i >= len(recs) {
120 return "", false
121 }
122 return recs[i], true
123}
124
125// save writes the match back into its bucket.
126func (m *Match) save() {
127 recs := bucket(m.ID)
128 i := (m.ID - 1) % bucketSize
129 switch {
130 case i == len(recs):
131 recs = append(recs, m.encode())
132 case i < len(recs):
133 recs[i] = m.encode()
134 default:
135 panic("match records out of order")
136 }
137 matches.Set(bucketKey(m.ID), strings.Join(recs, recSep))
138}
139
140// eachRecent calls fn on matches from newest to oldest, looking at no more
141// than maxScan of them, until fn returns true.
142func eachRecent(maxScan int, fn func(m *Match) bool) {
143 var recs []string
144 cached := -1
145 for id := lastID; id >= 1 && id > lastID-maxScan; id-- {
146 if b := (id - 1) / bucketSize; b != cached {
147 recs, cached = bucket(id), b
148 }
149 if fn(decode(id, recs[(id-1)%bucketSize])) {
150 return
151 }
152 }
153}