Best practises when it comes to server side configurations (offers, events, etc.)

Hey,

I’ve been looking at how to tackle different scenarios with Nakama. For example we might want to configure on the server that a specific event would be running, or enable some special offer for all users.

A way of doing this would be to have some object stored with this data that’s always fetched, I guess that’s the common practice? And this data would be set belonging to no user.

That leads to the question, how do you create these objects? The “Import storage data” doesn’t seem to accept data without a valid user id? So maybe the practice would be to create some custom admin that would then talk to nakama via RPCs?

Also, if all users access this one object, would it make sense to have some custom RPC for that as well that caches the value so it doesn’t need to hit the database?

Thanks!

A

1 Like

@nixarn We tend to refer to this kind of data used for live events and offers, etc. as tuning data for a game. The best way to manage these datasets depends on the technical level of the team who’s tasked to update and maintain it.

Some game teams use Google Sheets, others use in-house mini tools in their Unity editor environment to create the records but however you choose to maintain them you can generate the JSON import structure accepted by the Nakama Console. The general structure looks like:

[
  {
    "collection": "configuration",
    "key": "shop",
    "permission_read": 0,
    "permission_write": 0,
    "user_id": "00000000-0000-0000-0000-000000000000",
    "value": {
      "foo": "bar"
    }
  }
]

The input format is a JSON array of objects as described above.

That leads to the question, how do you create these objects? The “Import storage data” doesn’t seem to accept data without a valid user id?

The system user can be used to specify that the object is not owned by a real user. i.e. “00000000-0000-0000-0000-000000000000” (This value is often known as the “nil UUIDv4 value”)

So maybe the practice would be to create some custom admin that would then talk to nakama via RPCs?

You could push your data straight from a Google Sheet with an AppScript function into the game server over a Server To Server (S2S) RPC function or via the Nakama Console storage API itself.

Also, if all users access this one object, would it make sense to have some custom RPC for that as well that caches the value so it doesn’t need to hit the database?

You could cache these “configuration” storage objects into memory within the InitModule function and treat them as immutable until the next server restart.

nk.localcachePut("configuration-shop", { "foo": "bar" });
const value = nk.localcacheGet("configuration-shop");

NOTE: The localcache is not replicated so it’s best used just to cache immutable objects in RAM at server startup. Your use case is the typical intended use for this API.

Also have a look at the server function called nk.ReadFile(...) which was added in a recent version of the server and can be used to read your configuration data as simple JSON files to load them into your storage engine objects.

-- relative to the location of "--runtime.path"
const contents = nk.ReadFile("some/path/to/file")

Hope this helps.

6 Likes

Thanks a lot for a very thorough answer :+1:

1 Like