This crosswalk module simplifies the handling of game-specific currencies. It provides the necessary functions to add, spend or check funds for individual players.
This module requires the Channels and DataHandler modules.
Add @crosswalk-game/currency
in your dependencies:
yarn add @crosswalk-game/currency
Or if you are using npm
:
npm install @crosswalk-game/currency
Currency.give(player: Player, amount: number, currencyName?: string)
Give currency to a player. If currencyName
is provided, the specified custom currency will be incremented; otherwise, the default currency will be incremented.
Currency.spend(player: Player, amount: number, currencyName?: string): boolean
Spend currency on behalf of a player. If currencyName
is provided, the specified custom currency will be decremented; otherwise, the default currency will be decremented. Returns true
if the transaction is successful, otherwise false
if the player does not have sufficient funds.
Currency.hasFunds(player: Player, amount: number, currencyName?: string): boolean
Check if a player has sufficient funds. Returns true
if the player has enough currency (default or custom, depending on the presence of currencyName
), otherwise false
.
The module uses the Channels module to make the different currency amounts available. It will publish the amounts on different local channels:
currencies
: contains all the currencies amount in a dictionary. The default currency is indexed atdefault
. Take note that custom currencies maybe be undefined if a player has never received that currency.currency
: contains the default currency amountcurrency_*
: contains a custom currency amount. If a game has agems
currency, it would publish the value on the channelcurrency_gems
In client modules, connect using the Channels.Bind
function.
Modules.Channels.Bind('currency', function(amount: number)
-- todo: display the value somewhere
end)
In server modules, connect using the Channels.BindPlayer
function (since the data is published using Channels.SendLocal
).
Modules.Channels.BindPlayer('currency', function(player: Player, amount: number)
-- todo: the player's currency amount changed so server leaderboards
-- could be updated here for example.
end)
This project is available under the MIT license. See LICENSE.txt for details.