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
| const (
configCollection = "configuration"
configKey = "rc"
)
func SaveConfig(ctx context.Context, nk runtime.NakamaModule, logger runtime.NakamaModule) error {
parameters := map[string]interface{}{
"reachable_levels": 10,
"max_player_level": 90,
"min_version": 12,
}
b, err := json.Marshal(parameters)
if err != nil {
return err
}
objects := []\*runtime.StorageWrite{
&runtime.StorageWrite{
Collection: configCollection,
Key: configKey,
Value: string(b),
Version: "\*", // Only write if object does not exist already.
PermissionRead: 1,
PermissionWrite: 0,
},
}
if _, err := nk.StorageWrite(ctx, objects); err != nil {
return err
}
return nil
}
func RemoteConfig(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {
objectIds := []*runtime.StorageRead{
&runtime.StorageRead{
Collection: configCollection,
Key: configKey,
},
}
records, err := nk.StorageRead(ctx, objectIds)
if err != nil {
return "", err
}
if len(records) == 0 {
return "", errors.New("No config found.")
}
return records[0].Value, nil
}
// Ensure the configuration object is stored, this call should be in InitModule.
SaveConfig(ctx, nk, logger)
// Register as RPC function, this call should be in InitModule.
if err := initializer.RegisterRpc("rc", RemoteConfig); err != nil {
logger.Error("Unable to register: %v", err)
return err
}
|