Add random demay in AutoRelog

Useful when reconnecting many accounts at once
Related issues: #663, #740, #975, #1312
This commit is contained in:
ORelio 2020-11-04 19:23:46 +01:00
parent bea5161d6f
commit 330dda9c15
5 changed files with 47 additions and 24 deletions

View file

@ -10,22 +10,29 @@ namespace MinecraftClient.ChatBots
/// </summary>
public class AutoRelog : ChatBot
{
private static Random random = new Random();
private string[] dictionary = new string[0];
private int attempts;
private int delay;
private int delayMin;
private int delayMax;
/// <summary>
/// This bot automatically re-join the server if kick message contains predefined string
/// </summary>
/// <param name="DelayBeforeRelog">Delay before re-joining the server (in seconds)</param>
/// <param name="DelayBeforeRelogMin">Minimum delay before re-joining the server (in seconds)</param>
/// <param name="DelayBeforeRelogMax">Maximum delay before re-joining the server (in seconds)</param>
/// <param name="retries">Number of retries if connection fails (-1 = infinite)</param>
public AutoRelog(int DelayBeforeRelog, int retries)
public AutoRelog(int DelayBeforeRelogMin, int DelayBeforeRelogMax, int retries)
{
attempts = retries;
if (attempts == -1) { attempts = int.MaxValue; }
McClient.ReconnectionAttemptsLeft = attempts;
delay = DelayBeforeRelog;
if (delay < 1) { delay = 1; }
delayMin = DelayBeforeRelogMin;
delayMax = DelayBeforeRelogMax;
if (delayMin < 1)
delayMin = 1;
if (delayMax < delayMin)
delayMax = delayMin;
LogDebugToConsoleTranslated("bot.autoRelog.launch", attempts);
}
@ -63,7 +70,7 @@ namespace MinecraftClient.ChatBots
{
if (reason == DisconnectReason.UserLogout)
{
LogDebugToConsoleTranslated("bot.autoRelog.ignore");
LogDebugToConsoleTranslated("bot.autoRelog.ignore_user_logout");
}
else
{
@ -74,10 +81,7 @@ namespace MinecraftClient.ChatBots
if (Settings.AutoRelog_IgnoreKickMessage)
{
LogDebugToConsoleTranslated("bot.autoRelog.reconnect_always");
LogToConsoleTranslated("bot.autoRelog.wait", delay);
System.Threading.Thread.Sleep(delay * 1000);
ReconnectToTheServer();
LaunchDelayedReconnection(null);
return true;
}
@ -85,11 +89,7 @@ namespace MinecraftClient.ChatBots
{
if (comp.Contains(msg))
{
LogDebugToConsoleTranslated("bot.autoRelog.reconnect", msg);
LogToConsoleTranslated("bot.autoRelog.wait", delay);
System.Threading.Thread.Sleep(delay * 1000);
McClient.ReconnectionAttemptsLeft = attempts;
ReconnectToTheServer();
LaunchDelayedReconnection(msg);
return true;
}
}
@ -100,11 +100,20 @@ namespace MinecraftClient.ChatBots
return false;
}
private void LaunchDelayedReconnection(string msg)
{
int delay = random.Next(delayMin, delayMax);
LogDebugToConsoleTranslated(String.IsNullOrEmpty(msg) ? "bot.autoRelog.reconnect_always" : "bot.autoRelog.reconnect", msg);
LogToConsoleTranslated("bot.autoRelog.wait", delay);
System.Threading.Thread.Sleep(delay * 1000);
ReconnectToTheServer();
}
public static bool OnDisconnectStatic(DisconnectReason reason, string message)
{
if (Settings.AutoRelog_Enabled)
{
AutoRelog bot = new AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries);
AutoRelog bot = new AutoRelog(Settings.AutoRelog_Delay_Min, Settings.AutoRelog_Delay_Max, Settings.AutoRelog_Retries);
bot.Initialize();
return bot.OnDisconnect(reason, message);
}

View file

@ -185,7 +185,7 @@ namespace MinecraftClient
if (Settings.Alerts_Enabled) { BotLoad(new ChatBots.Alerts()); }
if (Settings.ChatLog_Enabled) { BotLoad(new ChatBots.ChatLog(Settings.ExpandVars(Settings.ChatLog_File), Settings.ChatLog_Filter, Settings.ChatLog_DateTime)); }
if (Settings.PlayerLog_Enabled) { BotLoad(new ChatBots.PlayerListLogger(Settings.PlayerLog_Delay, Settings.ExpandVars(Settings.PlayerLog_File))); }
if (Settings.AutoRelog_Enabled) { BotLoad(new ChatBots.AutoRelog(Settings.AutoRelog_Delay, Settings.AutoRelog_Retries)); }
if (Settings.AutoRelog_Enabled) { BotLoad(new ChatBots.AutoRelog(Settings.AutoRelog_Delay_Min, Settings.AutoRelog_Delay_Max, Settings.AutoRelog_Retries)); }
if (Settings.ScriptScheduler_Enabled) { BotLoad(new ChatBots.ScriptScheduler(Settings.ExpandVars(Settings.ScriptScheduler_TasksFile))); }
if (Settings.RemoteCtrl_Enabled) { BotLoad(new ChatBots.RemoteControl()); }
if (Settings.AutoRespond_Enabled) { BotLoad(new ChatBots.AutoRespond(Settings.AutoRespond_Matches)); }

View file

@ -88,10 +88,10 @@ command=/ping
[AutoRelog]
enabled=false
delay=10
retries=3 #-1 = unlimited
ignorekickmessage=false
kickmessagesfile=kickmessages.txt
delay=10 # use 10 for 10 seconds, 10-60 for a random delay between 10 and 60 seconds
retries=3 # retries when failing to relog to the server. use -1 for unlimited retries
kickmessagesfile=kickmessages.txt # file with list of matches in kick messages that will trigger autorelog
ignorekickmessage=false # when set to true, autorelog will reconnect regardless of kick messages
[ChatLog]
enabled=false

View file

@ -401,7 +401,7 @@ bot.autoRelog.loading=Loading messages from file: {0}
bot.autoRelog.loaded=Loaded message: {0}
bot.autoRelog.not_found=File not found: {0}
bot.autoRelog.curr_dir=Current directory was: {0}
bot.autoRelog.ignore=Disconnection initiated by User or MCC bot. Ignoring.
bot.autoRelog.ignore_user_logout=Disconnection initiated by User or MCC bot. Ignoring.
bot.autoRelog.disconnect_msg=Got disconnected with message: {0}
bot.autoRelog.reconnect_always=Ignoring kick message, reconnecting anyway.
bot.autoRelog.reconnect=Message contains '{0}'. Reconnecting.

View file

@ -129,7 +129,8 @@ namespace MinecraftClient
//AutoRelog Settings
public static bool AutoRelog_Enabled = false;
public static int AutoRelog_Delay = 10;
public static int AutoRelog_Delay_Min = 10;
public static int AutoRelog_Delay_Max = 10;
public static int AutoRelog_Retries = 3;
public static bool AutoRelog_IgnoreKickMessage = false;
public static string AutoRelog_KickMessagesFile = "kickmessages.txt";
@ -414,10 +415,23 @@ namespace MinecraftClient
switch (argName.ToLower())
{
case "enabled": AutoRelog_Enabled = str2bool(argValue); break;
case "delay": AutoRelog_Delay = str2int(argValue); break;
case "retries": AutoRelog_Retries = str2int(argValue); break;
case "ignorekickmessage": AutoRelog_IgnoreKickMessage = str2bool(argValue); break;
case "kickmessagesfile": AutoRelog_KickMessagesFile = argValue; break;
case "delay":
string[] delayParts = argValue.Split('-');
if (delayParts.Length == 1)
{
AutoRelog_Delay_Min = str2int(delayParts[0]);
AutoRelog_Delay_Max = AutoRelog_Delay_Min;
}
else
{
AutoRelog_Delay_Min = str2int(delayParts[0]);
AutoRelog_Delay_Max = str2int(delayParts[1]);
}
break;
}
break;