server/addons/db/functions/fnc_initDB.sqf
Jacob Schmidt 3bd5a4a9bf
All checks were successful
Build / Build (push) Successful in 24s
feat: Implement store persistence and improve store interface
This commit introduces store persistence and refactors the store interface for improved functionality. The changes include:

-   Adding a `save` function to persist store data.
-   Refactoring `createStore` to `create` and `getStore` to `get`.
-   Adding `set` function to create store if not exist.
-   Refactoring `set` to `hset` and `get` to `hget` for hash set and get operations.
-   Refactoring `delete` to `hdel` for hash delete operations.
-   Implementing store saving after each modification.
2025-03-30 17:15:28 -05:00

123 lines
3.3 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
}],
["save", {
private _stores = _self get "stores";
SETVAR(profileNamespace,FORGE_STORE_REG,_stores);
saveProfileNamespace;
true
}],
["create", {
params [["_name", "", [""]]];
if (_name == "") exitWith { ERROR_MSG("Store name cannot be empty"); nil };
private _stores = _self get "stores";
private _store = createHashMap;
_stores set [_name, _store];
_self call ["save"];
_store
}],
["set", {
params [["_name", "", [""]]];
if (_name == "") exitWith { ERROR_MSG("Store name cannot be empty"); nil };
private _stores = _self get "stores";
private _store = _self call ["get", [_name]];
if (isNil "_store") then {
_store = _self call ["create", [_name]];
};
_store
}],
["get", {
params [["_name", "", [""]]];
if (_name == "") exitWith { ERROR_MSG("Store name cannot be empty"); nil };
private _stores = _self get "stores";
_stores get _name
}],
["delete", {
params [["_name", "", [""]]];
if (_name == "") 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
}],
["hset", {
params [["_name", "", [""]], ["_key", "", [""]], ["_value", "", ["", [], 0, false, createHashMap]]];
if (_name == "" || _key == "") exitWith { ERROR_MSG("Store name and, or key 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 };
_store set [_key, _value];
_stores set [_name, _store];
_self call ["save"];
true
}],
["hget", {
params [["_name", "", [""]], ["_key", "", [""]]];
if (_name == "" || _key == "") exitWith { ERROR_MSG("Store name and, or key cannot be empty"); _default };
private _store = _self call ["get", [_name]];
if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found", _name); _default };
_store get _key
}],
["hdel", {
params [["_name", "", [""]], ["_key", "", [""]]];
if (_name == "" || _key == "") exitWith { ERROR_MSG("Store name and, or key cannot be empty"); false };
private _store = _self call ["get", [_name]];
if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found", _name); false };
_store deleteAt _key;
true
}]
]];
SETPVAR(missionNamespace,FORGE_STORE_REG,_storeInterface);
GETMVAR(FORGE_STORE_REG,nil)