Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invoke - And other stuff #74

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions PROBot/BotClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,30 @@ public BotClient()
TextOptions = new Dictionary<int, TextOption>();
}

public void CancelInvokes()
{
if (Script != null)
foreach (Invoker invoker in Script.Invokes)
invoker.Called = true;
}

public void CallInvokes()
{
if (Script != null)
{
for (int i = Script.Invokes.Count - 1; i >= 0; i--)
{
if (Script.Invokes[i].Time < DateTime.UtcNow)
{
if (Script.Invokes[i].Called)
Script.Invokes.RemoveAt(i);
else
Script.Invokes[i].Call();
}
}
}
}

public void CreateText(int index, string content)
{
TextOptions[index] = new TextOption("Text " + index + ": ", "Custom text option " + index + " for use in scripts.", content);
Expand Down Expand Up @@ -152,6 +176,8 @@ public void Logout(bool allowAutoReconnect)

public void Update()
{
CallInvokes();

AutoReconnector.Update();

if (_loginRequested)
Expand Down
3 changes: 3 additions & 0 deletions PROBot/Scripting/BaseScript.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

namespace PROBot.Scripting
{
Expand All @@ -24,6 +25,8 @@ public virtual void OnLearningMove(string moveName, int pokemonIndex) { }

public abstract bool ExecuteNextAction();

public List<Invoker> Invokes = new List<Invoker>();

protected void LogMessage(string message)
{
ScriptMessage?.Invoke(message);
Expand Down
125 changes: 125 additions & 0 deletions PROBot/Scripting/LuaScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,12 @@ private void CreateLuaInstance()
// File editing actions
_lua.Globals["logToFile"] = new Action<string, DynValue, bool>(LogToFile);
_lua.Globals["readLinesFromFile"] = new Func<string, string[]>(ReadLinesFromFile);

_lua.Globals["login"] = new Action<string, string, string, int, string, int, string, string>(Login);
_lua.Globals["relog"] = new Action<float, string>(Relog);
_lua.Globals["startScript"] = new Func<bool>(StartScript);
_lua.Globals["invoke"] = new Action<DynValue, float, DynValue[]>(Invoke);
_lua.Globals["cancelInvokes"] = new Action(CancelInvokes);

foreach (string content in _libsContent)
{
Expand Down Expand Up @@ -430,6 +436,7 @@ private void Fatal(string message)
{
LogMessage(message);
Bot.Stop();
Bot.CancelInvokes();
}

// API: Displays the specified message to the message log and logs out.
Expand Down Expand Up @@ -2766,5 +2773,123 @@ private void SetTextOptionDescription(int index, string content)

Bot.TextOptions[index].Description = content;
}

// API: Logs in using specified credentials
private void Login(string accountName, string password, string server, int socks = 0, string host = "", int port = 0, string socksUser = "", string socksPass = "")
{
server = server.ToUpperInvariant();

if (Bot.Game != null)
{
Fatal("error: login: tried to login while already logged in");
return;
}

if (server != "BLUE" && server != "RED" && server != "YELLOW")
{
Fatal("error: login: tried to connect to an invalid server: \"" + server + "\"");
return;
}

LogMessage("Connecting to the server...");
Account account = new Account(accountName);
account.Password = password;
account.Server = server;

if (socks == 4 || socks == 5)
{
account.Socks.Version = (SocksVersion)socks;
account.Socks.Host = host;
account.Socks.Port = port;
account.Socks.Username = socksUser;
account.Socks.Password = socksPass;
}

Bot.Login(account);
}

// API: Logs out and logs back in after the specified number of seconds, then starts the script shortly after
private void Relog(float seconds, string message)
{
DynValue name = DynValue.NewString(Bot.Account.Name);
DynValue password = DynValue.NewString(Bot.Account.Password);
DynValue server = DynValue.NewString(Bot.Account.Server);

if (Bot.Account.Socks.Version != SocksVersion.None)
{
DynValue socks = DynValue.NewNumber((int)Bot.Account.Socks.Version);
DynValue host = DynValue.NewString(Bot.Account.Socks.Host);
DynValue port = DynValue.NewNumber(Bot.Account.Socks.Port);
DynValue socksUser = DynValue.NewString(Bot.Account.Socks.Username);
DynValue socksPass = DynValue.NewString(Bot.Account.Socks.Password);
Invoke(_lua.Globals.Get("login"), seconds, name, password, server, socks, host, port, socksUser, socksPass);
}
else
{
Invoke(_lua.Globals.Get("login"), seconds, name, password, server);
}

Invoke(_lua.Globals.Get("startScript"), seconds + 10);
Logout(message);
}

// API: Starts the loaded script (usable in the outer scope or with invoke)
private bool StartScript()
{
if (Bot.Game != null && (Bot.Running == BotClient.State.Stopped || Bot.Running == BotClient.State.Paused))
{
Bot.Start();
return true;
}

return false;
}

// API: Calls the specified function after the specified number of seconds
public void Invoke(DynValue function, float seconds, params DynValue[] args)
{
if (function.Type != DataType.Function && function.Type != DataType.ClrFunction)
{
Fatal("error: invoke: tried to call an invalid function");
return;
}

if (seconds == 0)
{
_lua.Call(function, args);
return;
}

Invoker invoker = new Invoker()
{
Function = function,
Time = DateTime.UtcNow.AddSeconds(seconds),
Script = this,
Args = args
};

Invokes.Add(invoker);
}

// API: Cancels all queued Invokes
private void CancelInvokes()
{
Bot.CancelInvokes();
}
}

public class Invoker
{
public DynValue Function;
public DateTime Time;
public LuaScript Script;
public DynValue[] Args;
public bool Called = false;

public void Call()
{
Called = true;
Script.Invoke(Function, 0, Args);
}
}
}
4 changes: 3 additions & 1 deletion PROShine/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ private void BotStopMenuItem_Click(object sender, RoutedEventArgs e)
{
lock (Bot)
{
Bot.Stop();
Bot.Stop();
}
}

Expand Down Expand Up @@ -403,6 +403,7 @@ private void Bot_ConnectionOpened()
SetTitle(Bot.Account.Name + " - " + Bot.Game.Server);
UpdateBotMenu();
LogoutMenuItem.IsEnabled = true;
LoginMenuItem.IsEnabled = false;
LoginButton.IsEnabled = true;
LoginButtonIcon.Icon = FontAwesome.WPF.FontAwesomeIcon.SignOut;
LogMessage("Connected, authenticating...");
Expand Down Expand Up @@ -823,6 +824,7 @@ private void StopScriptButton_Click(object sender, RoutedEventArgs e)
lock (Bot)
{
Bot.Stop();
Bot.CancelInvokes();
}
}

Expand Down
1 change: 1 addition & 0 deletions PROShine/Views/TeamView.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<ListView.ItemContainerStyle>
<Style>
<Setter Property="ToolTipService.ShowDuration" Value="60000"/>
<Setter Property="ToolTipService.InitialShowDelay" Value="0"/>
<Setter Property="Control.ToolTip">
<Setter.Value>
<Canvas Height="300" Width="430" Background="#403F45">
Expand Down