From ebcf1687a7fd3eb342ef64d9e065614b2b592fe9 Mon Sep 17 00:00:00 2001 From: SilentSpike Date: Tue, 21 Jun 2016 18:42:15 +0100 Subject: [PATCH 1/2] Generate SSN of unit from their name Similar to how we're handling blood types, uses the unit's name to generate a valid three/two/four format SSN. For names less than 9 characters long this will currently generate a unique SSN only up to the length of the name and append valid digits as necessary. --- addons/dogtags/XEH_PREP.hpp | 1 + .../dogtags/functions/fnc_getDogtagData.sqf | 4 +--- addons/dogtags/functions/fnc_ssn.sqf | 23 +++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 addons/dogtags/functions/fnc_ssn.sqf diff --git a/addons/dogtags/XEH_PREP.hpp b/addons/dogtags/XEH_PREP.hpp index 370396e1a3..33e42ba191 100644 --- a/addons/dogtags/XEH_PREP.hpp +++ b/addons/dogtags/XEH_PREP.hpp @@ -9,4 +9,5 @@ PREP(getDogtagData); PREP(getDogtagItem); PREP(sendDogtagData); PREP(showDogtag); +PREP(ssn); PREP(takeDogtag); diff --git a/addons/dogtags/functions/fnc_getDogtagData.sqf b/addons/dogtags/functions/fnc_getDogtagData.sqf index 9e9fd6b387..485e09789e 100644 --- a/addons/dogtags/functions/fnc_getDogtagData.sqf +++ b/addons/dogtags/functions/fnc_getDogtagData.sqf @@ -23,9 +23,7 @@ private _targetName = [_target, false, true] call EFUNC(common,getName); private _dogTagData = [ _targetName, - str(floor random 10) + str(floor random 10) + str(floor random 10) + "-" + - str(floor random 10) + str(floor random 10) + "-" + - str(floor random 10) + str(floor random 10) + str(floor random 10), + _targetName call FUNC(ssn), _targetName call FUNC(bloodType) ]; // Store it diff --git a/addons/dogtags/functions/fnc_ssn.sqf b/addons/dogtags/functions/fnc_ssn.sqf new file mode 100644 index 0000000000..62f5745a40 --- /dev/null +++ b/addons/dogtags/functions/fnc_ssn.sqf @@ -0,0 +1,23 @@ +/* + * Author: SilentSpike + * Reports a social security number generated from the units name. + * + * Arguments: + * 0: Name of a unit + * + * Return Value: + * A random social security number + * + * Public: No + */ +#include "script_component.hpp" + +params ["_name"]; + +private _nums = ((toArray _name) select [0,9]) apply { _x % 10 }; + +while {count _nums < 9} do { + _nums pushBack (floor random 10); +}; + +([_nums select [0,3],_nums select [3,2], _nums select [5,4]] apply { _x joinString "" }) joinString "-" From 6d8121b5446276eab904279dcae0f3ecb0cd3fac Mon Sep 17 00:00:00 2001 From: SilentSpike Date: Tue, 21 Jun 2016 20:35:23 +0100 Subject: [PATCH 2/2] Improve unit name SSN generation Names shorter than the required 9 characters are now reused until we have 9 characters - making for fully unique generation. To keep things distributed as pleasantly as possible they're itterated over with a step of two - alternating between odd and even indexes every full loop. Similarly, the slice of string used for names longer than 9 characters is now offset based on the length of the name. --- addons/dogtags/functions/fnc_ssn.sqf | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/addons/dogtags/functions/fnc_ssn.sqf b/addons/dogtags/functions/fnc_ssn.sqf index 62f5745a40..adf5d1c6f8 100644 --- a/addons/dogtags/functions/fnc_ssn.sqf +++ b/addons/dogtags/functions/fnc_ssn.sqf @@ -6,18 +6,26 @@ * 0: Name of a unit * * Return Value: - * A random social security number + * A random three/two/four format social security number * * Public: No */ #include "script_component.hpp" params ["_name"]; +private _length = count _name; +private _chars = toArray _name; -private _nums = ((toArray _name) select [0,9]) apply { _x % 10 }; - -while {count _nums < 9} do { - _nums pushBack (floor random 10); +// For short names, reuse characters +if (_length < 9) then { + // Iterates every second character, swapping odd/even with each loop + for [{_i = 0},{_i < 2*(9 - _length)},{_i = _i + 2}] do { + _chars pushBack (_chars select floor((_i + (_i/_length % 2)) % _length)); + }; }; +// Offset array slice for long names to make generation more unique +private _slice = [0, _length % 9] select (_length > 9); +private _nums = (_chars select [_slice, 9]) apply { _x % 10 }; + ([_nums select [0,3],_nums select [3,2], _nums select [5,4]] apply { _x joinString "" }) joinString "-"