Advertisement
Krythic

GamePrice (Decommissioned)

Dec 9th, 2021
1,094
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 13.43 KB | None | 0 0
  1. using System;
  2. using VoidwalkerEngine.Framework.Maths;
  3.  
  4. namespace VoidwalkerEngine.Framework.DataTypes
  5. {
  6.     public enum CoinType
  7.     {
  8.         /// <summary>
  9.         /// Copper
  10.         /// </summary>
  11.         Copper = 1,
  12.  
  13.         /// <summary>
  14.         /// Silver
  15.         /// </summary>
  16.         Silver = 100,
  17.  
  18.         /// <summary>
  19.         /// Gold
  20.         /// </summary>
  21.         Gold = 10000
  22.     }
  23.  
  24.     public struct GamePrice : IEquatable<GamePrice>
  25.     {
  26.         public const string CopperName = "Copper";
  27.         public const string SilverName = "Silver";
  28.         public const string GoldName = "Gold";
  29.         public const char CopperAbbreviation = 'c';
  30.         public const char SilverAbbreviation = 's';
  31.         public const char GoldAbbreviation = 'g';
  32.         public const int MaximumBaseDenomination = 99999999;
  33.         public const int MaximumCopper = MaximumBaseDenomination;
  34.         public const int MaximumSilver = ((MaximumBaseDenomination / (int)CoinType.Silver) + 1);
  35.         public const int MaximumGold = ((MaximumBaseDenomination / (int)CoinType.Gold) + 1);
  36.  
  37.         /// <summary>
  38.         /// The internal value used to generate
  39.         /// other denominations.
  40.         /// </summary>
  41.         public int BaseDenomination { get; }
  42.  
  43.         /// <summary>
  44.         /// The displayable amount of Copper.
  45.         /// </summary>
  46.         public int Copper
  47.         {
  48.             get
  49.             {
  50.                 return TotalCopper % 100;
  51.             }
  52.         }
  53.  
  54.         /// <summary>
  55.         /// The displayable amount of Silver.
  56.         /// </summary>
  57.         public int Silver
  58.         {
  59.             get
  60.             {
  61.                 return TotalSilver % 100;
  62.             }
  63.         }
  64.  
  65.         /// <summary>
  66.         /// The displayable amount of Gold.
  67.         /// </summary>
  68.         public int Gold
  69.         {
  70.             get
  71.             {
  72.                 return TotalGold;
  73.             }
  74.         }
  75.  
  76.         /// <summary>
  77.         /// The total amount of Copper.
  78.         /// </summary>
  79.         public int TotalCopper
  80.         {
  81.             get
  82.             {
  83.                 return BaseDenomination / (int)CoinType.Copper;
  84.             }
  85.         }
  86.  
  87.         /// <summary>
  88.         /// The total amount of Silver.
  89.         /// </summary>
  90.         public int TotalSilver
  91.         {
  92.             get
  93.             {
  94.                 return BaseDenomination / (int)CoinType.Silver;
  95.             }
  96.         }
  97.  
  98.         /// <summary>
  99.         /// The total amount of Gold.
  100.         /// </summary>
  101.         public int TotalGold
  102.         {
  103.             get
  104.             {
  105.                 return BaseDenomination / (int)CoinType.Gold;
  106.             }
  107.         }
  108.  
  109.         public static GamePrice Empty
  110.         {
  111.             get
  112.             {
  113.                 return new GamePrice();
  114.             }
  115.         }
  116.  
  117.         public static GamePrice Full
  118.         {
  119.             get
  120.             {
  121.                 return new GamePrice(MaximumBaseDenomination);
  122.             }
  123.         }
  124.  
  125.         public bool IsFull
  126.         {
  127.             get
  128.             {
  129.                 return BaseDenomination == MaximumBaseDenomination;
  130.             }
  131.         }
  132.  
  133.         public bool IsEmpty
  134.         {
  135.             get
  136.             {
  137.                 return BaseDenomination == 0;
  138.             }
  139.         }
  140.  
  141.         public GamePrice(int baseDenomination)
  142.         {
  143.             this.BaseDenomination = baseDenomination;
  144.         }
  145.  
  146.         public GamePrice(int gold, int silver, int copper)
  147.             : this()
  148.         {
  149.             this.BaseDenomination = ComputeBaseDenomination(gold, silver, copper);
  150.         }
  151.  
  152.         public GamePrice(string data)
  153.             : this()
  154.         {
  155.             this.BaseDenomination = Parse(data).BaseDenomination;
  156.         }
  157.  
  158.         public static int ComputeBaseDenomination(int gold = 0, int silver = 0, int copper = 0, bool clampMaximum = true)
  159.         {
  160.             // Negative Clamp
  161.             copper = VoidwalkerMathHelper.ClampMinimum(copper, 0);
  162.             silver = VoidwalkerMathHelper.ClampMinimum(silver, 0);
  163.             gold = VoidwalkerMathHelper.ClampMinimum(gold, 0);
  164.             // Early limitation detection.
  165.             if (copper > MaximumCopper || silver > MaximumSilver || gold > MaximumGold)
  166.             {
  167.                 return MaximumBaseDenomination;
  168.             }
  169.             int computedDenomination = 0;
  170.             computedDenomination += copper;
  171.             computedDenomination += ((silver * (int)CoinType.Silver));
  172.             computedDenomination += ((gold * (int)CoinType.Gold));
  173.             if (clampMaximum)
  174.             {
  175.                 return computedDenomination > MaximumBaseDenomination
  176.                 ? MaximumBaseDenomination
  177.                 : computedDenomination;
  178.             }
  179.             return computedDenomination;
  180.         }
  181.  
  182.         public GamePrice Add(GamePrice other)
  183.         {
  184.             return new GamePrice(this.BaseDenomination + other.BaseDenomination);
  185.         }
  186.  
  187.         public GamePrice Add(int baseDenomination)
  188.         {
  189.             return Add(new GamePrice(baseDenomination));
  190.         }
  191.  
  192.         public GamePrice Subtract(GamePrice other)
  193.         {
  194.             return new GamePrice(this.BaseDenomination - other.BaseDenomination);
  195.         }
  196.  
  197.         public GamePrice Subtract(int baseDenomination)
  198.         {
  199.             return Subtract(new GamePrice(baseDenomination));
  200.         }
  201.  
  202.         /// <summary>
  203.         /// Splits this Moneybag into two equal bags.
  204.         /// </summary>
  205.         /// <returns></returns>
  206.         public void Split(out GamePrice bag1, out GamePrice bag2)
  207.         {
  208.             if (this.BaseDenomination <= 1)
  209.             {
  210.                 // A split is impossible
  211.                 bag1 = this; // Will equal 1
  212.                 bag2 = Empty;
  213.                 return;
  214.             }
  215.             int splitResult = (this.BaseDenomination / 2);
  216.             if (VoidwalkerMathHelper.IsEven(BaseDenomination))
  217.             {
  218.                 bag1 = new GamePrice(splitResult);
  219.                 bag2 = new GamePrice(splitResult);
  220.                 return;
  221.             }
  222.             bag1 = new GamePrice(splitResult);
  223.             bag2 = new GamePrice(splitResult + 1);
  224.         }
  225.  
  226.         /// <summary>
  227.         ///
  228.         /// </summary>
  229.         /// <param name="type"></param>
  230.         /// <returns></returns>
  231.         public bool Has(CoinType type)
  232.         {
  233.             switch (type)
  234.             {
  235.                 case CoinType.Copper:
  236.                     return TotalCopper > 0;
  237.  
  238.                 case CoinType.Silver:
  239.                     return TotalSilver > 0;
  240.  
  241.                 case CoinType.Gold:
  242.                     return TotalGold > 0;
  243.  
  244.                 default:
  245.                     throw new ArgumentOutOfRangeException(nameof(type), type, null);
  246.             }
  247.         }
  248.  
  249.         public bool Has(GamePrice amount)
  250.         {
  251.             return this.BaseDenomination >= amount.BaseDenomination;
  252.         }
  253.  
  254.         public bool CanHold(GamePrice amount)
  255.         {
  256.             if (this.IsFull)
  257.             {
  258.                 return false;
  259.             }
  260.             return this.BaseDenomination + amount.BaseDenomination <= MaximumBaseDenomination;
  261.         }
  262.  
  263.         /// <summary>
  264.         /// Acceptable Format: '0g,0s,0c'
  265.         /// </summary>
  266.         /// <param name="data"></param>
  267.         /// <returns></returns>
  268.         public static GamePrice Parse(string data)
  269.         {
  270.             TryParse(data, out GamePrice? result);
  271.             if (result != null)
  272.             {
  273.                 return result.Value;
  274.             }
  275.             else
  276.             {
  277.                 throw new Exception("GamePrice was not in a valid format!");
  278.             }
  279.         }
  280.  
  281.         /// <summary>
  282.         ///
  283.         /// </summary>
  284.         /// <param name="data"></param>
  285.         /// <param name="result"></param>
  286.         public static void TryParse(string data, out GamePrice? result)
  287.         {
  288.             if (data != null || data.Length > 0)
  289.             {
  290.                 string[] splitData = data.Split(',');
  291.                 if (splitData.Length == 3)
  292.                 {
  293.                     if (splitData[0].Length >= 2 &&
  294.                         splitData[1].Length >= 2 &&
  295.                         splitData[2].Length >= 2)
  296.                     {
  297.                         if (splitData[0][splitData[0].Length - 1] == GoldAbbreviation &&
  298.                             splitData[1][splitData[1].Length - 1] == SilverAbbreviation &&
  299.                             splitData[2][splitData[2].Length - 1] == CopperAbbreviation)
  300.                         {
  301.                             if (Int32.TryParse(splitData[0].Substring(0, splitData[0].Length - 1), out int goldResult) &&
  302.                                 Int32.TryParse(splitData[1].Substring(0, splitData[1].Length - 1), out int silverResult) &&
  303.                                 Int32.TryParse(splitData[2].Substring(0, splitData[2].Length - 1), out int copperResult))
  304.                             {
  305.                                 if (copperResult <= MaximumCopper && silverResult <= MaximumSilver && goldResult <= MaximumGold)
  306.                                 {
  307.                                     int baseDenomination = ComputeBaseDenomination(goldResult, silverResult, copperResult, false);
  308.                                     if (baseDenomination <= MaximumBaseDenomination)
  309.                                     {
  310.                                         result = new GamePrice(goldResult, silverResult, copperResult);
  311.                                         return;
  312.                                     }
  313.                                 }
  314.                             }
  315.                         }
  316.                     }
  317.                 }
  318.             }
  319.             result = null;
  320.         }
  321.  
  322.         /// <summary>
  323.         /// Acceptable Format: '0g,0s,0c'
  324.         /// </summary>
  325.         /// <param name="data"></param>
  326.         /// <returns></returns>
  327.         public static bool TryParse(string data)
  328.         {
  329.             TryParse(data, out GamePrice? result);
  330.             return result != null;
  331.         }
  332.  
  333.         /// <summary>
  334.         ///
  335.         /// </summary>
  336.         /// <param name="culture"></param>
  337.         /// <returns></returns>
  338.         public override string ToString()
  339.         {
  340.             return $"{Gold}" + $"{GoldAbbreviation}," + $"{Silver}" + $"{SilverAbbreviation}," + $"{Copper}" + $"{CopperAbbreviation}";
  341.         }
  342.  
  343.         static public explicit operator int(GamePrice price)
  344.         {
  345.             return price.BaseDenomination;
  346.         }
  347.  
  348.         /// <summary>
  349.         ///
  350.         /// </summary>
  351.         /// <returns></returns>
  352.         public int ToInt32()
  353.         {
  354.             return this.BaseDenomination;
  355.         }
  356.  
  357.         /// <summary>
  358.         /// Returns format: Gold, Silver, Copper
  359.         /// </summary>
  360.         public int[] ToArray()
  361.         {
  362.             return new[]
  363.             {
  364.                 Gold,
  365.                 Silver,
  366.                 Copper
  367.             };
  368.         }
  369.  
  370.         public static GamePrice FromArray(int[] array)
  371.         {
  372.             if (array.Length != 3)
  373.             {
  374.                 throw new Exception("GamePrice Array Source must be 3 indices long!");
  375.             }
  376.             return new GamePrice(array[0], array[1], array[3]);
  377.         }
  378.  
  379.         public bool Equals(GamePrice other)
  380.         {
  381.             return BaseDenomination == other.BaseDenomination;
  382.         }
  383.  
  384.         public override bool Equals(object obj)
  385.         {
  386.             return (obj is GamePrice bag) && Equals(bag);
  387.         }
  388.  
  389.         public override int GetHashCode()
  390.         {
  391.             return BaseDenomination.GetHashCode();
  392.         }
  393.  
  394.         public static bool operator ==(GamePrice a, GamePrice b)
  395.         {
  396.             return a.BaseDenomination == b.BaseDenomination;
  397.         }
  398.  
  399.         public static bool operator !=(GamePrice a, GamePrice b)
  400.         {
  401.             return a.BaseDenomination != b.BaseDenomination;
  402.         }
  403.  
  404.         public static bool operator <(GamePrice a, GamePrice b)
  405.         {
  406.             return a.BaseDenomination < b.BaseDenomination;
  407.         }
  408.  
  409.         public static bool operator >(GamePrice a, GamePrice b)
  410.         {
  411.             return a.BaseDenomination > b.BaseDenomination;
  412.         }
  413.  
  414.         public static GamePrice operator +(GamePrice left, GamePrice right)
  415.         {
  416.             return left.Add(right);
  417.         }
  418.  
  419.         public static GamePrice operator -(GamePrice left, GamePrice right)
  420.         {
  421.             return left.Subtract(right);
  422.         }
  423.  
  424.         public static GamePrice operator +(GamePrice left, int right)
  425.         {
  426.             return left.Add(new GamePrice(right));
  427.         }
  428.  
  429.         public static GamePrice operator -(GamePrice left, int right)
  430.         {
  431.             return left.Subtract(new GamePrice(right));
  432.         }
  433.  
  434.         public static GamePrice operator *(GamePrice left, double right)
  435.         {
  436.             return new GamePrice((int)(left.BaseDenomination * right));
  437.         }
  438.  
  439.         public static implicit operator GamePrice(int baseDenomination)
  440.         {
  441.             return new GamePrice(baseDenomination);
  442.         }
  443.  
  444.         public static implicit operator GamePrice(string data)
  445.         {
  446.             return new GamePrice(data);
  447.         }
  448.  
  449.         public string ToUSD()
  450.         {
  451.             int dollars = TotalSilver;
  452.             int cents = Copper;
  453.             return "$" + dollars + "." + cents;
  454.         }
  455.     }
  456. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement