world.gno
3.00 Kb · 129 lines
1package chunk
2
3import (
4 "chain"
5 "chain/runtime"
6 "strconv"
7
8 "gno.land/p/akkadia/v0/accesscontrol"
9 "gno.land/r/akkadia/v0/admin"
10)
11
12const (
13 CreateWorldEvent = "CreateWorld"
14 UpdateWorldEvent = "UpdateWorld"
15)
16
17func CreateWorld(cur realm, worldID uint32, propKeys string, propValues string) {
18 assertNotFrozen()
19 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
20
21 assertWorldID(worldID)
22 props := parsePropertiesCSV(propKeys, propValues)
23 worldCreateValidator.MustValidate(props)
24
25 biomeName := props["biome"]
26 name := props["name"]
27 slug := props["slug"]
28 mustParseWorldSeed(props["seed"])
29
30 assertBiomeName(biomeName)
31 assertWorldName(name)
32 assertWorldSlug(slug)
33 if worldStore.Has(worldID) {
34 panic("world already exists: " + formatWorldID(worldID))
35 }
36 if !worldStore.IsNameAvailable(name) {
37 panic("name already exists: " + name)
38 }
39 if !worldStore.IsSlugAvailable(slug) {
40 panic("slug already exists: " + slug)
41 }
42
43 heightStr := strconv.FormatInt(runtime.ChainHeight(), 10)
44 props["createdAt"] = heightStr
45 props["updatedAt"] = heightStr
46
47 worldStore.Create(worldID, props)
48 idStr := formatWorldID(worldID)
49
50 chain.Emit(
51 CreateWorldEvent,
52 "id", idStr,
53 "biome", props["biome"],
54 "name", props["name"],
55 "slug", props["slug"],
56 "seed", props["seed"],
57 )
58}
59
60func UpdateWorld(cur realm, worldID uint32, propKeys string, propValues string) {
61 assertNotFrozen()
62 accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
63
64 assertWorldID(worldID)
65 props := parsePropertiesCSV(propKeys, propValues)
66 worldUpdateValidator.MustValidate(props)
67
68 world := worldStore.MustGet(worldID)
69
70 if biomeName, found := props["biome"]; found {
71 assertBiomeName(biomeName)
72 }
73 if name, found := props["name"]; found && name != world["name"] {
74 assertWorldName(name)
75 if !worldStore.IsNameAvailable(name) {
76 panic("name already exists: " + name)
77 }
78 }
79 if slug, found := props["slug"]; found && slug != world["slug"] {
80 assertWorldSlug(slug)
81 if !worldStore.IsSlugAvailable(slug) {
82 panic("slug already exists: " + slug)
83 }
84 }
85
86 props["updatedAt"] = strconv.FormatInt(runtime.ChainHeight(), 10)
87 updatedWorld := worldStore.Update(worldID, props)
88
89 chain.Emit(
90 UpdateWorldEvent,
91 "id", updatedWorld["id"],
92 "biome", updatedWorld["biome"],
93 "name", updatedWorld["name"],
94 "slug", updatedWorld["slug"],
95 )
96}
97
98func GetWorld(worldID uint32) map[string]string {
99 assertMigrationStateAvailable()
100 return worldStore.MustGet(worldID)
101}
102
103func GetWorldBySlug(slug string) map[string]string {
104 assertMigrationStateAvailable()
105 return worldStore.MustGetBySlug(slug)
106}
107
108func GetTotalWorldSize() int {
109 assertMigrationStateAvailable()
110 return worldStore.Total()
111}
112
113func ListWorlds(page, count int) []map[string]string {
114 assertMigrationStateAvailable()
115 assertListPageCount(page, count)
116
117 return worldStore.List(page, count)
118}
119
120func ListWorldsByIDs(worldIDs ...uint32) []map[string]string {
121 assertMigrationStateAvailable()
122 assertListLimit("worldIDs", len(worldIDs))
123
124 if len(worldIDs) == 0 {
125 return []map[string]string{}
126 }
127
128 return worldStore.ListByIDs(worldIDs...)
129}