migration_research_store.gno
4.28 Kb · 167 lines
1package blueprint
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v0/v0/validate"
8 oldbp "gno.land/r/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v0/v0/blueprint"
9)
10
11type BlueprintStore = oldbp.BlueprintStore
12type VerifierStore = oldbp.VerifierStore
13
14const (
15 MigrationResearchExpectedBiome = "everdawn_plains"
16 MigrationResearchExpectedState = "draft"
17 MigrationResearchExpectedMetadata = `{"source":"migration-research"}`
18 MigrationResearchExpectedChunkKey = "0:0_0"
19 MigrationResearchExpectedVerifier = "migration_research_verifier"
20 MigrationResearchExpectedNamePrefix = "MRBP_"
21)
22
23var (
24 blueprintStore *BlueprintStore
25 verifierStore *VerifierStore
26 blueprintCreateValidator *validate.Validator
27 blueprintUpdateValidator *validate.Validator
28 creationCost int64
29 listLimit int
30 batchLimit int
31 frozen bool
32)
33
34func CreateBlueprint(cur realm, propKeys string, propValues string) uint32 {
35 props := migrationResearchProperties(propKeys, propValues)
36 blueprintCreateValidator.MustValidate(props)
37 id := blueprintStore.NextID()
38 props["id"] = formatBlueprintID(id)
39 props["owner"] = cur.Previous().Address().String()
40 props["createdAt"] = defaultString(props["createdAt"], "0")
41 props["updatedAt"] = defaultString(props["updatedAt"], "0")
42 return blueprintStore.Create(props)
43}
44
45func UpdateBlueprint(cur realm, blueprintID uint32, propKeys string, propValues string) {
46 current := copyStringMap(blueprintStore.MustGet(blueprintID))
47 props := migrationResearchProperties(propKeys, propValues)
48 for key, value := range props {
49 current[key] = value
50 }
51 current["updatedAt"] = "0"
52 blueprintUpdateValidator.MustValidate(props)
53 blueprintStore.Update(blueprintID, current)
54}
55
56func DeleteBlueprint(cur realm, blueprintID uint32) {
57 blueprintStore.Delete(blueprintID)
58 verifierStore.Delete(blueprintID)
59}
60
61func SetBlueprintMetadata(cur realm, blueprintID uint32, metadata string) {
62 blueprintStore.SetMetadata(blueprintID, metadata)
63}
64
65func GetBlueprintMetadata(blueprintID uint32) string {
66 return blueprintStore.GetMetadata(blueprintID)
67}
68
69func GetBlueprint(blueprintID uint32) map[string]string {
70 return blueprintStore.MustGet(blueprintID)
71}
72
73func GetBlueprintIDByName(name string) uint32 {
74 return blueprintStore.MustGetIDByName(name)
75}
76
77func GetTotalBlueprintSize() int {
78 return blueprintStore.Total()
79}
80
81func IsNameAvailable(name string) bool {
82 return blueprintStore.IsNameAvailable(name)
83}
84
85func GetBlueprintSizeByOwner(owner address) int {
86 return blueprintStore.OwnerCount(owner)
87}
88
89func GetBlueprintSizeByBiome(biomeName string) int {
90 return blueprintStore.BiomeCount(biomeName)
91}
92
93func GetBlueprintSizeByBiomeState(biomeName string, state string) int {
94 return blueprintStore.BiomeStateCount(biomeName, state)
95}
96
97func SetChunkVerifier(cur realm, blueprintID uint32, chunkKey string, verifier string) {
98 blueprintStore.AssertBlueprintExists(blueprintID)
99 verifierStore.Set(blueprintID, chunkKey, verifier)
100}
101
102func GetChunkVerifier(blueprintID uint32, chunkKey string) string {
103 verifier, found := verifierStore.Get(blueprintID, chunkKey)
104 if !found {
105 return ""
106 }
107 return verifier
108}
109
110func Unfreeze(cur realm) {
111 frozen = false
112}
113
114func IsFrozen() bool {
115 return frozen
116}
117
118func GetCreationCost() int64 {
119 return creationCost
120}
121
122func GetListLimit() int {
123 return listLimit
124}
125
126func GetBatchLimit() int {
127 return batchLimit
128}
129
130func formatBlueprintID(blueprintID uint32) string {
131 return strconv.FormatUint(uint64(blueprintID), 10)
132}
133
134func migrationResearchProperties(keys string, values string) map[string]string {
135 if keys == "" {
136 panic("properties keys must be not empty")
137 }
138 keyParts := strings.Split(keys, ",")
139 valueParts := strings.Split(values, ",")
140 if len(keyParts) != len(valueParts) {
141 panic("properties keys and values count mismatch")
142 }
143
144 props := map[string]string{}
145 for i, key := range keyParts {
146 if key == "" {
147 panic("properties key must be not empty")
148 }
149 props[key] = valueParts[i]
150 }
151 return props
152}
153
154func defaultString(value string, fallback string) string {
155 if value == "" {
156 return fallback
157 }
158 return value
159}
160
161func copyStringMap(source map[string]string) map[string]string {
162 result := make(map[string]string)
163 for key, value := range source {
164 result[key] = value
165 }
166 return result
167}