@crosswalk-game/currency

0.1.1 • Public • Published

Currency

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.

Installation

Add @crosswalk-game/currency in your dependencies:

yarn add @crosswalk-game/currency

Or if you are using npm:

npm install @crosswalk-game/currency

API

give

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.

spend

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.

hasFunds

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.

Listen to Currency Changes

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 at default. Take note that custom currencies maybe be undefined if a player has never received that currency.
  • currency: contains the default currency amount
  • currency_*: contains a custom currency amount. If a game has a gems currency, it would publish the value on the channel currency_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)

License

This project is available under the MIT license. See LICENSE.txt for details.

Package Sidebar

Install

npm i @crosswalk-game/currency

Weekly Downloads

4

Version

0.1.1

License

MIT

Unpacked Size

8.85 kB

Total Files

5

Last publish

Collaborators

  • jeparlefrancais