Skip to content

Commit 958b91a

Browse files
authored
Added Satori client support (#74)
* 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
1 parent 80d46d4 commit 958b91a

25 files changed

+12275
-1282
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
- name: Run tests
2929
run: |
3030
lua -v
31-
./tsc -f test/test_socket.lua test/test_client.lua test/test_session.lua
31+
./tsc -f test/test_socket.lua test/test_nakama.lua test/test_satori.lua test/test_session.lua

README.md

Lines changed: 99 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
![](https://img.shields.io/badge/Nakama%20gRPC%20-3.19.0-green)
22
![](https://img.shields.io/badge/Nakama%20RT%20-1.30.0-green)
33

4-
# Nakama Defold/Lua client
4+
# Nakama
55

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

294294

295-
## Adapting to other engines
295+
# Satori
296+
297+
> Lua client for Satori written in Lua 5.1.
298+
299+
[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.
300+
301+
## Getting started
302+
303+
Create a Satori client using the API key from the Satori dashboard.
296304

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

299306
```lua
300-
local myengine = require "nakama.engine.myengine"
301-
local nakama = require "nakama.nakama"
302-
local config = {
303-
engine = myengine,
304-
}
305-
local client = nakama.create_client(config)
307+
local config = {
308+
host = "myhost.com",
309+
api_key = "my-api-key",
310+
use_ssl = true,
311+
port = 443,
312+
retry_policy = retries.incremental(5, 1),
313+
engine = defold,
314+
}
315+
local client = satori.create_client(config)
306316
```
307317

308-
The engine module must provide the following functions:
318+
Then authenticate to obtain your session:
309319

310-
* `http(config, url_path, query_params, method, post_data, cancellation_token, callback)` - Make HTTP request.
311-
* `config` - Config table passed to `nakama.create()`
312-
* `url_path` - Path to append to the base uri
313-
* `query_params` - Key-value pairs to use as URL query parameters
314-
* `method` - "GET", "POST"
315-
* `post_data` - Data to post
316-
* `cancellation_token` - Check if `cancellation_token.cancelled` is true
317-
* `callback` - Function to call with result (response)
320+
```lua
321+
satori.sync(function()
322+
local uuid = defold.uuid()
323+
local result = client.authenticate(nil, nil, uuid)
324+
if not result.token then
325+
error("Unable to login")
326+
return
327+
end
328+
client.set_bearer_token(result.token)
329+
end)
330+
```
318331

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

323-
* `socket_connect(socket, callback)` - Connect socket.
324-
* `socket` - Socket instance returned from `socket_create()`
325-
* `callback` - Function to call with result (ok, err)
334+
```lua
335+
satori.sync(function()
336+
local experiments = satori.get_experiments(client)
337+
pprint(experiments)
326338

327-
* `socket_send(socket, message, callback)` - Send message on socket.
328-
* `socket` - Socket instance returned from `socket_create()`
329-
* `message` - Message to send
330-
* `callback` - Function to call with message returned as a response (message)
339+
local flags = satori.get_flags(client)
340+
pprint(flags)
341+
end)
342+
```
331343

332-
* `uuid()` - Create a UUID
333344

345+
# Contribute
346+
347+
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).
334348

335-
## API codegen
336349

337-
Refer to instructions in `codegen`.
350+
## Run tests
351+
352+
Unit tests can be found in the `tests` folder. Run them using [Telescope](https://github.com/defold/telescope) (fork which supports Lua 5.3+):
353+
354+
```
355+
./tsc -f test/test_nakama.lua test/test_satori.lua test/test_socket.lua test/test_session.lua
356+
```
338357

339358
## Generate Docs
340359

@@ -359,18 +378,60 @@ luarocks install ldoc
359378
doc . -d docs
360379
```
361380

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

382+
## Generate code
383+
384+
Refer to instructions in the [codegen folder](/codegen).
385+
386+
387+
## Adapting to other engines
388+
389+
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:
390+
391+
```lua
392+
-- nakama
393+
local myengine = require "nakama.engine.myengine"
394+
local nakama = require "nakama.nakama"
395+
local nakama_config = {
396+
engine = myengine,
397+
}
398+
local nakama_client = nakama.create_client(nakama_config)
399+
400+
-- satori
401+
local myengine = require "nakama.engine.myengine"
402+
local satori = require "satori.satori"
403+
local satori_config = {
404+
engine = myengine,
405+
}
406+
local satori_client = satori.create_client(satori_config)
365407
```
366-
./tsc -f test/test_client.lua test/test_socket.lua test/test_session.lua
367-
```
368408

369-
## Contribute
409+
The engine module must provide the following functions:
410+
411+
* `http(config, url_path, query_params, method, post_data, cancellation_token, callback)` - Make HTTP request.
412+
* `config` - Config table passed to `nakama.create()` or `satori.create()`
413+
* `url_path` - Path to append to the base uri
414+
* `query_params` - Key-value pairs to use as URL query parameters
415+
* `method` - "GET", "POST"
416+
* `post_data` - Data to post
417+
* `cancellation_token` - Check if `cancellation_token.cancelled` is true
418+
* `callback` - Function to call with result (response)
370419

371-
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).
420+
* `socket_create(config, on_message)` - Create socket. Must return socket instance (table with engine specific socket state).
421+
* `config` - Config table passed to `nakama.create()` or `satori.create()
422+
* `on_message` - Function to call when a message is sent from the server
423+
424+
* `socket_connect(socket, callback)` - Connect socket.
425+
* `socket` - Socket instance returned from `socket_create()`
426+
* `callback` - Function to call with result (ok, err)
427+
428+
* `socket_send(socket, message)` - Send message on socket.
429+
* `socket` - Socket instance returned from `socket_create()`
430+
* `message` - Message to send
431+
432+
* `uuid()` - Create a UUID
372433

373434

374-
### License
435+
# Licenses
375436

376437
This project is licensed under the [Apache-2 License](https://github.com/heroiclabs/nakama-defold/blob/master/LICENSE).

codegen/README.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
1-
Generates Lua code from the Nakama swagger definition in the main Nakama repository and the Nakama RealTime protobuf definition in the Nakama-Common repository.
1+
Generates Lua code from the Nakama and Satori API definitions (swagger and protobuf).
22

33
## Usage
44

5-
Generate the REST API:
5+
Generate Lua bindings for the Nakama and Satori REST APIs and the Nakama realtime API:
66

77
```shell
8-
go run rest.go /path/to/nakama/apigrpc/apigrpc.swagger.json > ../nakama/nakama.lua
9-
```
10-
11-
Generate the RealTime API:
12-
13-
```shell
14-
python realtime.py /path/to/nakama-common > ../nakama/socket.lua
8+
./generate.sh
159
```

0 commit comments

Comments
 (0)