Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace StardustEngine.Framework.Game.Logic
- {
- public enum CoinType
- {
- /// <summary>
- /// Copper is the lowest denominator of currency.
- /// It requires 100 Copper to make 1 Silver.
- /// </summary>
- Copper = 1,
- /// <summary>
- /// Silver is the second most common form of currency.
- /// It requires 100 Silver to Make 1 Gold.
- /// </summary>
- Silver = 2,
- /// <summary>
- /// Gold is the most common form of currency. It takes
- /// part in most expensive transactions.
- /// It requires 100 Gold to make 1 Platinum.
- /// </summary>
- Gold = 3,
- /// <summary>
- /// Platinum is a coin which most people never see. A single
- /// Platinum coin can purchase almost anything.
- /// 1 Platinum Coin = 100 Gold.
- /// 1 Platinum Coin = 10,000 Silver.
- /// 1 Platinum Coin = 1,000,000 Copper.
- /// </summary>
- Platinum = 4
- }
- public struct MoneyCache : IEquatable<MoneyCache>, IComparable<MoneyCache>
- {
- public const string CopperName = "Copper";
- public const string SilverName = "Silver";
- public const string GoldName = "Gold";
- public const string PlatinumName = "Platinum";
- public const int MaximumBaseDenomination = 999999999;
- public const int PlatinumMultiplier = 1000000;
- public const int GoldMultiplier = 10000;
- public const int SilverMultiplier = 100;
- public const char CopperAbbreviation = 'c';
- public const char SilverAbbreviation = 's';
- public const char GoldAbbreviation = 'g';
- public const char PlatinumAbbreviation = 'p';
- /// <summary>
- /// The internal value used to generate
- /// other denominations.
- /// </summary>
- public int BaseDenomination
- {
- get;
- }
- /// <summary>
- /// The total amount of Copper.
- /// </summary>
- public int Copper
- {
- get
- {
- return ComputeCopper(BaseDenomination);
- }
- }
- /// <summary>
- /// The total amount of Silver.
- /// </summary>
- public int Silver
- {
- get
- {
- return ComputeSilver(BaseDenomination);
- }
- }
- /// <summary>
- /// The total amount of Gold.
- /// </summary>
- public int Gold
- {
- get
- {
- return ComputeGold(BaseDenomination);
- }
- }
- /// <summary>
- /// The total amount of Platinum.
- /// </summary>
- public int Platinum
- {
- get
- {
- return ComputePlatinum(BaseDenomination);
- }
- }
- public bool IsFull
- {
- get
- {
- return BaseDenomination == MaximumBaseDenomination;
- }
- }
- public bool IsEmpty
- {
- get
- {
- return BaseDenomination == 0;
- }
- }
- public bool HasPlatinum
- {
- get
- {
- return BaseDenomination >= PlatinumMultiplier;
- }
- }
- public bool HasGold
- {
- get
- {
- return BaseDenomination >= GoldMultiplier;
- }
- }
- public bool HasSilver
- {
- get
- {
- return BaseDenomination >= SilverMultiplier;
- }
- }
- public bool HasCopper
- {
- get
- {
- return BaseDenomination > 0;
- }
- }
- public MoneyCache(int platinum, int gold, int silver, int copper)
- {
- BaseDenomination = ComputeBaseDenomination(platinum, gold, silver, copper);
- }
- public MoneyCache(int baseDenomination) : this()
- {
- BaseDenomination = BaseClamp(baseDenomination);
- }
- public static MoneyCache Add(MoneyCache a, int platinum, int gold, int silver, int copper)
- {
- return a + new MoneyCache(platinum, gold, silver, copper);
- }
- public static MoneyCache Add(int platinumA, int goldA, int silverA, int copperA, int platinumB, int goldB, int silverB, int copperB)
- {
- return new MoneyCache(platinumA, goldA, silverA, copperA) + new MoneyCache(platinumB, goldB, silverB, copperB);
- }
- public static MoneyCache Add(MoneyCache a, int baseDenomination)
- {
- return a + baseDenomination;
- }
- public static MoneyCache Add(int baseDenominationA, int baseDenominationB)
- {
- return baseDenominationA + baseDenominationB;
- }
- public static MoneyCache Add(MoneyCache a, MoneyCache other)
- {
- return a + other;
- }
- public static MoneyCache Subtract(MoneyCache a, int platinum, int gold, int silver, int copper)
- {
- return a - new MoneyCache(platinum, gold, silver, copper);
- }
- public static MoneyCache Subtract(int platinumA, int goldA, int silverA, int copperA, int platinumB, int goldB, int silverB, int copperB)
- {
- return new MoneyCache(platinumA, goldA, silverA, copperA) - new MoneyCache(platinumB, goldB, silverB, copperB);
- }
- public static MoneyCache Subtract(MoneyCache a, int baseDenomination)
- {
- return a - baseDenomination;
- }
- public static MoneyCache Subtract(int baseDenominationA, int baseDenominationB)
- {
- return baseDenominationA - baseDenominationB;
- }
- public static MoneyCache Subtract(MoneyCache a, MoneyCache other)
- {
- return a - other;
- }
- public static implicit operator int(MoneyCache cache)
- {
- return cache.BaseDenomination;
- }
- public static implicit operator MoneyCache(int baseDenomination)
- {
- return new MoneyCache(baseDenomination);
- }
- public static MoneyCache operator +(MoneyCache b1, MoneyCache b2)
- {
- return b1.BaseDenomination + b2.BaseDenomination;
- }
- public static MoneyCache operator -(MoneyCache b1, MoneyCache b2)
- {
- return b1.BaseDenomination - b2.BaseDenomination;
- }
- public bool Equals(MoneyCache other)
- {
- return BaseDenomination == other.BaseDenomination;
- }
- public override bool Equals(object obj)
- {
- if (null == obj) return false;
- return obj is MoneyCache && Equals((MoneyCache)obj);
- }
- /// <summary>
- /// Parses a string to create a MoneyCache object. The given
- /// string must yield the formatting of either: "0p,0g,0s,0c"
- /// or "0,0,0,0", where 0 = the desired value. Any other
- /// format will be considered corrupted data and throw an
- /// error.
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- public static MoneyCache Parse(string data)
- {
- const string trashSamples = "pgsc";
- if (string.IsNullOrEmpty(data) || string.IsNullOrEmpty(trashSamples))
- {
- throw new Exception("Invalid Parse Data; String was Empty!");
- }
- char[] tar = data.ToCharArray();
- for (int i = 0; i < tar.Length; i++)
- {
- for (int j = 0; j < trashSamples.Length; j++)
- {
- if (tar[i] == trashSamples[j])
- {
- tar[i] = '\0'; // \0 = null
- break;
- }
- }
- }
- string[] fragments = new string(tar).Split(',');
- if (fragments.Length != 4)
- {
- throw new Exception("Invalid Parse Data; Array data was invalid size!");
- }
- return new MoneyCache(Int32.Parse(fragments[0]), Int32.Parse(fragments[1]), Int32.Parse(fragments[2]), Int32.Parse(fragments[3]));
- }
- public static MoneyCache Parse(int[] fragments)
- {
- if (fragments.Length != 4)
- {
- throw new Exception("Invalid Parse Data; Array data was invalid size!");
- }
- return new MoneyCache(fragments[0],fragments[1], fragments[2], fragments[3]);
- }
- public override int GetHashCode()
- {
- return BaseDenomination;
- }
- public static bool operator !=(MoneyCache a, MoneyCache b)
- {
- return !(a == b);
- }
- public static bool operator ==(MoneyCache a, MoneyCache b)
- {
- return !(a != b);
- }
- public static bool operator <(MoneyCache a, MoneyCache b)
- {
- return a.CompareTo(b) < 0;
- }
- public static bool operator >(MoneyCache a, MoneyCache b)
- {
- return a.CompareTo(b) > 0;
- }
- public static bool operator <=(MoneyCache a, MoneyCache b)
- {
- return a < b || Equals(a, b);
- }
- public static bool operator >=(MoneyCache a, MoneyCache b)
- {
- return a > b || Equals(a, b);
- }
- public int CompareTo(MoneyCache other)
- {
- if (BaseDenomination > other.BaseDenomination)
- {
- return 1;
- }
- if (BaseDenomination < other.BaseDenomination)
- {
- return -1;
- }
- return 0;
- }
- public static MoneyCache ComputeWealth(int baseDenomination)
- {
- return new MoneyCache(baseDenomination);
- }
- public static int ComputeBaseDenomination(int platinum, int gold, int silver, int copper)
- {
- int computedDenomination = 0;
- computedDenomination += platinum * PlatinumMultiplier;
- computedDenomination += gold * GoldMultiplier;
- computedDenomination += silver * SilverMultiplier;
- computedDenomination += copper;
- return BaseClamp(computedDenomination);
- }
- /// <summary>
- /// Computes the total amount of Copper.
- /// </summary>
- /// <param name="baseDenomination"></param>
- /// <returns></returns>
- public static int ComputeCopper(int baseDenomination)
- {
- return BaseClamp(baseDenomination);
- }
- /// <summary>
- /// Computes the total amount of Silver.
- /// </summary>
- /// <param name="baseDenomination"></param>
- /// <returns></returns>
- public static int ComputeSilver(int baseDenomination)
- {
- return (BaseClamp(baseDenomination) / SilverMultiplier);
- }
- /// <summary>
- /// Computes the total amount of Gold.
- /// </summary>
- /// <param name="baseDenomination"></param>
- /// <returns></returns>
- public static int ComputeGold(int baseDenomination)
- {
- return (BaseClamp(baseDenomination) / GoldMultiplier);
- }
- /// <summary>
- /// Computes the total amount of Platinum.
- /// </summary>
- /// <param name="baseDenomination"></param>
- /// <returns></returns>
- public static int ComputePlatinum(int baseDenomination)
- {
- return BaseClamp(baseDenomination) / PlatinumMultiplier;
- }
- public static int BaseClamp(int baseDenomination)
- {
- return Math.Max(0, Math.Min(MaximumBaseDenomination, baseDenomination));
- }
- public override string ToString()
- {
- return $"{Platinum}p," +
- $"{Gold % 100}g," +
- $"{Silver % 100}s," +
- $"{Copper % 100}c";
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement