1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
| type PurchaseItemPayload struct {
ItemName string `json:"itemName"`
}
func RpcPurchaseItem(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
// Get the user ID
userId, ok := ctx.Value(runtime.RUNTIME_CTX_USER_ID).(string)
if !ok {
logger.Error("no user id found")
return "", runtime.NewError("no user id found", 16)
}
// Unmarshal the payload
request := &PurchaseItemPayload{}
if err := json.Unmarshal([]byte(payload), request); err != nil {
logger.Error("error unmarshaling payload", err)
return "", runtime.NewError("purchase item payload invalid", 3)
}
// Make sure the user specified an item to buy
if request.ItemName == "" {
logger.Warn("no item name specified")
return "", runtime.NewError("no item name specified", 3)
}
// Lookup the item prices
readRequest := &runtime.StorageRead{
Collection: "configuration",
Key: "prices",
UserID: "00000000-0000-0000-0000-000000000000",
}
readResult, err := nk.StorageRead(ctx, []*runtime.StorageRead{readRequest})
if err != nil {
logger.Error("error reading item prices from storage", err)
return "", runtime.NewError("error reading item prices from storage", 13)
}
if len(readResult) == 0 {
logger.Warn("no item prices in storage")
return "", runtime.NewError("no item prices in storage", 13)
}
// Check if there is a price for the requested item
var prices map[string]int
if err := json.Unmarshal([]byte(readResult[0].Value), &prices); err != nil {
logger.Error("error unmarshaling prices", err)
return "", runtime.NewError("error unmarshaling prices", 13)
}
if _, ok := prices[request.ItemName]; !ok {
logger.Warn("no price available for %s", request.ItemName)
return "", runtime.NewError(fmt.Sprintf("no price available for %s", request.ItemName), 5)
}
// Check that the player has enough coins to spend
account, err := nk.AccountGetId(ctx, userId)
if err != nil {
logger.Error("error getting account data", err)
return "", runtime.NewError("error getting account data", 13)
}
var wallet map[string]int
if err := json.Unmarshal([]byte(account.Wallet), &wallet); err != nil {
logger.Error("error unmarshaling wallet", err)
return "", runtime.NewError("error unmarshaling wallet", 13)
}
if wallet["coins"] < prices[request.ItemName] {
logger.Warn("not enough coins to purchase item")
return "", runtime.NewError("not enough coins to purchase item", 9)
}
// Decrease the player's coins
_, _, err = nk.WalletUpdate(ctx, userId, map[string]int64{"coins": int64(-prices[request.ItemName])}, nil, true)
if err != nil {
logger.Error("unable to update wallet", err)
return "", runtime.NewError("unable to update wallet", 13)
}
// Get the player's current inventory
var inventory map[string]int
readRequest = &runtime.StorageRead{
Collection: "economy",
Key: "inventory",
UserID: userId,
}
readResult, err = nk.StorageRead(ctx, []*runtime.StorageRead{readRequest})
if err != nil {
logger.Error("error reading inventory from storage", err)
return "", runtime.NewError("error reading inventory from storage", 13)
}
if len(readResult) > 0 {
if err := json.Unmarshal([]byte(readResult[0].Value), &inventory); err != nil {
logger.Error("error unmarshaling inventory", err)
return "", runtime.NewError("error unmarshaling inventory", 13)
}
} else {
inventory = make(map[string]int)
}
// Give the player the item (either increase quantity if they already possessed it or add one)
if _, ok := inventory[request.ItemName]; ok {
inventory[request.ItemName] += 1
} else {
inventory[request.ItemName] = 1
}
// Write the updated inventory to storage
inventoryJson, err := json.Marshal(inventory)
if err != nil {
logger.Error("error marshaling inventory")
return "", runtime.NewError("error marshaling inventory", 13)
}
writeRequest := &runtime.StorageWrite{
Collection: "economy",
Key: "inventory",
UserID: userId,
PermissionRead: 1,
PermissionWrite: 1,
Value: string(inventoryJson),
}
// Return an error if the write does not succeed
storageWriteAck, err := nk.StorageWrite(ctx, []*runtime.StorageWrite{writeRequest})
if err != nil || len(storageWriteAck) == 0 {
logger.Error("error saving inventory")
return "", runtime.NewError("error saving inventory", 13)
}
return "{}", nil
}
|