Skip to content

Commit

Permalink
Add command-line option to skip auto connect/disconnect in script. Al…
Browse files Browse the repository at this point in the history
…lows quicker testing of scripts that don't do any connection stuff or scripts that want more control over when to connect (for single bot instance)
  • Loading branch information
ethanmoffat committed Feb 3, 2022
1 parent 1b72feb commit 84c933b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
5 changes: 5 additions & 0 deletions EOBot/ArgumentsParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public class ArgumentsParser
public string Password { get; private set; }
public string Character { get; private set; }

public bool AutoConnect { get; private set; } = true;

public List<string> UserArgs { get; internal set; }

public ArgumentsParser(string[] args)
Expand Down Expand Up @@ -84,6 +86,9 @@ public ArgumentsParser(string[] args)
}
ScriptFile = pair[1];
break;
case "autoconnect":
AutoConnect = bool.Parse(pair[1]);
break;
case "host":
ParseHost(pair[1]);
break;
Expand Down
24 changes: 16 additions & 8 deletions EOBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,12 @@ static async Task Main(string[] args)
botFactory = new TrainerBotFactory(parsedArgs);
}

if (parsedArgs.NumBots > 1 && parsedArgs.ScriptFile != null && !parsedArgs.AutoConnect )
{
ConsoleHelper.WriteMessage(ConsoleHelper.Type.Error, "AutoConnect is required when using a script with more than 1 bot due to eoserv connection throttling");
return;
}

ConsoleHelper.WriteMessage(ConsoleHelper.Type.None, "Starting bots...");

try
Expand Down Expand Up @@ -276,15 +282,17 @@ static void ShowError(ArgumentsParser args)
" password=<password>\n" +
" character=<character>\n" +
" script=<file>\n" +
" autoconnect=<true|false>" +
" [-- arg1, [arg2..argn]]");
Console.WriteLine("\t host: hostname or IP address");
Console.WriteLine("\t port: port to connect on (probably 8078)");
Console.WriteLine("\t bots: number of bots to execute. \n\t numBots is the total number, simultaneousBots is how many will run at once");
Console.WriteLine("\t initDelay: Time in milliseconds to delay between doing the INIT handshake with the server");
Console.WriteLine("\t account: Account to connect with (created if it does not exist)");
Console.WriteLine("\t password: Password");
Console.WriteLine("\t character: Character to use (created if it does not exist)");
Console.WriteLine("\t script: script file to execute\n\t if script is not specified, default trainer bot will be used");
Console.WriteLine("\t host: hostname or IP address");
Console.WriteLine("\t port: port to connect on (probably 8078)");
Console.WriteLine("\t bots: number of bots to execute. \n\t numBots is the total number, simultaneousBots is how many will run at once");
Console.WriteLine("\t initDelay: time in milliseconds to delay between doing the INIT handshake with the server");
Console.WriteLine("\t account: account to connect with (created if it does not exist)");
Console.WriteLine("\t password: password for account");
Console.WriteLine("\t character: character to use (created if it does not exist)");
Console.WriteLine("\t script: script file to execute\n\t if script is not specified, default trainer bot will be used");
Console.WriteLine("\t autoconnect: (default true) true to automatically connect/disconnect to server with initDelay timeout between connection attempts for bots, false otherwise");
Console.WriteLine("\t --: Any arguments passed after '--' will be available in a script under the '$args' array");
}
}
Expand Down
25 changes: 14 additions & 11 deletions EOBot/ScriptedBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,23 @@ public override async Task InitializeAsync(string host, int port)
var tokens = _interpreter.Parse();
_programState = _interpreter.Prepare(_index, _parsedArgs, tokens);

var connectFunction = _programState.SymbolTable[PredefinedIdentifiers.CONNECT_FUNC].Identifiable as AsyncVoidFunction<string, int>;
if (connectFunction == null)
throw new InvalidOperationException("Something went wrong getting the connect function out of the symbol table");
if (_parsedArgs.AutoConnect)
{
var connectFunction = _programState.SymbolTable[PredefinedIdentifiers.CONNECT_FUNC].Identifiable as AsyncVoidFunction<string, int>;
if (connectFunction == null)
throw new InvalidOperationException("Something went wrong getting the connect function out of the symbol table");

// call connect function that uses user-defined $version variable instead of base logic that has it hard-coded
await connectFunction.CallAsync(new StringVariable(_parsedArgs.Host), new IntVariable(_parsedArgs.Port));
// call connect function that uses user-defined $version variable instead of base logic that has it hard-coded
await connectFunction.CallAsync(new StringVariable(_parsedArgs.Host), new IntVariable(_parsedArgs.Port));

WorkCompleted += () =>
{
Thread.Sleep(2000);
WorkCompleted += () =>
{
Thread.Sleep(2000);
var disconnectionFunction = _programState.SymbolTable[PredefinedIdentifiers.DISCONNECT_FUNC].Identifiable as VoidFunction;
disconnectionFunction.Call();
};
var disconnectionFunction = _programState.SymbolTable[PredefinedIdentifiers.DISCONNECT_FUNC].Identifiable as VoidFunction;
disconnectionFunction.Call();
};
}

_initialized = true;
}
Expand Down

0 comments on commit 84c933b

Please sign in to comment.