diff --git a/src/MaaWpfGui/Main/AsstProxy.cs b/src/MaaWpfGui/Main/AsstProxy.cs index faa812e934..be747a1b51 100644 --- a/src/MaaWpfGui/Main/AsstProxy.cs +++ b/src/MaaWpfGui/Main/AsstProxy.cs @@ -35,6 +35,7 @@ using MaaWpfGui.Services; using MaaWpfGui.Services.Notification; using MaaWpfGui.States; using MaaWpfGui.ViewModels.UI; +using MaaWpfGui.ViewModels.UserControl.TaskQueue; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Serilog; @@ -764,8 +765,8 @@ namespace MaaWpfGui.Main case "Infrast": Instances.TaskQueueViewModel.InfrastTaskRunning = false; - Instances.TaskQueueViewModel.IncreaseCustomInfrastPlanIndex(); - Instances.TaskQueueViewModel.RefreshCustomInfrastPlanIndexByPeriod(); + InfrastSettingsUserControlModel.Instance.IncreaseCustomInfrastPlanIndex(); + InfrastSettingsUserControlModel.Instance.RefreshCustomInfrastPlanIndexByPeriod(); break; } diff --git a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs index 5795ed7932..45a51c4f64 100644 --- a/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs +++ b/src/MaaWpfGui/ViewModels/UI/TaskQueueViewModel.cs @@ -34,7 +34,6 @@ using MaaWpfGui.Utilities; using MaaWpfGui.Utilities.ValueType; using MaaWpfGui.ViewModels.UserControl.Settings; using MaaWpfGui.ViewModels.UserControl.TaskQueue; -using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Serilog; using Stylet; @@ -416,7 +415,7 @@ namespace MaaWpfGui.ViewModels.UI HandleDatePromptUpdate(); HandleCheckForUpdates(); - RefreshCustomInfrastPlanIndexByPeriod(); + InfrastTask.RefreshCustomInfrastPlanIndexByPeriod(); await HandleTimerLogic(currentTime); } @@ -606,7 +605,7 @@ namespace MaaWpfGui.ViewModels.UI FightTask.ResetFightVariables(); ResetTaskSelection(); - RefreshCustomInfrastPlanIndexByPeriod(); + InfrastTask.RefreshCustomInfrastPlanIndexByPeriod(); } LinkStart(); @@ -708,7 +707,7 @@ namespace MaaWpfGui.ViewModels.UI FightTask.InitDrops(); NeedToUpdateDatePrompt(); UpdateDatePromptAndStagesLocally(); - RefreshCustomInfrastPlan(); + InfrastTask.RefreshCustomInfrastPlan(); if (DateTime.UtcNow.ToYjDate().IsAprilFoolsDay()) { @@ -1687,7 +1686,7 @@ namespace MaaWpfGui.ViewModels.UI Instances.AsstProxy.AsstSetFightTaskParams(FightTask.RemainingSanityStage, 0, 0, int.MaxValue, 1, string.Empty, 0, false); } - private void SetInfrastParams() + public void SetInfrastParams() { if (!Instances.AsstProxy.ContainsTask(AsstProxy.TaskType.Infrast)) { @@ -1705,13 +1704,13 @@ namespace MaaWpfGui.ViewModels.UI InfrastTask.OriginiumShardAutoReplenishment, InfrastTask.CustomInfrastEnabled, InfrastTask.CustomInfrastFile, - CustomInfrastPlanIndex); + InfrastTask.CustomInfrastPlanIndex); } private bool AppendInfrast() { // 被RemoteControlService反射调用,暂不移除 - if (InfrastTask.CustomInfrastEnabled && (!File.Exists(InfrastTask.CustomInfrastFile) || CustomInfrastPlanInfoList.Count == 0)) + if (InfrastTask.CustomInfrastEnabled && (!File.Exists(InfrastTask.CustomInfrastFile) || InfrastTask.CustomInfrastPlanInfoList.Count == 0)) { AddLog(LocalizationHelper.GetString("CustomizeInfrastSelectionEmpty"), UiLogColor.Error); return false; @@ -1728,7 +1727,7 @@ namespace MaaWpfGui.ViewModels.UI OriginiumShardAutoReplenishment = InfrastTask.OriginiumShardAutoReplenishment, IsCustom = InfrastTask.CustomInfrastEnabled, Filename = InfrastTask.CustomInfrastFile, - PlanIndex = CustomInfrastPlanIndex, + PlanIndex = InfrastTask.CustomInfrastPlanIndex, }.Serialize(); return Instances.AsstProxy.AsstAppendTaskWithEncoding(AsstProxy.TaskType.Infrast, type, param); @@ -1961,253 +1960,6 @@ namespace MaaWpfGui.ViewModels.UI } */ - public bool NeedAddCustomInfrastPlanInfo { get; set; } = true; - - private int _customInfrastPlanIndex = Convert.ToInt32(ConfigurationHelper.GetValue(ConfigurationKeys.CustomInfrastPlanIndex, "0")); - - public int CustomInfrastPlanIndex - { - get - { - if (CustomInfrastPlanInfoList.Count == 0) - { - return 0; - } - - if (_customInfrastPlanIndex >= CustomInfrastPlanInfoList.Count || _customInfrastPlanIndex < 0) - { - CustomInfrastPlanIndex = _customInfrastPlanIndex; - } - - return _customInfrastPlanIndex; - } - - set - { - if (CustomInfrastPlanInfoList.Count == 0) - { - return; - } - - if (value >= CustomInfrastPlanInfoList.Count || value < 0) - { - var count = CustomInfrastPlanInfoList.Count; - value = ((value % count) + count) % count; - _logger.Warning($"CustomInfrastPlanIndex out of range, reset to Index % Count: {value}"); - } - - if (value != _customInfrastPlanIndex && NeedAddCustomInfrastPlanInfo) - { - var plan = CustomInfrastPlanInfoList[value]; - AddLog(plan.Name, UiLogColor.Message); - - foreach (var period in plan.PeriodList) - { - AddLog($"[ {period.BeginHour:D2}:{period.BeginMinute:D2} - {period.EndHour:D2}:{period.EndMinute:D2} ]"); - } - - if (plan.Description != string.Empty) - { - AddLog(plan.Description); - } - } - - SetAndNotify(ref _customInfrastPlanIndex, value); - SetInfrastParams(); - ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastPlanIndex, value.ToString()); - } - } - - public ObservableCollection> CustomInfrastPlanList { get; } = []; - - public struct CustomInfrastPlanInfo - { - // ReSharper disable InconsistentNaming - public int Index; - - public string Name; - public string Description; - public string DescriptionPost; - - // 有效时间段 - public struct Period - { - public int BeginHour; - public int BeginMinute; - public int EndHour; - public int EndMinute; - } - - public List PeriodList; - - // ReSharper restore InconsistentNaming - } - - private List CustomInfrastPlanInfoList { get; } = []; - - private bool _customInfrastPlanHasPeriod; - private bool _customInfrastInfoOutput; - - public void RefreshCustomInfrastPlan() - { - CustomInfrastPlanInfoList.Clear(); - CustomInfrastPlanList.Clear(); - _customInfrastPlanHasPeriod = false; - - if (!InfrastTask.CustomInfrastEnabled) - { - return; - } - - if (!File.Exists(InfrastTask.CustomInfrastFile)) - { - return; - } - - try - { - string jsonStr = File.ReadAllText(InfrastTask.CustomInfrastFile); - var root = (JObject)JsonConvert.DeserializeObject(jsonStr); - - if (root != null && _customInfrastInfoOutput && root.TryGetValue("title", out var title)) - { - AddLog(LocalizationHelper.GetString("CustomInfrastTitle"), UiLogColor.Message); - AddLog($"title: {title}", UiLogColor.Info); - if (root.TryGetValue("description", out var value)) - { - AddLog($"description: {value}", UiLogColor.Info); - } - } - - var planList = (JArray)root?["plans"]; - if (planList != null) - { - for (int i = 0; i < planList.Count; ++i) - { - var plan = (JObject)planList[i]; - string display = plan.TryGetValue("name", out var name) ? name.ToString() : ("Plan " + ((char)('A' + i))); - CustomInfrastPlanList.Add(new GenericCombinedData { Display = display, Value = i }); - string desc = plan.TryGetValue("description", out var description) ? description.ToString() : string.Empty; - string descPost = plan.TryGetValue("description_post", out var descriptionPost) ? descriptionPost.ToString() : string.Empty; - - if (_customInfrastInfoOutput) - { - AddLog(display, UiLogColor.Message); - } - - var periodList = new List(); - if (plan.TryGetValue("period", out var token)) - { - var periodArray = (JArray)token; - foreach (var periodJson in periodArray) - { - var period = default(CustomInfrastPlanInfo.Period); - string beginTime = periodJson[0]?.ToString(); - if (beginTime != null) - { - var beginSplit = beginTime.Split(':'); - period.BeginHour = int.Parse(beginSplit[0]); - period.BeginMinute = int.Parse(beginSplit[1]); - } - - string endTime = periodJson[1]?.ToString(); - if (endTime != null) - { - var endSplit = endTime.Split(':'); - period.EndHour = int.Parse(endSplit[0]); - period.EndMinute = int.Parse(endSplit[1]); - } - - periodList.Add(period); - if (_customInfrastInfoOutput) - { - AddLog($"[ {period.BeginHour:D2}:{period.BeginMinute:D2} - {period.EndHour:D2}:{period.EndMinute:D2} ]"); - } - } - - if (periodList.Count != 0) - { - _customInfrastPlanHasPeriod = true; - } - } - - if (_customInfrastInfoOutput && desc != string.Empty) - { - AddLog(desc); - } - - if (_customInfrastInfoOutput && descPost != string.Empty) - { - AddLog(descPost); - } - - CustomInfrastPlanInfoList.Add(new CustomInfrastPlanInfo - { - Index = i, - Name = display, - Description = desc, - DescriptionPost = descPost, - PeriodList = periodList, - }); - } - } - - _customInfrastInfoOutput = true; - } - catch (Exception) - { - _customInfrastInfoOutput = true; - AddLog(LocalizationHelper.GetString("CustomInfrastFileParseFailed"), UiLogColor.Error); - return; - } - - RefreshCustomInfrastPlanIndexByPeriod(); - } - - public void RefreshCustomInfrastPlanIndexByPeriod() - { - if (!InfrastTask.CustomInfrastEnabled || !_customInfrastPlanHasPeriod || InfrastTaskRunning) - { - return; - } - - var now = DateTime.Now; - foreach (var plan in CustomInfrastPlanInfoList.Where( - plan => plan.PeriodList.Any( - period => TimeLess(period.BeginHour, period.BeginMinute, now.Hour, now.Minute) - && TimeLess(now.Hour, now.Minute, period.EndHour, period.EndMinute)))) - { - CustomInfrastPlanIndex = plan.Index; - return; - } - - if (CustomInfrastPlanIndex >= CustomInfrastPlanList.Count || CustomInfrastPlanList.Count < 0) - { - CustomInfrastPlanIndex = 0; - } - - return; - - static bool TimeLess(int lHour, int lMin, int rHour, int rMin) => (lHour != rHour) ? (lHour < rHour) : (lMin <= rMin); - } - - public void IncreaseCustomInfrastPlanIndex() - { - if (!InfrastTask.CustomInfrastEnabled || _customInfrastPlanHasPeriod || CustomInfrastPlanInfoList.Count == 0) - { - return; - } - - AddLog(LocalizationHelper.GetString("CustomInfrastPlanIndexAutoSwitch"), UiLogColor.Message); - var prePlanPostDesc = CustomInfrastPlanInfoList[CustomInfrastPlanIndex].DescriptionPost; - if (prePlanPostDesc != string.Empty) - { - AddLog(prePlanPostDesc); - } - - ++CustomInfrastPlanIndex; - } - private static IEnumerable InitTaskViewModelList() { var types = Assembly.GetExecutingAssembly().GetTypes().Where(t => t.Namespace == "MaaWpfGui.ViewModels.UserControl.TaskQueue" && t.IsClass && !t.IsAbstract && t.IsSubclassOf(typeof(TaskViewModel))); diff --git a/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/InfrastSettingsUserControlModel.cs b/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/InfrastSettingsUserControlModel.cs index 77f5a5e594..c06bbae5c5 100644 --- a/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/InfrastSettingsUserControlModel.cs +++ b/src/MaaWpfGui/ViewModels/UserControl/TaskQueue/InfrastSettingsUserControlModel.cs @@ -15,13 +15,16 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized; +using System.IO; using System.Linq; using MaaWpfGui.Constants; using MaaWpfGui.Helper; using MaaWpfGui.Models; using MaaWpfGui.Utilities.ValueType; -using MaaWpfGui.ViewModels.UI; using Microsoft.Win32; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using Serilog; namespace MaaWpfGui.ViewModels.UserControl.TaskQueue; @@ -37,6 +40,8 @@ public class InfrastSettingsUserControlModel : TaskViewModel public static InfrastSettingsUserControlModel Instance { get; } + private static readonly ILogger _logger = Log.ForContext(); + /// /// Gets the visibility of task setting views. /// @@ -327,7 +332,7 @@ public class InfrastSettingsUserControlModel : TaskViewModel { SetAndNotify(ref _customInfrastEnabled, value); ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastEnabled, value.ToString()); - Instances.TaskQueueViewModel.RefreshCustomInfrastPlan(); + RefreshCustomInfrastPlan(); } } @@ -360,16 +365,259 @@ public class InfrastSettingsUserControlModel : TaskViewModel { SetAndNotify(ref _customInfrastFile, value); ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastFile, value); - Instances.TaskQueueViewModel.RefreshCustomInfrastPlan(); + RefreshCustomInfrastPlan(); // SetAndNotify 在值没有变化时不会触发 PropertyChanged 事件,所以这里手动触发一下 - Instances.TaskQueueViewModel.NeedAddCustomInfrastPlanInfo = false; - { - Instances.TaskQueueViewModel.CustomInfrastPlanIndex--; - Instances.TaskQueueViewModel.CustomInfrastPlanIndex++; - } - - Instances.TaskQueueViewModel.NeedAddCustomInfrastPlanInfo = true; + NeedAddCustomInfrastPlanInfo = false; + NotifyOfPropertyChange(nameof(CustomInfrastPlanIndex)); + NeedAddCustomInfrastPlanInfo = true; } } + + public bool NeedAddCustomInfrastPlanInfo { get; set; } = true; + + private int _customInfrastPlanIndex = Convert.ToInt32(ConfigurationHelper.GetValue(ConfigurationKeys.CustomInfrastPlanIndex, "0")); + + public int CustomInfrastPlanIndex + { + get + { + if (CustomInfrastPlanInfoList.Count == 0) + { + return 0; + } + + if (_customInfrastPlanIndex >= CustomInfrastPlanInfoList.Count || _customInfrastPlanIndex < 0) + { + CustomInfrastPlanIndex = _customInfrastPlanIndex; + } + + return _customInfrastPlanIndex; + } + + set + { + if (CustomInfrastPlanInfoList.Count == 0) + { + return; + } + + if (value >= CustomInfrastPlanInfoList.Count || value < 0) + { + var count = CustomInfrastPlanInfoList.Count; + value = ((value % count) + count) % count; + _logger.Warning($"CustomInfrastPlanIndex out of range, reset to Index % Count: {value}"); + } + + if (value != _customInfrastPlanIndex && NeedAddCustomInfrastPlanInfo) + { + var plan = CustomInfrastPlanInfoList[value]; + Instances.TaskQueueViewModel.AddLog(plan.Name, UiLogColor.Message); + + foreach (var period in plan.PeriodList) + { + Instances.TaskQueueViewModel.AddLog($"[ {period.BeginHour:D2}:{period.BeginMinute:D2} - {period.EndHour:D2}:{period.EndMinute:D2} ]"); + } + + if (plan.Description != string.Empty) + { + Instances.TaskQueueViewModel.AddLog(plan.Description); + } + } + + SetAndNotify(ref _customInfrastPlanIndex, value); + Instances.TaskQueueViewModel.SetInfrastParams(); + ConfigurationHelper.SetValue(ConfigurationKeys.CustomInfrastPlanIndex, value.ToString()); + } + } + + public ObservableCollection> CustomInfrastPlanList { get; } = []; + + public struct CustomInfrastPlanInfo + { + // ReSharper disable InconsistentNaming + public int Index; + + public string Name; + public string Description; + public string DescriptionPost; + + // 有效时间段 + public struct Period + { + public int BeginHour; + public int BeginMinute; + public int EndHour; + public int EndMinute; + } + + public List PeriodList; + + // ReSharper restore InconsistentNaming + } + + public List CustomInfrastPlanInfoList { get; } = []; + + private bool _customInfrastPlanHasPeriod; + private bool _customInfrastInfoOutput; + + public void RefreshCustomInfrastPlan() + { + CustomInfrastPlanInfoList.Clear(); + CustomInfrastPlanList.Clear(); + _customInfrastPlanHasPeriod = false; + + if (!CustomInfrastEnabled) + { + return; + } + + if (!File.Exists(CustomInfrastFile)) + { + return; + } + + try + { + string jsonStr = File.ReadAllText(CustomInfrastFile); + var root = (JObject?)JsonConvert.DeserializeObject(jsonStr); + + if (root != null && _customInfrastInfoOutput && root.TryGetValue("title", out var title)) + { + Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("CustomInfrastTitle"), UiLogColor.Message); + Instances.TaskQueueViewModel.AddLog($"title: {title}", UiLogColor.Info); + if (root.TryGetValue("description", out var value)) + { + Instances.TaskQueueViewModel.AddLog($"description: {value}", UiLogColor.Info); + } + } + + var planList = (JArray?)root?["plans"]; + if (planList != null) + { + for (int i = 0; i < planList.Count; ++i) + { + var plan = (JObject)planList[i]; + string display = plan.TryGetValue("name", out var name) ? name.ToString() : ("Plan " + ((char)('A' + i))); + CustomInfrastPlanList.Add(new GenericCombinedData { Display = display, Value = i }); + string desc = plan.TryGetValue("description", out var description) ? description.ToString() : string.Empty; + string descPost = plan.TryGetValue("description_post", out var descriptionPost) ? descriptionPost.ToString() : string.Empty; + + if (_customInfrastInfoOutput) + { + Instances.TaskQueueViewModel.AddLog(display, UiLogColor.Message); + } + + var periodList = new List(); + if (plan.TryGetValue("period", out var token)) + { + var periodArray = (JArray)token; + foreach (var periodJson in periodArray) + { + var period = default(CustomInfrastPlanInfo.Period); + var beginTime = periodJson[0]?.ToString(); + if (beginTime != null) + { + var beginSplit = beginTime.Split(':'); + period.BeginHour = int.Parse(beginSplit[0]); + period.BeginMinute = int.Parse(beginSplit[1]); + } + + var endTime = periodJson[1]?.ToString(); + if (endTime != null) + { + var endSplit = endTime.Split(':'); + period.EndHour = int.Parse(endSplit[0]); + period.EndMinute = int.Parse(endSplit[1]); + } + + periodList.Add(period); + if (_customInfrastInfoOutput) + { + Instances.TaskQueueViewModel.AddLog($"[ {period.BeginHour:D2}:{period.BeginMinute:D2} - {period.EndHour:D2}:{period.EndMinute:D2} ]"); + } + } + + if (periodList.Count != 0) + { + _customInfrastPlanHasPeriod = true; + } + } + + if (_customInfrastInfoOutput && desc != string.Empty) + { + Instances.TaskQueueViewModel.AddLog(desc); + } + + if (_customInfrastInfoOutput && descPost != string.Empty) + { + Instances.TaskQueueViewModel.AddLog(descPost); + } + + CustomInfrastPlanInfoList.Add(new CustomInfrastPlanInfo + { + Index = i, + Name = display, + Description = desc, + DescriptionPost = descPost, + PeriodList = periodList, + }); + } + } + + _customInfrastInfoOutput = true; + } + catch (Exception) + { + _customInfrastInfoOutput = true; + Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("CustomInfrastFileParseFailed"), UiLogColor.Error); + return; + } + + RefreshCustomInfrastPlanIndexByPeriod(); + } + + public void RefreshCustomInfrastPlanIndexByPeriod() + { + if (!CustomInfrastEnabled || !_customInfrastPlanHasPeriod || Instances.TaskQueueViewModel.InfrastTaskRunning) + { + return; + } + + var now = DateTime.Now; + foreach (var plan in CustomInfrastPlanInfoList.Where( + plan => plan.PeriodList.Any( + period => TimeLess(period.BeginHour, period.BeginMinute, now.Hour, now.Minute) + && TimeLess(now.Hour, now.Minute, period.EndHour, period.EndMinute)))) + { + CustomInfrastPlanIndex = plan.Index; + return; + } + + if (CustomInfrastPlanIndex >= CustomInfrastPlanList.Count || CustomInfrastPlanList.Count < 0) + { + CustomInfrastPlanIndex = 0; + } + + return; + + static bool TimeLess(int lHour, int lMin, int rHour, int rMin) => (lHour != rHour) ? (lHour < rHour) : (lMin <= rMin); + } + + public void IncreaseCustomInfrastPlanIndex() + { + if (!CustomInfrastEnabled || _customInfrastPlanHasPeriod || CustomInfrastPlanInfoList.Count == 0) + { + return; + } + + Instances.TaskQueueViewModel.AddLog(LocalizationHelper.GetString("CustomInfrastPlanIndexAutoSwitch"), UiLogColor.Message); + var prePlanPostDesc = CustomInfrastPlanInfoList[CustomInfrastPlanIndex].DescriptionPost; + if (prePlanPostDesc != string.Empty) + { + Instances.TaskQueueViewModel.AddLog(prePlanPostDesc); + } + + ++CustomInfrastPlanIndex; + } } diff --git a/src/MaaWpfGui/Views/UserControl/TaskQueue/FightSettingsUserControl.xaml b/src/MaaWpfGui/Views/UserControl/TaskQueue/FightSettingsUserControl.xaml index 7cc378eca0..8164184c73 100644 --- a/src/MaaWpfGui/Views/UserControl/TaskQueue/FightSettingsUserControl.xaml +++ b/src/MaaWpfGui/Views/UserControl/TaskQueue/FightSettingsUserControl.xaml @@ -341,8 +341,8 @@ DisplayMemberPath="Display" IsHitTestVisible="{c:Binding !InfrastTaskRunning, Source={x:Static helper:Instances.TaskQueueViewModel}}" - ItemsSource="{Binding CustomInfrastPlanList, Source={x:Static helper:Instances.TaskQueueViewModel}}" - SelectedValue="{Binding CustomInfrastPlanIndex, Source={x:Static helper:Instances.TaskQueueViewModel}}" + ItemsSource="{Binding CustomInfrastPlanList, Source={x:Static task:InfrastSettingsUserControlModel.Instance}}" + SelectedValue="{Binding CustomInfrastPlanIndex, Source={x:Static task:InfrastSettingsUserControlModel.Instance}}" SelectedValuePath="Value" /> diff --git a/src/MaaWpfGui/Views/UserControl/TaskQueue/InfrastSettingsUserControl.xaml b/src/MaaWpfGui/Views/UserControl/TaskQueue/InfrastSettingsUserControl.xaml index 477d2b891a..cf03638846 100644 --- a/src/MaaWpfGui/Views/UserControl/TaskQueue/InfrastSettingsUserControl.xaml +++ b/src/MaaWpfGui/Views/UserControl/TaskQueue/InfrastSettingsUserControl.xaml @@ -20,62 +20,6 @@ s:View.ActionTarget="{Binding}" mc:Ignorable="d"> - - - - - - - - - - - - - - - - - - - - - - - -