Skip to content

Instantly share code, notes, and snippets.

@pauliusdotpro
Created August 23, 2016 16:21
Show Gist options
  • Save pauliusdotpro/ce2d376f69d406eb93cabc51d5d3636b to your computer and use it in GitHub Desktop.
Save pauliusdotpro/ce2d376f69d406eb93cabc51d5d3636b to your computer and use it in GitHub Desktop.
using serverMP;
using System.Collections.Generic;
using System.Timers;
using serverMP.APIScript;
namespace Gamemode
{
struct PlayerPing
{
public PlayerInfo player;
public int counter;
public PlayerPing(PlayerInfo playr, int count)
{
player = playr;
counter = count;
}
}
class PingCheck : ScriptAPI
{
public List<PlayerPing> playerPing = new List<PlayerPing>();
//SETINGS
public const int allowedPing = 150; // MAXIMUM ALLOWED PING AVERAGE
public const int allowedHits = 2; // MAXIMUM ALLOWED HITS;
public const int checkInterval = 5; // CHECK INTERVAL IN SECONDS
public void Start()
{
Timer aTimer = new Timer();
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);
aTimer.Interval = checkInterval * 1000;
aTimer.Enabled = true;
}
private void OnTimedEvent(object source, ElapsedEventArgs e)
{
foreach(PlayerInfo player in PlayerInfo.PlayersList)
{
if(player.GetAveragePing > allowedPing) // IF PLAYER'S PING IS HIGHER THEN ALLOWED
{
if(GetPlayerPing(player) != null)
{
PlayerPing ping = (PlayerPing)GetPlayerPing(player);
if(ping.counter >= allowedHits) // IF PLAYER REACHED MAX HITS LIMIT
{
player.Kick("Your average ping is too high! (Ping: " + player.GetAveragePing + ")");
playerPing.Remove(ping);
}
else // IF PLAYER HAVENT REACHED MAX HITS LIMIT
{
SendPlayerMessage(player, "Your average ping is too high! (Ping: " + player.GetAveragePing + ")");
ping.counter++;
}
}
else // IF IT IS FIRST TIME THAT PLAYER REACHED HIGHER PING
{
PlayerPing ping = new PlayerPing(player, 1);
playerPing.Add(ping);
}
}
else // IF PLAYER'S PING IS LESS THEN ALLOWED
{
if(GetPlayerPing(player) != null)
{
playerPing.Remove((PlayerPing)GetPlayerPing(player));
}
}
}
}
private PlayerPing? GetPlayerPing(PlayerInfo player)
{
foreach(PlayerPing ping in playerPing)
{
if(ping.player == player)
{
return ping;
}
}
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment