Commit d52a94fd authored by Razer's avatar Razer
Browse files

Fixed set/get world variables + added global state

parent 7dd98e77
......@@ -24,39 +24,44 @@
_data = ["myVariable","","NO VARIABLE FOUND"] call DB_fnc_getWorldVariable;
RETURNS:
Variable data, or default param
Variable data, or default param (nothing if error occured)
*/
/*
If world UUID
*/
// Client request
if(!isServer) exitWith {_this call DB_fnc_serverRequest};
#include "\ApmsDB\constants.hpp"
params [["_varName","",[""]],["_worldUUID","",[""]],"_default"];
params [["_varName","",[""]],["_worldUUID","",[""]],["_default",nil]];
if (_varName == "") exitWith {};
private _varIndex = DB_var_worldVariables findIf {_x select 0 == _varName};
private _varData = if (isRemoteExecuted || remoteExecutedOwner == 2) then {
DB_var_worldVariablesGlobal getVariable _varName;
} else {
DB_var_worldVariables getVariable _varName;
};
private _data = if (_varIndex == -1) then {
private _return = if (isNil "_varData") then {
if (_worldUUID == "" || {call DB_fnc_getWorldUUID == _worldUUID}) exitWith {};
private _request = [PROTOCOL_DBCALL_FUNCTION_GET_WORLD_VARIABLES,[PROTOCOL_DBCALL_ARGUMENT_WORLDUUID,_worldUUID]];
private _return = [_request] call DB_fnc_sendRequest;
private _return = [_request] call DB_fnc_sendRequest; // [[privateVariables],[globalVariables]]
if (_return isEqualType [] && {(_return select 0) isEqualTo "error"}) exitWith {};
_varIndex = _return findIf {_x select 0 == _varName};
[nil,((_return select _varIndex) select 1)] select (_varIndex != -1);
_return = [(_return select 0),(_return select 1)] select (isRemoteExecuted || remoteExecutedOwner == 2);
private _varIndex = _return findIf {_x select 0 == _varName};
[_default,((_return select _varIndex) select 1)] select (_varIndex != -1);
} else {
(DB_var_worldVariables select _varIndex) select 1
_varData
};
if (isNil "_data") exitWith {
if (isNil "_default") exitWith {};
_default
// Return data to client
if(isRemoteExecuted && (_fnc_scriptName == _fnc_scriptNameParent) && remoteExecutedOwner != 2) exitWith {
missionNamespace setVariable [("DB_var_response_" + _fnc_scriptName),_return,remoteExecutedOwner];
};
_data
\ No newline at end of file
_return
\ No newline at end of file
......@@ -13,10 +13,12 @@
/*
DESCRIPTION:
Add world variable to DB. You can only set variables to world where this function was called from.
This function is SERVER only. However, if you use global = true, this means any player can READ the data using getWorldVariable.
PARAMS:
1: STRING - Variable name.
2: ANY - Variable data.
2: ANYTHING - Variable data.
3: BOOL (optional) - Global - (default: false)
EXAMPLES:
_myData = [10,"TEST"];
......@@ -31,38 +33,24 @@
*/
#include "\ApmsDB\constants.hpp"
params [["_varName","",[""]],"_data"];
params [["_varName","",[""]],"_data",["_global",false,[true]]];
if (_varName == "") exitWith {false};
private _dataObject = [DB_var_worldVariables,DB_var_worldVariablesGlobal] select (_global);
private _oldData = _dataObject getVariable _varName;
private _noChanges = false;
if (!isNil "_oldData" && {_oldData isEqualTo _data}) exitWith {false}; // No data were changed
// Use global variable to remove need to call libredex everytime when using getVariable
private _varIndex = DB_var_worldVariables findIf {_x select 0 == _varName};
if (_varIndex != -1) then {
if (isNil "_data") exitWith {DB_var_worldVariables deleteAt _varIndex; _noChanges = true}; // Remove variable
private _oldVarName = ((DB_var_worldVariables select _varIndex) select 0);
private _oldData = ((DB_var_worldVariables select _varIndex) select 1);
if (_oldData isEqualTo _data && (_varName isEqualTo _oldVarName)) exitWith {_noChanges = true}; // Same data
DB_var_worldVariables set [_varIndex,[_varName,_data]];
} else {
if (isNil "_data") exitWith {_noChanges = true}; // No data to be set
DB_var_worldVariables pushBack [_varName,_data];
};
if (_noChanges) exitWith {true}; // No data were changed or it was reseted to nil
_dataObject setVariable [_varName,_data,_global];
(allVariables DB_var_worldVariables) apply {(_variables select 0) pushBack [_x,DB_var_worldVariables getVariable _x]};
(allVariables DB_var_worldVariablesGlobal) apply {(_variables select 1) pushBack [_x,DB_var_worldVariablesGlobal getVariable _x]};
private _request = [PROTOCOL_DBCALL_FUNCTION_SET_WORLD_VARIABLES,[PROTOCOL_DBCALL_ARGUMENT_VARIABLES,(DB_var_worldVariables)]];
private _request = [PROTOCOL_DBCALL_FUNCTION_SET_WORLD_VARIABLES,[PROTOCOL_DBCALL_ARGUMENT_VARIABLES,_variables]];
private _return = [_request] call DB_fnc_sendRequest;
if (_return isEqualType [] && {(_return select 0) isEqualTo "error"}) exitWith {
if (_varIndex != -1) then {
DB_var_worldVariables deleteAt _varIndex;
} else {
DB_var_worldVariables deleteAt ((count (DB_var_worldVariables)) - 1);
};
_dataObject setVariable [_varName,_oldData,_global]; // Reset to old data
false
};
......
......@@ -44,6 +44,10 @@ while{_doswitchloop} do {
if (_uuid != "") then {
_request = [PROTOCOL_DBCALL_FUNCTION_RETURN_ASYNC_MSG, ["msguuid", _uuid]];
if !(canSuspend) then {
diag_log ("<ApmsDB>: WARNING: (" + _fnc_scriptNameParent + "): Please use scheduled call for 'DB_fnc_sendRequest' for better performance!");
diag_log _originalrequest;
};
while{true} do {
_response = ("libredex" callExtension _request) select 0;
......@@ -51,9 +55,6 @@ while{_doswitchloop} do {
if (canSuspend) then {
uiSleep 0.01;
} else {
diag_log "<ApmsDB>: WARNING: Cannot sleep in context of";
diag_log _originalrequest;
};
};
......@@ -105,13 +106,13 @@ while{_doswitchloop} do {
#ifdef DB_DEBUG
if (DB_DEBUG) then {
diag_log "===========================APMSDB START OF REQUEST==================================";
diag_log format ["<ApmsDB>: DEBUG: CALLED FROM: %1",_fnc_scriptNameParent];
diag_log format ["<ApmsDB>: DEBUG: REQUEST TYPE: %1",_originalrequest select 0];
diag_log format ["<ApmsDB>: DEBUG: ORIGINAL REQUEST: %1 ",_originalrequest];
diag_log format ["<ApmsDB>: DEBUG: RESPONSE: %1 ",_response];
diag_log format ["<ApmsDB>: DEBUG: RETURN DATA: %1 ",_return];
diag_log "============================APMSDB END OF REQUEST===================================";
diag_log formatText ["===========================APMSDB START OF REQUEST=================================="];
diag_log formatText ["DEBUG: CALLED FROM: %1",_fnc_scriptNameParent];
diag_log formatText ["DEBUG: REQUEST TYPE: %1",_originalrequest select 0];
diag_log formatText ["DEBUG: ORIGINAL REQUEST: %1 ",_originalrequest];
diag_log formatText ["DEBUG: RESPONSE: %1 ",_response];
diag_log formatText ["DEBUG: RETURN DATA: %1 ",_return];
diag_log formatText ["============================APMSDB END OF REQUEST==================================="];
};
#endif
......
......@@ -170,7 +170,7 @@ class CfgFunctions
class getWorldUUID {};
class getLinkedWorlds {};
class getWorldVariable {};
class getWorldVariable {isGlobal = 1;};
class setWorldVariable {};
};
class Functions_Misc
......
......@@ -90,16 +90,17 @@ if (_startRestServer) then {
};
// Restore world variables
DB_var_worldVariables = uiNamespace;//call BASE_fnc_createNamespace;
// Create namespaces (simple object)
DB_var_worldVariables = createSimpleObject ["Helper_Base_F",[-1000,-1000,0],true];
DB_var_worldVariablesGlobal = createSimpleObject ["Helper_Base_F",[-1000,-1000,0]];
_request = [PROTOCOL_DBCALL_FUNCTION_GET_WORLD_VARIABLES,[]];
_variables = [_request] call DB_fnc_sendRequest;
_variables apply {
_x params ["_varName","_varData","_global"];
if (!(isNil "_global") && {_global isEqualTo true}) then {
DB_var_worldVariables setVariable [_varName,_varData,true];
} else {
DB_var_worldVariables setVariable [_varName,_varData];
};
_variables = [_request] call DB_fnc_sendRequest; // [[privateVariables],[globalVariables]]
if (!isNil "_variables" && {!(_variables isEqualTo [])}) then {
(_variables select 0) apply {DB_var_worldVariables setVariable [_x#0,_x#1]};
(_variables select 1) apply {DB_var_worldVariablesGlobal setVariable [_x#0,_x#1,true]};
diag_log format ["<ApmsDB>: INFO: Restored %1 world variable(s)",(count(_variables#0)) + (count(_variables#1))];
};
call DB_fnc_initExtensionCallback;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment