Skip to content

Instantly share code, notes, and snippets.

@TheLeftExit
Created December 18, 2023 12:19
Show Gist options
  • Save TheLeftExit/738b6904f2956c9216d097a280502480 to your computer and use it in GitHub Desktop.
Save TheLeftExit/738b6904f2956c9216d097a280502480 to your computer and use it in GitHub Desktop.
Upload Simulator 2 prestige point calculator
public class UploadSimulatorvalueModel(decimal? rawExponent = null)
{
public string Layer1 { get => this[1]; set => this[1] = value; }
public string Layer2 { get => this[2]; set => this[2] = value; }
public string Layer3 { get => this[3]; set => this[3] = value; }
public string Layer4 { get => this[4]; set => this[4] = value; }
public string this[int prestigeLayer]
{
get
{
var exponent = RawToPrestigeLayer(rawExponent, prestigeLayer);
return Format(exponent);
}
set
{
var exponent = Parse(value);
rawExponent = RawFromPrestigeLayer(exponent, prestigeLayer);
}
}
private const string alphabet = "abcdefghijklmnopqrstuvwxyz";
private static int SuffixToExponent(ReadOnlySpan<char> suffix)
{
return suffix switch
{
"" => 0,
"k" => 3,
"m" => 6,
"b" => 9,
"t" => 12,
_ => 15 +
alphabet.IndexOf(suffix[0]) * 3 * alphabet.Length
+ alphabet.IndexOf(suffix[1]) * 3
};
}
private static string ExponentToSuffix(int exponent)
{
if (exponent == 0) return string.Empty;
if (exponent % 3 != 0) throw new ArgumentException("Exponent must be a multiple of 3.");
switch (exponent)
{
case 3: return "k";
case 6: return "m";
case 9: return "b";
case 12: return "t";
}
exponent -= 15;
int firstLetterIndex = exponent / (3 * alphabet.Length);
int secondLetterIndex = (exponent / 3) % alphabet.Length;
return string.Format("{0}{1}", alphabet[firstLetterIndex], alphabet[secondLetterIndex]);
}
private static string Format(decimal? exponent)
{
if (exponent is null) return "0";
var remainder = exponent % 3;
var suffixableExponent = (int)(exponent - remainder);
var suffix = ExponentToSuffix(suffixableExponent);
var mantissa = Math.Pow(10, (float)remainder);
return $"{mantissa:F3}{suffix}";
}
private static decimal? Parse(ReadOnlySpan<char> number)
{
if(number is "0") return null;
var suffixIndex = number.IndexOfAnyInRange('a', 'z');
var suffix = number[suffixIndex..];
var mantissa = number[..suffixIndex];
var exponent = SuffixToExponent(suffix);
return (decimal)Math.Log10(float.Parse(mantissa)) + exponent;
}
private static decimal? RawToPrestigeLayer(decimal? exponent, decimal prestigeLayer)
{
if (exponent is null) return null;
ArgumentOutOfRangeException.ThrowIfGreaterThan(prestigeLayer, 4);
ArgumentOutOfRangeException.ThrowIfLessThan(prestigeLayer, 1);
for (int i = 1; i < prestigeLayer; i++)
{
exponent -= 50;
exponent /= 2;
if (exponent < 0) return null;
}
return exponent;
}
private static decimal? RawFromPrestigeLayer(decimal? exponent, decimal prestigeLayer)
{
if (exponent is null) return null;
ArgumentOutOfRangeException.ThrowIfGreaterThan(prestigeLayer, 4);
ArgumentOutOfRangeException.ThrowIfLessThan(prestigeLayer, 1);
for (int i = 1; i < prestigeLayer; i++)
{
exponent *= 2;
exponent += 50;
}
return exponent;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment