forge/arma/server/addons/bank/functions/fnc_initBankStore.sqf
2025-11-26 18:33:09 -06:00

171 lines
4.9 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Author: IDSolutions
* Initializes the bank store.
*
* Arguments:
* None
*
* Return Value:
* None
*
* Examples:
* [] call forge_server_bank_fnc_initBankStore
*
* Public: Yes
*/
#pragma hemtt ignore_variables ["_self"]
GVAR(BankStore) = createHashMapObject [[
["#type", "IBankStore"],
["#create", {
GVAR(BankRegistry) = createHashMap;
diag_log "[FORGE:Server:Bank] Bank Store Initialized!";
}],
["init", {
params [["_uid", "", [""]], ["_defaultAccount", createHashMap, [createHashMap]]];
private _finalAccount = createHashMap;
EXTCALL("bank:exists",[ARR_1(_uid)]);
private _exists = (_ext_res select 0) == "true";
if !(_exists) then {
_finalAccount = _defaultAccount;
_finalAccount set ["uid", _uid];
private _json = _self call ["toJSON", [_finalAccount]];
EXTCALL("bank:create",[ARR_2(_uid,_json)]);
diag_log format ["[FORGE:Server:Bank] Created new bank account for %1", _uid];
} else {
private _existingAccount = _self call ["fetch", [_uid]];
_finalAccount = _existingAccount;
{
if !(_x in _finalAccount) then { _finalAccount set [_x, _y]; };
} forEach _defaultAccount;
diag_log format ["[FORGE:Server:Bank] Found bank account for %1", _uid];
};
GVAR(BankRegistry) set [_uid, _finalAccount, true];
private _player = [_uid] call EFUNC(common,getPlayer);
[CRPC(bank,responseInitBank), [_finalAccount], _player] call CFUNC(targetEvent);
_finalAccount
}],
["fetch", {
params [["_uid", "", [""]]];
private _account = createHashMap;
EXTCALL("bank:get",[ARR_1(_uid)]);
diag_log format ["[FORGE:Server:Bank] Data: %1", _ext_res];
private _ext_res_account = _ext_res select 0;
if (count _ext_res_account > 0) then { _account = _self call ["toHashMap", [_ext_res_account]]; };
_account
}],
["get", {
params [["_uid", "", [""]], ["_sync", false, [false]]];
private _finalAccount = createHashMap;
if (_sync) then {
private _existingAccount = _self call ["fetch", [_uid]];
_finalAccount = _existingAccount;
GVAR(BankRegistry) set [_uid, _finalAccount];
} else {
_finalAccount = GVAR(BankRegistry) get _uid;
};
_finalAccount
}],
["set", {
params [["_uid", "", [""]], ["_field", "", [""]], ["_value", nil], ["_sync", false, [false]]];
private _existingAccount = GVAR(BankRegistry) get _uid;
private _finalAccount = +_existingAccount;
private _hashMap = createHashMap;
_finalAccount set [_field, _value];
_hashMap set [_field, _value];
GVAR(BankRegistry) set [_uid, _finalAccount];
if (_sync) then {
private _json = _self call ["toJSON", [_hashMap]];
EXTCALL("bank:update",[ARR_2(_uid,_json)]);
};
private _player = [_uid] call EFUNC(common,getPlayer);
[CRPC(bank,responseSyncBank), [_hashMap], _player] call CFUNC(targetEvent);
_hashMap
}],
["mset", {
params [["_uid", "", [""]], ["_fieldValuePairs", createHashMap, [createHashMap]], ["_sync", false, [false]]];
private _existingAccount = GVAR(BankRegistry) get _uid;
private _finalAccount = +_existingAccount;
private _hashMap = createHashMap;
{ _finalAccount set [_x, _y]; } forEach _fieldValuePairs;
{ _hashMap set [_x, _y]; } forEach _fieldValuePairs;
GVAR(BankRegistry) set [_uid, _finalAccount];
if (_sync) then {
private _json = _self call ["toJSON", [_hashMap]];
EXTCALL("bank:update",[ARR_2(_uid,_json)]);
};
private _player = [_uid] call EFUNC(common,getPlayer);
[CRPC(bank,responseSyncBank), [_hashMap], _player] call CFUNC(targetEvent);
_hashMap
}],
["save", {
params [["_uid", "", [""]], ["_sync", false, [false]]];
private _existingAccount = GVAR(BankRegistry) get _uid;
private _finalAccount = +_existingAccount;
private _json = _self call ["toJSON", [_finalAccount]];
EXTCALL("bank:update",[ARR_2(_uid,_json)]);
if (_sync) then {
private _player = [_uid] call EFUNC(common,getPlayer);
[CRPC(bank,responseSyncBank), [_finalAccount], _player] call CFUNC(targetEvent);
};
_finalAccount
}],
["remove", {
params [["_uid", "", [""]]];
GVAR(BankRegistry) deleteAt _uid;
}],
["toHashMap", {
params [["_data", "", [""]]];
private _hashMap = fromJSON _data;
_hashMap
}],
["toJSON", {
params [["_data", createHashMap, [createHashMap]]];
private _json = toJSON _data;
_json
}]
]];
SETMVAR(FORGE_BankStore,GVAR(BankStore));
GVAR(BankStore)