// revalidate any players in the queue
// compare their distance to the nearest base, and remove them if they're too far away (or dead)
#include "..\script_component.hpp"

private _stillValid = [];
private _maxRangeToReady = missionNamespace getVariable [QGVAR(setting_maxRangeToReady), 400];
{
	_x params ["_player", "_base", "_timeFiled"]; // _unitArr = [unit, baseName, timeInQueue]

	private _distanceToOriginalBase = _player distance _base;
	// get the closest base to the player
	private _nearestBase = [_player] call EFUNC(common,getNearestBase);
	private _isCloseEnoughToAnyBase = (_player distance _nearestBase) < _maxRangeToReady;

	if (not _isCloseEnoughToAnyBase || not (alive _player)) then {
		// don't include player in updated queue
		// log to rpt
		[
			LEVEL_INFO,
			QUOTE(COMPONENT),
			"PLAYER DEQUEUED AUTOMATICALLY",
			[_player, [
				["filedAtBase", [_base] call EFUNC(common,getNameOfBase)],
				["filedAtBaseDistance", _player distance _base],
				["closestBase", [_nearestBase] call EFUNC(common,getNameOfBase)],
				["closestBaseDistance", _player distance _nearestBase],
				["maxDistanceSetting", _maxRangeToReady],
				["inQueueDuration", diag_tickTime - _timeFiled]
			]] call EFUNC(common,addPlayerInfoToArray)
		] call EFUNC(common,log);
		// continue loop
		continue
	};

	// include player in updated queue, and update their location to nearest base
	_stillValid pushBackUnique [_player, _nearestBase, _timeFiled];
	// if player's base has changed, log to rpt
	if (_base != _nearestBase) then {
		[
			LEVEL_INFO,
			QUOTE(COMPONENT),
			"PLAYER BASE WAS UPDATED",
			[_player, [
				["filedAtBase", [_base] call EFUNC(common,getNameOfBase)],
				["filedAtBaseDistance", _player distance _base],
				["closestBase", [_nearestBase] call EFUNC(common,getNameOfBase)],
				["closestBaseDistance", _player distance _nearestBase],
				["maxDistanceSetting", _maxRangeToReady],
				["inQueueDuration", diag_tickTime - _timeFiled]
			]] call EFUNC(common,addPlayerInfoToArray)
		] call EFUNC(common,log);
	};
} forEach GVAR(reinsertionQueue);

// broadcast new list to all machines
GVAR(reinsertionQueue) = _stillValid;
publicVariable QGVAR(reinsertionQueue);