Fix ratio issues on SlipSolution
This commit is contained in:
@@ -181,11 +181,13 @@ namespace Content.Shared.Chemistry
|
|||||||
|
|
||||||
newSolution = new Solution();
|
newSolution = new Solution();
|
||||||
var newTotalVolume = ReagentUnit.New(0M);
|
var newTotalVolume = ReagentUnit.New(0M);
|
||||||
var ratio = (TotalVolume - quantity).Decimal() / TotalVolume.Decimal();
|
var remainingVolume = TotalVolume;
|
||||||
|
|
||||||
for (var i = 0; i < _contents.Count; i++)
|
for (var i = 0; i < _contents.Count; i++)
|
||||||
{
|
{
|
||||||
var reagent = _contents[i];
|
var reagent = _contents[i];
|
||||||
|
var ratio = (remainingVolume - quantity).Decimal() / remainingVolume.Decimal();
|
||||||
|
remainingVolume -= reagent.Quantity;
|
||||||
|
|
||||||
var newQuantity = reagent.Quantity * ratio;
|
var newQuantity = reagent.Quantity * ratio;
|
||||||
var splitQuantity = reagent.Quantity - newQuantity;
|
var splitQuantity = reagent.Quantity - newQuantity;
|
||||||
@@ -193,10 +195,11 @@ namespace Content.Shared.Chemistry
|
|||||||
_contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
|
_contents[i] = new ReagentQuantity(reagent.ReagentId, newQuantity);
|
||||||
newSolution._contents.Add(new ReagentQuantity(reagent.ReagentId, splitQuantity));
|
newSolution._contents.Add(new ReagentQuantity(reagent.ReagentId, splitQuantity));
|
||||||
newTotalVolume += splitQuantity;
|
newTotalVolume += splitQuantity;
|
||||||
|
quantity -= splitQuantity;
|
||||||
}
|
}
|
||||||
|
|
||||||
TotalVolume = TotalVolume * ratio;
|
|
||||||
newSolution.TotalVolume = newTotalVolume;
|
newSolution.TotalVolume = newTotalVolume;
|
||||||
|
TotalVolume -= newTotalVolume;
|
||||||
|
|
||||||
return newSolution;
|
return newSolution;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -255,7 +255,25 @@ namespace Content.Tests.Shared.Chemistry
|
|||||||
Assert.That(splitSolution.GetReagentQuantity("water").Float(), Is.EqualTo(reduce));
|
Assert.That(splitSolution.GetReagentQuantity("water").Float(), Is.EqualTo(reduce));
|
||||||
Assert.That(splitSolution.TotalVolume.Float(), Is.EqualTo(reduce));
|
Assert.That(splitSolution.TotalVolume.Float(), Is.EqualTo(reduce));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
[TestCase(2)]
|
||||||
|
[TestCase(10)]
|
||||||
|
[TestCase(100)]
|
||||||
|
[TestCase(1000)]
|
||||||
|
public void SplitRounding(int amount)
|
||||||
|
{
|
||||||
|
var solutionOne = new Solution();
|
||||||
|
solutionOne.AddReagent("foo", ReagentUnit.New(amount));
|
||||||
|
solutionOne.AddReagent("bar", ReagentUnit.New(amount));
|
||||||
|
solutionOne.AddReagent("baz", ReagentUnit.New(amount));
|
||||||
|
|
||||||
|
var splitAmount = ReagentUnit.New(5);
|
||||||
|
var split = solutionOne.SplitSolution(splitAmount);
|
||||||
|
|
||||||
|
Assert.That(split.TotalVolume, Is.EqualTo(splitAmount));
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void SplitSolutionMoreThanTotalRemovesAll()
|
public void SplitSolutionMoreThanTotalRemovesAll()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user