Misc refactor after #3287

This commit is contained in:
Łukasz Domeradzki 2024-09-13 10:04:56 +02:00
parent 8e85b87764
commit 3bb83610b8
No known key found for this signature in database
GPG key ID: 6B138B4C64555AEA
3 changed files with 30 additions and 27 deletions

View file

@ -21,9 +21,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
using JetBrains.Annotations;
namespace ArchiSteamFarm.Steam.Data;
#pragma warning disable CA1027 // Aliases are intentional, we don't plan to combine fields
[PublicAPI]
public enum EAssetRarity : byte {
Unknown,
Common,

View file

@ -214,18 +214,16 @@ public sealed class InventoryDescription {
return CachedRarity.Value;
}
case "RARITY":
string internalName = tag.internal_name.Split('_', StringSplitOptions.RemoveEmptyEntries).Skip(1).FirstOrDefault() ?? tag.internal_name;
string[] internalNameArgs = tag.internal_name.Split('_', 2, StringSplitOptions.RemoveEmptyEntries);
if (Enum.TryParse<EAssetRarity>(internalName, true, out EAssetRarity assetRarity)) {
if ((internalNameArgs.Length >= 2) && Enum.TryParse(internalNameArgs[1], true, out EAssetRarity assetRarity) && Enum.IsDefined(assetRarity)) {
CachedRarity = assetRarity;
} else {
ASF.ArchiLogger.LogGenericError(Strings.FormatWarningUnknownValuePleaseReport(nameof(tag.internal_name), tag.internal_name));
return CachedRarity.Value;
CachedRarity = EAssetRarity.Unknown;
}
ASF.ArchiLogger.LogGenericError(Strings.FormatWarningUnknownValuePleaseReport(nameof(tag.internal_name), tag.internal_name));
CachedRarity = EAssetRarity.Unknown;
return CachedRarity.Value;
}
}

View file

@ -520,6 +520,24 @@ public sealed class Commands {
return gamesOwned;
}
private static HashSet<EAssetRarity>? ParseAssetRarities(string assetRaritiesText) {
ArgumentException.ThrowIfNullOrEmpty(assetRaritiesText);
string[] assetRaritiesArgs = assetRaritiesText.Split(SharedInfo.ListElementSeparators, StringSplitOptions.RemoveEmptyEntries);
HashSet<EAssetRarity> assetRarities = [];
foreach (string assetRarityArg in assetRaritiesArgs) {
if (!Enum.TryParse(assetRarityArg, true, out EAssetRarity assetRarity) || !Enum.IsDefined(assetRarity)) {
return null;
}
assetRarities.Add(assetRarity);
}
return assetRarities;
}
private async Task<string?> Response2FA(EAccess access) {
if (!Enum.IsDefined(access)) {
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
@ -780,7 +798,7 @@ public sealed class Commands {
HashSet<EAssetRarity>? assetRarities = ParseAssetRarities(assetRaritiesText);
if (assetRarities == null) {
if ((assetRarities == null) || (assetRarities.Count == 0)) {
return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities)));
}
@ -993,7 +1011,7 @@ public sealed class Commands {
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
private async Task<string?> ResponseAdvancedTransferByAssetRarity(EAccess access, uint appID, ulong contextID, Bot targetBot, HashSet<EAssetRarity> assetRarities) {
private async Task<string?> ResponseAdvancedTransferByAssetRarity(EAccess access, uint appID, ulong contextID, Bot targetBot, IReadOnlyCollection<EAssetRarity> assetRarities) {
if (!Enum.IsDefined(access)) {
throw new InvalidEnumArgumentException(nameof(access), (int) access, typeof(EAccess));
}
@ -1045,7 +1063,7 @@ public sealed class Commands {
HashSet<EAssetRarity>? assetRarities = ParseAssetRarities(assetRaritiesText);
if (assetRarities == null) {
if ((assetRarities == null) || (assetRarities.Count == 0)) {
return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities)));
}
@ -1085,7 +1103,7 @@ public sealed class Commands {
HashSet<EAssetRarity>? assetRarities = ParseAssetRarities(assetRaritiesText);
if (assetRarities == null) {
if ((assetRarities == null) || (assetRarities.Count == 0)) {
return FormatStaticResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities)));
}
@ -3427,22 +3445,6 @@ public sealed class Commands {
return responses.Count > 0 ? string.Join(Environment.NewLine, responses) : null;
}
private static HashSet<EAssetRarity>? ParseAssetRarities(string assetRaritiesText) {
string[] assetRaritiesArgs = assetRaritiesText.Split(SharedInfo.ListElementSeparators, StringSplitOptions.RemoveEmptyEntries);
HashSet<EAssetRarity> assetRarities = [];
foreach (string assetRarityArg in assetRaritiesArgs) {
if (!Enum.TryParse(assetRarityArg, true, out EAssetRarity assetRarity) || !Enum.IsDefined(assetRarity)) {
return null;
}
assetRarities.Add(assetRarity);
}
return assetRarities;
}
[Flags]
private enum ERedeemFlags : ushort {
None = 0,