JSPM

@crosswalk-game/currency

0.1.3
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 35
  • Score
    100M100P100Q47946F
  • License MIT

A crosswalk module to handle in-game currencies

Package Exports

  • @crosswalk-game/currency
  • @crosswalk-game/currency/src/init.lua

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@crosswalk-game/currency) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

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.

Throws an error if the player data is not available.

tryGive

Currency.tryGive(player: Player, amount: number, currencyName?: string): boolean

Give currency to a player. If currencyName is provided, the specified custom currency will be incremented; otherwise, the default currency will be incremented.

Returns false if the player data is not available.

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.

get

Currency.get(player: Player, currencyName?: string): number

Return the current amount of currency (default or custom, depending on the presence of currencyName) of a given player.

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.