Created
July 21, 2017 12:11
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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