Exports
Loyalty System Complete API Documentation
The Loyalty System provides a set of functions to manage player loyalty cards and points. These functions are exported from the 'Loyalty' resource and can be used in other resources within your QBCore-based server.
Table of Contents
HasCard
GetPoints
AddPoints
RemovePoints
General Notes
Functions
HasCard
Checks if a player has a loyalty card.
Parameters:
playerId
(number): The server ID of the player.
Returns:
boolean
:true
if the player has a loyalty card,false
otherwise.
Usage:
GetPoints
Retrieves the current points balance for a player.
Parameters:
playerId
(number): The server ID of the player.
Returns:
number
: The current points balance of the player. Returns 0 if the player doesn't have a loyalty card.
Usage:
AddPoints
Adds points to a player's loyalty card based on a dollar amount.
Parameters:
playerId
(number): The server ID of the player.amount
(number): The dollar amount to convert to points.reason
(string, optional): A description of why points are being added. Defaults to "Purchase" if not provided.
Returns:
boolean
:true
if points were successfully added,false
if the operation failed.string
: A message describing the result of the operation.number
: The actual number of points added after conversion.
Usage:
Notes:
This function converts the dollar amount to points using
Config.PointsEach1Dollar
.The converted amount is rounded down to the nearest integer.
Points addition is logged in the
loyalty_card_logs
table.After successful point addition, a notification is sent to the player using the "lb-phone" resource.
If the player is not found or the amount is invalid, the function will return false with an appropriate error message.
RemovePoints
Removes points from a player's loyalty card.
Parameters:
playerId
(number): The server ID of the player.pointsToRemove
(number): The amount of points to remove.customMessage
(string, optional): A custom message for the reason of point removal. If not provided, defaults to "Points removed".
Returns:
boolean
:true
if points were successfully removed,false
if the operation failed.string
: A message describing the result of the operation.
Usage:
Notes:
This function checks if the player has enough points before removing them. If not, the operation will fail.
Point removal is logged in the
loyalty_card_logs
table with a negative value forpoints_added
.After successful point removal, a notification is sent to the player using the "lb-phone" resource.
General Notes
Server-Side Usage: All these functions should be called server-side.
Resource Dependency: Ensure that the 'Loyalty' resource is started before calling these functions.
Database Interactions:
The system uses a
loyalty_cards
table to store player card information.A
loyalty_card_logs
table is used to log all point transactions.
Player Verification: All functions first verify if the player exists using QBCore's
GetPlayer
function.Notifications: The system uses the "lb-phone" resource to send notifications to players after successful point operations.
Configuration:
The
AddPoints
function uses a configuration valueConfig.PointsEach1Dollar
to convert dollar amounts to points. Ensure this configuration is properly set in your resource.
Error Handling:
Functions return
false
with an error message if operations fail (e.g., player not found, insufficient points).Always check the return values to handle success and failure cases appropriately in your scripts.
Loyalty Card Requirement:
GetPoints
will return 0 if a player doesn't have a loyalty card.AddPoints
andRemovePoints
will fail if the player doesn't have a loyalty card.Use
HasCard
to check for card existence before performing operations if you need to handle card/no-card situations differently.
Asynchronous Operations:
Database operations are performed asynchronously to prevent blocking the main thread.
The
AddPoints
andRemovePoints
functions use MySQL transactions to ensure data integrity.
Rounding: When adding points, the dollar amount is converted to points and rounded down to the nearest integer.
Logging: All point additions and removals are logged in the
loyalty_card_logs
table for auditing purposes.
Last updated