using System.Collections.ObjectModel; using AchiesUtilities.Models; using SteamLib.Exceptions; using SteamLib.Utility; namespace SteamLib.Core.StatusCodes; public partial class SteamStatusCode : Enumeration { public static IReadOnlyDictionary StatusCodes { get; } static SteamStatusCode() { StatusCodes = new ReadOnlyDictionary(GetAll() .ToDictionary(ssc => ssc.Id, ssc => ssc)); } protected SteamStatusCode(int id, string name) : base(id, name) { } /// /// Tries to translate status code. If no status code was found with /// will be thrown /// /// /// /// /// /// if value is universal. if value is /// specific /// /// public static SteamStatusCode Translate(string response, out bool isUniversal) where T : SteamStatusCode { int statusCode; try { statusCode = Utilities.GetSuccessCode(response); } catch { throw new SteamStatusCodeException(Undefined, response); } return Translate(statusCode, out isUniversal); } /// /// Translate to specific or /// /// /// /// /// /// if value is universal. if value is /// specific /// /// public static SteamStatusCode Translate(int statusCode, out bool isUniversal) where T : SteamStatusCode { if (statusCode < 2) { var status = StatusCodes[statusCode]; isUniversal = true; return status; } var allSpecific = GetAll(); var translated = allSpecific.FirstOrDefault(s => s.Id == statusCode); isUniversal = translated == null; if (translated != null) { return translated; } if (translated == null && StatusCodes.TryGetValue(statusCode, out var universal)) { return universal; } return translated ?? new SteamStatusCode(statusCode, nameof(Unknown)); } public override bool Equals(object? obj) { if (obj is not SteamStatusCode translated) return false; if (Id < 2) { return translated.Id == Id; } return base.Equals(translated); } public override int GetHashCode() { return base.GetHashCode(); } /// /// /// /// public static void ValidateSuccessOrThrow(string response) where T : SteamStatusCode { var translated = Translate(response, out _); if (translated.Id != 1) throw new SteamStatusCodeException(translated, response); } /// /// Same as with generic parameter ///
/// (Used in case when steam status code is not defined for this operation) ///
/// /// public static void ValidateSuccessOrThrow(string response) { ValidateSuccessOrThrow(response); } /// /// /// /// public static void ValidateSuccessOrThrow(int statusCode) where T : SteamStatusCode { if (statusCode == 1) return; var translated = Translate(statusCode, out _); throw new SteamStatusCodeException(translated, statusCode.ToString()); } /// /// Same as with generic parameter
/// (Used in case when steam status code is not defined for this operation) ///
/// /// public static void ValidateSuccessOrThrow(int statusCode) { ValidateSuccessOrThrow(statusCode); } /// /// Validates that status code is or specific /// /// /// /// /// public static SteamStatusCode TranslateOrThrow(string response, out bool isUniversal) where T : SteamStatusCode { var translated = Translate(response, out isUniversal); if (translated.Id < 1) throw new SteamStatusCodeException(translated, response); return translated; } public override string ToString() { return $"{Name} ({Id})"; } }