JD2022-TU1/main/tools/modules/JD.Guinness.WPF.Module/ViewModel/XBoxSeriesXViewModel.cs

149 lines
7.7 KiB
C#

using System;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JD.Guinness.WPF.Module.Extension;
using JD.Guinness.WPF.Module.Model;
using JD.Guinness.WPF.Module.Model.Ps5;
using JD.Guinness.WPF.Module.Model.XBoxOne;
using JD.Guinness.WPF.Module.Model.XBoxSeriesX;
using JD.MicrosoftAchievement.Service;
namespace JD.Guinness.WPF.Module.ViewModel
{
public sealed class XBoxSeriesXViewModel : APlatformChallengeViewModel<XBoxSeriesXAchievement>
{
private readonly IMicrosoftAchievementService _microsoftAchievementService;
public int TotalPoints => PlatformChallenges.Where(a => a.IsBaseAchievement).Sum(a => a.Gamerscore);
public DelegateCommand<XBoxSeriesXAchievement> SetImageCommand { get; }
public DelegateCommand SetImageFromIdCommand { get; }
public DelegateCommand SetGamerscoreFromPs5TrophyCommand { get; }
public DelegateCommand SelectSettingsFileCommand { get; }
public AsyncDelegateCommand SetSameAsXBoxOneCommand { get; }
public XBoxSeriesXConfiguration Configuration { get; }
public XBoxSeriesXViewModel(GameViewModel gameViewModel, IMicrosoftAchievementService microsoftAchievementService)
: base(gameViewModel)
{
_microsoftAchievementService = microsoftAchievementService ?? throw new ArgumentNullException(nameof(microsoftAchievementService));
Configuration = gameViewModel.XBoxSeriesXConfiguration;
SetImageCommand = new DelegateCommand<XBoxSeriesXAchievement>(OnSetImage, _ => Game.IsAvailable);
SetImageFromIdCommand = new DelegateCommand(OnSetImageFromId, () => Game.IsAvailable);
SetGamerscoreFromPs5TrophyCommand = new DelegateCommand(OnSetGamerscoreFromPs5Trophy, () => Game.IsAvailable);
SelectSettingsFileCommand = new DelegateCommand(OnSelectSettingsFile, () => Game.IsAvailable);
SetSameAsXBoxOneCommand = new AsyncDelegateCommand(OnSetSameAsXBoxOne, () => Game.IsAvailable);
}
private void OnSetImage(XBoxSeriesXAchievement achievement) => SelectFile("Select the achievement image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.XBoxSeriesXImagesFolderPath), "png|*.png", file => achievement.ImagePath = file);
private void OnSetImageFromId() => SetImageFromId(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.XBoxSeriesXImagesFolderPath), (achievement, file) => achievement.ImagePath = file);
private void OnSetGamerscoreFromPs5Trophy()
{
foreach (XBoxSeriesXAchievement achievement in PlatformChallenges)
{
Ps5Trophy trophy = achievement.Challenge.GetPlatformChallenge<Ps5Trophy>();
if (trophy != null)
{
achievement.Gamerscore = trophy.Grade.ToPoints();
}
}
}
private void OnSelectSettingsFile() => SelectFile("Select the settings file to update", Game.Settings.WorkspacePath, "zip|*.zip", file => Configuration.SettingsFilePath = file);
private async Task OnSetSameAsXBoxOne()
{
await Game.StartProcess(
Task.Run(() =>
{
foreach (XBoxSeriesXAchievement achievement in PlatformChallenges.ToList())
{
achievement.Remove();
achievement.Challenge.NotifyPlatformChallenges();
}
foreach (Challenge challenge in Game.Challenges)
{
XBoxOneAchievement xBoxOne = challenge.GetPlatformChallenge<XBoxOneAchievement>();
if (xBoxOne != null)
{
XBoxSeriesXAchievement xBoxSeriesX = challenge.GetOrAddPlatformChallenge<XBoxSeriesXAchievement>();
xBoxSeriesX.CustomData.Add(xBoxOne.CustomData.Values);
xBoxSeriesX.Gamerscore = xBoxOne.Gamerscore;
xBoxSeriesX.Id = xBoxOne.Id;
xBoxSeriesX.ImagePath = xBoxOne.ImagePath;
xBoxSeriesX.IsBaseAchievement = xBoxOne.IsBaseAchievement;
xBoxSeriesX.IsSecret = xBoxOne.IsSecret;
xBoxSeriesX.LockedObjective = xBoxOne.LockedObjective;
xBoxSeriesX.UnlockedObjective = xBoxOne.UnlockedObjective;
challenge.NotifyPlatformChallenges();
}
}
}),
"XBOX SERIES X - Replicating XBOX ONE achievements",
"XBOX SERIES X - Replicated XBOX ONE achievements",
"XBOX SERIES X - Failed to replicate XBOX ONE achievements").ConfigureAwait(true);
}
protected override XBoxSeriesXAchievement DuplicatePlatFormChallengeInto(XBoxSeriesXAchievement platformChallenge, Challenge challenge) => platformChallenge.DuplicateInto(challenge);
protected override void OnPlatformChallengeChanged(object target, PropertyChangedEventArgs args)
{
if (target is null
|| (target is XBoxSeriesXAchievement
&& (args.PropertyName == nameof(XBoxSeriesXAchievement.Gamerscore)
|| args.PropertyName == nameof(XBoxSeriesXAchievement.IsBaseAchievement))))
{
OnPropertyChanged(nameof(TotalPoints));
}
}
protected override async Task OnGenerate()
{
await Game.StartProcess(
Task.Run(async () =>
{
foreach (Achievement achievement in await _microsoftAchievementService.ReadAchievementsAsync(Configuration.SettingsFilePath).ConfigureAwait(false))
{
XBoxSeriesXAchievement xBoxSeriesX = PlatformChallenges.FirstOrDefault(a => a.Id == achievement.Id);
if (xBoxSeriesX is null)
{
Game.Warn($"XBOX SERIES X - Achievement with id \"{achievement.Id}\" found in settings file but not in declared XBOX SERIES X achievements");
}
else
{
xBoxSeriesX.NameId = achievement.NameId;
xBoxSeriesX.LockedObjectiveId = achievement.LockedDescriptionId;
xBoxSeriesX.UnlockedObjectiveId = achievement.UnlockedDescriptionId;
}
}
await Game.OpenForEditOrAdd(Configuration.SettingsFilePath).ConfigureAwait(true);
await _microsoftAchievementService.UpdateAchievementsAsync(
Configuration.SettingsFilePath,
PlatformChallenges
.Select(a => a.ToServiceAchievement())
.ToArray()).ConfigureAwait(false);
await _microsoftAchievementService.UpdateLocalizationAsync(
Configuration.SettingsFilePath,
PlatformChallenges
.SelectMany(a => a.ToLocalizedStrings(Configuration.Languages.Where(l => l.IsSelected).Select(l => l.Value)))
.ToArray()).ConfigureAwait(false);
}),
$"XBOX SERIES X - Updating XBOX SERIES X settings \"{Configuration.SettingsFilePath}\"",
$"XBOX SERIES X - Updated XBOX SERIES X settings \"{Configuration.SettingsFilePath}\"",
$"XBOX SERIES X - Failed to update XBOX SERIES X settings \"{Configuration.SettingsFilePath}\"").ConfigureAwait(true);
}
}
}