Implementation of ISelfSerialize

This commit is contained in:
PrPleGoo
2020-04-08 19:07:33 +02:00
parent 070ab51754
commit a484b6fd52
3 changed files with 41 additions and 9 deletions

View File

@@ -199,6 +199,7 @@ namespace Content.Server.GameObjects.Components.Chemistry
bool checkForNewReaction = false;
while (true)
{
//TODO: make a hashmap at startup and then look up reagents in the contents for a reaction
//Check the solution for every reaction
foreach (var reaction in _reactions)
{

View File

@@ -1,10 +1,12 @@
using System;
using Robust.Shared.Interfaces.Serialization;
using Robust.Shared.Serialization;
using System;
using System.Linq;
namespace Content.Shared.Chemistry
{
[Serializable]
public struct ReagentUnit
public struct ReagentUnit : ISelfSerialize
{
private int _value;
private static readonly int Shift = 2;
@@ -16,11 +18,6 @@ namespace Content.Shared.Chemistry
return _value / (decimal)Math.Pow(10, Shift);
}
private decimal ShiftUp()
{
return _value * (decimal)Math.Pow(10, Shift);
}
private ReagentUnit(int value)
{
_value = value;
@@ -38,7 +35,12 @@ namespace Content.Shared.Chemistry
public static ReagentUnit New(float value)
{
return new ReagentUnit((int) Math.Round(value * (float) Math.Pow(10, Shift)));
return new ReagentUnit(FromFloat(value));
}
private static int FromFloat(float value)
{
return (int) Math.Round(value * (float) Math.Pow(10, Shift));
}
public static ReagentUnit New(double value)
@@ -46,6 +48,16 @@ namespace Content.Shared.Chemistry
return new ReagentUnit((int) Math.Round(value * Math.Pow(10, Shift)));
}
public static ReagentUnit New(string value)
{
return New(FloatFromString(value));
}
private static float FloatFromString(string value)
{
return float.Parse(value);
}
public static ReagentUnit operator +(ReagentUnit a) => a;
public static ReagentUnit operator -(ReagentUnit a) => new ReagentUnit(-a._value);
@@ -135,7 +147,7 @@ namespace Content.Shared.Chemistry
public decimal Decimal()
{
return (decimal) ShiftDown();
return ShiftDown();
}
public int Int()
@@ -158,5 +170,15 @@ namespace Content.Shared.Chemistry
{
return HashCode.Combine(_value);
}
public void Deserialize(string value)
{
_value = FromFloat(FloatFromString(value));
}
public string Serialize()
{
return ToString();
}
}
}

View File

@@ -45,6 +45,15 @@ namespace Content.Tests.Shared.Chemistry
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase("1.005", "1")]
[TestCase("0.999", "1")]
public void ReagentUnitStringTests(string value, string expected)
{
var result = ReagentUnit.New(value);
Assert.AreEqual(expected, $"{result}");
}
[Test]
[TestCase(1.001f, 1.001f, "2")]
[TestCase(1.001f, 1.004f, "2")]