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.XBoxOne; using JD.Guinness.WPF.Module.Model.XBoxSeriesX; using JD.MicrosoftAchievement.Service; namespace JD.Guinness.WPF.Module.ViewModel { public sealed class XBoxOneViewModel : APlatformChallengeViewModel { private readonly IMicrosoftAchievementService _microsoftAchievementService; public int TotalPoints => PlatformChallenges.Where(a => a.IsBaseAchievement).Sum(a => a.Gamerscore); public DelegateCommand SetImageCommand { get; } public DelegateCommand SetImageFromIdCommand { get; } public DelegateCommand SetGamerscoreFromPs4TrophyCommand { get; } public DelegateCommand SelectSettingsFileCommand { get; } public AsyncDelegateCommand SetSameAsXBoxSeriesXCommand { get; } public XBoxOneConfiguration Configuration { get; } public XBoxOneViewModel(GameViewModel gameViewModel, IMicrosoftAchievementService microsoftAchievementService) : base(gameViewModel) { _microsoftAchievementService = microsoftAchievementService ?? throw new ArgumentNullException(nameof(microsoftAchievementService)); Configuration = gameViewModel.XBoxOneConfiguration; SetImageCommand = new DelegateCommand(OnSetImage, _ => Game.IsAvailable); SetImageFromIdCommand = new DelegateCommand(OnSetImageFromId, () => Game.IsAvailable); SetGamerscoreFromPs4TrophyCommand = new DelegateCommand(OnSetGamerscoreFromPs4Trophy, () => Game.IsAvailable); SelectSettingsFileCommand = new DelegateCommand(OnSelectSettingsFile, () => Game.IsAvailable); SetSameAsXBoxSeriesXCommand = new AsyncDelegateCommand(OnSetSameAsXBoxSeriesX, () => Game.IsAvailable); } private void OnSetImage(XBoxOneAchievement achievement) => SelectFile("Select the achievement image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.XBoxOneImagesFolderPath), "png|*.png", file => achievement.ImagePath = file); private void OnSetImageFromId() => SetImageFromId(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.XBoxOneImagesFolderPath), (achievement, file) => achievement.ImagePath = file); private void OnSetGamerscoreFromPs4Trophy() { foreach (XBoxOneAchievement achievement in PlatformChallenges) { Ps4Trophy trophy = achievement.Challenge.GetPlatformChallenge(); 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 OnSetSameAsXBoxSeriesX() { await Game.StartProcess( Task.Run(() => { foreach (XBoxOneAchievement achievement in PlatformChallenges.ToList()) { achievement.Remove(); achievement.Challenge.NotifyPlatformChallenges(); } foreach (Challenge challenge in Game.Challenges) { XBoxSeriesXAchievement xBoxSeriesX = challenge.GetPlatformChallenge(); if (xBoxSeriesX != null) { XBoxOneAchievement xBoxOne = challenge.GetOrAddPlatformChallenge(); xBoxOne.CustomData.Add(xBoxSeriesX.CustomData.Values); xBoxOne.Gamerscore = xBoxSeriesX.Gamerscore; xBoxOne.Id = xBoxSeriesX.Id; xBoxOne.ImagePath = xBoxSeriesX.ImagePath; xBoxOne.IsBaseAchievement = xBoxSeriesX.IsBaseAchievement; xBoxOne.IsSecret = xBoxSeriesX.IsSecret; xBoxOne.LockedObjective = xBoxSeriesX.LockedObjective; xBoxOne.UnlockedObjective = xBoxSeriesX.UnlockedObjective; challenge.NotifyPlatformChallenges(); } } }), "XBOX ONE - Replicating XBOX SERIES X achievements", "XBOX ONE - Replicated XBOX SERIES X achievements", "XBOX ONE - Failed to replicate XBOX SERIES X achievements").ConfigureAwait(true); } protected override XBoxOneAchievement DuplicatePlatFormChallengeInto(XBoxOneAchievement platformChallenge, Challenge challenge) => platformChallenge.DuplicateInto(challenge); protected override void OnPlatformChallengeChanged(object target, PropertyChangedEventArgs args) { if (target is null || (target is XBoxOneAchievement && (args.PropertyName == nameof(XBoxOneAchievement.Gamerscore) || args.PropertyName == nameof(XBoxOneAchievement.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)) { XBoxOneAchievement xBoxOne = PlatformChallenges.FirstOrDefault(a => a.Id == achievement.Id); if (xBoxOne is null) { Game.Warn($"XBOX ONE - Achievement with id \"{achievement.Id}\" found in settings file but not in declared XBOX ONE achievements"); } else { xBoxOne.NameId = achievement.NameId; xBoxOne.LockedObjectiveId = achievement.LockedDescriptionId; xBoxOne.UnlockedObjectiveId = achievement.UnlockedDescriptionId; } } await Game.OpenForEditOrAdd(Configuration.SettingsFilePath).ConfigureAwait(false); 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 ONE - Updating XBOX ONE settings \"{Configuration.SettingsFilePath}\"", $"XBOX ONE - Updated XBOX ONE settings \"{Configuration.SettingsFilePath}\"", $"XBOX ONE - Failed to update XBOX ONE settings \"{Configuration.SettingsFilePath}\"").ConfigureAwait(true); } } }