Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into optional-file-for-r…
Browse files Browse the repository at this point in the history
…epository-settings
  • Loading branch information
commy2 committed Aug 6, 2016
2 parents b52d257 + 945260c commit afcd318
Show file tree
Hide file tree
Showing 13 changed files with 570 additions and 87 deletions.
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
language: python
python:
- '3.4'
script:
- python3 tools/sqf_validator.py
- python3 tools/config_style_checker.py
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Community Base Addons

[![](https://travis-ci.org/CBATeam/CBA_A3.svg?style=flat-square)](https://travis-ci.org/CBATeam/CBA_A3)
[![](https://img.shields.io/badge/Changelog-3.0.0-orange.svg?style=flat-square)](https://github.com/CBATeam/CBA_A3/issues?q=milestone%3A3.0.0+is%3Aclosed)
[![](https://img.shields.io/badge/Release-3.0.0-blue.svg?style=flat-square)](https://github.com/CBATeam/CBA_A3/releases/tag/v3.0.0.160713)
[![](https://img.shields.io/badge/Github-Wiki-lightgrey.svg?style=flat-square)](https://github.com/CBATeam/CBA_A3/wiki)
Expand Down
5 changes: 5 additions & 0 deletions addons/hashes/CfgFunctions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class CfgFunctions
description = "Iterate through all keys and values in a Hash.";
file = "\x\cba\addons\hashes\fnc_hashEachPair.sqf";
};
class hashFilter
{
description = "Iterate through all keys and values in a Hash. Code must return a bool, if false then key is removed";
file = "\x\cba\addons\hashes\fnc_hashFilter.sqf";
};
// CBA_fnc_hashGet
class hashGet
{
Expand Down
59 changes: 59 additions & 0 deletions addons/hashes/fnc_hashFilter.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/* ----------------------------------------------------------------------------
Function: CBA_fnc_hashFilter
Description:
Iterate through all keys and values in a Hash.
If function returns false, the key is removed from the hash (just like `[] select {}`)
Data passed to the function on each iteration,
* _key - Key from the Hash.
* _value - The value from the Hash corresponding to _key.
See <CBA_fnc_hashCreate>.
Parameters:
_hash - Hash to iterate [Array which is a Hash structure]
_code - Function to call with each pair which returns a bool (false will remove key from hash) [Code]
Returns:
Number of removed entrys [Number]
Example:
(begin example)
_hash = [[["A1", 1], ["A2", 1], ["B", 2], ["C", 3], ["D1", 4], ["D2", 4], ["E1", 5], ["E2", 5]]] call CBA_fnc_hashCreate;
_removeOddValues = {
diag_log format ["Key: %1, Value: %2", _key, _value];
((_value % 2) == 0)
};
_removedCount = [_hash, _removeOddValues] call CBA_fnc_hashFilter;
(end)
Author:
PabstMirror
---------------------------------------------------------------------------- */
//#define DEBUG_MODE_FULL
#include "script_component.hpp"
#include "script_hashes.hpp"

SCRIPT(hashFilter);

// -----------------------------------------------------------------------------
params [["_hash", [], [[]]], ["_code", {}, [{}]]];

_hash params ["", "_keys", "_values"];

private _removedKeys = 0;

{
private _key = _x;
private _index = _forEachIndex - _removedKeys;
private _value = _values select _index;
if (!(call _code)) then { // If code returns false, delete the key/value from hash
_keys deleteAt _index;
_values deleteAt _index;
_removedKeys = _removedKeys + 1;
};
nil
} forEach +_keys; // Create copy of _keys as the original can be modified during iteration

_removedKeys // Return.
2 changes: 1 addition & 1 deletion addons/hashes/test.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#define DEBUG_MODE_FULL
#include "script_component.hpp"

#define TESTS ["hashEachPair", "hashes", "parseYaml"]
#define TESTS ["hashEachPair", "hashes", "parseYaml", "hashFilter"]

SCRIPT(test-hashes);

Expand Down
60 changes: 60 additions & 0 deletions addons/hashes/test_hashFilter.sqf
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// ----------------------------------------------------------------------------
#define DEBUG_MODE_FULL
#include "script_component.hpp"

SCRIPT(test_hashFilter);

// ----------------------------------------------------------------------------

private ["_hash", "_expected", "_sumKeys", "_sumValues", "_totalIterations", "_removeOddValues"];

_fn = "CBA_fnc_hashFilter";
LOG("Testing " + _fn);

TEST_DEFINED("CBA_fnc_hashFilter","");

_hash = [[[1, 12], [5, 25]], 88] call CBA_fnc_hashCreate;

_sumKeys = 0;
_sumValues = 0;
_totalIterations = 0;

[_hash,
{
ADD(_sumKeys,_key);
ADD(_sumValues,_value);
INC(_totalIterations);
true
}] call CBA_fnc_hashFilter;

_expected = 6;
TEST_OP(_sumKeys,==,_expected,"");
_expected = 37;
TEST_OP(_sumValues,==,_expected,"");
_expected = 2;
TEST_OP(_totalIterations,==,_expected,"");


_hash = [[["A1", 1], ["A2", 1], ["B", 2], ["C", 3], ["D1", 4], ["D2", 4], ["E1", 5], ["E2", 5]]] call CBA_fnc_hashCreate;

_sumValues = 0;
_totalIterations = 0;

_removeOddValues = {
ADD(_sumValues,_value);
INC(_totalIterations);
((_value % 2) == 0)
};
_ret = [_hash, _removeOddValues] call CBA_fnc_hashFilter;

_expected = 5;
TEST_OP(_ret,isEqualTo,_expected,"");
_expected = ["B", "D1", "D2"];
TEST_OP(_hash select 1,isEqualTo,_expected,"");
_expected = [2, 4, 4];
TEST_OP(_hash select 2,isEqualTo,_expected,"");
_expected = 25;
TEST_OP(_sumValues,==,_expected,"");
_expected = 8;
TEST_OP(_totalIterations,==,_expected,"");

10 changes: 5 additions & 5 deletions addons/modules/fnc_getPosFromString.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Function: CBA_fnc_getPosFromString
Description:
A function used to get the position of an item when passed as a string.
Designed with modules in mind since information can, presently, only be passed as a string
or scalar. The type name needs to be passed along with the item so the function knows
how to extract the desired position.
Designed with modules in mind since information can, presently, only be passed as a string
or scalar. The type name needs to be passed along with the item so the function knows
how to extract the desired position.
Parameters:
- _type (String)
Expand All @@ -16,7 +16,7 @@ Parameters:
Example:
(begin example)
["OBJECT", "player"] call CBA_fnc_getPosFromString;
["MARKER","myMarker"] call CBA_fnc_getPosFromString;
["MARKER","myMarker"] call CBA_fnc_getPosFromString;
(end)
Returns:
Expand Down
76 changes: 38 additions & 38 deletions addons/network/fnc_publicVariable.sqf
Original file line number Diff line number Diff line change
Expand Up @@ -2,73 +2,73 @@
Function: CBA_fnc_publicVariable
Description:
CBA_fnc_publicVariable does only broadcast the new value if it doesn't exist in missionNamespace or the new value is different to the one in missionNamespace.
CBA_fnc_publicVariable does only broadcast the new value if it doesn't exist in missionNamespace or the new value is different to the one in missionNamespace.
Checks also for different types. Nil as value gets always broadcasted.
Checks also for different types. Nil as value gets always broadcasted.
Should reduce network traffic.
Should reduce network traffic.
Parameters:
_pv - Name of the publicVariable [String]
_value - Value to check and broadcast if it is not the same as the previous one, code will always be broadcasted [Any]
_pv - Name of the publicVariable [String]
_value - Value to check and broadcast if it is not the same as the previous one, code will always be broadcasted [Any]
Returns:
True if if broadcasted, otherwise false [Boolean]
True if if broadcasted, otherwise false [Boolean]
Example:
(begin example)
// This will only broadcast "somefish" if it either doesn't exist yet in the missionNamespace or the value is not 50
_broadcasted = ["somefish", 50] call CBA_fnc_publicVariable;
(end)
(begin example)
// This will only broadcast "somefish" if it either doesn't exist yet in the missionNamespace or the value is not 50
_broadcasted = ["somefish", 50] call CBA_fnc_publicVariable;
(end)
Author:
Xeno
Xeno
*/
// #define DEBUG_MODE_FULL
#include "script_component.hpp"

params ["_pv","_value"];

if (typeName _pv != typeName "") exitWith {
WARNING("The first parameter is not of type string!");
false
WARNING("The first parameter is not of type string!");
false
};

private ["_var","_s"];
_var = missionNamespace getVariable _pv;

if (isNil "_var") exitWith {
TRACE_2("Broadcasting",_pv,_value);
missionNamespace setVariable [_pv, _value];
publicVariable _pv;
true
TRACE_2("Broadcasting",_pv,_value);
missionNamespace setVariable [_pv, _value];
publicVariable _pv;
true
};

_s = if (typeName _value != typeName _var) then {
TRACE_2("Different typenames",_var,_value);
false
TRACE_2("Different typenames",_var,_value);
false
} else {
switch (typename _value) do {
case "BOOL": {
((_var && {_value}) || {(!_var && {!_value})})
};
case "ARRAY": {
(_var isEqualTo _value)
};
case "CODE": {
false
};
case "SCRIPT": {
false
};
default {
(_var == _value)
};
}
switch (typename _value) do {
case "BOOL": {
((_var && {_value}) || {(!_var && {!_value})})
};
case "ARRAY": {
(_var isEqualTo _value)
};
case "CODE": {
false
};
case "SCRIPT": {
false
};
default {
(_var == _value)
};
}
};
if (_s) exitwith {
TRACE_2("Not broadcasting, _var and _value are equal",_var,_value);
false
TRACE_2("Not broadcasting, _var and _value are equal",_var,_value);
false
};

TRACE_2("Broadcasting",_pv,_value);
Expand Down
Loading

0 comments on commit afcd318

Please sign in to comment.