Advertisement
Krythic

RPG Currency System

Mar 19th, 2016
1,388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.13 KB | None | 0 0
  1. using System;
  2.  
  3. namespace StardustEngine.Framework.Game.Logic
  4. {
  5.  
  6.     public enum CoinType
  7.     {
  8.         /// <summary>
  9.         /// Copper is the lowest denominator of currency.
  10.         /// It requires 100 Copper to make 1 Silver.
  11.         /// </summary>
  12.         Copper = 1,
  13.         /// <summary>
  14.         /// Silver is the second most common form of currency.
  15.         /// It requires 100 Silver to Make 1 Gold.
  16.         /// </summary>
  17.         Silver = 2,
  18.         /// <summary>
  19.         /// Gold is the most common form of currency. It takes
  20.         /// part in most expensive transactions.
  21.         /// It requires 100 Gold to make 1 Platinum.
  22.         /// </summary>
  23.         Gold = 3,
  24.         /// <summary>
  25.         /// Platinum is a coin which most people never see. A single
  26.         /// Platinum coin can purchase almost anything.
  27.         /// 1 Platinum Coin = 100 Gold.
  28.         /// 1 Platinum Coin = 10,000 Silver.
  29.         /// 1 Platinum Coin = 1,000,000 Copper.
  30.         /// </summary>
  31.         Platinum = 4
  32.     }
  33.  
  34.     public struct MoneyCache : IEquatable<MoneyCache>, IComparable<MoneyCache>
  35.     {
  36.         public const string CopperName = "Copper";
  37.         public const string SilverName = "Silver";
  38.         public const string GoldName = "Gold";
  39.         public const string PlatinumName = "Platinum";
  40.         public const int MaximumBaseDenomination = 999999999;
  41.         public const int PlatinumMultiplier = 1000000;
  42.         public const int GoldMultiplier = 10000;
  43.         public const int SilverMultiplier = 100;
  44.         public const char CopperAbbreviation = 'c';
  45.         public const char SilverAbbreviation = 's';
  46.         public const char GoldAbbreviation = 'g';
  47.         public const char PlatinumAbbreviation = 'p';
  48.  
  49.         /// <summary>
  50.         /// The internal value used to generate
  51.         /// other denominations.
  52.         /// </summary>
  53.         public int BaseDenomination
  54.         {
  55.             get;
  56.         }
  57.  
  58.         /// <summary>
  59.         /// The total amount of Copper.
  60.         /// </summary>
  61.         public int Copper
  62.         {
  63.             get
  64.             {
  65.                 return ComputeCopper(BaseDenomination);
  66.             }
  67.         }
  68.  
  69.         /// <summary>
  70.         /// The total amount of Silver.
  71.         /// </summary>
  72.         public int Silver
  73.         {
  74.             get
  75.             {
  76.                 return ComputeSilver(BaseDenomination);
  77.             }
  78.         }
  79.  
  80.         /// <summary>
  81.         /// The total amount of Gold.
  82.         /// </summary>
  83.         public int Gold
  84.         {
  85.             get
  86.             {
  87.                 return ComputeGold(BaseDenomination);
  88.             }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// The total amount of Platinum.
  93.         /// </summary>
  94.         public int Platinum
  95.         {
  96.             get
  97.             {
  98.                 return ComputePlatinum(BaseDenomination);
  99.             }
  100.         }
  101.  
  102.         public bool IsFull
  103.         {
  104.             get
  105.             {
  106.                 return BaseDenomination == MaximumBaseDenomination;
  107.             }
  108.         }
  109.  
  110.         public bool IsEmpty
  111.         {
  112.             get
  113.             {
  114.                 return BaseDenomination == 0;
  115.             }
  116.         }
  117.  
  118.         public bool HasPlatinum
  119.         {
  120.             get
  121.             {
  122.                 return BaseDenomination >= PlatinumMultiplier;
  123.             }
  124.         }
  125.  
  126.         public bool HasGold
  127.         {
  128.             get
  129.             {
  130.                 return BaseDenomination >= GoldMultiplier;
  131.             }
  132.         }
  133.  
  134.         public bool HasSilver
  135.         {
  136.             get
  137.             {
  138.                 return BaseDenomination >= SilverMultiplier;
  139.             }
  140.         }
  141.  
  142.         public bool HasCopper
  143.         {
  144.             get
  145.             {
  146.                 return BaseDenomination > 0;
  147.             }
  148.         }
  149.  
  150.         public MoneyCache(int platinum, int gold, int silver, int copper)
  151.         {
  152.             BaseDenomination = ComputeBaseDenomination(platinum, gold, silver, copper);
  153.         }
  154.  
  155.         public MoneyCache(int baseDenomination) : this()
  156.         {
  157.             BaseDenomination = BaseClamp(baseDenomination);
  158.         }
  159.  
  160.         public static MoneyCache Add(MoneyCache a, int platinum, int gold, int silver, int copper)
  161.         {
  162.             return a + new MoneyCache(platinum, gold, silver, copper);
  163.         }
  164.  
  165.         public static MoneyCache Add(int platinumA, int goldA, int silverA, int copperA, int platinumB, int goldB, int silverB, int copperB)
  166.         {
  167.             return new MoneyCache(platinumA, goldA, silverA, copperA) + new MoneyCache(platinumB, goldB, silverB, copperB);
  168.         }
  169.  
  170.         public static MoneyCache Add(MoneyCache a, int baseDenomination)
  171.         {
  172.             return a + baseDenomination;
  173.         }
  174.  
  175.         public static MoneyCache Add(int baseDenominationA, int baseDenominationB)
  176.         {
  177.             return baseDenominationA + baseDenominationB;
  178.         }
  179.  
  180.         public static MoneyCache Add(MoneyCache a, MoneyCache other)
  181.         {
  182.             return a + other;
  183.         }
  184.  
  185.         public static MoneyCache Subtract(MoneyCache a, int platinum, int gold, int silver, int copper)
  186.         {
  187.             return a - new MoneyCache(platinum, gold, silver, copper);
  188.         }
  189.  
  190.         public static MoneyCache Subtract(int platinumA, int goldA, int silverA, int copperA, int platinumB, int goldB, int silverB, int copperB)
  191.         {
  192.             return new MoneyCache(platinumA, goldA, silverA, copperA) - new MoneyCache(platinumB, goldB, silverB, copperB);
  193.         }
  194.  
  195.         public static MoneyCache Subtract(MoneyCache a, int baseDenomination)
  196.         {
  197.             return a - baseDenomination;
  198.         }
  199.  
  200.         public static MoneyCache Subtract(int baseDenominationA, int baseDenominationB)
  201.         {
  202.             return baseDenominationA - baseDenominationB;
  203.         }
  204.  
  205.         public static MoneyCache Subtract(MoneyCache a, MoneyCache other)
  206.         {
  207.             return a - other;
  208.         }
  209.  
  210.         public static implicit operator int(MoneyCache cache)
  211.         {
  212.             return cache.BaseDenomination;
  213.         }
  214.  
  215.         public static implicit operator MoneyCache(int baseDenomination)
  216.         {
  217.             return new MoneyCache(baseDenomination);
  218.         }
  219.  
  220.         public static MoneyCache operator +(MoneyCache b1, MoneyCache b2)
  221.         {
  222.             return b1.BaseDenomination + b2.BaseDenomination;
  223.         }
  224.  
  225.         public static MoneyCache operator -(MoneyCache b1, MoneyCache b2)
  226.         {
  227.             return b1.BaseDenomination - b2.BaseDenomination;
  228.         }
  229.  
  230.         public bool Equals(MoneyCache other)
  231.         {
  232.             return BaseDenomination == other.BaseDenomination;
  233.         }
  234.  
  235.         public override bool Equals(object obj)
  236.         {
  237.             if (null == obj) return false;
  238.             return obj is MoneyCache && Equals((MoneyCache)obj);
  239.         }
  240.  
  241.         /// <summary>
  242.         /// Parses a string to create a MoneyCache object. The given
  243.         /// string must yield the formatting of either: "0p,0g,0s,0c"
  244.         /// or "0,0,0,0", where 0 = the desired value. Any other
  245.         /// format will be considered corrupted data and throw an
  246.         /// error.
  247.         /// </summary>
  248.         /// <param name="data"></param>
  249.         /// <returns></returns>
  250.         public static MoneyCache Parse(string data)
  251.         {
  252.             const string trashSamples = "pgsc";
  253.             if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(trashSamples))
  254.             {
  255.                 throw new Exception("Invalid Parse Data; String was Empty!");
  256.             }
  257.             char[] tar = data.ToCharArray();
  258.             for (int i = 0; i < tar.Length; i++)
  259.             {
  260.                 for (int j = 0; j < trashSamples.Length; j++)
  261.                 {
  262.                     if (tar[i] == trashSamples[j])
  263.                     {
  264.                         tar[i] = '\0'; // \0 = null
  265.                         break;
  266.                     }
  267.                 }
  268.             }
  269.             string[] fragments = new string(tar).Split(',');
  270.             if (fragments.Length != 4)
  271.             {
  272.                 throw new Exception("Invalid Parse Data; Array data was invalid size!");
  273.             }
  274.             return new MoneyCache(Int32.Parse(fragments[0]), Int32.Parse(fragments[1]), Int32.Parse(fragments[2]), Int32.Parse(fragments[3]));
  275.         }
  276.  
  277.         public static MoneyCache Parse(int[] fragments)
  278.         {
  279.             if (fragments.Length != 4)
  280.             {
  281.                 throw new Exception("Invalid Parse Data; Array data was invalid size!");
  282.             }
  283.             return new MoneyCache(fragments[0],fragments[1], fragments[2], fragments[3]);
  284.         }
  285.  
  286.         public override int GetHashCode()
  287.         {
  288.             return BaseDenomination;
  289.         }
  290.  
  291.         public static bool operator !=(MoneyCache a, MoneyCache b)
  292.         {
  293.             return !(a == b);
  294.         }
  295.  
  296.         public static bool operator ==(MoneyCache a, MoneyCache b)
  297.         {
  298.             return !(a != b);
  299.         }
  300.  
  301.         public static bool operator <(MoneyCache a, MoneyCache b)
  302.         {
  303.             return a.CompareTo(b) < 0;
  304.         }
  305.  
  306.         public static bool operator >(MoneyCache a, MoneyCache b)
  307.         {
  308.             return a.CompareTo(b) > 0;
  309.         }
  310.  
  311.         public static bool operator <=(MoneyCache a, MoneyCache b)
  312.         {
  313.             return a < b || Equals(a, b);
  314.         }
  315.  
  316.         public static bool operator >=(MoneyCache a, MoneyCache b)
  317.         {
  318.             return a > b || Equals(a, b);
  319.         }
  320.  
  321.         public int CompareTo(MoneyCache other)
  322.         {
  323.             if (BaseDenomination > other.BaseDenomination)
  324.             {
  325.                 return 1;
  326.             }
  327.             if (BaseDenomination < other.BaseDenomination)
  328.             {
  329.                 return -1;
  330.             }
  331.             return 0;
  332.         }
  333.  
  334.         public static MoneyCache ComputeWealth(int baseDenomination)
  335.         {
  336.             return new MoneyCache(baseDenomination);
  337.         }
  338.  
  339.         public static int ComputeBaseDenomination(int platinum, int gold, int silver, int copper)
  340.         {
  341.             int computedDenomination = 0;
  342.             computedDenomination += platinum * PlatinumMultiplier;
  343.             computedDenomination += gold * GoldMultiplier;
  344.             computedDenomination += silver * SilverMultiplier;
  345.             computedDenomination += copper;
  346.             return BaseClamp(computedDenomination);
  347.         }
  348.  
  349.         /// <summary>
  350.         /// Computes the total amount of Copper.
  351.         /// </summary>
  352.         /// <param name="baseDenomination"></param>
  353.         /// <returns></returns>
  354.         public static int ComputeCopper(int baseDenomination)
  355.         {
  356.             return BaseClamp(baseDenomination);
  357.         }
  358.  
  359.         /// <summary>
  360.         /// Computes the total amount of Silver.
  361.         /// </summary>
  362.         /// <param name="baseDenomination"></param>
  363.         /// <returns></returns>
  364.         public static int ComputeSilver(int baseDenomination)
  365.         {
  366.             return (BaseClamp(baseDenomination) / SilverMultiplier);
  367.         }
  368.  
  369.         /// <summary>
  370.         /// Computes the total amount of Gold.
  371.         /// </summary>
  372.         /// <param name="baseDenomination"></param>
  373.         /// <returns></returns>
  374.         public static int ComputeGold(int baseDenomination)
  375.         {
  376.             return (BaseClamp(baseDenomination) / GoldMultiplier);
  377.         }
  378.  
  379.         /// <summary>
  380.         /// Computes the total amount of Platinum.
  381.         /// </summary>
  382.         /// <param name="baseDenomination"></param>
  383.         /// <returns></returns>
  384.         public static int ComputePlatinum(int baseDenomination)
  385.         {
  386.             return BaseClamp(baseDenomination) / PlatinumMultiplier;
  387.         }
  388.  
  389.         public static int BaseClamp(int baseDenomination)
  390.         {
  391.             return Math.Max(0, Math.Min(MaximumBaseDenomination, baseDenomination));
  392.         }
  393.  
  394.         public override string ToString()
  395.         {
  396.             return $"{Platinum}p," +
  397.                    $"{Gold % 100}g," +
  398.                    $"{Silver % 100}s," +
  399.                    $"{Copper % 100}c";
  400.         }
  401.     }
  402. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement