server/addons/db/functions/fnc_initDB.sqf
Jacob Schmidt 5b30efa3b0
All checks were successful
Build / Build (push) Successful in 26s
feat: Refactor database system for improved persistence and functionality
This commit refactors the database system to improve persistence, functionality, and code clarity. The key changes include:

-   **Removed direct store access:** Removed `createStore`, `getFromStore`, and `getStore` PREP macros.
-   **Centralized store management:** Introduced a central store registry (`FORGE_STORE_REG`) managed by the database interface.
-   **Namespace-based persistence:** Stores are now persisted in mission and profile namespaces instead of a global store.
-   **Simplified load/save functions:** `loadFromMission`, `loadFromProfile`, `saveToMission`, `saveToProfile`, and `saveToTemp` functions are updated to use the new namespace-based persistence. They now accept a `keyField` parameter for retrieving specific fields within a key's data.
-   **Refactored `processDBRequest`:** Updated to handle new request types and parameters, aligning with the refactored load/save functions.
-   **Improved error handling:** Added more robust error handling and logging, including checks for empty store names and missing keys.
-   **Removed client registration:** Removed client registration and cleanup logic as it's no longer needed with the new persistence model.
-   **Updated `verifyDB`:** Simplified to directly return the store registry.
-   **Updated `initDB`:** Refactored to use a HashMap object for the store interface and added more database functions.
-   **Added .gitignore entries:** Added entries for Visual Studio and other common build artifacts.
-   **Updated `loadGameState` and `saveGameState`:** Updated to support loading and saving game state to either mission or profile namespace.
2025-04-05 14:16:35 -05:00

226 lines
8.5 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Function: forge_server_db_fnc_initDB
* Author: J. Schmidt
*
* Description:
* Initializes the database system, creating the interface for other modules
*
* Arguments:
* None
*
* Return Value:
* Database interface object
*/
private _storeInterface = createHashMapObject [[
["#type", "IStore"],
["#create", {
private _storeRegistry = GETVAR(profileNamespace,FORGE_STORE_REG,createHashMap);
_self set ["stores", _storeRegistry];
true
}],
["_create", {
params [["_name", "", [""]]];
if (_name isEqualTo "") exitWith { ERROR_MSG("Store name cannot be empty"); nil };
private _stores = _self get "stores";
private _store = createHashMap;
_stores set [_name, _store];
_store
}],
["_read", {
params [["_name", "", [""]], ["_key", "", [""]]];
if (_name isEqualTo "") exitWith { ERROR_MSG("Store name cannot be empty"); nil };
private _stores = _self get "stores";
private _store = _stores get _name;
if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); nil };
if (_key isEqualTo "") then { _store } else { _store get _key };
}],
["_write", {
params [["_name", "", [""]], ["_data", nil, ["", [], 0, true, createHashMap]]];
if (_name isEqualTo "" || isNil "_data") exitWith { ERROR_MSG("Store name and, or data cannot be empty"); nil };
private _stores = _self get "stores";
private _store = _self call ["_read", [_name]];
if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); nil };
_stores set [_name, _data];
SETVAR(profileNamespace,FORGE_STORE_REG,_stores);
saveMissionProfileNamespace;
_store
}],
["_update", {
params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]], ["_data", nil, ["", [], 0, true, createHashMap]]];
if (_name isEqualTo "" || _key isEqualTo "" || _keyField isEqualTo "" || isNil "_data") exitWith { ERROR_MSG("Store name, key, key field and, or data cannot be empty"); nil };
private _stores = _self get "stores";
private _store = _self call ["_read", [_name]];
if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); nil };
private _keyData = _store get _key;
if (isNil "_keyData") exitWith { ERROR_MSG_2("Key %1 not found in store %2",_key,_name); nil };
_keyData set [_keyField, _data];
_store set [_key, _keyData];
_stores set [_name, _store];
_store
}],
["_delete", {
params [["_name", "", [""]]];
if (_name isEqualTo "") exitWith { ERROR_MSG("Store name cannot be empty"); false };
private _stores = _self get "stores";
private _store = _self call ["get", [_name]];
if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); false };
_stores deleteAt _name;
true
}],
["set", {
params [["_key", "", [""]], ["_value", nil, [[]]]];
if (_key isEqualTo "" || isNil "_value") exitWith { ERROR_MSG("Key and, or value cannot be empty") };
["set", _key, "", -1, _value] call dragonfly_db_fnc_addTask;
}],
["get", {
params [["_key", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_key isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key and, or function cannot be empty") };
["get", _key, "", -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["delete", {
params [["_key", "", [""]]];
if (_key isEqualTo "") exitWith { ERROR_MSG("Key cannot be empty") };
["del", _key] call dragonfly_db_fnc_addTask;
}],
["hashGet", {
params [["_keyField", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_keyField isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key field and, or function cannot be empty") };
["hget", "", _keyField, -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["hashGetId", {
params [["_key", "", [""]], ["_keyField", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_key isEqualTo "" || _keyField isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key, key field and, or function cannot be empty") };
["hgetid", _key, _keyField, -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["hashGetAll", {
params [["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_function isEqualTo "") exitWith { ERROR_MSG("Function cannot be empty") };
["hgetall", "", "", -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["hashGetAllId", {
params [["_key", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_key isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key and, or function cannot be empty") };
["hgetallid", _key, "", -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["hashSet", {
params [["_keyField", "", [""]], ["_value", nil, [[]]]];
if (_keyField isEqualTo "" || isNil "_value") exitWith { ERROR_MSG("Key field and, or value cannot be empty") };
["hset", "", _keyField, -1, _value] call dragonfly_db_fnc_addTask;
}],
["hashSetId", {
params [["_key", "", [""]], ["_keyField", "", [""]], ["_value", nil, [[]]]];
if (_key isEqualTo "" || _keyField isEqualTo "" || isNil "_value") exitWith { ERROR_MSG("Key, keyField and, or value cannot be empty") };
["hsetid", _key, _keyField, -1, _value] call dragonfly_db_fnc_addTask;
}],
["hashSetBulk", {
params [["_value", nil, [[]]]];
if (isNil "_value") exitWith { ERROR_MSG("Value cannot be empty") };
["hsetbulk", "", "", -1, _value] call dragonfly_db_fnc_addTask;
}],
["hashSetIdBulk", {
params [["_value", nil, [[]]]];
if (isNil "_value") exitWith { ERROR_MSG("Value cannot be empty") };
["hsetidbulk", "", "", -1, _value] call dragonfly_db_fnc_addTask;
}],
["hashDelete", {
params [["_keyField", "", [""]]];
if (_keyField isEqualTo "") exitWith { ERROR_MSG("Key field cannot be empty") };
["hdel", "", _keyField] call dragonfly_db_fnc_addTask;
}],
["hashDeleteId", {
params [["_key", "", [""]], ["_keyField", "", [""]]];
if (_key isEqualTo "" || _keyField isEqualTo "") exitWith { ERROR_MSG("Key and, or key field cannot be empty") };
["hdelid", _key, _keyField] call dragonfly_db_fnc_addTask;
}],
["listAdd", {
params [["_key", "", [""]], ["_value", nil, [[]]]];
if (_key isEqualTo "" || isNil "_value") exitWith { ERROR_MSG("Key and, or value cannot be empty") };
["listadd", _key, "", -1, _value] call dragonfly_db_fnc_addTask;
}],
["listGet", {
params [["_key", "", [""]], ["_index", -1, [0]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_key isEqualTo "" || _index isEqualTo -1 || _function isEqualTo "") exitWith { ERROR_MSG("Key, index and, or function cannot be empty or -1") };
["listidx", _key, "", _index, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["listLoad", {
params [["_key", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]];
if (_key isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key and, or function cannot be empty") };
["listrng", _key, "", -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask;
}],
["listRemove", {
params [["_key", "", [""]], ["_index", -1, [0]]];
if (_key isEqualTo "" || _index isEqualTo -1) exitWith { ERROR_MSG("Key and, or index cannot be empty or -1") };
["listrem", _key, "", _index] call dragonfly_db_fnc_addTask;
}],
["listSet", {
params [["_key", "", [""]], ["_index", -1, [0]], ["_value", nil, [[]]]];
if (_key isEqualTo "" || _index isEqualTo -1 || isNil "_value") exitWith { ERROR_MSG("Key, index and, or value cannot be empty or -1") };
["listset", _key, "", _index, _value] call dragonfly_db_fnc_addTask;
}]
]];
SETMVAR(FORGE_STORE_REG,_storeInterface);
SETPVAR(missionNamespace,FORGE_STORE_REG,_storeInterface);
GETMVAR(FORGE_STORE_REG,_storeInterface)