From a484b6fd52b9c40391b1ad17396722e0fc902144 Mon Sep 17 00:00:00 2001 From: PrPleGoo Date: Wed, 8 Apr 2020 19:07:33 +0200 Subject: [PATCH] Implementation of ISelfSerialize --- .../Components/Chemistry/SolutionComponent.cs | 1 + Content.Shared/Chemistry/ReagentUnit.cs | 40 ++++++++++++++----- .../Shared/Chemistry/ReagentUnit_Tests.cs | 9 +++++ 3 files changed, 41 insertions(+), 9 deletions(-) diff --git a/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs b/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs index 0328dcae67..4cd0a7d1ed 100644 --- a/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs +++ b/Content.Server/GameObjects/Components/Chemistry/SolutionComponent.cs @@ -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) { diff --git a/Content.Shared/Chemistry/ReagentUnit.cs b/Content.Shared/Chemistry/ReagentUnit.cs index a9350c5b3c..184f7aa5d7 100644 --- a/Content.Shared/Chemistry/ReagentUnit.cs +++ b/Content.Shared/Chemistry/ReagentUnit.cs @@ -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(); + } } } diff --git a/Content.Tests/Shared/Chemistry/ReagentUnit_Tests.cs b/Content.Tests/Shared/Chemistry/ReagentUnit_Tests.cs index f2f11b4fdb..1cae6e1db2 100644 --- a/Content.Tests/Shared/Chemistry/ReagentUnit_Tests.cs +++ b/Content.Tests/Shared/Chemistry/ReagentUnit_Tests.cs @@ -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")]