store.gno
1.48 Kb · 66 lines
1package game_world
2
3import (
4 "strconv"
5
6 "gno.land/p/nt/bptree/v0"
7)
8
9var (
10 gameWorlds *bptree.BPTree = bptree.NewBPTree32()
11 ownerGameWorldIDs *bptree.BPTree = bptree.NewBPTree32()
12 restrictedGameWorldIDs *bptree.BPTree = bptree.NewBPTree32()
13 latestGameWorldID uint64
14)
15
16func addOwnerGameWorldID(owner address, gameWorldID uint64) {
17 value := ownerGameWorldIDs.Get(owner.String())
18 var ids *bptree.BPTree
19 if value != nil {
20 ids = value.(*bptree.BPTree)
21 } else {
22 ids = bptree.NewBPTree32()
23 ownerGameWorldIDs.Set(owner.String(), ids)
24 }
25 ids.Set(uint64Key(gameWorldID), true)
26}
27
28func removeOwnerGameWorldID(owner address, gameWorldID uint64) {
29 ids, found := getOwnerGameWorldIDs(owner)
30 if !found {
31 return
32 }
33 ids.Remove(uint64Key(gameWorldID))
34}
35
36func getOwnerGameWorldIDs(owner address) (*bptree.BPTree, bool) {
37 value := ownerGameWorldIDs.Get(owner.String())
38 if value == nil {
39 return nil, false
40 }
41 return value.(*bptree.BPTree), true
42}
43
44func resetGameWorldStore() {
45 gameWorlds = bptree.NewBPTree32()
46 ownerGameWorldIDs = bptree.NewBPTree32()
47 restrictedGameWorldIDs = bptree.NewBPTree32()
48 latestGameWorldID = 0
49}
50
51func uint64Key(id uint64) string {
52 // Zero-pad so BPTree key order matches numeric ID order.
53 s := strconv.FormatUint(id, 10)
54 if len(s) >= 20 {
55 return s
56 }
57 return "00000000000000000000"[len(s):] + s
58}
59
60func parseUint64Key(key string) uint64 {
61 id, err := strconv.ParseUint(key, 10, 64)
62 if err != nil {
63 panic("invalid uint64 key: " + key)
64 }
65 return id
66}