server/addons/db/functions/fnc_loadPlayerState.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

85 lines
3.1 KiB
Plaintext

#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 <OBJECT>
* 1: _nameSpace - Namespace to load from (mission, profile) <STRING> (default: mission)
*
* Return Value:
* Success <BOOL>
*/
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