store.gno
3.31 Kb · 141 lines
1package arena
2
3import (
4 "encoding/binary"
5
6 "gno.land/p/nym-gfanton001/gnomepit/engine/v0"
7 "gno.land/p/nt/avl/v0"
8)
9
10var (
11 chunks avl.Tree // 4-byte chunk number → chunk string
12 players avl.Tree // address → player string
13 openList string
14)
15
16func chunkKey(id int) string {
17 var k [4]byte
18 binary.BigEndian.PutUint32(k[:], uint32((id-1)/chunkSize))
19 return string(k[:])
20}
21
22// loadMatch returns match id, or false when it does not exist or was removed.
23func loadMatch(rules *engine.Ruleset, id int) (record, bool) {
24 if id < 1 || id > lastID {
25 return record{}, false
26 }
27 rec := chunkRecord(stringAt(&chunks, chunkKey(id)), (id-1)%chunkSize)
28 if rec == "" {
29 return record{}, false
30 }
31 return decodeMatch(rules, rec), true
32}
33
34func saveMatch(rules *engine.Ruleset, id int, m record) {
35 putRecord(id, encodeMatch(rules, m))
36}
37
38// dropMatch removes match id; an absent id is left alone.
39func dropMatch(id int) {
40 if id < 1 {
41 return
42 }
43 if chunkRecord(stringAt(&chunks, chunkKey(id)), (id-1)%chunkSize) != "" {
44 putRecord(id, "")
45 }
46}
47
48func putRecord(id int, rec string) {
49 chunks.Set(chunkKey(id), withChunkRecord(stringAt(&chunks, chunkKey(id)), (id-1)%chunkSize, rec))
50}
51
52// stringAt returns the string under key, "" when there is none.
53func stringAt(t *avl.Tree, key string) string {
54 v := t.Get(key)
55 if v == nil {
56 return ""
57 }
58 return v.(string)
59}
60
61func loadPlayer(a address) playerRecord {
62 return decodePlayer(stringAt(&players, string(a)))
63}
64
65func savePlayer(a address, p playerRecord) {
66 players.Set(string(a), encodePlayer(p))
67}
68
69// add appends id unless the player already has it.
70func (p *playerRecord) add(id int) {
71 for _, have := range p.ids {
72 if have == id {
73 return
74 }
75 }
76 p.ids = append(p.ids, id)
77}
78
79func (p *playerRecord) remove(id int) {
80 for i, have := range p.ids {
81 if have == id {
82 p.ids = append(p.ids[:i:i], p.ids[i+1:]...)
83 return
84 }
85 }
86}
87
88// newest returns up to limit ids, newest first, after skipping offset of them.
89func (p playerRecord) newest(offset, limit int) []int {
90 var out []int
91 for i := len(p.ids) - 1 - offset; i >= 0 && len(out) < limit; i-- {
92 out = append(out, p.ids[i])
93 }
94 return out
95}
96
97// ceiling: each change rewrites the whole open list, 8 bytes per open challenge, so its gas
98// grows with the board; an avl index keyed by expiry and id if the board holds thousands.
99func openAdd(id int, expires int64) {
100 openList += encodeOpen([]openEntry{{id: id, expires: expires}})
101}
102
103func openRemove(id int) {
104 entries := openEntries(openList)
105 for i, e := range entries {
106 if e.id == id {
107 openList = encodeOpen(append(entries[:i:i], entries[i+1:]...))
108 return
109 }
110 }
111}
112
113// openNewest returns up to limit unexpired ids, newest first, after skipping offset of them.
114func openNewest(now int64, offset, limit int) []int {
115 entries := openEntries(openList)
116 var out []int
117 for i := len(entries) - 1; i >= 0 && entries[i].expires >= now; i-- {
118 if offset > 0 {
119 offset--
120 continue
121 }
122 if len(out) == limit {
123 break
124 }
125 out = append(out, entries[i].id)
126 }
127 return out
128}
129
130// openClear removes up to max expired entries from the front and returns their ids.
131func openClear(now int64, max int) []int {
132 entries := openEntries(openList)
133 n := 0
134 var ids []int
135 for n < len(entries) && n < max && entries[n].expires < now {
136 ids = append(ids, entries[n].id)
137 n++
138 }
139 openList = encodeOpen(entries[n:])
140 return ids
141}