Skip to content

Commit

Permalink
Misc refactor after #3287
Browse files Browse the repository at this point in the history
  • Loading branch information
JustArchi committed Sep 13, 2024
1 parent 8e85b87 commit 3bb8361
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
3 changes: 3 additions & 0 deletions ArchiSteamFarm/Steam/Data/EAssetRarity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
12 changes: 5 additions & 7 deletions ArchiSteamFarm/Steam/Data/InventoryDescription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,18 +214,16 @@ public EAssetRarity Rarity {
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;
}
}
Expand Down
42 changes: 22 additions & 20 deletions ArchiSteamFarm/Steam/Interaction/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,24 @@ internal void OnNewLicenseList() {
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));
Expand Down Expand Up @@ -780,7 +798,7 @@ internal void OnNewLicenseList() {

HashSet<EAssetRarity>? assetRarities = ParseAssetRarities(assetRaritiesText);

if (assetRarities == null) {
if ((assetRarities == null) || (assetRarities.Count == 0)) {
return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities)));
}

Expand Down Expand Up @@ -993,7 +1011,7 @@ internal void OnNewLicenseList() {
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));
}
Expand Down Expand Up @@ -1045,7 +1063,7 @@ internal void OnNewLicenseList() {

HashSet<EAssetRarity>? assetRarities = ParseAssetRarities(assetRaritiesText);

if (assetRarities == null) {
if ((assetRarities == null) || (assetRarities.Count == 0)) {
return FormatBotResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities)));
}

Expand Down Expand Up @@ -1085,7 +1103,7 @@ internal void OnNewLicenseList() {

HashSet<EAssetRarity>? assetRarities = ParseAssetRarities(assetRaritiesText);

if (assetRarities == null) {
if ((assetRarities == null) || (assetRarities.Count == 0)) {
return FormatStaticResponse(Strings.FormatErrorIsInvalid(nameof(assetRarities)));
}

Expand Down Expand Up @@ -3427,22 +3445,6 @@ internal void OnNewLicenseList() {
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,
Expand Down

0 comments on commit 3bb8361

Please sign in to comment.