From 4725af5491e390d66b244d305ad9829f4f067ef4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Fri, 1 Nov 2024 21:37:44 +0800 Subject: [PATCH] impl js run task --- .../Core/Script/Dependence/Dispatcher.cs | 30 +++++-- .../Pages/TaskSettingsPageViewModel.cs | 90 ++++++++++++++----- 2 files changed, 90 insertions(+), 30 deletions(-) diff --git a/BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs b/BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs index efc08068..19764bc2 100644 --- a/BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs +++ b/BetterGenshinImpact/Core/Script/Dependence/Dispatcher.cs @@ -4,6 +4,10 @@ using BetterGenshinImpact.ViewModel.Pages; using System; using System.Threading.Tasks; 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; @@ -61,15 +65,29 @@ public class Dispatcher switch (soloTask.Name) { 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; case "AutoWood": - taskSettingsPageViewModel.SwitchAutoWoodCommand.Execute(null); + await new AutoWoodTask(new WoodTaskParam(taskSettingsPageViewModel.AutoWoodRoundNum, taskSettingsPageViewModel.AutoWoodDailyMaxCount)).Start(CancellationContext.Instance.Cts.Token); break; 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; case "AutoDomain": @@ -80,9 +98,9 @@ public class Dispatcher await new AutoDomainTask(new AutoDomainParam(0, path)).Start(CancellationContext.Instance.Cts.Token); break; - case "AutoMusicGame": - taskSettingsPageViewModel.SwitchAutoMusicGameCommand.Execute(null); - break; + // case "AutoMusicGame": + // taskSettingsPageViewModel.SwitchAutoMusicGameCommand.Execute(null); + // break; default: throw new ArgumentException($"未知的任务名称: {soloTask.Name}", nameof(soloTask.Name)); diff --git a/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs index de05e6d3..ae1dd667 100644 --- a/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/TaskSettingsPageViewModel.cs @@ -36,24 +36,53 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw private CancellationTokenSource? _cts; private static readonly object _locker = new(); - [ObservableProperty] private string[] _strategyList; - [ObservableProperty] private bool _switchAutoGeniusInvokationEnabled; - [ObservableProperty] private string _switchAutoGeniusInvokationButtonText = "启动"; + [ObservableProperty] + private string[] _strategyList; - [ObservableProperty] private int _autoWoodRoundNum; - [ObservableProperty] private int _autoWoodDailyMaxCount = 2000; - [ObservableProperty] private bool _switchAutoWoodEnabled; - [ObservableProperty] private string _switchAutoWoodButtonText = "启动"; + [ObservableProperty] + private bool _switchAutoGeniusInvokationEnabled; - [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 = "启动"; + [ObservableProperty] + private string _switchAutoGeniusInvokationButtonText = "启动"; + + [ObservableProperty] + private int _autoWoodRoundNum; + + [ObservableProperty] + private int _autoWoodDailyMaxCount = 2000; + + [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) { @@ -81,6 +110,7 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw { strategyName = strategyName[1..]; } + strategyList[i] = strategyName; } } @@ -131,10 +161,24 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw [RelayCommand] 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)) { Toast.Warning("请先选择策略"); - return; + return true; } var path = Global.Absolute(@"User\AutoGeniusInvokation\" + Config.AutoGeniusInvokationConfig.StrategyName + ".txt"); @@ -142,15 +186,11 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw if (!File.Exists(path)) { Toast.Error("策略文件不存在"); - return; + return true; } - var content = await File.ReadAllTextAsync(path); - - SwitchAutoGeniusInvokationEnabled = true; - await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage) - .RunSoloTaskAsync(new AutoGeniusInvokationTask(new GeniusInvokationTaskParam(content))); - SwitchAutoGeniusInvokationEnabled = false; + content = File.ReadAllText(path); + return false; } [RelayCommand] @@ -207,6 +247,7 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw { return; } + SwitchAutoDomainEnabled = true; await new TaskRunner(DispatcherTimerOperationEnum.UseCacheImage) .RunSoloTaskAsync(new AutoDomainTask(new AutoDomainParam(AutoDomainRoundNum, path))); @@ -220,6 +261,7 @@ public partial class TaskSettingsPageViewModel : ObservableObject, INavigationAw { path = Global.Absolute(@"User\AutoFight\"); } + if (!File.Exists(path) && !Directory.Exists(path)) { Toast.Error("战斗策略文件不存在");