client/addons/org/functions/fnc_handleOrgLoad.sqf
Jacob Schmidt 31402b40db
All checks were successful
Build / Build (push) Successful in 27s
Refactor: Organization Store CRUD Operations and Function Simplification
This commit refactors the organization store to utilize the new CRUD (Create, Read, Update, Delete) operations and simplifies several organization-related functions.

*   **Organization Store CRUD Operations:** Implements a new interface for the organization store, providing methods for creating, reading, updating, and deleting organization data. This includes methods for adding and removing assets, managing funds and reputation, and handling member operations. The store now interfaces with the ArmaDragonflyClient database for persistence.
*   **Function Simplification:** Simplifies several organization functions by leveraging the new CRUD operations within the organization store. This reduces code duplication and improves readability. Specifically:
    *   `fnc_addAsset.sqf`: Simplified to use the `addAsset` method.
    *   `fnc_removeAsset.sqf`: Simplified to use the `removeAsset` method.
    *   `fnc_create.sqf`: Simplified to use the `createOrg` method.
    *   `fnc_leave.sqf`: Simplified to use the `leaveOrg` method.
    *   `fnc_disband.sqf`: Simplified to use the `deleteOrg` method.
    *   `fnc_addFunds.sqf`: Simplified to use the `updateFunds` method.
    *   `fnc_addReputation.sqf`: Simplified to use the `updateReputation` method.
    *   `fnc_verifyOrgStore.sqf`: Improved to ensure the store is initialized.
*   **XEH Updates:** Updated `XEH_PREP.hpp` files to reflect the changes in function calls.
*   **Locker Fix:** Fixed an issue in `fnc_equipGear.sqf` where the `isNull` check was incorrect for certain gear types.
2025-03-30 17:15:02 -05:00

230 lines
8.5 KiB
Plaintext

#include "..\script_component.hpp"
/*
* Function: forge_client_org_fnc_handleOrgLoad
* Author: J. Schmidt
*
* Description:
* Handles organization data loaded from database
*
* Arguments:
* 0: Result <ARRAY> - Redis HGETALL result
*
* Return Value:
* None
*/
diag_log text format ["[FORGE Organization] Organization data received: '%1'", _this];
private _data = _this;
private _playerNetId = netId player;
private _store = call FUNC(verifyOrgStore);
if (isNil "_data" || {!(_data isEqualType [])} || {count _data < 2} || {_data isEqualTo [""]}) exitWith {
diag_log text "[FORGE Organization] Empty or invalid organization data received";
if !(isNil "_store") then {
_store set ["currentOrganization", nil];
_store set ["isLoaded", true];
};
};
// Create organization data hashMap
private _orgData = createHashMap;
// Process the data array
for "_i" from 0 to (count _data - 1) step 2 do {
private _key = _data select _i;
private _value = _data select (_i + 1);
// Unwrap single-item arrays
if (_value isEqualType []) then {
switch (_key) do {
case "assets": {
// Convert to hashMap by assetType
private _flattenedAssets = [];
// Handle multi-level nesting and ensure it's flattened first
if (count _value > 0 && {_value select 0 isEqualType []}) then {
{
if (_x isEqualType []) then {
_flattenedAssets append _x;
} else {
_flattenedAssets pushBack _x;
};
} forEach _value;
} else {
_flattenedAssets = _value;
};
// Create hashMap from flattened assets
private _assetsMap = createHashMap;
{
if (_x isEqualType [] && count _x >= 2) then {
private _type = _x select 0;
private _class = _x select 1;
// Create a hashMap for this asset
private _assetMap = createHashMap;
// Try to get ID or generate one
private _assetId = "";
private _idIndex = _x findIf {_x == "id"};
if (_idIndex != -1 && _idIndex + 1 < count _x) then {
_assetId = _x select (_idIndex + 1);
} else {
_assetId = format ["%1_%2", _class, diag_tickTime];
};
_assetMap set ["id", _assetId];
_assetMap set ["className", _class];
// Add any extra properties (key-value pairs)
for "_j" from 2 to (count _x - 1) step 2 do {
if (_j + 1 < count _x) then {
private _propName = _x select _j;
private _propValue = _x select (_j + 1);
if (_propName != "className") then {
_assetMap set [_propName, _propValue];
};
};
};
// Add this asset to its type array
private _typeArray = _assetsMap getOrDefault [_type, []];
_typeArray pushBack _assetMap;
_assetsMap set [_type, _typeArray];
};
} forEach _flattenedAssets;
_value = _assetsMap;
};
case "members": {
// Convert to hashMap by memberUID
private _flattenedMembers = [];
// Handle multi-level nesting and ensure it's flattened first
if (count _value > 0 && {_value select 0 isEqualType []}) then {
{
if (_x isEqualType []) then {
_flattenedMembers append _x;
} else {
_flattenedMembers pushBack _x;
};
} forEach _value;
} else {
_flattenedMembers = _value;
};
// Create member hashMap
private _membersMap = createHashMap;
{
if (_x isEqualType [] && count _x >= 3) then {
private _uid = _x select 0;
private _name = _x select 1;
private _role = _x select 2;
private _joinDate = if (count _x > 3) then {_x select 3} else {""};
// Replace underscores with spaces in name
if (_name isEqualType "") then {
_name = (_name splitString "_") joinString " ";
};
// Create member hashMap
private _memberMap = createHashMap;
_memberMap set ["uid", _uid];
_memberMap set ["name", _name];
_memberMap set ["role", _role];
_memberMap set ["joinDate", _joinDate];
// Add extra properties if any (for future expansion)
for "_j" from 4 to (count _x - 1) step 2 do {
if (_j + 1 < count _x) then {
private _propName = _x select _j;
private _propValue = _x select (_j + 1);
_memberMap set [_propName, _propValue];
};
};
_membersMap set [_uid, _memberMap];
};
} forEach _flattenedMembers;
_value = _membersMap;
};
case "logs": {
// Flatten arrays but don't process contents
private _flattenedArray = [];
// Handle multi-level nesting
if (count _value > 0 && {_value select 0 isEqualType []}) then {
{
if (_x isEqualType []) then {
_flattenedArray append _x;
} else {
_flattenedArray pushBack _x;
};
} forEach _value;
_value = _flattenedArray;
};
};
default {
if (count _value == 1) then {
_value = _value select 0;
};
};
};
};
// Process string values
if (_value isEqualType "" && _key in ["name", "owner"]) then {
_value = (_value splitString "_") joinString " ";
};
// Convert numeric values
if (_key in ["funds", "reputation"] && _value isEqualType "") then {
_value = parseNumber _value;
};
// Store in hashMap
_orgData set [_key, _value];
};
// Ensure we have all required fields in the hashMap
private _requiredFields = ["id", "name", "owner", "funds", "reputation", "assets", "members", "logs", "created", "lastModified"];
{
if (isNil {_orgData get _x}) then {
switch (_x) do {
case "funds";
case "reputation": {
_orgData set [_x, 0];
};
case "assets";
case "members": {
_orgData set [_x, createHashMap];
};
case "logs": {
_orgData set [_x, []];
};
case "created";
case "lastModified": {
_orgData set [_x, ""];
};
default {
_orgData set [_x, ""];
};
};
};
} forEach _requiredFields;
// Update the organization store with the complete hashMap
_store set ["currentOrganization", _orgData];
_store set ["isLoaded", true];
// Log successful load
if (!isNil { _orgData get "name" }) then {
diag_log text format ["[FORGE Organization] Organization successfully loaded: %1", _orgData get "name"];
};