Search Apps Documentation Source Content File Folder Download Copy Actions Download State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

store.gno

5.37 Kb · 185 lines
  1package game_vault
  2
  3import (
  4	"strconv"
  5
  6	"gno.land/p/nt/bptree/v0"
  7)
  8
  9var (
 10	rewardPools                      *bptree.BPTree
 11	rewardPoolIDs                    *bptree.BPTree
 12	gameWorldRewardPoolIDs           *bptree.BPTree
 13	participationProcesses           *bptree.BPTree
 14	participationProcessIDs          *bptree.BPTree
 15	gameWorldParticipationProcessIDs *bptree.BPTree
 16	participationFunds               *bptree.BPTree
 17	participationFundIDs             *bptree.BPTree
 18	participationOwnerFundIDs        *bptree.BPTree
 19	participationGroupFundIDs        *bptree.BPTree
 20	entryProcesses                   *bptree.BPTree
 21	entryProcessIDs                  *bptree.BPTree
 22	gameWorldEntryProcessIDs         *bptree.BPTree
 23	entryFunds                       *bptree.BPTree
 24	entryFundIDs                     *bptree.BPTree
 25	entryOwnerFundIDs                *bptree.BPTree
 26	entryGroupFundIDs                *bptree.BPTree
 27	systemFundings                   *bptree.BPTree
 28	systemFundingIDs                 *bptree.BPTree
 29	poolSystemFundingIDs             *bptree.BPTree
 30	rewardDistributions              *bptree.BPTree
 31	rewardDistributionIDs            *bptree.BPTree
 32	poolDistributionIDs              *bptree.BPTree
 33	vaultRefunds                     *bptree.BPTree
 34	vaultRefundIDs                   *bptree.BPTree
 35	poolRefundIDs                    *bptree.BPTree
 36	processedOperationKeys           *bptree.BPTree
 37	latestRewardPoolID               uint64
 38	latestParticipationProcessID     uint64
 39	latestParticipationFundID        uint64
 40	latestEntryProcessID             uint64
 41	latestEntryFundID                uint64
 42	latestSystemFundingID            uint64
 43	latestRewardDistributionID       uint64
 44	latestVaultRefundID              uint64
 45	accountedUgnotTotal              int64
 46)
 47
 48func init() {
 49	resetGameVaultState()
 50}
 51
 52func resetGameVaultState() {
 53	rewardPools = bptree.NewBPTree32()
 54	rewardPoolIDs = bptree.NewBPTree32()
 55	gameWorldRewardPoolIDs = bptree.NewBPTree32()
 56	participationProcesses = bptree.NewBPTree32()
 57	participationProcessIDs = bptree.NewBPTree32()
 58	gameWorldParticipationProcessIDs = bptree.NewBPTree32()
 59	participationFunds = bptree.NewBPTree32()
 60	participationFundIDs = bptree.NewBPTree32()
 61	participationOwnerFundIDs = bptree.NewBPTree32()
 62	participationGroupFundIDs = bptree.NewBPTree32()
 63	entryProcesses = bptree.NewBPTree32()
 64	entryProcessIDs = bptree.NewBPTree32()
 65	gameWorldEntryProcessIDs = bptree.NewBPTree32()
 66	entryFunds = bptree.NewBPTree32()
 67	entryFundIDs = bptree.NewBPTree32()
 68	entryOwnerFundIDs = bptree.NewBPTree32()
 69	entryGroupFundIDs = bptree.NewBPTree32()
 70	systemFundings = bptree.NewBPTree32()
 71	systemFundingIDs = bptree.NewBPTree32()
 72	poolSystemFundingIDs = bptree.NewBPTree32()
 73	rewardDistributions = bptree.NewBPTree32()
 74	rewardDistributionIDs = bptree.NewBPTree32()
 75	poolDistributionIDs = bptree.NewBPTree32()
 76	vaultRefunds = bptree.NewBPTree32()
 77	vaultRefundIDs = bptree.NewBPTree32()
 78	poolRefundIDs = bptree.NewBPTree32()
 79	processedOperationKeys = bptree.NewBPTree32()
 80	latestRewardPoolID = 0
 81	latestParticipationProcessID = 0
 82	latestParticipationFundID = 0
 83	latestEntryProcessID = 0
 84	latestEntryFundID = 0
 85	latestSystemFundingID = 0
 86	latestRewardDistributionID = 0
 87	latestVaultRefundID = 0
 88	accountedUgnotTotal = 0
 89	distributionBatchMax = distributionBatchMaxDefault
 90}
 91
 92func ownerKey(processID uint64, owner address) string {
 93	return strconv.FormatUint(processID, 10) + ":" + owner.String()
 94}
 95
 96func keyedOwnerKey(processID uint64, owner address, key string) string {
 97	return ownerKey(processID, owner) + ":" + key
 98}
 99
100func uint64Key(id uint64) string {
101	// Zero-pad so BPTree key order matches numeric ID order.
102	s := strconv.FormatUint(id, 10)
103	if len(s) >= 20 {
104		return s
105	}
106	return "00000000000000000000"[len(s):] + s
107}
108
109func parseUint64Key(key string) uint64 {
110	id, err := strconv.ParseUint(key, 10, 64)
111	if err != nil {
112		panic("invalid uint64 key: " + key)
113	}
114	return id
115}
116
117func treeGet(store *bptree.BPTree, id uint64) (any, bool) {
118	key := uint64Key(id)
119	if !store.Has(key) {
120		return nil, false
121	}
122	return store.Get(key), true
123}
124
125func treeSet(store *bptree.BPTree, id uint64, value any) {
126	store.Set(uint64Key(id), value)
127}
128
129func stringGet(store *bptree.BPTree, key string) (any, bool) {
130	if !store.Has(key) {
131		return nil, false
132	}
133	return store.Get(key), true
134}
135
136func addStringSetID(store *bptree.BPTree, key string, id uint64) {
137	value := store.Get(key)
138	var ids *bptree.BPTree
139	if value != nil {
140		ids = value.(*bptree.BPTree)
141	} else {
142		ids = bptree.NewBPTree32()
143		store.Set(key, ids)
144	}
145	ids.Set(uint64Key(id), true)
146}
147
148func getStringSet(store *bptree.BPTree, key string) (*bptree.BPTree, bool) {
149	value := store.Get(key)
150	if value == nil {
151		return nil, false
152	}
153	return value.(*bptree.BPTree), true
154}
155
156func getOrCreateIDSet(store *bptree.BPTree, key uint64) *bptree.BPTree {
157	value := store.Get(uint64Key(key))
158	if value != nil {
159		return value.(*bptree.BPTree)
160	}
161	ids := bptree.NewBPTree32()
162	store.Set(uint64Key(key), ids)
163	return ids
164}
165
166func getIDSet(store *bptree.BPTree, key uint64) (*bptree.BPTree, bool) {
167	value := store.Get(uint64Key(key))
168	if value == nil {
169		return nil, false
170	}
171	return value.(*bptree.BPTree), true
172}
173
174func listIDs(ids *bptree.BPTree, page, count int) []uint64 {
175	assertPageCount(page, count)
176	result := []uint64{}
177	if ids == nil {
178		return result
179	}
180	ids.ReverseIterateByOffset((page-1)*count, count, func(key string, _ any) bool {
181		result = append(result, parseUint64Key(key))
182		return false
183	})
184	return result
185}