migration_research_store.gno
6.66 Kb · 238 lines
1package personal_world
2
3import (
4 "strconv"
5 "strings"
6
7 "gno.land/p/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v0/v0/rbac"
8 oldpw "gno.land/r/g1nqnrt3aldzhu6zzeg75yw97wvavqy7wr77g56q/deploy-test/v0/v0/personal_world"
9)
10
11type WorldStore = oldpw.WorldStore
12type WorldConfigStore = oldpw.WorldConfigStore
13type VerifierStore = oldpw.VerifierStore
14
15const (
16 MigrationResearchExpectedBiome = "migration_research_biome"
17 MigrationResearchExpectedBiomeName = "Migration Research Biome"
18 MigrationResearchExpectedRole = "migration_research_role"
19 MigrationResearchExpectedRoleLabel = "Migration Research Role"
20 MigrationResearchExpectedPermission = "migration:research"
21 MigrationResearchExpectedPermissionDesc = "Migration Research Permission"
22 MigrationResearchExpectedGrantableRole = "editor"
23)
24
25var MigrationResearchExpectedGrantee = address("g1x8l886kqzdlqt8v0znrj89mhas0h242caseqe9")
26
27var (
28 worldStore *WorldStore
29 worldConfigStore *WorldConfigStore
30 verifierStore *VerifierStore
31 feeCollectorBPS int
32 listLimit int
33 batchLimit int
34 frozen bool
35 personalWorldAuthzRBAC *rbac.RBAC
36)
37
38func IsFrozen() bool {
39 return frozen
40}
41
42func Unfreeze(cur realm) {
43 frozen = false
44}
45
46func GetFeeCollectorBPS() int {
47 return feeCollectorBPS
48}
49
50func GetDefaultBiome() string {
51 return worldConfigStore.DefaultBiome()
52}
53
54func GetBiomeInfo(biomeName string) map[string]string {
55 return worldConfigStore.MustGetBiomeInfo(biomeName)
56}
57
58func GetWorldExpansionCost(worldID uint32) int64 {
59 world := worldStore.MustGet(worldID)
60 cost, _ := worldConfigStore.ExpansionCost(world["sizeId"], world["biome"])
61 return cost
62}
63
64func GetTotalWorldSize() int {
65 return worldStore.Total()
66}
67
68func GetWorld(worldID uint32) map[string]string {
69 return worldStore.MustGet(worldID)
70}
71
72func GetWorldIDBySlug(slug string) uint32 {
73 return worldStore.MustGetIDBySlug(slug)
74}
75
76func GetWorldIDByName(name string) uint32 {
77 return worldStore.MustGetIDByName(name)
78}
79
80func GetWorldBySlug(slug string) map[string]string {
81 return worldStore.MustGet(worldStore.MustGetIDBySlug(slug))
82}
83
84func GetWorldSizeByOwner(owner address) int {
85 return worldStore.OwnerCount(owner)
86}
87
88func IsSlugAvailable(slug string) bool {
89 return worldStore.IsSlugAvailable(slug)
90}
91
92func CreateWorld(cur realm, propKeys string, propValues string) uint32 {
93 props := migrationResearchProperties(propKeys, propValues)
94 id := worldStore.NextID()
95 props["id"] = formatWorldID(id)
96 props["owner"] = cur.Previous().Address().String()
97 props["sizeId"] = defaultString(props["sizeId"], "0")
98 props["size"] = defaultString(props["size"], worldConfigStore.MustGetSizeInfo("0")["size"])
99 props["isVisible"] = defaultString(props["isVisible"], "true")
100 props["createdAt"] = defaultString(props["createdAt"], "0")
101 props["updatedAt"] = defaultString(props["updatedAt"], "0")
102 props["totalPaid"] = defaultString(props["totalPaid"], "0")
103 return worldStore.Create(props)
104}
105
106func UpdateWorld(cur realm, worldID uint32, propKeys string, propValues string) {
107 world := copyStringMap(worldStore.MustGet(worldID))
108 props := migrationResearchProperties(propKeys, propValues)
109 for key, value := range props {
110 world[key] = value
111 }
112 world["updatedAt"] = "0"
113 worldStore.Update(worldID, world)
114}
115
116func ExpandWorld(cur realm, worldID uint32) {
117 world := copyStringMap(worldStore.MustGet(worldID))
118 nextSizeInfo := worldConfigStore.MustGetNextSizeInfo(world["sizeId"])
119 world["sizeId"] = nextSizeInfo["id"]
120 world["size"] = nextSizeInfo["size"]
121 world["updatedAt"] = "0"
122 worldStore.Update(worldID, world)
123}
124
125func DeleteWorld(cur realm, worldID uint32) {
126 worldStore.Delete(worldID)
127 verifierStore.Delete(worldID)
128 personalWorldAuthzRBAC.DeleteEntity(formatWorldID(worldID))
129}
130
131func GetPermission(name string) map[string]string {
132 if !personalWorldAuthzRBAC.HasPermission(name) {
133 return nil
134 }
135 permission := personalWorldAuthzRBAC.GetPermission(name)
136 return map[string]string{
137 "name": permission.Name,
138 "description": permission.Description,
139 }
140}
141
142func GetRoleInfo(roleName string) map[string]string {
143 if !personalWorldAuthzRBAC.HasRole(roleName) {
144 return nil
145 }
146 role := personalWorldAuthzRBAC.GetRole(roleName)
147 info := copyStringMap(role.Metadata)
148 info["name"] = role.Name
149 return info
150}
151
152func ListGrantables(granterRole string, page int, count int) []string {
153 if !personalWorldAuthzRBAC.HasRole(granterRole) {
154 return []string{}
155 }
156 return personalWorldAuthzRBAC.ListGrantableRoles(granterRole, page, count)
157}
158
159func GrantRole(cur realm, worldID uint32, roleName string, user address) {
160 worldStore.AssertWorldExists(worldID)
161 personalWorldAuthzRBAC.AssignRole(formatWorldID(worldID), user, roleName)
162}
163
164func RevokeRole(cur realm, worldID uint32, roleName string, user address) {
165 worldStore.AssertWorldExists(worldID)
166 personalWorldAuthzRBAC.UnassignRole(formatWorldID(worldID), user, roleName)
167}
168
169func HasRole(worldID uint32, user address, roleName string) bool {
170 if !personalWorldAuthzRBAC.HasRole(roleName) {
171 return false
172 }
173 return personalWorldAuthzRBAC.HasUserRole(formatWorldID(worldID), user, roleName)
174}
175
176func ListRoleGrants(worldID uint32, roleName string, page int, count int) []address {
177 if !personalWorldAuthzRBAC.HasRole(roleName) {
178 return []address{}
179 }
180 return personalWorldAuthzRBAC.ListRoleUsers(formatWorldID(worldID), roleName, page, count)
181}
182
183func ListWorldIDsByUserRole(user address, roleName string, page int, count int) []uint32 {
184 if !personalWorldAuthzRBAC.HasRole(roleName) {
185 return []uint32{}
186 }
187 result := []uint32{}
188 worldIDs := personalWorldAuthzRBAC.ListEntitiesByUserRole(user, roleName, page, count)
189 for _, worldKey := range worldIDs {
190 worldID, err := strconv.ParseUint(worldKey, 10, 32)
191 if err != nil {
192 panic("invalid worldID: " + worldKey)
193 }
194 result = append(result, uint32(worldID))
195 }
196 return result
197}
198
199func formatWorldID(worldID uint32) string {
200 return strconv.FormatUint(uint64(worldID), 10)
201}
202
203func migrationResearchProperties(keys string, values string) map[string]string {
204 if keys == "" {
205 panic("properties keys must be not empty")
206 }
207 keyList := strings.Split(keys, ",")
208 valueList := strings.Split(values, ",")
209 if len(keyList) != len(valueList) {
210 panic("properties keys and values count mismatch")
211 }
212 result := map[string]string{}
213 for i, key := range keyList {
214 if key == "" {
215 panic("properties key must be not empty")
216 }
217 result[key] = valueList[i]
218 }
219 return result
220}
221
222func defaultString(value string, fallback string) string {
223 if value == "" {
224 return fallback
225 }
226 return value
227}
228
229func copyStringMap(source map[string]string) map[string]string {
230 if source == nil {
231 return nil
232 }
233 result := map[string]string{}
234 for key, value := range source {
235 result[key] = value
236 }
237 return result
238}