Search Apps Documentation Source Content File Folder Download Copy Actions Download

size_info.gno

2.52 Kb · 95 lines
 1package personal_world
 2
 3import (
 4	"chain"
 5	"strconv"
 6
 7	"gno.land/p/akkadia/v0/accesscontrol"
 8	"gno.land/r/akkadia/v0/admin"
 9)
10
11const (
12	SetSizeInfoEvent = "SetSizeInfo"
13)
14
15func init() {
16	worldConfigStore.SetSizeInfo(map[string]string{"id": "0", "size": "100", "cost": "0"})
17	worldConfigStore.SetSizeInfo(map[string]string{"id": "1", "size": "200", "cost": "20000000"})
18	worldConfigStore.SetSizeInfo(map[string]string{"id": "2", "size": "300", "cost": "30000000"})
19	worldConfigStore.SetSizeInfo(map[string]string{"id": "3", "size": "400", "cost": "40000000"})
20}
21
22// SetSizeInfo sets a single size info (admin only)
23func SetSizeInfo(cur realm, propKeys string, propValues string) {
24	assertNotFrozen()
25	accesscontrol.AssertIsAdmin(0, cur, admin.IsAdmin)
26
27	props := parsePropertiesCSV(propKeys, propValues)
28
29	sizeInfoValidator.MustValidate(props)
30	mustParseSizeID(props["id"])
31	mustParseWorldSize(props["size"])
32	mustParseSizeCost(props["cost"])
33
34	worldConfigStore.SetSizeInfo(props)
35
36	chain.Emit(
37		SetSizeInfoEvent,
38		"sizeID", props["id"],
39		"size", props["size"],
40		"cost", props["cost"],
41	)
42}
43
44func ListSizeInfos() []map[string]string {
45	assertMigrationStateAvailable()
46	return worldConfigStore.ListSizeInfos()
47}
48
49// GetWorldExpansionCost returns the cost to expand a world to the next size
50func GetWorldExpansionCost(worldID uint32) int64 {
51	assertMigrationStateAvailable()
52	world := worldStore.MustGet(worldID)
53	cost, _ := expansionCost(world["sizeId"], world["biome"])
54	return cost
55}
56
57func mustGetNextSizeInfo(sizeID string) map[string]string {
58	info, found := getNextSizeInfo(sizeID)
59	if !found {
60		currentSizeID, err := strconv.Atoi(sizeID)
61		if err != nil {
62			panic("size info not found: " + sizeID)
63		}
64		panic("size info not found: " + strconv.Itoa(currentSizeID+1))
65	}
66	return info
67}
68
69func getNextSizeInfo(sizeID string) (map[string]string, bool) {
70	currentSizeID, err := strconv.Atoi(sizeID)
71	if err != nil {
72		return nil, false
73	}
74	return worldConfigStore.GetSizeInfo(strconv.Itoa(currentSizeID + 1))
75}
76
77func expansionCost(sizeID string, biomeName string) (int64, bool) {
78	nextSizeInfo, found := getNextSizeInfo(sizeID)
79	if !found {
80		return maxInt64, false
81	}
82	baseCost, err := strconv.ParseInt(nextSizeInfo["cost"], 10, 64)
83	if err != nil {
84		return maxInt64, false
85	}
86	biomeInfo, found := worldConfigStore.GetBiomeInfo(biomeName)
87	if !found {
88		return maxInt64, false
89	}
90	priceMultiplierBPS, err := strconv.ParseInt(biomeInfo["priceMultiplierBPS"], 10, 64)
91	if err != nil {
92		return maxInt64, false
93	}
94	return calculateExpansionCost(baseCost, priceMultiplierBPS)
95}