impl js run task

This commit is contained in:
辉鸭蛋 2024-11-01 21:37:44 +08:00
parent a1cdf15c7e
commit 4725af5491
2 changed files with 90 additions and 30 deletions

View File

@ -4,6 +4,10 @@ using BetterGenshinImpact.ViewModel.Pages;
using System; using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using BetterGenshinImpact.GameTask.AutoDomain; using BetterGenshinImpact.GameTask.AutoDomain;
using BetterGenshinImpact.GameTask.AutoFight;
using BetterGenshinImpact.GameTask.AutoWood;
using BetterGenshinImpact.GameTask.Model.Enum;
using BetterGenshinImpact.GameTask.AutoGeniusInvokation;
namespace BetterGenshinImpact.Core.Script.Dependence; namespace BetterGenshinImpact.Core.Script.Dependence;
@ -61,15 +65,29 @@ public class Dispatcher
switch (soloTask.Name) switch (soloTask.Name)
{ {
case "AutoGeniusInvokation": case "AutoGeniusInvokation":
taskSettingsPageViewModel.SwitchAutoGeniusInvokationCommand.Execute(null); if (taskSettingsPageViewModel.GetTcgStrategy(out var content))
{
return;
}
await new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content)).Start(CancellationContext.Instance.Cts.Token);
break; break;
case "AutoWood": case "AutoWood":
taskSettingsPageViewModel.SwitchAutoWoodCommand.Execute(null); await new AutoWoodTask(new WoodTaskParam(taskSettingsPageViewModel.AutoWoodRoundNum, taskSettingsPageViewModel.AutoWoodDailyMaxCount)).Start(CancellationContext.Instance.Cts.Token);
break; break;
case "AutoFight": case "AutoFight":
taskSettingsPageViewModel.SwitchAutoFightCommand.Execute(null); if (taskSettingsPageViewModel.GetFightStrategy(out var path1))
{
return;
}
var autoFightConfig = TaskContext.Instance().Config.AutoFightConfig;
var param = new AutoFightParam(path1)
{
FightFinishDetectEnabled = autoFightConfig.FightFinishDetectEnabled,
PickDropsAfterFightEnabled = autoFightConfig.PickDropsAfterFightEnabled
};
await new AutoFightTask(param).Start(CancellationContext.Instance.Cts.Token);
break; break;
case "AutoDomain": case "AutoDomain":
@ -80,9 +98,9 @@ public class Dispatcher
await new AutoDomainTask(new AutoDomainParam(0, path)).Start(CancellationContext.Instance.Cts.Token); await new AutoDomainTask(new AutoDomainParam(0, path)).Start(CancellationContext.Instance.Cts.Token);
break; break;
case "AutoMusicGame": // case "AutoMusicGame":
taskSettingsPageViewModel.SwitchAutoMusicGameCommand.Execute(null); // taskSettingsPageViewModel.SwitchAutoMusicGameCommand.Execute(null);
break; // break;
default: default:
throw new ArgumentException($"未知的任务名称: {soloTask.Name}", nameof(soloTask.Name)); throw new ArgumentException($"未知的任务名称: {soloTask.Name}", nameof(soloTask.Name));

View File

@ -36,24 +36,53 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
private CancellationTokenSource? _cts; private CancellationTokenSource? _cts;
private static readonly object _locker = new(); private static readonly object _locker = new();
[ObservableProperty] private string[] _strategyList; [ObservableProperty]
[ObservableProperty] private bool _switchAutoGeniusInvokationEnabled; private string[] _strategyList;
[ObservableProperty] private string _switchAutoGeniusInvokationButtonText = "启动";
[ObservableProperty] private int _autoWoodRoundNum; [ObservableProperty]
[ObservableProperty] private int _autoWoodDailyMaxCount = 2000; private bool _switchAutoGeniusInvokationEnabled;
[ObservableProperty] private bool _switchAutoWoodEnabled;
[ObservableProperty] private string _switchAutoWoodButtonText = "启动";
[ObservableProperty] private string[] _combatStrategyList; [ObservableProperty]
[ObservableProperty] private int _autoDomainRoundNum; private string _switchAutoGeniusInvokationButtonText = "启动";
[ObservableProperty] private bool _switchAutoDomainEnabled;
[ObservableProperty] private string _switchAutoDomainButtonText = "启动"; [ObservableProperty]
[ObservableProperty] private bool _switchAutoFightEnabled; private int _autoWoodRoundNum;
[ObservableProperty] private string _switchAutoFightButtonText = "启动";
[ObservableProperty] private string _switchAutoTrackButtonText = "启动"; [ObservableProperty]
[ObservableProperty] private string _switchAutoTrackPathButtonText = "启动"; private int _autoWoodDailyMaxCount = 2000;
[ObservableProperty] private string _switchAutoMusicGameButtonText = "启动";
[ObservableProperty]
private bool _switchAutoWoodEnabled;
[ObservableProperty]
private string _switchAutoWoodButtonText = "启动";
[ObservableProperty]
private string[] _combatStrategyList;
[ObservableProperty]
private int _autoDomainRoundNum;
[ObservableProperty]
private bool _switchAutoDomainEnabled;
[ObservableProperty]
private string _switchAutoDomainButtonText = "启动";
[ObservableProperty]
private bool _switchAutoFightEnabled;
[ObservableProperty]
private string _switchAutoFightButtonText = "启动";
[ObservableProperty]
private string _switchAutoTrackButtonText = "启动";
[ObservableProperty]
private string _switchAutoTrackPathButtonText = "启动";
[ObservableProperty]
private string _switchAutoMusicGameButtonText = "启动";
public TaskSettingsPageViewModel(IConfigService configService, INavigationService navigationService, TaskTriggerDispatcher taskTriggerDispatcher) public TaskSettingsPageViewModel(IConfigService configService, INavigationService navigationService, TaskTriggerDispatcher taskTriggerDispatcher)
{ {
@ -81,6 +110,7 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
{ {
strategyName = strategyName[1..]; strategyName = strategyName[1..];
} }
strategyList[i] = strategyName; strategyList[i] = strategyName;
} }
} }
@ -131,10 +161,24 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
[RelayCommand] [RelayCommand]
public async Task OnSwitchAutoGeniusInvokation() public async Task OnSwitchAutoGeniusInvokation()
{ {
if (GetTcgStrategy(out var content))
{
return;
}
SwitchAutoGeniusInvokationEnabled = true;
await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage)
.RunSoloTaskAsync(new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content)));
SwitchAutoGeniusInvokationEnabled = false;
}
public bool GetTcgStrategy(out string content)
{
content = string.Empty;
if (string.IsNullOrEmpty(Config.AutoGeniusInvokationConfig.StrategyName)) if (string.IsNullOrEmpty(Config.AutoGeniusInvokationConfig.StrategyName))
{ {
Toast.Warning("请先选择策略"); Toast.Warning("请先选择策略");
return; return true;
} }
var path = Global.Absolute(@"User\AutoGeniusInvokation\" + Config.AutoGeniusInvokationConfig.StrategyName + ".txt"); var path = Global.Absolute(@"User\AutoGeniusInvokation\" + Config.AutoGeniusInvokationConfig.StrategyName + ".txt");
@ -142,15 +186,11 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
if (!File.Exists(path)) if (!File.Exists(path))
{ {
Toast.Error("策略文件不存在"); Toast.Error("策略文件不存在");
return; return true;
} }
var content = await File.ReadAllTextAsync(path); content = File.ReadAllText(path);
return false;
SwitchAutoGeniusInvokationEnabled = true;
await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage)
.RunSoloTaskAsync(new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content)));
SwitchAutoGeniusInvokationEnabled = false;
} }
[RelayCommand] [RelayCommand]
@ -207,6 +247,7 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
{ {
return; return;
} }
SwitchAutoDomainEnabled = true; SwitchAutoDomainEnabled = true;
await new TaskRunner(DispatcherTimerOperationEnum.UseCacheImage) await new TaskRunner(DispatcherTimerOperationEnum.UseCacheImage)
.RunSoloTaskAsync(new AutoDomainTask(new AutoDomainParam(AutoDomainRoundNum, path))); .RunSoloTaskAsync(new AutoDomainTask(new AutoDomainParam(AutoDomainRoundNum, path)));
@ -220,6 +261,7 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw
{ {
path = Global.Absolute(@"User\AutoFight\"); path = Global.Absolute(@"User\AutoFight\");
} }
if (!File.Exists(path) && !Directory.Exists(path)) if (!File.Exists(path) && !Directory.Exists(path))
{ {
Toast.Error("战斗策略文件不存在"); Toast.Error("战斗策略文件不存在");