From 87451615e81c153a6a43318083a76895bd23a261 Mon Sep 17 00:00:00 2001 From: Archi Date: Fri, 23 Feb 2024 14:14:16 +0100 Subject: [PATCH] Extract addlicense logic to actions --- ArchiSteamFarm/Steam/Interaction/Actions.cs | 24 ++++++++++++++++++++ ArchiSteamFarm/Steam/Interaction/Commands.cs | 20 ++++++++-------- 2 files changed, 35 insertions(+), 9 deletions(-) diff --git a/ArchiSteamFarm/Steam/Interaction/Actions.cs b/ArchiSteamFarm/Steam/Interaction/Actions.cs index 01c9df571..900fa18ec 100644 --- a/ArchiSteamFarm/Steam/Interaction/Actions.cs +++ b/ArchiSteamFarm/Steam/Interaction/Actions.cs @@ -78,6 +78,30 @@ public sealed class Actions : IAsyncDisposable, IDisposable { } } + [PublicAPI] + public async Task<(EResult Result, IReadOnlyCollection? GrantedApps, IReadOnlyCollection? GrantedPackages)> AddFreeLicenseApp(uint appID) { + ArgumentOutOfRangeException.ThrowIfZero(appID); + + SteamApps.FreeLicenseCallback callback; + + try { + callback = await Bot.SteamApps.RequestFreeLicense(appID).ToLongRunningTask().ConfigureAwait(false); + } catch (Exception e) { + Bot.ArchiLogger.LogGenericWarningException(e); + + return (EResult.Timeout, null, null); + } + + return (callback.Result, callback.GrantedApps, callback.GrantedPackages); + } + + [PublicAPI] + public async Task<(EResult Result, EPurchaseResultDetail PurchaseResultDetail)> AddFreeLicensePackage(uint subID) { + ArgumentOutOfRangeException.ThrowIfZero(subID); + + return await Bot.ArchiWebHandler.AddFreeLicense(subID).ConfigureAwait(false); + } + [PublicAPI] public static string? Encrypt(ArchiCryptoHelper.ECryptoMethod cryptoMethod, string stringToEncrypt) { if (!Enum.IsDefined(cryptoMethod)) { diff --git a/ArchiSteamFarm/Steam/Interaction/Commands.cs b/ArchiSteamFarm/Steam/Interaction/Commands.cs index 4cca8be2c..2274e1f70 100644 --- a/ArchiSteamFarm/Steam/Interaction/Commands.cs +++ b/ArchiSteamFarm/Steam/Interaction/Commands.cs @@ -623,7 +623,7 @@ public sealed class Commands { } switch (type.ToUpperInvariant()) { - case "A" or "APP": + case "A" or "APP": { HashSet? packageIDs = ASF.GlobalDatabase?.GetPackageIDs(gameID, Bot.OwnedPackageIDs.Keys, 1); if (packageIDs is { Count: > 0 }) { @@ -632,32 +632,34 @@ public sealed class Commands { break; } - SteamApps.FreeLicenseCallback callback; + (EResult result, IReadOnlyCollection? grantedApps, IReadOnlyCollection? grantedPackages) = await Bot.Actions.AddFreeLicenseApp(gameID).ConfigureAwait(false); - try { - callback = await Bot.SteamApps.RequestFreeLicense(gameID).ToLongRunningTask().ConfigureAwait(false); - } catch (Exception e) { - Bot.ArchiLogger.LogGenericWarningException(e); + if (((grantedApps == null) || (grantedApps.Count == 0)) && ((grantedPackages == null) || (grantedPackages.Count == 0))) { response.AppendLine(FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotAddLicense, $"app/{gameID}", EResult.Timeout))); break; } - response.AppendLine(FormatBotResponse((callback.GrantedApps.Count > 0) || (callback.GrantedPackages.Count > 0) ? string.Format(CultureInfo.CurrentCulture, Strings.BotAddLicenseWithItems, $"app/{gameID}", callback.Result, string.Join(", ", callback.GrantedApps.Select(static appID => $"app/{appID}").Union(callback.GrantedPackages.Select(static subID => $"sub/{subID}")))) : string.Format(CultureInfo.CurrentCulture, Strings.BotAddLicense, $"app/{gameID}", callback.Result))); + grantedApps ??= Array.Empty(); + grantedPackages ??= Array.Empty(); + + response.AppendLine(FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotAddLicenseWithItems, $"app/{gameID}", result, string.Join(", ", grantedApps.Select(static appID => $"app/{appID}").Union(grantedPackages.Select(static subID => $"sub/{subID}")))))); break; - default: + } + default: { if (Bot.OwnedPackageIDs.ContainsKey(gameID)) { response.AppendLine(FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotAddLicense, $"sub/{gameID}", $"{EResult.Fail}/{EPurchaseResultDetail.AlreadyPurchased}"))); break; } - (EResult result, EPurchaseResultDetail purchaseResult) = await Bot.ArchiWebHandler.AddFreeLicense(gameID).ConfigureAwait(false); + (EResult result, EPurchaseResultDetail purchaseResult) = await Bot.Actions.AddFreeLicensePackage(gameID).ConfigureAwait(false); response.AppendLine(FormatBotResponse(string.Format(CultureInfo.CurrentCulture, Strings.BotAddLicense, $"sub/{gameID}", $"{result}/{purchaseResult}"))); break; + } } }