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

117 lines
5.8 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.Ps4;
using JD.Guinness.WPF.Module.Model.Ps5;
using JD.SonyTrophy.Service;
namespace JD.Guinness.WPF.Module.ViewModel
{
public sealed class Ps5ViewModel : APlatformChallengeViewModel<Ps5Trophy>
{
private readonly ISonyTrophyService _sonyTrophyService;
public DelegateCommand SetTitleImageCommand { get; }
public DelegateCommand<Ps5Trophy> SetImageCommand { get; }
public DelegateCommand SetImageFromIdCommand { get; }
public DelegateCommand SelectSettingsFileCommand { get; }
public AsyncDelegateCommand SetSameAsPs4Command { get; }
public Ps5Configuration Configuration => Game.Ps5Configuration;
public int TotalPoints => PlatformChallenges.Sum(t => t.Grade.ToPoints());
public Ps5ViewModel(GameViewModel gameViewModel, ISonyTrophyService sonyTrophyService)
: base(gameViewModel)
{
_sonyTrophyService = sonyTrophyService ?? throw new ArgumentNullException(nameof(sonyTrophyService));
SetTitleImageCommand = new DelegateCommand(OnSetTitleImage, () => Game.IsAvailable);
SetImageCommand = new DelegateCommand<Ps5Trophy>(OnSetImage, _ => Game.IsAvailable);
SetImageFromIdCommand = new DelegateCommand(OnSetImageFromId, () => Game.IsAvailable);
SelectSettingsFileCommand = new DelegateCommand(OnSelectSettingsFile, () => Game.IsAvailable);
SetSameAsPs4Command = new AsyncDelegateCommand(OnSetSameAsPs4, () => Game.IsAvailable);
}
private void OnSetTitleImage() => SelectFile("Select the title image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps5ImagesFolderPath), "png|*.png", file => Configuration.TitleImagePath = file);
private void OnSetImage(Ps5Trophy trophy) => SelectFile("Select the trophy image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps5ImagesFolderPath), "png|*.png", file => trophy.ImagePath = file);
private void OnSetImageFromId() => SetImageFromId(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps5ImagesFolderPath), (trophy, file) => trophy.ImagePath = file);
private void OnSelectSettingsFile() => SelectFile("Select the settings file to update", Game.Settings.WorkspacePath, "json|*.json", file => Configuration.SettingsFilePath = file);
private async Task OnSetSameAsPs4() => await Game.StartProcess(
Task.Run(() =>
{
foreach (Ps5Trophy achievement in PlatformChallenges.ToList())
{
achievement.Remove();
achievement.Challenge.NotifyPlatformChallenges();
}
Configuration.TitleName = Game.Ps4Configuration.TitleName;
foreach (Challenge challenge in Game.Challenges)
{
Ps4Trophy ps4 = challenge.GetPlatformChallenge<Ps4Trophy>();
if (ps4 != null)
{
Ps5Trophy ps5 = challenge.GetOrAddPlatformChallenge<Ps5Trophy>();
ps5.CustomData.Add(ps4.CustomData.Values);
ps5.Id = ps4.Id;
ps5.ImagePath = ps4.ImagePath.FromDifferentDirectory(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.Ps5ImagesFolderPath));
ps5.Grade = ps4.Grade;
ps5.IsRequiredForPlatinium = ps4.IsRequiredForPlatinium;
ps5.IsSecret = ps4.IsSecret;
challenge.NotifyPlatformChallenges();
}
}
}),
"PS5 - Replicating PS4 achievements",
"PS5 - Replicated PS4 achievements",
"PS5 - Failed to replicate PS4 achievements");
protected override Ps5Trophy DuplicatePlatFormChallengeInto(Ps5Trophy platformChallenge, Challenge challenge) => platformChallenge.DuplicateInto(challenge);
protected override void OnPlatformChallengeChanged(object target, PropertyChangedEventArgs args)
{
if (target is null
|| (target is Ps5Trophy && args.PropertyName == nameof(Ps5Trophy.Grade)))
{
OnPropertyChanged(nameof(TotalPoints));
}
}
protected override async Task OnGenerate()
{
await Game.StartProcess(
Task.Run(async () =>
{
await Game.OpenForEditOrAdd(Configuration.SettingsFilePath).ConfigureAwait(false);
await _sonyTrophyService.UpdateTrophiesAsync(
Configuration.SettingsFilePath,
new TrophySet(
Configuration.Version,
Configuration.Languages.Where(s => s.IsSelected && !string.IsNullOrEmpty(s.Value.Ps5Id)).ToDictionary(
s => s.Value.Ps5Id,
s => new Text(Configuration.TitleName.GetTranslation(s.Value.Language), null)),
Configuration.TitleImagePath,
PlatformChallenges
.Select(t => t.ToServiceTrophy(Configuration.Languages.Where(s => s.IsSelected && !string.IsNullOrEmpty(s.Value.Ps5Id)).Select(s => s.Value)))
.ToArray())).ConfigureAwait(false);
}),
$"PS5 - Updating PS5 settings \"{Configuration.SettingsFilePath}\"",
$"PS5 - Updated PS5 settings \"{Configuration.SettingsFilePath}\"",
$"PS5 - Failed to update PS5 settings \"{Configuration.SettingsFilePath}\"").ConfigureAwait(true);
}
}
}