size_info.gno
2.63 Kb ยท 103 lines
1package personal_world
2
3import (
4 "chain"
5 "chain/runtime"
6 "strconv"
7
8 "gno.land/p/nt/avl"
9 "gno.land/p/nt/ufmt"
10)
11
12const (
13 SetSizeInfoEvent = "SetSizeInfo"
14)
15
16var (
17 sizeInfos avl.Tree // sizeID -> map[string]string
18)
19
20func init() {
21 // Initialize SizeInfo as map[string]string
22 sizeInfos.Set("0", map[string]string{"id": "0", "size": "100", "cost": "0"})
23 sizeInfos.Set("1", map[string]string{"id": "1", "size": "200", "cost": "20000000"})
24 sizeInfos.Set("2", map[string]string{"id": "2", "size": "300", "cost": "30000000"})
25 sizeInfos.Set("3", map[string]string{"id": "3", "size": "400", "cost": "40000000"})
26}
27
28// SetSizeInfo sets a single size info (admin only)
29func SetSizeInfo(cur realm, sizeID int, size int, cost int64, option string) {
30 caller := runtime.PreviousRealm().Address()
31 assertIsAdmin(caller)
32
33 // Validate inputs
34 if sizeID < 0 {
35 panic("sizeID must be non-negative")
36 }
37 if size < 0 {
38 panic("size must be non-negative")
39 }
40 if cost < 0 {
41 panic("cost must be non-negative")
42 }
43
44 // Create size info
45 key := ufmt.Sprintf("%d", sizeID)
46 sizeInfo := map[string]string{
47 "id": ufmt.Sprintf("%d", sizeID),
48 "size": ufmt.Sprintf("%d", size),
49 "cost": ufmt.Sprintf("%d", cost),
50 "option": option,
51 }
52
53 sizeInfos.Set(key, sizeInfo)
54
55 chain.Emit(
56 SetSizeInfoEvent,
57 "admin", caller.String(),
58 "sizeID", ufmt.Sprintf("%d", sizeID),
59 "size", ufmt.Sprintf("%d", size),
60 "cost", ufmt.Sprintf("%d", cost),
61 )
62}
63
64func ListSizeInfos() []map[string]string {
65 var result []map[string]string
66 sizeInfos.Iterate("", "", func(key string, value interface{}) bool {
67 sizeInfo := value.(map[string]string)
68 result = append(result, sizeInfo)
69 return false
70 })
71 return result
72}
73
74// mustGetSizeInfo gets size info with panic on not found
75func mustGetSizeInfo(sizeID int) map[string]string {
76 sizeInfo, found := sizeInfos.Get(ufmt.Sprintf("%d", sizeID))
77 if !found {
78 panic("size info not found: " + ufmt.Sprintf("%d", sizeID))
79 }
80 return sizeInfo.(map[string]string)
81}
82
83// GetWorldExpansionCost returns the cost to expand a world to the next size
84func GetWorldExpansionCost(worldID uint32) int64 {
85 world := mustGetWorld(worldID)
86
87 // Get current size ID and world biome
88 currentSizeID, _ := strconv.Atoi(world["sizeId"])
89
90 // Get next size info
91 nextSizeID := currentSizeID + 1
92 nextSizeInfo := mustGetSizeInfo(nextSizeID)
93 baseCost, _ := strconv.ParseInt(nextSizeInfo["cost"], 10, 64)
94
95 // Get world type info for multiplier
96 biomeInfo := mustGetBiomeInfo(world["biome"])
97 multiple, _ := strconv.ParseFloat(biomeInfo["priceMultiplier"], 64)
98
99 // Calculate expansion cost with multiplier
100 expansionCost := int64(float64(baseCost) * multiple)
101
102 return expansionCost
103}