Skip to content

Commit

Permalink
Added Satori client support (#74)
Browse files Browse the repository at this point in the history
* Added Satori API generator

* Merged REST API generators

* Removed extra space

* Improved session validation

* Update example.script

* Simplified result handling

* Reverted session change

* Added required param

* Check for nil or value

* Revert test fix

* Pass nil for extra vars

* Update paths.lua.mtl

* Update generate-rest.py

* Session handling and better code generation

* Update nakama.lua

* Moved code gen to go for Satori

* Sort keys

* Update README.md

* Updated Satori example with event

* Update game.project

* Updated docs. Added tests

* Update test.yml

* Fixed mising uri_encode

* Update README.md

* Update example-satori.script
  • Loading branch information
britzl authored Sep 6, 2024
1 parent 80d46d4 commit 958b91a
Show file tree
Hide file tree
Showing 25 changed files with 12,275 additions and 1,282 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ jobs:
- name: Run tests
run: |
lua -v
./tsc -f test/test_socket.lua test/test_client.lua test/test_session.lua
./tsc -f test/test_socket.lua test/test_nakama.lua test/test_satori.lua test/test_session.lua
137 changes: 99 additions & 38 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
![](https://img.shields.io/badge/Nakama%20gRPC%20-3.19.0-green)
![](https://img.shields.io/badge/Nakama%20RT%20-1.30.0-green)

# Nakama Defold/Lua client
# Nakama

> Lua client for Nakama server written in Lua 5.1.
Expand Down Expand Up @@ -292,49 +292,68 @@ end)
Messages initiated _by the server_ in an authoritative match will come as valid JSON by default.


## Adapting to other engines
# Satori

> Lua client for Satori written in Lua 5.1.
[Satori](https://heroiclabs.com/satori/) is a liveops server for games that powers actionable analytics, A/B testing and remote configuration. Use the Satori Defold client to communicate with Satori from within your Defold game.

## Getting started

Create a Satori client using the API key from the Satori dashboard.

Adapting the Nakama Defold client to another Lua based engine should be as easy as providing another engine module when configuring the Nakama client:

```lua
local myengine = require "nakama.engine.myengine"
local nakama = require "nakama.nakama"
local config = {
engine = myengine,
}
local client = nakama.create_client(config)
local config = {
host = "myhost.com",
api_key = "my-api-key",
use_ssl = true,
port = 443,
retry_policy = retries.incremental(5, 1),
engine = defold,
}
local client = satori.create_client(config)
```

The engine module must provide the following functions:
Then authenticate to obtain your session:

* `http(config, url_path, query_params, method, post_data, cancellation_token, callback)` - Make HTTP request.
* `config` - Config table passed to `nakama.create()`
* `url_path` - Path to append to the base uri
* `query_params` - Key-value pairs to use as URL query parameters
* `method` - "GET", "POST"
* `post_data` - Data to post
* `cancellation_token` - Check if `cancellation_token.cancelled` is true
* `callback` - Function to call with result (response)
```lua
satori.sync(function()
local uuid = defold.uuid()
local result = client.authenticate(nil, nil, uuid)
if not result.token then
error("Unable to login")
return
end
client.set_bearer_token(result.token)
end)
```

* `socket_create(config, on_message)` - Create socket. Must return socket instance (table with engine specific socket state).
* `config` - Config table passed to `nakama.create()`
* `on_message` - Function to call when a message is sent from the server
Using the client you can get any experiments or feature flags, the user belongs to.

* `socket_connect(socket, callback)` - Connect socket.
* `socket` - Socket instance returned from `socket_create()`
* `callback` - Function to call with result (ok, err)
```lua
satori.sync(function()
local experiments = satori.get_experiments(client)
pprint(experiments)

* `socket_send(socket, message, callback)` - Send message on socket.
* `socket` - Socket instance returned from `socket_create()`
* `message` - Message to send
* `callback` - Function to call with message returned as a response (message)
local flags = satori.get_flags(client)
pprint(flags)
end)
```

* `uuid()` - Create a UUID

# Contribute

The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested to enhance the code please open an issue to discuss the changes or drop in and discuss it in the [community forum](https://forum.heroiclabs.com).

## API codegen

Refer to instructions in `codegen`.
## Run tests

Unit tests can be found in the `tests` folder. Run them using [Telescope](https://github.com/defold/telescope) (fork which supports Lua 5.3+):

```
./tsc -f test/test_nakama.lua test/test_satori.lua test/test_socket.lua test/test_session.lua
```

## Generate Docs

Expand All @@ -359,18 +378,60 @@ luarocks install ldoc
doc . -d docs
```

## Unit tests
Unit tests can be found in the `tests` folder. Run them using [Telescope](https://github.com/defold/telescope) (fork which supports Lua 5.3+):

## Generate code

Refer to instructions in the [codegen folder](/codegen).


## Adapting to other engines

Adapting the Nakama and Satori Defold clients to another Lua based engine should be as easy as providing another engine module when configuring the Nakama client:

```lua
-- nakama
local myengine = require "nakama.engine.myengine"
local nakama = require "nakama.nakama"
local nakama_config = {
engine = myengine,
}
local nakama_client = nakama.create_client(nakama_config)

-- satori
local myengine = require "nakama.engine.myengine"
local satori = require "satori.satori"
local satori_config = {
engine = myengine,
}
local satori_client = satori.create_client(satori_config)
```
./tsc -f test/test_client.lua test/test_socket.lua test/test_session.lua
```

## Contribute
The engine module must provide the following functions:

* `http(config, url_path, query_params, method, post_data, cancellation_token, callback)` - Make HTTP request.
* `config` - Config table passed to `nakama.create()` or `satori.create()`
* `url_path` - Path to append to the base uri
* `query_params` - Key-value pairs to use as URL query parameters
* `method` - "GET", "POST"
* `post_data` - Data to post
* `cancellation_token` - Check if `cancellation_token.cancelled` is true
* `callback` - Function to call with result (response)

The development roadmap is managed as GitHub issues and pull requests are welcome. If you're interested to enhance the code please open an issue to discuss the changes or drop in and discuss it in the [community forum](https://forum.heroiclabs.com).
* `socket_create(config, on_message)` - Create socket. Must return socket instance (table with engine specific socket state).
* `config` - Config table passed to `nakama.create()` or `satori.create()
* `on_message` - Function to call when a message is sent from the server

* `socket_connect(socket, callback)` - Connect socket.
* `socket` - Socket instance returned from `socket_create()`
* `callback` - Function to call with result (ok, err)

* `socket_send(socket, message)` - Send message on socket.
* `socket` - Socket instance returned from `socket_create()`
* `message` - Message to send

* `uuid()` - Create a UUID


### License
# Licenses

This project is licensed under the [Apache-2 License](https://github.com/heroiclabs/nakama-defold/blob/master/LICENSE).
12 changes: 3 additions & 9 deletions codegen/README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
Generates Lua code from the Nakama swagger definition in the main Nakama repository and the Nakama RealTime protobuf definition in the Nakama-Common repository.
Generates Lua code from the Nakama and Satori API definitions (swagger and protobuf).

## Usage

Generate the REST API:
Generate Lua bindings for the Nakama and Satori REST APIs and the Nakama realtime API:

```shell
go run rest.go /path/to/nakama/apigrpc/apigrpc.swagger.json > ../nakama/nakama.lua
```

Generate the RealTime API:

```shell
python realtime.py /path/to/nakama-common > ../nakama/socket.lua
./generate.sh
```
Loading

0 comments on commit 958b91a

Please sign in to comment.