codec.gno
4.02 Kb · 159 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 codecV2 = "2"
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 codecV2,
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 itoa(m.Season), itoa(m.Tournament),
64 }, sep)
65}
66
67func decode(id int, s string) *Match {
68 f := strings.Split(s, sep)
69 if len(f) != 25 || f[0] != codecV2 {
70 panic("corrupt match record " + itoa(id))
71 }
72 n := func(i int) int {
73 v, err := strconv.Atoi(f[i])
74 if err != nil {
75 panic("corrupt match record " + itoa(id))
76 }
77 return v
78 }
79 n64 := func(i int) int64 { return int64(n(i)) }
80 return &Match{
81 ID: id,
82 BotA: f[1],
83 BotB: f[2],
84 Status: Status(n(3)),
85 Game: game.Game{
86 Seed: n(4), Turn: n(5), CellA: n(6), CellB: n(7), GoldA: n(8), GoldB: n(9),
87 Coins: unshift(f[10]), Moves: unshift(f[11]),
88 },
89 Challenger: address(f[12]),
90 Created: n64(13),
91 Rated: f[14] == "1",
92 Waiting: n(15),
93 Held: bot.Action(n(16)),
94 Deadline: n64(17),
95 Winner: n(18),
96 Forfeit: f[19] == "1",
97 Reason: f[20],
98 Delta: n(21),
99 Settled: n64(22),
100 Season: n(23),
101 Tournament: n(24),
102 }
103}
104
105func bucketKey(id int) string { return key((id - 1) / bucketSize) }
106
107func bucket(id int) []string {
108 v := matches.Get(bucketKey(id))
109 if v == nil {
110 return nil
111 }
112 return strings.Split(v.(string), recSep)
113}
114
115// record returns the stored string for match id.
116func record(id int) (string, bool) {
117 if id < 1 || id > lastID {
118 return "", false
119 }
120 recs := bucket(id)
121 i := (id - 1) % bucketSize
122 if i >= len(recs) {
123 return "", false
124 }
125 return recs[i], true
126}
127
128// save writes the match back into its bucket.
129func (m *Match) save() {
130 recs := bucket(m.ID)
131 i := (m.ID - 1) % bucketSize
132 switch {
133 case i == len(recs):
134 recs = append(recs, m.encode())
135 case i < len(recs):
136 recs[i] = m.encode()
137 default:
138 panic("match records out of order")
139 }
140 matches.Set(bucketKey(m.ID), strings.Join(recs, recSep))
141 if m.Status == Finished && m.Tournament != 0 {
142 mustTournament(m.Tournament).checkDone()
143 }
144}
145
146// eachRecent calls fn on matches from newest to oldest, looking at no more
147// than maxScan of them, until fn returns true.
148func eachRecent(maxScan int, fn func(m *Match) bool) {
149 var recs []string
150 cached := -1
151 for id := lastID; id >= 1 && id > lastID-maxScan; id-- {
152 if b := (id - 1) / bucketSize; b != cached {
153 recs, cached = bucket(id), b
154 }
155 if fn(decode(id, recs[(id-1)%bucketSize])) {
156 return
157 }
158 }
159}