server/addons/bank/functions/fnc_initBankStore.sqf
Jacob Schmidt 991ee5019a feat: Enhance admin and bank event handling with improved logging
This commit refines the event handling for both admin and bank functionalities, ensuring better clarity and consistency in logging.

Key changes include:

- Updated event names in admin handling to follow a consistent naming convention.
- Improved parameter handling in admin event functions, ensuring validation checks are in place.
- Enhanced logging messages to provide clearer context regarding the source of events and actions taken.
- Introduced bank event handling for deposit, transfer, withdrawal, and balance inquiries, with appropriate validation and logging.

These changes aim to improve the maintainability and readability of the codebase while ensuring robust event management.
2025-05-10 17:50:21 -05:00

178 lines
5.9 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Author: IDSolutions
* Initializes a server-side bank store interface for managing banking operations.
* Provides CRUD operations for bank data, including database persistence.
*
* Arguments:
* None
*
* Return Value:
* <HASHMAP> Bank store object
*
* Example:
* private _bankStore = call forge_server_bank_fnc_initBank;
* _bankStore call ["deposit", [getPlayerUID player, 1000]];
*
* Public: Yes
*/
private _bankStore = createHashMapObject [[
["#type", "IBankStore"],
["#create", {
_self set ["accounts", createHashMap];
_self set ["transactions", []];
_self set ["isLoaded", true];
true
}],
["handlePlayerLoad", {
params [["_uid", "", [""]], ["_bankValue", 0, [0]], ["_cashValue", 0, [0]]];
if (_uid isEqualTo "") exitWith { false };
_self call ["updateAccount", [_uid, _bankValue, _cashValue]];
diag_log text format ["[FORGE::Server::Bank::Store::handlePlayerLoad] Loaded player data for '%1' - Bank: '%2', Cash: '%3'", _uid, _bankValue, _cashValue];
true
}],
["updateAccount", {
params [["_uid", "", [""]], ["_bankValue", 0, [0]], ["_cashValue", 0, [0]], ["_isSave", false, [false]]];
if (_uid isEqualTo "") exitWith { false };
private _accounts = _self get "accounts";
private _account = _accounts getOrDefault [_uid, createHashMap];
_account set ["bank", _bankValue];
_account set ["cash", _cashValue];
_accounts set [_uid, _account];
_self set ["accounts", _accounts];
if (_isSave) then {
["hsetid", _uid, "bank", -1, [_bankValue]] call dragonfly_db_fnc_addTask;
["hsetid", _uid, "cash", -1, [_cashValue]] call dragonfly_db_fnc_addTask;
};
diag_log text format ["[FORGE::Server::Bank::Store::updateAccount] Updated player data for '%1' - Bank: '%2', Cash: '%3'", _uid, _bankValue, _cashValue];
true
}],
["getBalance", {
params [["_uid", "", [""]]];
if (_uid isEqualTo "") exitWith { 0 };
private _accounts = _self get "accounts";
private _account = _accounts getOrDefault [_uid, createHashMap];
_account getOrDefault ["bank", 0]
}],
["getCash", {
params [["_uid", "", [""]]];
if (_uid isEqualTo "") exitWith { 0 };
private _accounts = _self get "accounts";
private _account = _accounts getOrDefault [_uid, createHashMap];
_account getOrDefault ["cash", 0]
}],
["deposit", {
params [["_uid", "", [""]], ["_amount", 0, [0]]];
if (_uid isEqualTo "" || _amount <= 0) exitWith { false };
private _accounts = _self get "accounts";
private _account = _accounts getOrDefault [_uid, createHashMap];
private _currentCash = _account getOrDefault ["cash", 0];
private _currentBalance = _account getOrDefault ["bank", 0];
if (_currentCash < _amount) exitWith { false };
private _newBalance = _currentBalance + _amount;
private _newCash = _currentCash - _amount;
_self call ["updateAccount", [_uid, _newBalance, _newCash, true]];
private _transactions = _self get "transactions";
_transactions pushBack [_uid, "deposit", _amount, time];
_self set ["transactions", _transactions];
true
}],
["withdraw", {
params [["_uid", "", [""]], ["_amount", 0, [0]]];
if (_uid isEqualTo "" || _amount <= 0) exitWith { false };
private _accounts = _self get "accounts";
private _account = _accounts getOrDefault [_uid, createHashMap];
private _currentBalance = _account getOrDefault ["bank", 0];
private _currentCash = _account getOrDefault ["cash", 0];
if (_currentBalance < _amount) exitWith { false };
private _newBalance = _currentBalance - _amount;
private _newCash = _currentCash + _amount;
_self call ["updateAccount", [_uid, _newBalance, _newCash, true]];
private _transactions = _self get "transactions";
_transactions pushBack [_uid, "withdraw", _amount, time];
_self set ["transactions", _transactions];
true
}],
["transfer", {
params [["_fromUid", "", [""]], ["_toUid", "", [""]], ["_amount", 0, [0]]];
if (_fromUid isEqualTo "" || _toUid isEqualTo "" || _amount <= 0) exitWith { false };
private _accounts = _self get "accounts";
private _fromAccount = _accounts getOrDefault [_fromUid, createHashMap];
private _toAccount = _accounts getOrDefault [_toUid, createHashMap];
private _fromBalance = _fromAccount getOrDefault ["bank", 0];
private _toBalance = _toAccount getOrDefault ["bank", 0];
if (_fromBalance < _amount) exitWith { false };
_self call ["updateAccount", [_fromUid, _fromBalance - _amount, 0, true]];
_self call ["updateAccount", [_toUid, _toBalance + _amount, 0, true]];
private _transactions = _self get "transactions";
_transactions pushBack [_fromUid, "transfer_out", _amount, time, _toUid];
_transactions pushBack [_toUid, "transfer_in", _amount, time, _fromUid];
_self set ["transactions", _transactions];
true
}],
["getTransactionHistory", {
params [["_uid", "", [""]], ["_limit", 10, [0]]];
private _transactions = _self get "transactions";
private _filteredTransactions = [];
if (_uid isEqualTo "") then {
_filteredTransactions = _transactions;
} else {
{
if ((_x select 0) == _uid) then { _filteredTransactions pushBack _x; };
} forEach _transactions;
};
_filteredTransactions sort false;
_filteredTransactions resize (_limit min (count _filteredTransactions));
_filteredTransactions
}]
]];
SETMVAR(FORGE_BANK_STORE_REG,_bankStore);
GETMVAR(FORGE_BANK_STORE_REG,_bankStore);