Skip to content

Instantly share code, notes, and snippets.

@novabyte
Created October 6, 2017 04:31
Show Gist options
  • Save novabyte/750031c4d6535f820cc58745f4630ccb to your computer and use it in GitHub Desktop.
Save novabyte/750031c4d6535f820cc58745f4630ccb to your computer and use it in GitHub Desktop.
An example on different ways to manage static game state with Nakama server.
{
"skills": [
{
"context": {"bucket": "b", "collection": "skills", "record": "fireball"},
"value": {
"name": "Fireball",
"element": 0,
"base_damage": 20
}
},
{
"context": {"bucket": "b", "collection": "skills", "record": "iceshard"},
"value": {
"name": "Ice Shard",
"element": 1,
"base_damage": 14
}
}
]
}
--[[
Copyright 2017 The Nakama Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
]]--
local nk = require("nakama")
------------
-- A module which shows different ways to manage static game data within the server.
-- @module static
--- Option 1
-- Load all game state via a http hook which can be called after server start.
-- expected input:
-- {"skills": [{"context": {"bucket": "b", "collection": "skills", "record": "skill1"}}, ...]}
-- usage:
-- curl -X POST "http://127.0.0.1:7350/runtime/static.setup?key=defaultkey" \
-- -d @static.json \
-- -H 'Content-Type: application/json'
-- -H 'Accept: application/json'
nk.register_http(function(context, payload)
local skills = assert(payload.skills, "'skills' field required.")
local records = {}
for i, v in ipairs(skills)
do
local context = assert(v.context, "'context' field required.")
local record = {
Bucket = assert(context.bucket, "'bucket' field required."),
Collection = assert(context.collection, "'collection' field required."),
Record = assert(context.record, "'record' field required."),
Value = assert(v.value, "'value' field required."),
PermissionRead = 2,
PermissionWrite = 0
}
table.insert(records, record)
end
nk.storage_write(records)
end, "static.setup")
--- Option 2
-- Manage the game state directly as code which is loaded at server start.
do
local records = {
-- Skills
{
Bucket = "b", Collection = "skills", Record = "fireball", PermissionRead = 2, PermissionWrite = 0,
Value = {
name = "Fireball",
element = 0, -- fire(0), water(1), earth(2), wind(3)
base_damage = 20
}
}, {
Bucket = "b", Collection = "skills", Record = "iceshard", PermissionRead = 2, PermissionWrite = 0,
Value = {
name = "Ice Shard",
element = 1,
base_damage = 14
}
}
}
nk.storage_write(records)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment