From 5b30efa3b09cc8173b6205bcd259115ec1078597 Mon Sep 17 00:00:00 2001 From: Jacob Schmidt Date: Sat, 5 Apr 2025 14:16:35 -0500 Subject: [PATCH] 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. --- .gitignore | 405 +++++++++++++++++++ addons/db/XEH_PREP.hpp | 7 +- addons/db/XEH_preInit.sqf | 22 +- addons/db/functions/fnc_createStore.sqf | 26 -- addons/db/functions/fnc_getFromStore.sqf | 39 -- addons/db/functions/fnc_getStore.sqf | 25 -- addons/db/functions/fnc_initDB.sqf | 233 ++++++++--- addons/db/functions/fnc_loadFromMission.sqf | 29 +- addons/db/functions/fnc_loadFromProfile.sqf | 29 +- addons/db/functions/fnc_loadFromTemp.sqf | 33 ++ addons/db/functions/fnc_loadGameState.sqf | 54 +-- addons/db/functions/fnc_loadPlayerState.sqf | 85 ++++ addons/db/functions/fnc_processDBRequest.sqf | 64 ++- addons/db/functions/fnc_saveGameState.sqf | 42 +- addons/db/functions/fnc_saveToMission.sqf | 29 +- addons/db/functions/fnc_saveToProfile.sqf | 29 +- addons/db/functions/fnc_saveToStore.sqf | 44 -- addons/db/functions/fnc_saveToTemp.sqf | 34 +- addons/db/functions/fnc_verifyDB.sqf | 6 +- 19 files changed, 863 insertions(+), 372 deletions(-) delete mode 100644 addons/db/functions/fnc_createStore.sqf delete mode 100644 addons/db/functions/fnc_getFromStore.sqf delete mode 100644 addons/db/functions/fnc_getStore.sqf create mode 100644 addons/db/functions/fnc_loadFromTemp.sqf create mode 100644 addons/db/functions/fnc_loadPlayerState.sqf delete mode 100644 addons/db/functions/fnc_saveToStore.sqf diff --git a/.gitignore b/.gitignore index c19c54b..9aeffd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,410 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore + +# User-specific files +*.rsuser +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Mono auto generated files +mono_crash.* + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +[Rr]eleases/ +x64/ +x86/ +[Ww][Ii][Nn]32/ +[Aa][Rr][Mm]/ +[Aa][Rr][Mm]64/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ +[Ll]ogs/ + +# Visual Studio 2015/2017 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# Visual Studio 2017 auto generated files +Generated\ Files/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUnit +*.VisualState.xml +TestResult.xml +nunit-*.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# Benchmark Results +BenchmarkDotNet.Artifacts/ + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ + +# ASP.NET Scaffolding +ScaffoldingReadMe.txt + +# StyleCop +StyleCopReport.xml + +# Files built by Visual Studio +*_i.c +*_p.c +*_h.h +*.ilk +*.meta +*.obj +*.iobj +*.pch +*.pdb +*.ipdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*_wpftmp.csproj +*.log +*.tlog +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# Visual Studio Trace Files +*.e2e + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# AxoCover is a Code Coverage Tool +.axoCover/* +!.axoCover/settings.json + +# Coverlet is a free, cross platform Code Coverage Tool +coverage*.json +coverage*.xml +coverage*.info + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# Note: Comment the next line if you want to checkin your web deploy settings, +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# NuGet Symbol Packages +*.snupkg +# The packages folder can be ignored because of Package Restore +**/[Pp]ackages/* +# except build/, which is used as an MSBuild target. +!**/[Pp]ackages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/[Pp]ackages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt +*.appx +*.appxbundle +*.appxupload + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!?*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Including strong name files can present a security risk +# (https://github.com/github/gitignore/pull/2483#issue-259490424) +#*.snk + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm +ServiceFabricBackup/ +*.rptproj.bak + +# SQL Server files +*.mdf +*.ldf +*.ndf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings +*.rptproj.rsuser +*- [Bb]ackup.rdl +*- [Bb]ackup ([0-9]).rdl +*- [Bb]ackup ([0-9][0-9]).rdl + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio 6 auto-generated project file (contains which files were open etc.) +*.vbp + +# Visual Studio 6 workspace and project file (working project files containing files to include in project) +*.dsw +*.dsp + +# Visual Studio 6 technical files +*.ncb +*.aps + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# CodeRush personal settings +.cr/personal + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Tabs Studio +*.tss + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs + +# OpenCover UI analysis results +OpenCover/ + +# Azure Stream Analytics local run output +ASALocalRun/ + +# MSBuild Binary and Structured Log +*.binlog + +# NVidia Nsight GPU debugger configuration file +*.nvuser + +# MFractors (Xamarin productivity tool) working folder +.mfractor/ + +# Local History for Visual Studio +.localhistory/ + +# Visual Studio History (VSHistory) files +.vshistory/ + +# BeatPulse healthcheck temp database +healthchecksdb + +# Backup folder for Package Reference Convert tool in Visual Studio 2017 +MigrationBackup/ + +# Ionide (cross platform F# VS Code tools) working folder +.ionide/ + +# Fody - auto-generated XML schema +FodyWeavers.xsd + +# VS Code files for those working on multiple tools +.vscode/* +!.vscode/settings.json +!.vscode/tasks.json +!.vscode/launch.json +!.vscode/extensions.json +*.code-workspace + +# Local History for Visual Studio Code +.history/ + +# Windows Installer files from build outputs +*.cab +*.msi +*.msix +*.msm +*.msp + +# JetBrains Rider +*.sln.iml + +# Hemtt *.pbo .hemttout hemtt hemtt.exe *.biprivatekey + + +# Added by cargo + +/target \ No newline at end of file diff --git a/addons/db/XEH_PREP.hpp b/addons/db/XEH_PREP.hpp index f99e560..4b537be 100644 --- a/addons/db/XEH_PREP.hpp +++ b/addons/db/XEH_PREP.hpp @@ -1,13 +1,12 @@ -PREP(createStore); -PREP(getFromStore); -PREP(getStore); PREP(initDB); PREP(loadFromMission); PREP(loadFromProfile); +PREP(loadFromTemp); PREP(loadGameState); +PREP(loadPlayerState); +PREP(processDBRequest); PREP(saveGameState); PREP(saveToMission); PREP(saveToProfile); -PREP(saveToStore); PREP(saveToTemp); PREP(verifyDB); \ No newline at end of file diff --git a/addons/db/XEH_preInit.sqf b/addons/db/XEH_preInit.sqf index 33ccbc9..1df75a8 100644 --- a/addons/db/XEH_preInit.sqf +++ b/addons/db/XEH_preInit.sqf @@ -5,29 +5,15 @@ PREP_RECOMPILE_START; #include "XEH_PREP.hpp" PREP_RECOMPILE_END; -GVAR(playerOwners) = createHashMap; -GVAR(tempStores) = createHashMap; +SETMVAR(FORGE_STORE_REG,createHashMap); // Process database requests ["forge_db_request", { - params ["_playerUID", "_requestType", "_data", "_requestID"]; + params ["_playerUID", "_type", "_name", "_key", "_keyField", "_data", "_requestID"]; - TRACE_4("DB Request",_playerUID,_requestType,_data,_requestID); + private _result = [_type, _name, _key, _keyField, _data] call FUNC(processDBRequest); - private _result = [_requestType, _data] call FUNC(processDBRequest); - ["forge_db_response", [_playerUID, _requestType, _result, _requestID]] call CBA_fnc_globalEvent; + ["forge_db_response", [_playerUID, _type, _result, _requestID]] call CBA_fnc_globalEvent; }] call CBA_fnc_addEventHandler; -// Register clients for direct messaging -["forge_db_registerClient", { - params ["_playerUID", "_ownerID"]; - GVAR(playerOwners) set [_playerUID, _ownerID]; -}] call CBA_fnc_addEventHandler; - -// Clean up on disconnect -addMissionEventHandler ["PlayerDisconnected", { - params ["_id", "_uid", "_name", "_jip", "_owner"]; - GVAR(playerOwners) deleteAt _uid; -}]; - ADDON = true; \ No newline at end of file diff --git a/addons/db/functions/fnc_createStore.sqf b/addons/db/functions/fnc_createStore.sqf deleted file mode 100644 index dcdc6c1..0000000 --- a/addons/db/functions/fnc_createStore.sqf +++ /dev/null @@ -1,26 +0,0 @@ -#include "..\script_component.hpp" - -/* - * Function: forge_server_db_fnc_createStore - * Author: J. Schmidt - * - * Description: - * Creates a new collection in the database - * - * Arguments: - * 0: _name - Store name - * 1: _schema - Store schema - * - * Return Value: - * Store object or nil if failed - */ - -params [["_name", "", [""]], ["_schema", [], [[]]]]; - -if (_name isEqualTo "") exitWith { - ERROR_MSG("Store name cannot be empty"); - nil -}; - -private _database = call FUNC(verifyDB); -_database call ["createStore", [_name, _schema]] \ No newline at end of file diff --git a/addons/db/functions/fnc_getFromStore.sqf b/addons/db/functions/fnc_getFromStore.sqf deleted file mode 100644 index ee86b6d..0000000 --- a/addons/db/functions/fnc_getFromStore.sqf +++ /dev/null @@ -1,39 +0,0 @@ -#include "..\script_component.hpp" - -/* - * Function: forge_server_db_fnc_getFromStore - * Author: J. Schmidt - * - * Description: - * Retrieves data from a store - * - * Arguments: - * 0: _name - Store name - * 1: _key - Key to retrieve (Optional, get entire store if empty) - * 2: _default - Default value if key not found - * 3: _namespace - Namespace to use (Optional, default: missionNamespace) - * - * Return Value: - * Retrieved data or default value - */ - -params [ - ["_name", "", [""]], - ["_key", "", [""]], - ["_default", nil], - ["_namespace", missionNamespace, [missionNamespace]] -]; - -if (_name isEqualTo "") exitWith { - ERROR_MSG_1("Invalid store name: %1",_name); - _default -}; - -private _stores = _namespace getVariable [QGVAR(stores), createHashMap]; -private _store = _stores getOrDefault [_name, createHashMap]; - -if (_key isEqualTo "") then { - _store -} else { - _store getOrDefault [_key, _default] -} \ No newline at end of file diff --git a/addons/db/functions/fnc_getStore.sqf b/addons/db/functions/fnc_getStore.sqf deleted file mode 100644 index 2ac3e28..0000000 --- a/addons/db/functions/fnc_getStore.sqf +++ /dev/null @@ -1,25 +0,0 @@ -#include "..\script_component.hpp" - -/* - * Function: forge_server_db_fnc_getStore - * Author: J. Schmidt - * - * Description: - * Retrieves a store from the database - * - * Arguments: - * 0: _name - Store name - * - * Return Value: - * Store object or nil if not found - */ - -params [["_name", "", [""]]]; - -if (_name isEqualTo "") exitWith { - ERROR_MSG("Store name cannot be empty"); - nil -}; - -private _database = GETMVAR(GVAR(store),nil); -_database call ["getStore", [_name]] \ No newline at end of file diff --git a/addons/db/functions/fnc_initDB.sqf b/addons/db/functions/fnc_initDB.sqf index 5e6801e..e5f5205 100644 --- a/addons/db/functions/fnc_initDB.sqf +++ b/addons/db/functions/fnc_initDB.sqf @@ -22,102 +22,205 @@ private _storeInterface = createHashMapObject [[ true }], - ["save", { - private _stores = _self get "stores"; - - SETVAR(profileNamespace,FORGE_STORE_REG,_stores); - saveProfileNamespace; - - true - }], - ["create", { + ["_create", { params [["_name", "", [""]]]; - if (_name == "") exitWith { ERROR_MSG("Store name cannot be empty"); nil }; + if (_name isEqualTo "") 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 + }], + ["_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 }], - ["set", { + ["_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 == "") exitWith { ERROR_MSG("Store name cannot be empty"); nil }; + 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") 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 }; + 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]]]; + ["set", { + params [["_key", "", [""]], ["_value", nil, [[]]]]; - if (_name == "" || _key == "") exitWith { ERROR_MSG("Store name and, or key cannot be empty"); false }; + if (_key isEqualTo "" || isNil "_value") exitWith { ERROR_MSG("Key and, or value cannot be empty") }; - 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 + ["set", _key, "", -1, _value] call dragonfly_db_fnc_addTask; }], - ["hget", { - params [["_name", "", [""]], ["_key", "", [""]]]; + ["get", { + params [["_key", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]]; - if (_name == "" || _key == "") exitWith { ERROR_MSG("Store name and, or key cannot be empty"); _default }; + if (_key isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key and, or function cannot be empty") }; - private _store = _self call ["get", [_name]]; - - if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found", _name); _default }; - - _store get _key + ["get", _key, "", -1, [], _function, _call, _netId] call dragonfly_db_fnc_addTask; }], - ["hdel", { - params [["_name", "", [""]], ["_key", "", [""]]]; + ["delete", { + params [["_key", "", [""]]]; - if (_name == "" || _key == "") exitWith { ERROR_MSG("Store name and, or key cannot be empty"); false }; + if (_key isEqualTo "") exitWith { ERROR_MSG("Key cannot be empty") }; - private _store = _self call ["get", [_name]]; + ["del", _key] call dragonfly_db_fnc_addTask; + }], + ["hashGet", { + params [["_keyField", "", [""]], ["_function", "", [""]], ["_call", false, [false]], ["_netId", "", [""]]]; - if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found", _name); false }; + if (_keyField isEqualTo "" || _function isEqualTo "") exitWith { ERROR_MSG("Key field and, or function cannot be empty") }; - _store deleteAt _key; - true + ["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,nil) \ No newline at end of file +GETMVAR(FORGE_STORE_REG,_storeInterface) \ No newline at end of file diff --git a/addons/db/functions/fnc_loadFromMission.sqf b/addons/db/functions/fnc_loadFromMission.sqf index 7e034ea..d287e55 100644 --- a/addons/db/functions/fnc_loadFromMission.sqf +++ b/addons/db/functions/fnc_loadFromMission.sqf @@ -5,28 +5,29 @@ * Author: J. Schmidt * * Description: - * Loads data from mission namespace using the database interface + * Loads data from mission profile namespace * * Arguments: * 0: _name - Store name * 1: _key - Key to retrieve - * 2: _default - Default value if not found + * 2: _keyField - Field of the key to retrieve * * Return Value: - * Retrieved data or default value + * Retrieved data */ -params [ - ["_name", "", [""]], - ["_key", "", [""]], - ["_default", nil] -]; +params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]]]; -private _database = call FUNC(verifyDB); -private _store = _database call ["getStore", [_name]]; +if (_name isEqualTo "") exitWith { ERROR_MSG("Store name cannot be empty"); nil }; -if (isNil "_store") exitWith { - _default -}; +private _store = missionProfileNamespace getVariable _name; -[_name, _key, _default, missionNamespace] call FUNC(getFromStore) \ No newline at end of file +if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); nil }; +if (_key isEqualTo "") then { _store } else { _store get _key }; +if (_keyField isNotEqualTo "") then { + private _keyData = _store get _key; + + if (isNil "_keyData") exitWith { ERROR_MSG_3("KeyField %1 not found in key %2 of store %3",_keyField,_key,_name); nil }; + + _keyData get _keyField +}; \ No newline at end of file diff --git a/addons/db/functions/fnc_loadFromProfile.sqf b/addons/db/functions/fnc_loadFromProfile.sqf index cd66065..4ed1e5a 100644 --- a/addons/db/functions/fnc_loadFromProfile.sqf +++ b/addons/db/functions/fnc_loadFromProfile.sqf @@ -9,24 +9,25 @@ * * Arguments: * 0: _name - Store name - * 1: _key - Key to retrieve (Optional, get entire collection if empty) - * 2: _default - Default value if key not found + * 1: _key - Key to retrieve + * 2: _keyField - Field of the key to retrieve * * Return Value: - * Retrieved data or default value + * Retrieved data */ -params [ - ["_name", "", [""]], - ["_key", "", [""]], - ["_default", nil] -]; +params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]]]; -private _database = call FUNC(verifyDB); -private _store = _database call ["getStore", [_name]]; +if (_name isEqualTo "") exitWith { ERROR_MSG("Store name cannot be empty"); nil }; -if (isNil "_store") exitWith { - _default -}; +private _store = profileNamespace getVariable _name; -[_name, _key, _default, profileNamespace] call FUNC(getFromStore); \ No newline at end of file +if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); nil }; +if (_key isEqualTo "") then { _store } else { _store get _key }; +if (_keyField isNotEqualTo "") then { + private _keyData = _store get _key; + + if (isNil "_keyData") exitWith { ERROR_MSG_3("KeyField %1 not found in key %2 of store %3",_keyField,_key,_name); nil }; + + _keyData get _keyField +}; \ No newline at end of file diff --git a/addons/db/functions/fnc_loadFromTemp.sqf b/addons/db/functions/fnc_loadFromTemp.sqf new file mode 100644 index 0000000..9f606e1 --- /dev/null +++ b/addons/db/functions/fnc_loadFromTemp.sqf @@ -0,0 +1,33 @@ +#include "..\script_component.hpp" + +/* + * Function: forge_server_db_fnc_loadFromTemp + * Author: J. Schmidt + * + * Description: + * Loads data from temporary mission store (not persisted between sessions) + * + * Arguments: + * 0: _name - Store name + * 1: _key - Key to load from + * 2: _keyField - Field of the key to load from + * + * Return Value: + * Data + */ + +params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]]]; + +if (_name isEqualTo "") exitWith { ERROR_MSG("Store name cannot be empty"); nil }; + +private _store = missionNamespace getVariable _name; + +if (isNil "_store") exitWith { ERROR_MSG_1("Store %1 not found",_name); nil }; +if (_key isEqualTo "") then { _store } else { _store get _key }; +if (_keyField isNotEqualTo "") then { + private _keyData = _store get _key; + + if (isNil "_keyData") exitWith { ERROR_MSG_3("KeyField %1 not found in key %2 of store %3",_keyField,_key,_name); nil }; + + _keyData get _keyField +}; \ No newline at end of file diff --git a/addons/db/functions/fnc_loadGameState.sqf b/addons/db/functions/fnc_loadGameState.sqf index 43aca01..ddbf75b 100644 --- a/addons/db/functions/fnc_loadGameState.sqf +++ b/addons/db/functions/fnc_loadGameState.sqf @@ -5,16 +5,25 @@ * Author: J. Schmidt * * Description: - * Loads game state from mission namespace and sets appropriate variables + * Loads game state from mission or profile namespace and sets appropriate variables * * Arguments: - * None + * 0: _nameSpace - Namespace to load from (mission, profile) (default: mission) * * Return Value: * Success */ -private _companyState = ["companyStore", "CompanyState", nil] call FUNC(loadFromMission); +params [["_nameSpace", "mission", [""]]]; + +private _companyState = createHashMap; + +if (_nameSpace == "mission") then { + _companyState = ["companyStore", "CompanyState"] call FUNC(loadFromMission); +} else { + _companyState = ["companyStore", "CompanyState"] call FUNC(loadFromProfile); +}; + if (!isNil "_companyState") then { companyFunds = _companyState getOrDefault ["funds", 0]; companyRating = _companyState getOrDefault ["rating", 0]; @@ -22,43 +31,4 @@ if (!isNil "_companyState") then { companyGarageUnlocks = _companyState getOrDefault ["garage_unlocks", []]; }; -private _playerUID = getPlayerUID player; -private _playerState = ["playerStore", _playerUID, nil] call FUNC(loadFromMission); -if (!isNil "_playerState") then { - private _armory_unlocks = _playerState getOrDefault ["armory_unlocks", [[],[],[],[]]]; - private _garage_unlocks = _playerState getOrDefault ["garage_unlocks", [[],[],[],[],[],[]]]; - private _locker = _playerState getOrDefault ["locker", []]; - private _garage = _playerState getOrDefault ["garage", []]; - private _cash = _playerState getOrDefault ["cash", 0]; - private _bank = _playerState getOrDefault ["bank", 0]; - private _number = _playerState getOrDefault ["number", "unknown"]; - private _email = _playerState getOrDefault ["email", "unknown@spearnet.mil"]; - private _paygrade = _playerState getOrDefault ["paygrade", "E1"]; - private _holster = _playerState getOrDefault ["holster", true]; - - EGVAR(arsenal,armory_unlocks) = _armory_unlocks; - EGVAR(arsenal,garage_unlocks) = _garage_unlocks; - SETPVAR(player,FORGE_Locker,_locker); - SETPVAR(player,FORGE_Garage,_garage); - SETPVAR(player,FORGE_Cash,_cash); - SETPVAR(player,FORGE_Bank,_bank); - SETPVAR(player,FORGE_Phone_Number,_number); - SETPVAR(player,FORGE_Email,_email); - SETPVAR(player,FORGE_Paygrade,_paygrade); - SETPVAR(player,FORGE_Holster_Weapon,_holster); - - if (isNull objectParent player) then { - player setUnitLoadout (_playerState getOrDefault ["loadout", []]); - if (_playerState getOrDefault ["currentWeapon", ""] != "") then { - player selectWeapon (_playerState get "currentWeapon"); - }; - player setPosASL (_playerState getOrDefault ["position", getPosASL player]); - player setDir (_playerState getOrDefault ["direction", 0]); - - if (_playerState getOrDefault ["stance", ""] != "") then { - [player, _playerState get "stance"] call ace_common_fnc_setStance; - }; - }; -}; - true \ No newline at end of file diff --git a/addons/db/functions/fnc_loadPlayerState.sqf b/addons/db/functions/fnc_loadPlayerState.sqf new file mode 100644 index 0000000..4155682 --- /dev/null +++ b/addons/db/functions/fnc_loadPlayerState.sqf @@ -0,0 +1,85 @@ +#include "..\script_component.hpp" + +/* + * Function: forge_server_db_fnc_loadPlayerState + * Author: J. Schmidt + * + * Description: + * Loads player state from mission or profile namespace and sets appropriate variables + * + * Arguments: + * 0: _player - Player object + * 1: _nameSpace - Namespace to load from (mission, profile) (default: mission) + * + * Return Value: + * Success + */ + +params [["_player", objNull, [objNull]], ["_nameSpace", "mission", [""]]]; + +if (isNull _player) exitWith { ERROR_MSG("Player object cannot be null"); false }; + +private _playerUID = getPlayerUID _player; +private _playerState = createHashMap; + +if (_nameSpace == "mission") then { + _playerState = ["playerStore", _playerUID] call FUNC(loadFromMission); +} else { + _playerState = ["playerStore", _playerUID] call FUNC(loadFromProfile); +}; + +if (isNil "_playerState") exitWith { ERROR_MSG_1("Player state for %1 not found",_playerUID); false }; + +private _reputation = _playerState getOrDefault ["reputation", 0]; +private _loadout = _playerState getOrDefault ["loadout", []]; +private _direction = _playerState getOrDefault ["direction", 0]; +private _cash = _playerState getOrDefault ["cash", 0]; +private _bank = _playerState getOrDefault ["bank", 0]; +private _armory_unlocks = _playerState getOrDefault ["armory_unlocks", [[],[],[],[]]]; +private _garage_unlocks = _playerState getOrDefault ["garage_unlocks", [[],[],[],[],[],[]]]; +private _locker = _playerState getOrDefault ["locker", []]; +private _garage = _playerState getOrDefault ["garage", []]; +private _email = _playerState getOrDefault ["email", "unknown@spearnet.mil"]; +private _number = _playerState getOrDefault ["number", "unknown"]; +private _paygrade = _playerState getOrDefault ["paygrade", "E1"]; +private _stance = _playerState getOrDefault ["stance", ""]; +private _holster = _playerState getOrDefault ["holster", true]; +private _position = _playerState getOrDefault ["position", getPosASL _player]; + +SETPVAR(_player,Reputation,_reputation); +SETPVAR(_player,Loadout,_loadout); +SETPVAR(_player,Direction,_direction); +SETPVAR(_player,FORGE_Cash,_cash); +SETPVAR(_player,FORGE_Bank,_bank); +SETPVAR(_player,FORGE_Armory_Unlocks,_armory_unlocks); +SETPVAR(_player,FORGE_Garage_Unlocks,_garage_unlocks); +SETPVAR(_player,FORGE_Locker,_locker); +SETPVAR(_player,FORGE_Garage,_garage); +SETPVAR(_player,FORGE_Email,_email); +SETPVAR(_player,FORGE_Phone_Number,_number); +SETPVAR(_player,FORGE_Paygrade,_paygrade); +SETPVAR(_player,Stance,_stance); +SETPVAR(_player,FORGE_Holster_Weapon,_holster); +SETPVAR(_player,Position,_position); + +_player playAction _stance; + +if (_holster) then { + [player] call AFUNC(weaponselect,putWeaponAway); +}; + +_player setPosASL _position; + +private _pAlt = ((getPosATLVisual _player) select 2); +private _pVelZ = ((velocity _player) select 2); +if (_pAlt > 5 && _pVelZ < 0) then { + _player setVelocity [0, 0, 0]; + _player setPosATL [((getPosATLVisual _player) select 0), ((getPosATLVisual _player) select 1), 1]; + hint "You logged off mid air. You were moved to a safe position on the ground."; +}; + +if (needReload _player == 1) then { reload _player }; + +SETPVAR(_player,value_loadDone,true); + +true \ No newline at end of file diff --git a/addons/db/functions/fnc_processDBRequest.sqf b/addons/db/functions/fnc_processDBRequest.sqf index 185336c..1ee84d7 100644 --- a/addons/db/functions/fnc_processDBRequest.sqf +++ b/addons/db/functions/fnc_processDBRequest.sqf @@ -5,39 +5,75 @@ * Author: J. Schmidt * * Description: - * Processes database requests from clients + * Processes database requests * * Arguments: * 0: _type - Type of database operation - * 1: _data - Data for the operation + * 1: _name - Name of the store + * 2: _key - Key of the store + * 3: _keyField - Field of the key to update + * 4: _data - Data for the operation * * Return Value: - * Operation result + * None */ -params [["_type", "", [""]], ["_data", nil, []]]; +params [["_type", "", [""]], ["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]], ["_data", "", ["", [], 0, true, createHashMap]]]; + +private _store = call FUNC(verifyDB); switch (_type) do { + case "createStore": { + params ["_name"]; + _store call ["_create", [_name]]; + }; case "getStore": { params ["_name"]; - [_name] call FUNC(getStore); + _store call ["_read", [_name]]; }; - case "saveTostore": { - _data params ["_name", "_data", "_key"]; - [_name, _data, _key] call FUNC(saveToStore); + case "saveToMission": { + params ["_name", "_key", "_data"]; + [_name, _key, _data] call FUNC(saveToMission); }; - case "getFromstore": { - _data params ["_name", "_key"]; - [_name, _key] call FUNC(getFromStore); + case "saveToProfile": { + params ["_name", "_key", "_data"]; + [_name, _key, _data] call FUNC(saveToProfile); + }; + case "saveToStore": { + params ["_name", "_data"]; + _store call ["_write", [_name, _data]]; + }; + case "saveToTemp": { + params ["_name", "_key", "_data"]; + [_name, _key, _data] call FUNC(saveToTemp); + }; + case "getFromMission": { + params ["_name", "_key", "_keyField"]; + [_name, _key, _keyField] call FUNC(loadFromMission); + }; + case "getFromProfile": { + params ["_name", "_key", "_keyField"]; + [_name, _key, _keyField] call FUNC(loadFromProfile); + }; + case "getFromStore": { + params ["_name", "_key"]; + _store call ["_read", [_name, _key]]; + }; + case "getFromTemp": { + params ["_name", "_key", "_keyField"]; + [_name, _key, _keyField] call FUNC(loadFromTemp); + }; + case "updateStore": { + params ["_name", "_key", "_keyField", "_data"]; + _store call ["_update", [_name, _key, _keyField, _data]]; }; case "loadGameState": { [] call FUNC(loadGameState); }; case "saveGameState": { - [_data] call FUNC(saveGameState); + [] call FUNC(saveGameState); }; default { - WARNING_1("Unknown database request type: %1", _type); - nil + WARNING_1("Unknown database request type: %1",_type) }; }; \ No newline at end of file diff --git a/addons/db/functions/fnc_saveGameState.sqf b/addons/db/functions/fnc_saveGameState.sqf index 64bb5bb..966e885 100644 --- a/addons/db/functions/fnc_saveGameState.sqf +++ b/addons/db/functions/fnc_saveGameState.sqf @@ -5,21 +5,24 @@ * Author: J. Schmidt * * Description: - * Collects and saves the current game state to mission namespace + * Collects and saves the current game state to mission or profile namespace * * Arguments: - * None + * 0: _nameSpace - Namespace to save to (mission, profile) (default: mission) * * Return Value: * Success */ +params [["_nameSpace", "mission", [""]]]; + if (isNil "companyFunds") then { companyFunds = 0 }; if (isNil "companyRating") then { companyRating = 0 }; if (isNil "companyGenerals") then { companyGenerals = [] }; if (isNil "companyGarageUnlocks") then { companyGarageUnlocks = [] }; -if (isNil EGVAR(arsenal,armory_unlocks)) then { EGVAR(arsenal,armory_unlocks) = [[],[],[],[]] }; -if (isNil EGVAR(arsenal,garage_unlocks)) then { EGVAR(arsenal,garage_unlocks) = [[],[],[],[],[],[]] }; + +private _default_armory_unlocks = [[],[],[],[]]; +private _default_garage_unlocks = [[],[],[],[],[],[]]; private _companyState = createHashMapFromArray [ ["key", "CompanyState"], @@ -29,14 +32,18 @@ private _companyState = createHashMapFromArray [ ["garage_unlocks", companyGarageUnlocks] ]; -["companyStore", _companyState, "CompanyState"] call FUNC(saveToMission); +if (_nameSpace == "mission") then { + ["companyStore", "CompanyState", _companyState] call FUNC(saveToMission); +} else { + ["companyStore", "CompanyState", _companyState] call FUNC(saveToProfile); +}; { if (alive _x) then { private _playerState = createHashMapFromArray [ ["key", getPlayerUID _x], - ["armory_unlocks", EGVAR(arsenal,armory_unlocks)], - ["garage_unlocks", EGVAR(arsenal,garage_unlocks)], + ["armory_unlocks", GETVAR(_x,FORGE_Armory_Unlocks,_default_armory_unlocks)], + ["garage_unlocks", GETVAR(_x,FORGE_Garage_Unlocks,_default_garage_unlocks)], ["locker", GETVAR(_x,FORGE_Locker,[])], ["garage", GETVAR(_x,FORGE_Garage,[])], ["cash", GETVAR(_x,FORGE_Cash,0)], @@ -56,23 +63,12 @@ private _companyState = createHashMapFromArray [ _playerState set ["stance", stance _x]; }; - ["playerStore", _playerState, getPlayerUID _x] call FUNC(saveToMission); + if (_nameSpace == "mission") then { + ["playerStore", getPlayerUID _x, _playerState] call FUNC(saveToMission); + } else { + ["playerStore", getPlayerUID _x, _playerState] call FUNC(saveToProfile); + }; }; } forEach playableUnits; -private _vehicles = nearestObjects [player, ["LandVehicle"], 50]; -{ - if (alive _x) then { - private _vehicleState = createHashMapFromArray [ - ["key", netId _x], - ["class", typeOf _x], - ["position", getPosATL _x], - ["direction", getDir _x], - ["health", damage _x] - ]; - - ["vehicleStore", _vehicleState, netId _x] call FUNC(saveToMission); - }; -} forEach _vehicles; - true \ No newline at end of file diff --git a/addons/db/functions/fnc_saveToMission.sqf b/addons/db/functions/fnc_saveToMission.sqf index 45b32b1..f36d6ec 100644 --- a/addons/db/functions/fnc_saveToMission.sqf +++ b/addons/db/functions/fnc_saveToMission.sqf @@ -9,26 +9,33 @@ * * Arguments: * 0: _name - Store name - * 1: _data - Data to save - * 2: _key - Key to save under + * 1: _key - Key to save under + * 2: _keyField - Field of the key to update + * 3: _data - Data to save * * Return Value: * Success */ -params [ - ["_name", "", [""]], - ["_data", nil, [createHashMap, [], "", 0, true]], - ["_key", "", [""]] -]; +params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]], ["_data", nil, ["", [], 0, true, createHashMap]]]; + +if (_name isEqualTo "" || _key isEqualTo "" || isNil "_data") exitWith { ERROR_MSG("Store name, key and, or data cannot be empty"); false }; private _database = call FUNC(verifyDB); -private _store = _database call ["getStore", [_name]]; +private _store = _database call ["_read", [_name]]; -if (isNil "_store") then { - _store = _database call ["createStore", [_name, []]]; +if (isNil "_store") then { _store = _database call ["_create", [_name, createHashMap]]; }; +if (_keyField isEqualTo "") then { + _store set [_key, _data]; +} else { + private _keyData = _store get _key; + + _keyData set [_keyField, _data]; + _store set [_key, _keyData]; }; -[_name, _data, _key, missionNamespace] call FUNC(saveToStore); +_database call ["_write", [_name, _store]]; +missionNamespace setVariable [_name, _store]; +saveMissionProfileNamespace; true \ No newline at end of file diff --git a/addons/db/functions/fnc_saveToProfile.sqf b/addons/db/functions/fnc_saveToProfile.sqf index 48b52d6..1efc1c3 100644 --- a/addons/db/functions/fnc_saveToProfile.sqf +++ b/addons/db/functions/fnc_saveToProfile.sqf @@ -9,26 +9,33 @@ * * Arguments: * 0: _name - Store name - * 1: _data - Data to save - * 2: _key - Key to save under + * 1: _key - Key to save under + * 2: _keyField - Field of the key to update + * 3: _data - Data to save * * Return Value: * Success */ -params [ - ["_name", "", [""]], - ["_data", nil, [createHashMap, [], "", 0, true]], - ["_key", "", [""]] -]; +params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]], ["_data", nil, ["", [], 0, true, createHashMap]]]; + +if (_name isEqualTo "" || _key isEqualTo "" || isNil "_data") exitWith { ERROR_MSG("Store name, key and, or data cannot be empty"); false }; private _database = call FUNC(verifyDB); -private _store = _database call ["getStore", [_name]]; +private _store = _database call ["_read", [_name]]; -if (isNil "_store") then { - _store = _database call ["createStore", [_name, []]]; +if (isNil "_store") then { _store = _database call ["_create", [_name, createHashMap]]; }; +if (_keyField isEqualTo "") then { + _store set [_key, _data]; +} else { + private _keyData = _store get _key; + + _keyData set [_keyField, _data]; + _store set [_key, _keyData]; }; -[_name, _data, _key, profileNamespace] call FUNC(saveToStore); +_database call ["_write", [_name, _store]]; +profileNamespace setVariable [_name, _store]; +saveProfileNamespace; true \ No newline at end of file diff --git a/addons/db/functions/fnc_saveToStore.sqf b/addons/db/functions/fnc_saveToStore.sqf deleted file mode 100644 index e854570..0000000 --- a/addons/db/functions/fnc_saveToStore.sqf +++ /dev/null @@ -1,44 +0,0 @@ -#include "..\script_component.hpp" - -/* - * Function: forge_server_db_fnc_saveToStore - * Author: J. Schmidt - * - * Description: - * Saves data to a store in the specified namespace - * - * Arguments: - * 0: _name - Store name - * 1: _data - Data to save - * 2: _key - Key to save under - * 3: _namespace - Namespace to use (Optional, default: missionNamespace) - * - * Return Value: - * Success - */ - -params [ - ["_name", "", [""]], - ["_data", nil, [createHashMap, [], "", 0, true]], - ["_key", "", [""]], - ["_namespace", missionNamespace, [missionNamespace]] -]; - -if (_name isEqualTo "" || _key isEqualTo "") exitWith { - ERROR_MSG_2("Invalid store name or key: %1, %2", _name, _key); - false -}; - -private _stores = _namespace getVariable [QGVAR(stores), createHashMap]; -private _store = _stores getOrDefault [_name, createHashMap]; - -_store set [_key, _data]; -_stores set [_name, _store]; -_namespace setVariable [QGVAR(stores), _stores]; - -switch (_namespace) do { - case (missionNamespace): { saveMissionProfileNamespace; }; - case (profileNamespace): { saveProfileNamespace; }; -}; - -true \ No newline at end of file diff --git a/addons/db/functions/fnc_saveToTemp.sqf b/addons/db/functions/fnc_saveToTemp.sqf index 1d9773e..4ef4411 100644 --- a/addons/db/functions/fnc_saveToTemp.sqf +++ b/addons/db/functions/fnc_saveToTemp.sqf @@ -9,31 +9,29 @@ * * Arguments: * 0: _name - Store name - * 1: _data - Data to save - * 2: _key - Key to save under + * 1: _key - Key to save under + * 2: _keyField - Field of the key to update + * 3: _data - Data to save * * Return Value: * Success */ -params [ - ["_name", "", [""]], - ["_data", nil, [createHashMap, [], "", 0, true]], - ["_key", "", [""]] -]; +params [["_name", "", [""]], ["_key", "", [""]], ["_keyField", "", [""]], ["_data", nil, ["", [], 0, true, createHashMap]]]; -private _database = call FUNC(verifyDB); -private _store = _database call ["getStore", [_name]]; +if (_name isEqualTo "" || _key isEqualTo "" || isNil "_data") exitWith { ERROR_MSG("Store name, key and, or data cannot be empty"); false }; -if (isNil "_store") then { - _store = _database call ["createStore", [_name, []]]; +private _store = missionNamespace getVariable [_name, createHashMap]; + +if (_keyField isEqualTo "") then { + _store set [_key, _data]; +} else { + private _keyData = _store get _key; + + _keyData set [_keyField, _data]; + _store set [_key, _keyData]; }; -private _tempStores = missionNamespace getVariable [QGVAR(tempStores), createHashMap]; -private _tempStore = _tempStores getOrDefault [_name, createHashMap]; +missionNamespace setVariable [_name, _store]; -_tempStore set [_key, _data]; -_tempStores set [_name, _tempStore]; -missionNamespace setVariable [QGVAR(tempStores), _tempStores]; - -true +true \ No newline at end of file diff --git a/addons/db/functions/fnc_verifyDB.sqf b/addons/db/functions/fnc_verifyDB.sqf index fa7687d..4ebf2a7 100644 --- a/addons/db/functions/fnc_verifyDB.sqf +++ b/addons/db/functions/fnc_verifyDB.sqf @@ -14,9 +14,7 @@ * Database object */ -private _store = GETMVAR(GVAR(store),nil); -if (isNil "_store") then { - _store = [] call FUNC(initDB); -}; +private _store = GETMVAR(FORGE_STORE_REG,createHashMap); +if (isNil "_store") then { _store = [] call FUNC(initDB) }; _store \ No newline at end of file