code format

This commit is contained in:
辉鸭蛋 2024-12-28 23:01:22 +08:00
parent 510ca82304
commit 0a5c78ebbe
5 changed files with 137 additions and 120 deletions

View File

@ -2,24 +2,26 @@
namespace BetterGenshinImpact.GameTask.AutoFight;
public class AutoFightParam: BaseTaskParam
public class AutoFightParam : BaseTaskParam
{
public AutoFightParam(string path,AutoFightConfig autoFightConfig)
public AutoFightParam(string path, AutoFightConfig autoFightConfig)
{
CombatStrategyPath=path;
CombatStrategyPath = path;
Timeout = autoFightConfig.Timeout;
FightFinishDetectEnabled = autoFightConfig.FightFinishDetectEnabled;
PickDropsAfterFightEnabled = autoFightConfig.PickDropsAfterFightEnabled;
//下面参数固定,只取自动战斗里面的
autoFightConfig=TaskContext.Instance().Config.AutoFightConfig;
BattleEndProgressBarColor=autoFightConfig.BattleEndProgressBarColor;
BattleEndProgressBarColorTolerance=autoFightConfig.BattleEndProgressBarColorTolerance;
}
public AutoFightParam(string path)
{
CombatStrategyPath=path;
autoFightConfig = TaskContext.Instance().Config.AutoFightConfig;
BattleEndProgressBarColor = autoFightConfig.BattleEndProgressBarColor;
BattleEndProgressBarColorTolerance = autoFightConfig.BattleEndProgressBarColorTolerance;
}
public AutoFightParam(string path)
{
CombatStrategyPath = path;
}
public string CombatStrategyPath { get; set; }
public bool FightFinishDetectEnabled { get; set; } = false;
@ -27,10 +29,10 @@ public class AutoFightParam: BaseTaskParam
public bool PickDropsAfterFightEnabled { get; set; } = false;
public int Timeout { get; set; } = 120;
public string BattleEndProgressBarColor = "";
public string BattleEndProgressBarColorTolerance = "";
}
}

View File

@ -47,25 +47,28 @@ public class AutoFightTask : ISoloTask
_predictor = BgiYoloV8PredictorFactory.GetPredictor(@"Assets\Model\World\bgi_world.onnx");
}
_battleEndProgressBarColor = ParseStringToTuple(taskParam.BattleEndProgressBarColor,(95, 235, 255));
_battleEndProgressBarColorTolerance = ParseSingleOrCommaSeparated(taskParam.BattleEndProgressBarColorTolerance,(6, 6, 6));
_battleEndProgressBarColor = ParseStringToTuple(taskParam.BattleEndProgressBarColor, (95, 235, 255));
_battleEndProgressBarColorTolerance = ParseSingleOrCommaSeparated(taskParam.BattleEndProgressBarColorTolerance, (6, 6, 6));
}
// 方法1判断是否是单个数字
static bool IsSingleNumber(string input, out int result)
{
return int.TryParse(input, out result);
}
static (int, int, int) ParseSingleOrCommaSeparated(string input,(int, int, int) defaultValue)
static (int, int, int) ParseSingleOrCommaSeparated(string input, (int, int, int) defaultValue)
{
// 如果是单个数字
if (IsSingleNumber(input, out var singleNumber))
{
return (singleNumber, singleNumber, singleNumber);
}
return ParseStringToTuple(input,defaultValue);
return ParseStringToTuple(input, defaultValue);
}
static (int, int, int) ParseStringToTuple(string input,(int, int, int) defaultValue)
static (int, int, int) ParseStringToTuple(string input, (int, int, int) defaultValue)
{
// 尝试按逗号分割字符串
var parts = input.Split(',');
@ -80,6 +83,7 @@ public class AutoFightTask : ISoloTask
// 如果解析失败,返回默认值
return defaultValue;
}
public async Task Start(CancellationToken ct)
{
_ct = ct;
@ -189,6 +193,7 @@ public class AutoFightTask : ISoloTask
Logger.LogWarning("游戏窗口分辨率不是 16:9 !当前分辨率为 {Width}x{Height} , 非 16:9 分辨率的游戏可能无法正常使用自动战斗功能 !", gameScreenSize.Width, gameScreenSize.Height);
}
}
static bool AreDifferencesWithinBounds((int, int, int) a, (int, int, int) b, (int, int, int) c)
{
// 计算每个位置的差值绝对值并进行比较
@ -196,6 +201,7 @@ public class AutoFightTask : ISoloTask
Math.Abs(a.Item2 - b.Item2) < c.Item2 &&
Math.Abs(a.Item3 - b.Item3) < c.Item3;
}
private async Task<bool> CheckFightFinish()
{
// YOLO 判断血条和怪物位置
@ -215,7 +221,7 @@ public class AutoFightTask : ISoloTask
//if ((DateTime.Now - _lastFightFlagTime).TotalSeconds > randomNumber)
//{
// 旋转完毕后都没有检测到血条和怪物位置则按L键确认战斗结束
/**
/**
Simulation.SendInput.Mouse.MiddleButtonClick();
await Delay(300, _ct);
for (var i = 0; i < 8; i++)
@ -229,27 +235,28 @@ public class AutoFightTask : ISoloTask
}
}
**/
//检查延时,根据队伍不同可以进行优化,可做成配置
await Delay(1500, _ct);
Logger.LogInformation("按L检查战斗是否结束");
// 最终方案确认战斗结束
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_L);
await Delay(450, _ct);
var ra = CaptureToRectArea();
var b3 = ra.SrcMat.At<Vec3b>(50, 790);
if (AreDifferencesWithinBounds(_battleEndProgressBarColor,(b3.Item0,b3.Item1,b3.Item2),_battleEndProgressBarColorTolerance))
{
Logger.LogInformation("识别到战斗结束");
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_SPACE);
return true;
}
//检查延时,根据队伍不同可以进行优化,可做成配置
await Delay(1500, _ct);
Logger.LogInformation("按L检查战斗是否结束");
// 最终方案确认战斗结束
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_L);
await Delay(450, _ct);
var ra = CaptureToRectArea();
var b3 = ra.SrcMat.At<Vec3b>(50, 790);
if (AreDifferencesWithinBounds(_battleEndProgressBarColor, (b3.Item0, b3.Item1, b3.Item2), _battleEndProgressBarColorTolerance))
{
Logger.LogInformation("识别到战斗结束");
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_SPACE);
Logger.LogInformation($"未识别到战斗结束{b3.Item0},{b3.Item1},{b3.Item2}");
_lastFightFlagTime = DateTime.Now;
return false;
// }
return true;
}
Simulation.SendInput.Keyboard.KeyPress(User32.VK.VK_SPACE);
Logger.LogInformation($"未识别到战斗结束{b3.Item0},{b3.Item1},{b3.Item2}");
_lastFightFlagTime = DateTime.Now;
return false;
// }
return false;
}

View File

@ -17,29 +17,32 @@ internal class AutoFightHandler : IActionHandler
await StartFight(ct, config);
}
private async Task StartFight(CancellationToken ct,object? config = null)
private async Task StartFight(CancellationToken ct, object? config = null)
{
TaskControl.Logger.LogInformation("执行 {Text}", "自动战斗");
// 爷们要战斗
AutoFightParam taskParams = null;
if (config!=null && config is PathingPartyConfig patyConfig && patyConfig.AutoFightEabled) {
if (config != null && config is PathingPartyConfig patyConfig && patyConfig.AutoFightEabled)
{
//替换配置为路径追踪
taskParams = GetFightAutoFightParam(patyConfig.AutoFightConfig);
}
else {
taskParams = new AutoFightParam(GetFightStrategy(),TaskContext.Instance().Config.AutoFightConfig);
else
{
taskParams = new AutoFightParam(GetFightStrategy(), TaskContext.Instance().Config.AutoFightConfig);
}
var fightSoloTask = new AutoFightTask(taskParams);
await fightSoloTask.Start(ct);
}
private AutoFightParam GetFightAutoFightParam(AutoFightConfig? config)
{
AutoFightParam autoFightParam = new AutoFightParam(GetFightStrategy(config), config);
return autoFightParam;
}
private string GetFightStrategy(AutoFightConfig config)
{
var path = Global.Absolute(@"User\AutoFight\" + config.StrategyName + ".txt");
@ -47,6 +50,7 @@ internal class AutoFightHandler : IActionHandler
{
path = Global.Absolute(@"User\AutoFight\");
}
if (!File.Exists(path) && !Directory.Exists(path))
{
throw new Exception("战斗策略文件不存在");
@ -54,8 +58,9 @@ internal class AutoFightHandler : IActionHandler
return path;
}
private string GetFightStrategy()
{
return GetFightStrategy(TaskContext.Instance().Config.AutoFightConfig);
}
}
}

View File

@ -13,7 +13,7 @@
d:DesignHeight="850"
d:DesignWidth="800"
mc:Ignorable="d">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="800">
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Height="800">
<StackPanel Margin="42,16,42,12">
<ui:CardExpander Margin="0,0,0,12"
@ -244,7 +244,7 @@
</ui:CardExpander.Icon>
<ui:CardExpander.Header>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
@ -262,14 +262,13 @@
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
TextWrapping="Wrap">
开启此项可制定战斗配置,关闭此项,则用于自动任务中的配置
开启此项可制定战斗配置,关闭此项,则用于独立任务中的配置
</ui:TextBlock>
<ui:ToggleSwitch Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,24,0"
IsChecked="{Binding PathingConfig.AutoFightEabled, Mode=TwoWay}"
/>
IsChecked="{Binding PathingConfig.AutoFightEabled, Mode=TwoWay}" />
<!---->
</Grid>
</ui:CardExpander.Header>
@ -286,39 +285,39 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
Text="选择战斗策略"
TextWrapping="Wrap" />
Grid.Column="0"
FontTypography="Body"
Text="选择战斗策略"
TextWrapping="Wrap" />
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="用于战斗"
TextWrapping="Wrap" />
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="用于战斗"
TextWrapping="Wrap" />
<ui:Button Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,12,0"
Content="脚本仓库"
Command="{Binding OpenLocalScriptRepoCommand}"
Icon="{ui:SymbolIcon Archive24}"/>
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,12,0"
Content="脚本仓库"
Command="{Binding OpenLocalScriptRepoCommand}"
Icon="{ui:SymbolIcon Archive24}" />
<ui:Button Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="2"
Margin="0,0,12,0"
Command="{Binding OpenFightFolderCommand}"
Content="打开目录" />
Grid.RowSpan="2"
Grid.Column="2"
Margin="0,0,12,0"
Command="{Binding OpenFightFolderCommand}"
Content="打开目录" />
<ComboBox Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="3"
Width="180"
Margin="0,0,36,0"
ItemsSource="{Binding AutoFightViewModel.CombatStrategyList}"
SelectedItem="{Binding PathingConfig.AutoFightConfig.StrategyName, Mode=TwoWay}">
Grid.RowSpan="2"
Grid.Column="3"
Width="180"
Margin="0,0,36,0"
ItemsSource="{Binding AutoFightViewModel.CombatStrategyList}"
SelectedItem="{Binding PathingConfig.AutoFightConfig.StrategyName, Mode=TwoWay}">
<b:Interaction.Triggers>
<b:EventTrigger EventName="DropDownOpened">
<b:InvokeCommandAction Command="{Binding StrategyDropDownOpenedCommand}"
CommandParameter="Combat" />
CommandParameter="Combat" />
</b:EventTrigger>
</b:Interaction.Triggers>
</ComboBox>
@ -333,23 +332,23 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
Text="强制指定配队"
TextWrapping="Wrap" />
Grid.Column="0"
FontTypography="Body"
Text="强制指定配队"
TextWrapping="Wrap" />
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="队伍无法被识别时,请按顺序填写队伍内角色名称,逗号分割"
TextWrapping="Wrap" />
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="队伍无法被识别时,请按顺序填写队伍内角色名称,逗号分割"
TextWrapping="Wrap" />
<ui:TextBox Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
MinWidth="180"
MaxWidth="800"
Margin="0,0,36,0"
Text="{Binding PathingConfig.AutoFightConfig.TeamNames, Mode=TwoWay}"
TextWrapping="Wrap" />
Grid.RowSpan="2"
Grid.Column="1"
MinWidth="180"
MaxWidth="800"
Margin="0,0,36,0"
Text="{Binding PathingConfig.AutoFightConfig.TeamNames, Mode=TwoWay}"
TextWrapping="Wrap" />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>
@ -361,20 +360,20 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
Text="自动检测战斗结束"
TextWrapping="Wrap" />
Grid.Column="0"
FontTypography="Body"
Text="自动检测战斗结束"
TextWrapping="Wrap" />
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="检测到战斗已经结束的情况下,停止自动战斗功能"
TextWrapping="Wrap" />
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="检测到战斗已经结束的情况下,停止自动战斗功能"
TextWrapping="Wrap" />
<ui:ToggleSwitch Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,36,0"
IsChecked="{Binding PathingConfig.AutoFightConfig.FightFinishDetectEnabled, Mode=TwoWay}" />
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,36,0"
IsChecked="{Binding PathingConfig.AutoFightConfig.FightFinishDetectEnabled, Mode=TwoWay}" />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>
@ -386,20 +385,20 @@
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ui:TextBlock Grid.Row="0"
Grid.Column="0"
FontTypography="Body"
Text="战斗结束后自动拾取掉落物"
TextWrapping="Wrap" />
Grid.Column="0"
FontTypography="Body"
Text="战斗结束后自动拾取掉落物"
TextWrapping="Wrap" />
<ui:TextBlock Grid.Row="1"
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="在战斗结束检测启用的情况下才会生效"
TextWrapping="Wrap" />
Grid.Column="0"
Foreground="{ui:ThemeResource TextFillColorTertiaryBrush}"
Text="在战斗结束检测启用的情况下才会生效"
TextWrapping="Wrap" />
<ui:ToggleSwitch Grid.Row="0"
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,36,0"
IsChecked="{Binding PathingConfig.AutoFightConfig.PickDropsAfterFightEnabled, Mode=TwoWay}" />
Grid.RowSpan="2"
Grid.Column="1"
Margin="0,0,36,0"
IsChecked="{Binding PathingConfig.AutoFightConfig.PickDropsAfterFightEnabled, Mode=TwoWay}" />
</Grid>
<Grid Margin="16">
<Grid.RowDefinitions>

View File

@ -13,9 +13,10 @@ using Wpf.Ui;
namespace BetterGenshinImpact.ViewModel.Pages.View;
public partial class AutoFightViewModel : ObservableObject,IViewModel
public partial class AutoFightViewModel : ObservableObject, IViewModel
{
public AllConfig Config { get; set; }
public AutoFightViewModel()
{
Config = TaskContext.Instance().Config;
@ -25,13 +26,14 @@ public partial class AutoFightViewModel : ObservableObject,IViewModel
public AutoFightViewModel(AllConfig config)
{
Config = config;
Config = config;
_strategyList = LoadCustomScript(Global.Absolute(@"User\AutoGeniusInvokation"));
_combatStrategyList = ["根据队伍自动选择", .. LoadCustomScript(Global.Absolute(@"User\AutoFight"))];
}
[ObservableProperty]
private string[] _combatStrategyList;
[ObservableProperty]
private string[] _strategyList;
@ -57,6 +59,7 @@ public partial class AutoFightViewModel : ObservableObject,IViewModel
return strategyList;
}
[RelayCommand]
public void OnStrategyDropDownOpened(string type)
{
@ -71,16 +74,17 @@ public partial class AutoFightViewModel : ObservableObject,IViewModel
break;
}
}
[RelayCommand]
public void OnOpenLocalScriptRepo()
{
Config.ScriptConfig.ScriptRepoHintDotVisible = false;
ScriptRepoUpdater.Instance.OpenLocalRepoInWebView();
}
[RelayCommand]
public void OnOpenFightFolder()
{
Process.Start("explorer.exe", Global.Absolute(@"User\AutoFight\"));
}
}
}