Skip to content

Instantly share code, notes, and snippets.

@novabyte
Created July 21, 2017 12:11
Show Gist options
  • Save novabyte/ed47ecfc8bd018febfd6b8727b01d2ed to your computer and use it in GitHub Desktop.
Save novabyte/ed47ecfc8bd018febfd6b8727b01d2ed to your computer and use it in GitHub Desktop.
An example of optimistic concurrency control to setup a new user's storage records with Nakama server.
local nk = require("nakama")
local function setup_user(context, _)
local value = {
coins = 100
level = 1
}
local record = {
Bucket = "mygame",
Collection = "saves",
Record = "save"
UserId = context.user_id, -- must be context.UserId after 1.0 release.
Value = value,
Version = "*" -- only write record if one doesn't already exist.
}
local success, err = pcall(nk.storage_write, {record})
-- An error will be returned if the OCC check fails.
if (not success) then
local str = ("Store write error: %q"):format(err)
nk.logger_info(str)
end
end
nk.register_after(setup_user, "authenticaterequest_device")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment