Skip to content

seaofvoices/crosswalk-currency

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A crosswalk module to handle in-game currencies

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published