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

Macro Keybind in UI + Macro Timer in UI + Fix Lure Casting Timer Issue #1 #52

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
54 changes: 40 additions & 14 deletions Source/FishingFunBot/Bot/FishBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ public class FishingBot
public static ILog logger = LogManager.GetLogger("Fishbot");

private ConsoleKey castKey;
private List<ConsoleKey> tenMinKey;

private List<ConsoleKey> macroKeys;
private int macroTimer;
private DateTime StartTime = DateTime.Now;

private IBobberFinder bobberFinder;
private IBiteWatcher biteWatcher;
private bool isEnabled;
Expand All @@ -23,12 +27,13 @@ public class FishingBot

public event EventHandler<FishingEvent> FishingEventHandler;

public FishingBot(IBobberFinder bobberFinder, IBiteWatcher biteWatcher, ConsoleKey castKey, List<ConsoleKey> tenMinKey)
public FishingBot(IBobberFinder bobberFinder, IBiteWatcher biteWatcher, ConsoleKey castKey, List<ConsoleKey> macroKeys, int macroTimer)
{
this.bobberFinder = bobberFinder;
this.biteWatcher = biteWatcher;
this.castKey = castKey;
this.tenMinKey = tenMinKey;
this.macroKeys = macroKeys;
this.macroTimer = macroTimer;

logger.Info("FishBot Created.");

Expand All @@ -41,15 +46,15 @@ public void Start()

isEnabled = true;

DoTenMinuteKey();
DoMacroKeys();

while (isEnabled)
{
try
{
logger.Info($"Pressing key {castKey} to Cast.");

PressTenMinKeyIfDue();
PressMacroKeysIfDue();

FishingEventHandler?.Invoke(this, new FishingEvent { Action = FishingAction.Cast });
WowProcess.PressKey(castKey);
Expand All @@ -73,6 +78,21 @@ public void SetCastKey(ConsoleKey castKey)
this.castKey = castKey;
}

public void SetMacro1Key(ConsoleKey castKey)
{
this.macroKeys[0] = castKey;
}

public void SetMacro2Key(ConsoleKey castKey)
{
this.macroKeys[1] = castKey;
}

public void SetMacroTimer(int time)
{
this.macroTimer = time;
}

private void Watch(int milliseconds)
{
bobberFinder.Reset();
Expand Down Expand Up @@ -116,21 +136,27 @@ private void WaitForBite()
if (this.biteWatcher.IsBite(currentBobberPosition))
{
Loot(bobberPosition);
PressTenMinKeyIfDue();
PressMacroKeysIfDue();
return;
}

if (!timedTask.ExecuteIfDue()) { return; }
}
}

private DateTime StartTime = DateTime.Now;

private void PressTenMinKeyIfDue()
private void PressMacroKeysIfDue()
{
if ((DateTime.Now - StartTime).TotalMinutes > 10 && tenMinKey.Count > 0)
if (macroKeys.Count <= 0)
{
return;
}

// Use seconds to get fidelity with the slush timer.
// Issue #35: There was potential for the few seconds it takes to cast lure to not be waited on for second lure,
// causing every other lure application to fail.
if ((DateTime.Now - StartTime).TotalSeconds > (this.macroTimer * 60) + 10)
{
DoTenMinuteKey();
DoMacroKeys();
}
}

Expand All @@ -143,18 +169,18 @@ private void PressTenMinKeyIfDue()
/// Or a macro to delete junk:
/// /run for b=0,4 do for s=1,GetContainerNumSlots(b) do local n=GetContainerItemLink(b,s) if n and (strfind(n,"Raw R") or strfind(n,"Raw Spot") or strfind(n,"Raw Glo") or strfind(n,"roup")) then PickupContainerItem(b,s) DeleteCursorItem() end end end
/// </summary>
private void DoTenMinuteKey()
private void DoMacroKeys()
{
StartTime = DateTime.Now;

if (tenMinKey.Count == 0)
if (macroKeys.Count == 0)
{
logger.Info($"Ten Minute Key: No keys defined in tenMinKey, so nothing to do (Define in call to FishingBot constructor).");
}

FishingEventHandler?.Invoke(this, new FishingEvent { Action = FishingAction.Cast });

foreach (var key in tenMinKey)
foreach (var key in macroKeys)
{
logger.Info($"Ten Minute Key: Pressing key {key} to run a macro, delete junk fish or apply a lure etc.");
WowProcess.PressKey(key);
Expand Down
2 changes: 1 addition & 1 deletion Source/FishingFunConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ private static void Main(string[] args)
var bobberFinder = new SearchBobberFinder(pixelClassifier);
var biteWatcher = new PositionBiteWatcher(strikeValue);

var bot = new FishingBot(bobberFinder, biteWatcher, ConsoleKey.D4, new List<ConsoleKey> { ConsoleKey.D5 });
var bot = new FishingBot(bobberFinder, biteWatcher, ConsoleKey.D4, new List<ConsoleKey> { ConsoleKey.D5 }, 10);
bot.FishingEventHandler += (b, e) => LogManager.GetLogger("Fishbot").Info(e);

WowProcess.PressKey(ConsoleKey.Spacebar);
Expand Down
58 changes: 54 additions & 4 deletions Source/FishingFunUI/UI/KeyBindChooser.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,28 @@

namespace FishingFun
{
public class Macro1KeyBindChooser : KeyBindChooser {
public Macro1KeyBindChooser() : base()
{
StorageIndex = 1;
ReadConfiguration();
}
}

public class Macro2KeyBindChooser : KeyBindChooser
{
public Macro2KeyBindChooser() : base()
{
StorageIndex = 2;
ReadConfiguration();
}
}

public partial class KeyBindChooser : UserControl
{
private string[] KeybindTexts = new string[] { "4", "5", "6" };
protected int StorageIndex { get; set; } = 0;

public ConsoleKey CastKey { get; set; } = ConsoleKey.D4;

private static string Filename = "keybind.txt";
Expand All @@ -21,14 +41,19 @@ public KeyBindChooser()
ReadConfiguration();
}

private void ReadConfiguration()
protected void ReadConfiguration()
{
try
{
if (File.Exists(Filename))
{
var contents = File.ReadAllText(Filename);
CastKey = (ConsoleKey)int.Parse(contents);
var fileContents = File.ReadAllText(Filename);
var keybindChunks = fileContents.Split(';');
if (keybindChunks.Length == 3)
{
KeybindTexts = keybindChunks;
}
CastKey = GetConsoleKey();
KeyBind.Text = GetCastKeyText(this.CastKey);
}
}
Expand All @@ -42,7 +67,18 @@ private void ReadConfiguration()

private void WriteConfiguration()
{
File.WriteAllText(Filename, ((int)CastKey).ToString());
KeybindTexts[StorageIndex] = ((int)CastKey).ToString();

string output = "";
for (int i = 0; i < KeybindTexts.Length; i++)
{
output += KeybindTexts[i];
if (i < KeybindTexts.Length - 1)
{
output += ";";
}
}
File.WriteAllText(Filename, output);
}

private void CastKey_Focus(object sender, RoutedEventArgs e)
Expand All @@ -63,7 +99,11 @@ private void ProcessKeybindText(string key)
ConsoleKey ck;
if (Enum.TryParse<ConsoleKey>(key, out ck))
{
// Read first so we pick up changes from all 3 classes that share this file. Maybe a shared storage manager
// would be best, but this is quick and dirty to allow multiple keybinds.
ReadConfiguration();
this.CastKey = ck;
KeyBind.Text = GetCastKeyText(this.CastKey);
WriteConfiguration();
CastKeyChanged?.Invoke(this, null);
return;
Expand All @@ -72,6 +112,16 @@ private void ProcessKeybindText(string key)
KeyBind.Text = "";
}

private ConsoleKey GetConsoleKey()
{
if (this.StorageIndex < 0 || this.StorageIndex >= this.KeybindTexts.Length)
{
return ConsoleKey.D4;
}

return (ConsoleKey)int.Parse(this.KeybindTexts[this.StorageIndex]);
}

private string GetCastKeyText(ConsoleKey ck)
{
string keyText = ck.ToString();
Expand Down
16 changes: 15 additions & 1 deletion Source/FishingFunUI/UI/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,21 @@
<local:KeyBindChooser x:Name="KeyChooser" />
</Grid>
</Button>
</WrapPanel>
<Button Name="Macro1Key" Padding="0" Height="50" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="50" ToolTip="Cast key - click to change" Click="CastKey_Click" BorderThickness="0" Background="Transparent">
<Grid>
<Image Source="/Chrome;component/Resources/Button.png" Style="{StaticResource HQRender}" />
<local:Macro1KeyBindChooser x:Name="Macro1KeyChooser" />
</Grid>
</Button>
<Button Name="Macro2Key" Padding="0" Height="50" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="50" ToolTip="Cast key - click to change" Click="CastKey_Click" BorderThickness="0" Background="Transparent">
<Grid>
<Image Source="/Chrome;component/Resources/Button.png" Style="{StaticResource HQRender}" />
<local:Macro2KeyBindChooser x:Name="Macro2KeyChooser" />
</Grid>
</Button>
<Label Width="82" Height="26" Content="Macro Timer:" />
<TextBox Name="MacroTimerText" Width="36" Height="22" Text="10" TextChanged="MacroTimer_TextChanged" />
</WrapPanel>
</materialDesign:Card>

<DockPanel Name="ImageBackground" Grid.Column="0" Grid.Row="0" Margin="0" Grid.RowSpan="2" Background="Black">
Expand Down
28 changes: 27 additions & 1 deletion Source/FishingFunUI/UI/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public partial class MainWindow : Window, IAppender
private bool setImageBackgroundColour = true;
private Timer WindowSizeChangedTimer;
private System.Threading.Thread? botThread;
private int MacroTimer = 10;

public MainWindow()
{
Expand Down Expand Up @@ -57,6 +58,18 @@ public MainWindow()
this.Settings.Focus();
this.bot?.SetCastKey(this.KeyChooser.CastKey);
};

this.Macro1KeyChooser.CastKeyChanged += (s, e) =>
{
this.Settings.Focus();
this.bot?.SetMacro1Key(this.Macro1KeyChooser.CastKey);
};

this.Macro2KeyChooser.CastKeyChanged += (s, e) =>
{
this.Settings.Focus();
this.bot?.SetMacro2Key(this.Macro2KeyChooser.CastKey);
};
}

private void MainWindow_SizeChanged(object sender, SizeChangedEventArgs e)
Expand Down Expand Up @@ -88,6 +101,10 @@ private void SizeChangedTimer_Elapsed(object sender, ElapsedEventArgs e)

private void CastKey_Click(object sender, RoutedEventArgs e) => this.KeyChooser.Focus();

private void Macro1Key_Click(object sender, RoutedEventArgs e) => this.Macro1KeyChooser.Focus();

private void Macro2Key_Click(object sender, RoutedEventArgs e) => this.Macro2KeyChooser.Focus();

private void FishingEventHandler(object sender, FishingEvent e)
{
Dispatch(() =>
Expand Down Expand Up @@ -164,7 +181,7 @@ private void Play_Click(object sender, RoutedEventArgs e)

public void BotThread()
{
bot = new FishingBot(bobberFinder, this.biteWatcher, KeyChooser.CastKey, new List<ConsoleKey> { ConsoleKey.D5, ConsoleKey.D6 });
bot = new FishingBot(bobberFinder, this.biteWatcher, KeyChooser.CastKey, new List<ConsoleKey> { Macro1KeyChooser.CastKey, Macro2KeyChooser.CastKey }, MacroTimer);
bot.FishingEventHandler += FishingEventHandler;
bot.Start();

Expand Down Expand Up @@ -198,5 +215,14 @@ private void Dispatch(Action action)
Application.Current?.Dispatcher.BeginInvoke((Action)(() => action()));
Application.Current?.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background, new Action(delegate { }));
}

private void MacroTimer_TextChanged(object sender, TextChangedEventArgs e)
{
if (int.TryParse(this.MacroTimerText.Text, out Int32 result))
{
this.MacroTimer = result;
this.bot?.SetMacroTimer(this.MacroTimer);
}
}
}
}
Loading