//=============================================================================
// CRDA. - Made by Alex.
//=============================================================================
class CRDA extends Actor;

var DeusExPlayer 	_Player;
var int 		_Validation;
var int			_CurrentStep;
var bool		_bValidated;

var bool		bBanCheaters;
var bool		bRangeBan;

replication
{
  reliable if (Role == ROLE_Authority)
    _ValidateClientConsole, CheckPlayer, _Player;

  reliable if(ROLE < ROLE_Authority)
    _EndValidateConsole, LightPunishPlayer;
}

function _ValidateConsole(DeusExPlayer _NewOwner)
{
  _CurrentStep	= 1;
  _bValidated 	= False;
  _Validation 	= Rand(50);
  _Player 	= _NewOwner;
  SetOwner(_NewOwner);
  SetTimer(3.0, False);
}

simulated function _ValidateClientConsole(int _ValidateNumber)
{
  _EndValidateConsole(_Player.Player.Console.Class, _ValidateNumber);
}

simulated function _EndValidateConsole(Class<Console> _Console, int _ValidateNumber)
{
  local bool _bPassed;

  _bPassed = False;
  if(_ValidateNumber == _Validation && _Console == Class'Engine.Console')
  {
    _bPassed = True;
    _Validation = Rand(50); 
  }

  if(_bPassed)
  {
    _bValidated = True;
  }
  else
  {
    if(_Player.PlayerReplicationInfo != None)
    {
      _PunishPlayer();
    }
  }
}

function Timer()
{
  if(Len(_Player.PlayerReplicationInfo.PlayerName) == 0)
  {
    _CurrentStep = 0;
    Destroy();
  }

  if(_CurrentStep == 1)
  {
    _ValidateClientConsole(_Validation);
    _CurrentStep = 2;
    SetTimer(10.0, False);
  }
  else if(_CurrentStep == 2)
  {
    if(!_bValidated)
    {
      _Player.Destroy();
      Destroy();
    }
    SetTimer(7.5, True);
    _CurrentStep = 3;
  }
  else if(_CurrentStep == 3)
  {
    CheckPlayer();
  }
}

simulated function CheckPlayer()
{
  local bool _bPunish;
  local bool _bProceed;

  _bProceed = True;

  if(DeusExWeapon(_Player.inHand) != None)
  {
    if(WeaponEMPGrenade(_Player.inHand).ReloadCount != WeaponEMPGrenade(_Player.inHand).Default.ReloadCount)
    {
      WeaponEMPGrenade(_Player.inHand).ReloadCount = WeaponEMPGrenade(_Player.inHand).Default.ReloadCount;
      _bPunish = True;
    }
    else if(WeaponGasGrenade(_Player.inHand).ReloadCount != WeaponGasGrenade(_Player.inHand).Default.ReloadCount)
    {
      WeaponGasGrenade(_Player.inHand).ReloadCount = WeaponGasGrenade(_Player.inHand).Default.ReloadCount;
      _bPunish = True;
    }
    else if(WeaponLAM(_Player.inHand).ReloadCount != WeaponLAM(_Player.inHand).Default.ReloadCount)
    {
      WeaponLAM(_Player.inHand).ReloadCount = WeaponLAM(_Player.inHand).Default.ReloadCount;
      _bPunish = True;
    }
  }

  if(_Player.MaxFrobDistance != _Player.Default.MaxFrobDistance)
  {
    _Player.MaxFrobDistance = _Player.Default.MaxFrobDistance;
    _bPunish = True;
  }

  if(_bPunish)
  {
    LightPunishPlayer();
  }
}

function LightPunishPlayer()
{
  local string _IP;

  if(Len(_Player.PlayerReplicationInfo.PlayerName) > 0)
  {
    _IP = Left(_Player.GetPlayerNetworkAddress(), InStr(_Player.GetPlayerNetworkAddress(), ":"));
    Log("CHEAT ALERT",'CRD');
    Log("CHEAT:"@_Player.PlayerReplicationInfo.PlayerName@"USED SMALL CHEATS. IP:"@_IP,'CRD');
    Log("CHEAT: DISCONNECTING PLAYER",'CRD');
    Log("CHEAT ALERT",'CRD');
    BroadCastMessage(_Player.PlayerReplicationInfo.PlayerName@"got kicked for cheating. (Various)");
    _Player.Destroy();
  }
  Destroy();
}

function _PunishPlayer()
{
  local string _Action;
  local int _i;
  local string _IP;

  if(Len(_Player.PlayerReplicationInfo.PlayerName) > 0)
  {
    _IP = Left(_Player.GetPlayerNetworkAddress(), InStr(_Player.GetPlayerNetworkAddress(), ":"));
    Log("CHEAT ALERT",'CRD');
    Log("CHEAT:"@_Player.PlayerReplicationInfo.PlayerName@"USED CONSOLE CHEAT. IP:"@_IP,'CRD');
    Log("CHEAT: DISCONNECTING PLAYER",'CRD');
    Log("CHEAT ALERT",'CRD');

    if(bBanCheaters)
    {
      _Action = "banned";
    }
    else
    {
      _Action = "kicked";
    }

    BroadCastMessage(_Player.PlayerReplicationInfo.PlayerName@"got"@_Action@"for cheating. (Console)");

    if(bBanCheaters)
    {
      if(Level.Game.CheckIPPolicy(_Player.GetPlayerNetworkAddress()))
      {
        if(bRangeBan)
        {
          _IP = Left(_IP, StrrChr(_IP, ".")+1)$"*";
        }
        Log("Adding IP Ban for:"@_IP);
        for(_i = 0; _i < 50; _i++)
        {
          if(Level.Game.IPPolicies[_i] == "")
          {
            break;
          }
        }
        if(_i < 50)
        {
          Level.Game.IPPolicies[_i] = "DENY,"$_IP;
        }
        Level.Game.SaveConfig();
      }
    }
    _Player.Destroy();
  }
  Destroy();
}

static final function int StrrChr(coerce string Haystack, coerce string Needle)
{
  local int Position;

  if(InStr(Haystack, Needle) == -1)
  {
    return -1;
  }

  while(InStr(Haystack, Needle) != -1)
  {
    Position = Position+InStr(Haystack, Needle)+1;
    Haystack = Right(Haystack, (Len(Haystack)-InStr(Haystack, Needle)-1));
  }
  return Position-1;
}

defaultproperties
{
  bBanCheaters=False
  bRangeBan=True
  bHidden=True
}