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

132 lines
8 KiB
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using JD.GoogleAchievement.Service;
using JD.Guinness.WPF.Module.Model;
using JD.Guinness.WPF.Module.Model.Stadia;
namespace JD.Guinness.WPF.Module.ViewModel
{
public sealed class StadiaViewModel : APlatformChallengeViewModel<StadiaAchievement>
{
private readonly IGoogleAchievementService _googleAchievementService;
public DelegateCommand<StadiaAchievement> SetLockedImageCommand { get; }
public DelegateCommand<StadiaAchievement> SetUnlockedImageCommand { get; }
public DelegateCommand SetLockedImageFromIdCommand { get; }
public DelegateCommand SetUnlockedImageFromIdCommand { get; }
public DelegateCommand SetLanguagesFromStoreFrontFileCommand { get; }
public DelegateCommand SelectStoreFrontFileCommand { get; }
public DelegateCommand SelectExcelFileCommand { get; }
public DelegateCommand SelectIconsFolderCommand { get; }
public StadiaConfiguration Configuration => Game.StadiaConfiguration;
public StadiaViewModel(GameViewModel gameViewModel, IGoogleAchievementService googleAchievementService)
: base(gameViewModel)
{
_googleAchievementService = googleAchievementService ?? throw new ArgumentNullException(nameof(googleAchievementService));
SetLockedImageCommand = new DelegateCommand<StadiaAchievement>(OnSetLockedImage, _ => Game.IsAvailable);
SetUnlockedImageCommand = new DelegateCommand<StadiaAchievement>(OnSetUnlockedImage, _ => Game.IsAvailable);
SetLockedImageFromIdCommand = new DelegateCommand(OnSetLockedImageFromId, () => Game.IsAvailable);
SetUnlockedImageFromIdCommand = new DelegateCommand(OnSetUnlockedImageFromId, () => Game.IsAvailable);
SetLanguagesFromStoreFrontFileCommand = new DelegateCommand(OnSetLanguagesFromStoreFrontFile, () => Game.IsAvailable && !string.IsNullOrEmpty(Configuration.StoreFrontFilePath));
SelectStoreFrontFileCommand = new DelegateCommand(OnSelectStoreFrontFile, () => Game.IsAvailable);
SelectExcelFileCommand = new DelegateCommand(OnSelectExcelFile, () => Game.IsAvailable);
SelectIconsFolderCommand = new DelegateCommand(OnSelectIconsFolder, () => Game.IsAvailable);
}
private void OnSetLockedImage(StadiaAchievement achievement) => SelectFile("Select the achievement locked image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.StadiaImagesFolderPath), "png|*.png", file => achievement.LockedImagePath = file);
private void OnSetUnlockedImage(StadiaAchievement achievement) => SelectFile("Select the achievement unlocked image", Path.Combine(Game.Settings.WorkspacePath, Game.Settings.StadiaImagesFolderPath), "png|*.png", file => achievement.UnlockedImagePath = file);
private void OnSetLockedImageFromId() => SetImageFromId(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.StadiaImagesFolderPath), (achievement, file) => achievement.LockedImagePath = file);
private void OnSetUnlockedImageFromId() => SetImageFromId(Path.Combine(Game.Settings.WorkspacePath, Game.Settings.StadiaImagesFolderPath), (achievement, file) => achievement.UnlockedImagePath = file);
private async void OnSetLanguagesFromStoreFrontFile()
{
await Game.StartProcess(
Task.Run(async () =>
{
HashSet<string> languages = new HashSet<string>(await _googleAchievementService.ReadLanguagesFromStoreFrontAsync(Configuration.StoreFrontFilePath).ConfigureAwait(true));
foreach (Selectable<LanguageInfo> language in Configuration.Languages)
{
language.IsSelected = languages.Contains(language.Value.GoogleId);
}
}),
$"Stadia - Reading Stadia languages from store front \"{Configuration.StoreFrontFilePath}\"",
$"Stadia - Read Stadia languages from store front \"{Configuration.StoreFrontFilePath}\"",
$"Stadia - Failed to read Stadia languages from store front \"{Configuration.StoreFrontFilePath}\"").ConfigureAwait(true);
}
private void OnSelectStoreFrontFile() => SelectFile("Select the store front file to update", Game.Settings.WorkspacePath, "tar|*.tar", file => Configuration.StoreFrontFilePath = file);
private void OnSelectExcelFile() => SaveFile("Select where to generate the excel file", "xlsx|*.xlsx", file => Configuration.ExcelFilePath = file);
private void OnSelectIconsFolder() => SelectFolder("Select folder where achievement images will be copied", Game.Settings.WorkspacePath, directory => Configuration.IconsFolderPath = directory);
protected override StadiaAchievement DuplicatePlatFormChallengeInto(StadiaAchievement platformChallenge, Challenge challenge) => platformChallenge.DuplicateInto(challenge);
protected async override Task OnGenerate()
{
if (Configuration.SyncToStoreFront)
{
await Game.StartProcess(
Task.Run(async () =>
{
await Game.OpenForEditOrAdd(Configuration.StoreFrontFilePath).ConfigureAwait(false);
HashSet<string> languageIds = new HashSet<string>();
HashSet<LanguageInfo> languages = new HashSet<LanguageInfo>(Configuration.Languages.Select(l => l.Value).Where(l => languageIds.Add(l.GoogleId)));
await _googleAchievementService.UpdateStoreFrontAsync(
Configuration.StoreFrontFilePath,
PlatformChallenges
.Select(a => a.ToServiceAchievement(languages))
.ToArray()).ConfigureAwait(false);
}),
$"Stadia - Updating Stadia store front \"{Configuration.StoreFrontFilePath}\"",
$"Stadia - Updated Stadia store front \"{Configuration.StoreFrontFilePath}\"",
$"Stadia - Failed to update Stadia store front \"{Configuration.StoreFrontFilePath}\"").ConfigureAwait(true);
}
if (Configuration.GenerateExcel)
{
await Game.StartProcess(
Task.Run(async () =>
{
await Game.OpenForEditOrAdd(Configuration.ExcelFilePath).ConfigureAwait(false);
string[] iconFiles = Array.Empty<string>();
if (!string.IsNullOrEmpty(Configuration.IconsFolderPath))
{
await Game.Revert(Configuration.IconsFolderPath + "\\*.png").ConfigureAwait(false);
iconFiles = Directory.GetFiles(Configuration.IconsFolderPath, "*.png");
}
await _googleAchievementService.GenerateExcelAsync(
Configuration.ExcelFilePath,
PlatformChallenges
.Select(a => a.ToServiceAchievement(Configuration.Languages.Where(l => l.IsSelected).Select(l => l.Value)))
.ToArray(),
Configuration.IconsFolderPath).ConfigureAwait(false);
if (!string.IsNullOrEmpty(Configuration.IconsFolderPath))
{
await Game.OpenForEditOrAdd(Directory.GetFiles(Configuration.IconsFolderPath, "*.png")).ConfigureAwait(true);
await Game.OpenForDeleteIfMissing(iconFiles).ConfigureAwait(true);
}
}),
$"Stadia - Generating Stadia Excel file \"{Configuration.ExcelFilePath}\"",
$"Stadia - Generated Stadia Excel file \"{Configuration.ExcelFilePath}\"",
$"Stadia - Failed to generate Stadia Excel file \"{Configuration.ExcelFilePath}\"").ConfigureAwait(true);
}
}
}
}