From f34935c96e99affb6e4cfd29e40affd2891bbb3f Mon Sep 17 00:00:00 2001 From: Bobo Date: Sat, 3 Sep 2022 10:54:25 +0200 Subject: [PATCH] Done! --- Classes/Base/Stash.cs | 4 +- Classes/Drug.cs | 6 +- Constants.cs | 6 +- Enums.cs | 235 ++++++++++++++++++----------------- Extension/Utils.cs | 10 +- GUI/Elements/DrugListItem.cs | 26 ++-- 6 files changed, 149 insertions(+), 138 deletions(-) diff --git a/Classes/Base/Stash.cs b/Classes/Base/Stash.cs index 2781110c..f165a4c1 100644 --- a/Classes/Base/Stash.cs +++ b/Classes/Base/Stash.cs @@ -6,7 +6,7 @@ namespace Los.Santos.Dope.Wars.Classes.Base; /// /// The class is the base class for drug stashes. -/// Implements the members of the interface +/// Implements the members of the interface. /// [XmlRoot(ElementName = nameof(Stash), IsNullable = false)] public abstract class Stash : IStash @@ -22,7 +22,7 @@ public abstract class Stash : IStash /// /// The empty constructor for the class /// - public Stash() => Drugs = new(); + protected Stash() => Drugs = new(); #endregion #region IStash members diff --git a/Classes/Drug.cs b/Classes/Drug.cs index bbe4edf5..e2db08f1 100644 --- a/Classes/Drug.cs +++ b/Classes/Drug.cs @@ -3,7 +3,7 @@ namespace Los.Santos.Dope.Wars.Classes; /// -/// The class is the root element for the drug class +/// The class is the root element for the drug class. /// [XmlRoot(ElementName = nameof(Drug), IsNullable = false)] public class Drug @@ -34,7 +34,7 @@ public Drug() /// public Drug(Enums.DrugType drugType, string description, int marketValue) { - Name = drugType.ToString(); + Name = drugType.GetName(); Description = description; CurrentPrice = default; AveragePrice = marketValue; @@ -96,7 +96,7 @@ public Drug(string drugName, string description, int marketValue) [XmlAttribute(AttributeName = nameof(Quantity))] public int Quantity { - get { return _quantity; } + get => _quantity; set { _quantity = value; diff --git a/Constants.cs b/Constants.cs index 4605fed6..1b164fa2 100644 --- a/Constants.cs +++ b/Constants.cs @@ -45,17 +45,17 @@ public static class Constants /// /// The are the drugs the player can peddle from level 1 on. /// - public const DrugType TradePackOne = DrugType.Mushrooms | DrugType.Amphetamine | DrugType.Oxycodone | DrugType.Marijuana | DrugType.Hashish; + public const DrugType TradePackOne = DrugType.SHROOMS | DrugType.SPEED | DrugType.OXY | DrugType.CANA | DrugType.HASH; /// /// The are the drugs the player can additional peddle from level 10 on. /// - public const DrugType TradePackTwo = DrugType.Mescaline | DrugType.MDMA | DrugType.Ecstasy | DrugType.Acid | DrugType.PCP; + public const DrugType TradePackTwo = DrugType.PEYO | DrugType.MDMA | DrugType.XTC | DrugType.LSD | DrugType.PCP; /// /// The are the drugs the player can additional peddle from level 20 on. /// - public const DrugType TradePackThree = DrugType.Heroin | DrugType.Cocaine | DrugType.Methamphetamine | DrugType.Crack | DrugType.Ketamine; + public const DrugType TradePackThree = DrugType.SMACK | DrugType.COKE | DrugType.METH | DrugType.CRACK | DrugType.KETA; /// /// The is the maximum level the player can reach for each character. diff --git a/Enums.cs b/Enums.cs index 6fa5d353..b6f58363 100644 --- a/Enums.cs +++ b/Enums.cs @@ -100,101 +100,85 @@ public enum DifficultyType public enum DrugType { /// - /// The drug type enum. + /// The drug type enum. /// - [Description("The none flag.")] - [DrugPrice(0)] - None = 0, + [DrugType("None", 0, "The none flag.")] + NONE = 0, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Cocaine is a powerful stimulant and narcotic.")] - [DrugPrice(865)] - Cocaine = 1, + [DrugType("Cocaine", 2029, "Cocaine is a powerful stimulant and narcotic.")] + COKE = 1, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Heroin is a semi-synthetic, strongly analgesic opioid.")] - [DrugPrice(895)] - Heroin = 2, + [DrugType("Heroin", 1207, "Heroin is a semi-synthetic, strongly analgesic opioid.")] + SMACK = 2, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Marijuana is a psychoactive drug from the Cannabis plant")] - [DrugPrice(165)] - Marijuana = 4, + [DrugType("Marijuana", 283, "Marijuana is a psychoactive drug from the Cannabis plant")] + CANA = 4, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Hashish refers to the resin extracted from the cannabis plant.")] - [DrugPrice(125)] - Hashish = 8, + [DrugType("Hashish", 266, "Hashish refers to the resin extracted from the cannabis plant.")] + HASH = 8, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Psychoactive mushrooms, also known as magic mushrooms.")] - [DrugPrice(245)] - Mushrooms = 16, + [DrugType("Mushrooms", 245, "Psychoactive mushrooms, also known as magic mushrooms.")] + SHROOMS = 16, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Amphetamine has a strong stimulating and uplifting effect.")] - [DrugPrice(215)] - Amphetamine = 32, + [DrugType("Amphetamine", 337, "Illegally trafficked amphetamine is also known as 'speed' or 'pep'.")] + SPEED = 32, /// /// The drug type enum. /// - [Description("Also known as Angel Dust or Peace Pill in the drug scene.")] - [DrugPrice(255)] + [DrugType("Angel Dust", 255, "Also known as PCP or Peace Pill in the drug scene.")] PCP = 64, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Methamphetamine is a powerful psychostimulant.")] - [DrugPrice(785)] - Methamphetamine = 128, + [DrugType("Methamphetamine", 2211, "On the black market, methamphetamine is also known as meth or crystal.")] + METH = 128, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Ketamine is a dissociative anaesthetic used in human medicine.")] - [DrugPrice(545)] - Ketamine = 256, + [DrugType("Ketamine", 545, "Ketamine can greatly reduce the sensation of pain and cause unconsciousness.")] + KETA = 256, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Mescaline or mescaline is a psychedelic and hallucinogenic alkaloid.")] - [DrugPrice(470)] - Mescaline = 512, + [DrugType("Mescaline", 470, "In terms of effect, mescaline is a typical hallucinogen, also called peyotl.")] + PEYO = 512, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Ecstasy, also XTC, is a term for so-called 'party pills'.")] - [DrugPrice(275)] - Ecstasy = 1024, + [DrugType("Ecstasy", 231, "On the illegal market, Ecstasy, also XTC, is a term for so-called 'party pills'.")] + XTC = 1024, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Acid, also known as LSD, is one of the strongest known hallucinogens.")] - [DrugPrice(265)] - Acid = 2048, + [DrugType("Acid", 285, "LSD, or 'acid', is sold in the form of small 'cardboards' printed with various designs.")] + LSD = 2048, /// /// The drug type enum. /// - [Description("MDMA is particularly known as a party drug that is widely used worldwide.")] - [DrugPrice(315)] + [DrugType("Molly", 315, "MDMA, also known as 'Molly', is a white or off-white powder or crystal, purer than XTC.")] MDMA = 4096, /// - /// The drug type enum. + /// The drug type enum. /// - [Description("Crack is a drug made from cocaine salt and sodium bicarbonate.")] - [DrugPrice(615)] - Crack = 8192, + [DrugType("Crack", 2078, "Crack and also known as 'rock', is a free base form of cocaine that can be smoked.")] + CRACK = 8192, /// - /// The drug type enum. + /// The drug type enum. /// - [DrugPrice(185)] - [Description("A semi-synthetic opioid, highly addictive and a common drug of abuse.")] - Oxycodone = 16384 + [DrugType("Oxycodone", 185, "A semi-synthetic opioid, highly addictive and a common drug of abuse.")] + OXY = 16384 } /// @@ -294,68 +278,112 @@ public enum WarehouseMissionStates } /// - /// The class. + /// The class. /// /// /// Should only be used with the enum. + /// Inherits from the class. /// - [AttributeUsage(AttributeTargets.Field)] - private class DrugPriceAttribute : Attribute + [AttributeUsage(AttributeTargets.Field, AllowMultiple = false)] + private sealed class DrugTypeAttribute : DescriptionAttribute { - public int Price { get; private set; } + /// + /// Initializes a new instance of the class. + /// + /// + /// Will throw an exception if is . + /// + /// The normal display name of the drug, cannot be . + /// The normal market price of the drug. + /// The description of the drug, can be . + public DrugTypeAttribute(string name, int price, string? description) : base(description) + { + Name = name ?? throw new ArgumentNullException(nameof(name)); + Price = price; + } + + /// + /// The property is the display name of the drug. + /// + public string Name { get; private set; } - public DrugPriceAttribute(int price) => Price = price; + /// + /// The property is the normal market price of the drug. + /// + public int Price { get; private set; } } /// - /// The extension method will try to get the of an enum if used + /// The extension method will try to get the of an enumerator of type . /// /// - /// If the enum has no description attribute, the enum name will be returned. + /// If the enumerator has no or the + /// is , the name of the enumerator will be returned. /// - /// - /// - /// The Description of type or the name of the enum. - public static string GetDescription(this T enumValue) where T : struct, IConvertible + /// The enumerator itself. + /// The enumerator itself. + /// The drug description or the name of the enumerator. + public static string GetDescription(this T enumValue) where T : Enum { FieldInfo? fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); if (fieldInfo is not null) { - DescriptionAttribute[]? attributes = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; - if (attributes is not null && attributes.Length > 0) - return attributes[0].Description; + DescriptionAttribute? attribute = fieldInfo.GetCustomAttribute(typeof(DrugTypeAttribute), true) as DescriptionAttribute; + if (attribute is not null && attribute.Description is not null) + return attribute.Description; } return enumValue.ToString(); } /// - /// The method will try to get the of an enum if used. + /// The extension method will try to get the of an enumerator of type . /// /// - /// If the enum has no drug price attribute, the value of 0 will be returned. + /// If the enumerator has no , the value of 0 is returned. /// - /// - /// - /// The drug price of type or the the value of 0. - public static int GetPrice(this T enumValue) where T : struct, IConvertible + /// The enumerator itself. + /// The enumerator itself. + /// The drug price or the the value of 0. + public static int GetPrice(this T enumValue) where T : Enum { FieldInfo? fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); if (fieldInfo is not null) { - DrugPriceAttribute[]? attributes = fieldInfo.GetCustomAttributes(typeof(DrugPriceAttribute), false) as DrugPriceAttribute[]; - if (attributes is not null && attributes.Length > 0) - return attributes[0].Price; + DrugTypeAttribute? attribute = fieldInfo.GetCustomAttribute(typeof(DrugTypeAttribute), false) as DrugTypeAttribute; + if (attribute is not null) + return attribute.Price; } return default; } /// - /// The extension method returns a + /// The extension method will try to get the of an enumerator of type . /// - /// - /// - /// A of the provided enum flags. - public static List FlagsToList(this T enumFlags) where T : Enum, IConvertible + /// + /// If the enumerator has no , the name of the enumerator will be returned. + /// + /// The enumerator itself. + /// The enumerator itself. + /// The drug name or the name of the enumerator. + public static string GetName(this T enumValue) where T : Enum + { + FieldInfo? fieldInfo = enumValue.GetType().GetField(enumValue.ToString()); + if (fieldInfo is not null) + { + DrugTypeAttribute? attribute = fieldInfo.GetCustomAttribute(typeof(DrugTypeAttribute), false) as DrugTypeAttribute; + if (attribute is not null) + return attribute.Name; + } + return enumValue.ToString(); + } + + /// + /// The extension method should return a list of enumerators which are included in the enumerator flags. + /// + /// The enumerator itself. + /// The enumerator itself. + /// A list of enums out of the provided enum flags. + public static List FlagsToList(this T enumFlags) where T : Enum { List list = new(); foreach (T flagToCheck in Enum.GetValues(typeof(T))) @@ -365,28 +393,11 @@ public static List FlagsToList(this T enumFlags) where T : Enum, IConverti } /// - /// The method should return a list of all enumerators of the given type of enum. + /// The extension method should return a list of all enumerators of the given type of enumerator. /// - /// - /// A of the provided enum. - public static List GetListFromEnum(this T @enum) where T : Enum - => Enum.GetValues(@enum.GetType()).Cast().ToList(); - - /// - /// The method should return a dictornary with enums and their description. - /// - /// - /// If the enum has no description property, the enum name will be returned. - /// - /// - /// - /// A dictionary with enums and their description. - public static Dictionary GetEnumsWithDescription(this T @enum) where T : struct, IConvertible - { - List enumList = Enum.GetValues(@enum.GetType()).Cast().ToList(); - Dictionary dictToReturn = new(); - foreach (T e in enumList) - dictToReturn.Add(e, e.GetDescription()); - return dictToReturn; - } -} \ No newline at end of file + /// The enumerator itself. + /// The enumerator itself. + /// A list of all enumerators out of the provided enumerator. + public static List GetListFromEnum(this T enumValue) where T : Enum + => Enum.GetValues(enumValue.GetType()).Cast().ToList(); +} diff --git a/Extension/Utils.cs b/Extension/Utils.cs index f14b1022..70c3075e 100644 --- a/Extension/Utils.cs +++ b/Extension/Utils.cs @@ -96,7 +96,7 @@ public static List GetDrugFlagTypes(DrugType drugTypes) { Logger.Trace(" called"); List enumList = drugTypes.FlagsToList(); - enumList.Remove(DrugType.None); + enumList.Remove(DrugType.NONE); return enumList; } @@ -104,17 +104,17 @@ public static List GetDrugFlagTypes(DrugType drugTypes) /// The method returns the available drugs for the trading in general. /// /// - /// The will be ignored. + /// The will be ignored. /// /// A list of drugs, description and price included. public static List GetAvailableDrugs() { Logger.Trace(" called"); - List enumList = DrugType.None.GetListFromEnum(); - enumList.Remove(DrugType.None); + List enumList = DrugType.NONE.GetListFromEnum(); + enumList.Remove(DrugType.NONE); List drugList = new(); foreach (DrugType e in enumList) - drugList.Add(new Drug(e, e.GetDescription(), e.GetPrice())); + drugList.Add(new Drug(e.GetName(), e.GetDescription(), e.GetPrice())); return drugList; } diff --git a/GUI/Elements/DrugListItem.cs b/GUI/Elements/DrugListItem.cs index f087b678..308a6720 100644 --- a/GUI/Elements/DrugListItem.cs +++ b/GUI/Elements/DrugListItem.cs @@ -71,35 +71,35 @@ private static BadgeSet GetDrugBadgeSet(string drugName) BadgeSet heroBadgeSet = new("mpinventory", "mp_specitem_heroin", "mp_specitem_heroin_black"); BadgeSet defaultBadgeSet = new("mpinventory", "mp_specitem_cash", "mp_specitem_cash_black"); - if (DrugType.Cocaine.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.COKE.ToString().Equals(drugName, StringComparison.Ordinal)) return cokeBadgeSet; - if (DrugType.Heroin.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.SMACK.ToString().Equals(drugName, StringComparison.Ordinal)) return heroBadgeSet; - if (DrugType.Marijuana.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.CANA.ToString().Equals(drugName, StringComparison.Ordinal)) return weedBadgeSet; - if (DrugType.Hashish.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.HASH.ToString().Equals(drugName, StringComparison.Ordinal)) return weedBadgeSet; - if (DrugType.Mushrooms.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.SHROOMS.ToString().Equals(drugName, StringComparison.Ordinal)) return weedBadgeSet; - if (DrugType.Amphetamine.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.SPEED.ToString().Equals(drugName, StringComparison.Ordinal)) return cokeBadgeSet; if (DrugType.PCP.ToString().Equals(drugName, StringComparison.Ordinal)) return cokeBadgeSet; - if (DrugType.Methamphetamine.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.METH.ToString().Equals(drugName, StringComparison.Ordinal)) return methBadgeSet; - if (DrugType.Ketamine.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.KETA.ToString().Equals(drugName, StringComparison.Ordinal)) return cokeBadgeSet; - if (DrugType.Mescaline.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.PEYO.ToString().Equals(drugName, StringComparison.Ordinal)) return methBadgeSet; - if (DrugType.Ecstasy.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.XTC.ToString().Equals(drugName, StringComparison.Ordinal)) return methBadgeSet; - if (DrugType.Acid.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.LSD.ToString().Equals(drugName, StringComparison.Ordinal)) return methBadgeSet; if (DrugType.MDMA.ToString().Equals(drugName, StringComparison.Ordinal)) return methBadgeSet; - if (DrugType.Crack.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.CRACK.ToString().Equals(drugName, StringComparison.Ordinal)) return heroBadgeSet; - if (DrugType.Oxycodone.ToString().Equals(drugName, StringComparison.Ordinal)) + if (DrugType.OXY.ToString().Equals(drugName, StringComparison.Ordinal)) return methBadgeSet; else return defaultBadgeSet; }