Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ci_workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: '17'
java-version: '21'

- name: Build && Run
run: |
Expand All @@ -25,4 +25,4 @@ jobs:
uses: codecov/codecov-action@v4.0.1
with:
token: ${{ secrets.CODECOV_TOKEN }}
slug: insality/defold-token
slug: insality/defold-token
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ dist
deployer_build_stats.csv
bob*.jar
manifest.*.der
/.editor_settings
/.editor_settings
deployer_version_settings.ini
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
"timer",
"socket",
"assert_equal",
"sys"
"sys",
"event_context_manager"
],
"Lua.workspace.library": [
"~/Library/Application Support/Cursor/User/globalStorage/astronachos.defold",
"~/Library/Application Support/Cursor/User/workspaceStorage/939867c92961f8e45a79cf9cab057eac/astronachos.defold"
]
}
123 changes: 119 additions & 4 deletions USE_CASES.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local token = require("token.token")

function init(self)
saver.init()
saver.bind_save_part("token", token.state)
saver.bind_save_part("token", token.get_state())

token.init()
end
Expand All @@ -39,8 +39,9 @@ end


function init(self)
token.state = load_token_state()
token.init(token_state)
local state = load_token_state()
token.set_state(state)
token.init()
end
```

Expand All @@ -51,4 +52,118 @@ end
## How to use infinity tokens config


## Example: wallet container
## Example: wallet container

Often we need to create a container for a player's wallet or other "global" container.
You can do this by choosing one name for the container. For example `token.container("wallet")` and look like this:

```lua
local token = require("token.token")
token.container("wallet"):add("gold", 100, "task_completed")

-- Or with const name?
local const = require("game.const")
token.container(const.WALLET_ID):add(const.ITEM.GOLD, 100, const.REASON.TASK_COMPLETED)
```

or you can create a module file which will be used as a container itself

```lua
-- /game/wallet.lua
local token = require("token.token")

---@class wallet
local M = {}
local METATABLE = { __index = nil }

---@param wallet token.container
function M.set_wallet(wallet)
METATABLE.__index = wallet
end

M.set_wallet(token.container("wallet"))

return setmetatable(M, METATABLE)
```

And use it in your code like this (no any `token` required in each place)

```lua
local wallet = require("game.wallet")
wallet:add("gold", 100, "task_completed")
```

## Tokens Config

This config can be a Lua file, for example placed at `/game/tokens.lua` and look like this:

```lua
---@class game.tokens
return {
gold = {},
exp = {},
token_basic = { default = 8, min = 0, max = 10000 },
token_interaction = { default = 0, min = 0, max = 10000 },
damage_power = { default = 1 },
damage_crit = { default = 0.05 },
}
```

This allow to use this file as a config for `token.init` function.

```lua
token.init(require("game.tokens"))
```

And you can use Lua checks to validate your token is exists in the code:

```lua
-- Instead of "gold", where you can made a typo and get an unexpected behavior
wallet:add("gold", 100, "task_completed")

-- You can use Lua checks to validate your token is exists
local tokens = require("game.tokens")

-- If gold is not exists, it will be an syntax error here
wallet:add(tokens.gold, 100, "task_completed")

-- (token container can accept both string token id or token config)

-- You still can use any token id as a string, the token config is not required to count as a valid token id
wallet:add("token_without_config", 100, "task_completed")
```


## Token Groups and Lots

Token groups and lots should be registered separately using dedicated functions:

```lua
local token = require("token.token")

-- Initialize with tokens config
token.init(require("game.tokens"))

-- Register token groups (collections of tokens for rewards/prices)
token.register_token_groups({
starter_pack = {
gold = 100,
exp = 50,
},
starter_pack_price = {
money = 5,
},
daily_reward = {
gold = 10,
token_basic = 5,
},
})

-- Register lots (price + reward pairs)
token.register_lots({
shop_item_1 = {
price = "starter_pack_price", -- group id
reward = "starter_pack", -- group id
},
})
```
Loading