Skip to content

Commit

Permalink
Merge pull request #515 from PoESkillTree/calculation-items-skills
Browse files Browse the repository at this point in the history
Calculation Rewriting Part 2.3: Skill and Item parsing
  • Loading branch information
brather1ng committed Dec 31, 2018
2 parents c2beb4b + fe4035d commit fe131c6
Show file tree
Hide file tree
Showing 416 changed files with 18,769 additions and 4,650 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using PoESkillTree.Computation.Common;
using PoESkillTree.Computation.Common.Builders.Entities;
using PoESkillTree.Computation.Common.Builders.Stats;
using PoESkillTree.GameModel;

namespace PoESkillTree.Computation.Builders.Tests.Actions
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void HitWithOnBuildsToCorrectResult()
public void HitWithMultipleOnThrows()
{
var damageTypes = new[] { DamageType.Chaos, DamageType.Fire };
var damageTypeBuilder = Mock.Of<IDamageTypeBuilder>(b => b.BuildDamageTypes() == damageTypes);
var damageTypeBuilder = Mock.Of<IDamageTypeBuilder>(b => b.BuildDamageTypes(default) == damageTypes);
var sut = CreateSut();

var result = sut.HitWith(damageTypeBuilder).On.Build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Linq;
using Moq;
using NUnit.Framework;
using PoESkillTree.Computation.Builders.Behaviors;
using PoESkillTree.Computation.Builders.Stats;
using PoESkillTree.Computation.Common;
using PoESkillTree.Utils.Extensions;

namespace PoESkillTree.Computation.Builders.Tests.Behaviors
{
[TestFixture]
public class AffectedByModifiersToOtherStatValueTest
{
[TestCase(true, true)]
[TestCase(true, false)]
[TestCase(false, false)]
public void CalculateReturnsCorrectResult(bool formIsAffected, bool conditionStatValue)
{
var expected = (NodeValue?) 42;
var affectedForm = Form.Increase;
var retrievedForm = formIsAffected ? affectedForm : Form.BaseAdd;

var transformedStat = new Stat("transformed");
var otherStat = new Stat("other");
var conditionStat = new Stat("condition");
var expectedPaths = new (IStat, PathDefinition)[] { (transformedStat, PathDefinition.MainPath) }
.AsEnumerable();
if (formIsAffected && conditionStatValue)
expectedPaths = expectedPaths.Append((otherStat, PathDefinition.MainPath));

var context = Mock.Of<IValueCalculationContext>(c =>
c.GetValues(retrievedForm, expectedPaths) == new[] { expected } &&
c.GetValue(conditionStat, NodeType.Total, PathDefinition.MainPath) == (NodeValue?) conditionStatValue);
var transformedValue = new FunctionalValue(c => c.GetValues(retrievedForm, transformedStat).Sum(), "");
var sut = new AffectedByModifiersToOtherStatValue(transformedStat, otherStat, conditionStat, affectedForm,
transformedValue);

var actual = sut.Calculate(context);

Assert.AreEqual(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using Moq;
using NUnit.Framework;
using PoESkillTree.Computation.Builders.Behaviors;
using PoESkillTree.Computation.Builders.Stats;
using PoESkillTree.Computation.Common;

namespace PoESkillTree.Computation.Builders.Tests.Behaviors
{
[TestFixture]
public class DamageEffectivenessBaseValueTest
{
[TestCase(null, null)]
[TestCase(1.5, 2)]
public void CalculateReturnsCorrectResult(double? baseSetEffectiveness, double? baseAddEffectiveness)
{
var baseSet = (NodeValue?) 2;
var baseAdd = (NodeValue?) 3;
var expected = baseSet * (baseSetEffectiveness ?? 1) + baseAdd * (baseAddEffectiveness ?? 1);

var context = Mock.Of<IValueCalculationContext>(c =>
c.GetValue(TransformedStat, NodeType.BaseSet, PathDefinition.MainPath) == baseSet &&
c.GetValue(TransformedStat, NodeType.BaseAdd, PathDefinition.MainPath) == baseAdd &&
c.GetValue(SetEffectivenessStat, NodeType.Total, PathDefinition.MainPath) ==
(NodeValue?) baseSetEffectiveness &&
c.GetValue(AddEffectivenessStat, NodeType.Total, PathDefinition.MainPath) ==
(NodeValue?) baseAddEffectiveness);
var sut = CreateSut(
c => c.GetValue(TransformedStat, NodeType.BaseSet) + c.GetValue(TransformedStat, NodeType.BaseAdd));

var actual = sut.Calculate(context);

Assert.AreEqual(expected, actual);
}

[Test]
public void CalculateDoesNotTransformOtherForms()
{
var expected = (NodeValue?) 42;
var context = Mock.Of<IValueCalculationContext>(c =>
c.GetValue(TransformedStat, NodeType.Increase, PathDefinition.MainPath) == expected &&
c.GetValue(SetEffectivenessStat, NodeType.Total, PathDefinition.MainPath) == (NodeValue?) 2 &&
c.GetValue(AddEffectivenessStat, NodeType.Total, PathDefinition.MainPath) == (NodeValue?) 2);
var sut = CreateSut(c => c.GetValue(TransformedStat, NodeType.Increase));

var actual = sut.Calculate(context);

Assert.AreEqual(expected, actual);
}

[Test]
public void CalculateDoesNotTransformOtherStats()
{
var expected = (NodeValue?) 42;
var otherStat = new Stat("other");
var context = Mock.Of<IValueCalculationContext>(c =>
c.GetValue(otherStat, NodeType.BaseSet, PathDefinition.MainPath) == expected &&
c.GetValue(SetEffectivenessStat, NodeType.Total, PathDefinition.MainPath) == (NodeValue?) 2 &&
c.GetValue(AddEffectivenessStat, NodeType.Total, PathDefinition.MainPath) == (NodeValue?) 2);
var sut = CreateSut(c => c.GetValue(otherStat, NodeType.BaseSet));

var actual = sut.Calculate(context);

Assert.AreEqual(expected, actual);
}

private static DamageEffectivenessBaseValue CreateSut(Func<IValueCalculationContext, NodeValue?> baseCalculate)
=> new DamageEffectivenessBaseValue(TransformedStat, SetEffectivenessStat, AddEffectivenessStat,
new FunctionalValue(baseCalculate, ""));

private static readonly IStat TransformedStat = new Stat("transformed");
private static readonly IStat SetEffectivenessStat = new Stat("setEffectiveness");
private static readonly IStat AddEffectivenessStat = new Stat("addEffectiveness");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Linq;
using Moq;
using NUnit.Framework;
using PoESkillTree.Computation.Builders.Behaviors;
using PoESkillTree.Computation.Builders.Stats;
using PoESkillTree.Computation.Common;

namespace PoESkillTree.Computation.Builders.Tests.Behaviors
{
[TestFixture]
public class MaximumFormAggregatingValueTest
{
[Test]
public void CalculateReturnsNullWithOnlyNullValues()
{
var sut = CreateSut();
var context = MockContext(null, null);

var actual = sut.Calculate(context);

Assert.IsNull(actual);
}

[TestCase(0, ExpectedResult = 0)]
[TestCase(0, null, 1, 3, 4, 2, ExpectedResult = 4)]
public double CalculateReturnsCorrectResult(params int?[] values)
{
var sut = CreateSut();
var context = MockContext(values);

var actual = sut.Calculate(context);

return actual.Single();
}

[Test]
public void CalculateChecksForSameForm()
{
var transformedValue = new FunctionalValue(c => c.GetValues(Form.BaseSet, Paths).First(), "");
var sut = CreateSut(Stat, Form.More, transformedValue);
var context = MockContext(2, 4);

var actual = sut.Calculate(context);

Assert.AreEqual(2, actual.Single());
}

[Test]
public void CalculateChecksForSameSta()
{
var transformedValue = new FunctionalValue(c => c.GetValues(Form.BaseSet, Paths).First(), "");
var sut = CreateSut(new Stat("a"), Form.BaseSet, transformedValue);
var context = MockContext(2, 4);

var actual = sut.Calculate(context);

Assert.AreEqual(2, actual.Single());
}

[Test]
public void CalculateThrowsIfPathsContainsSameAndDifferentStats()
{
var paths = new[] { (Stat, PathDefinition.MainPath), (new Stat("a"), PathDefinition.MainPath) };
var transformedValue = new FunctionalValue(c => c.GetValues(Form.BaseSet, paths).Single(), "");
var sut = CreateSut(Stat, Form.BaseSet, transformedValue);
var nodeValues = new NodeValue?[0];
var context = Mock.Of<IValueCalculationContext>(c => c.GetValues(Form.BaseSet, paths) == nodeValues);

Assert.Throws<InvalidOperationException>(() => sut.Calculate(context));
}

private static readonly IStat Stat = new Stat("s");

private static readonly (IStat, PathDefinition)[] Paths =
{
(Stat, PathDefinition.MainPath), (Stat, new PathDefinition(new ModifierSource.Local.Given()))
};

private static MaximumFormAggregatingValue CreateSut()
{
var transformedValue = new FunctionalValue(c => c.GetValues(Form.BaseSet, Paths).Single(), "");
return CreateSut(Stat, Form.BaseSet, transformedValue);
}

private static MaximumFormAggregatingValue CreateSut(IStat stat, Form form, IValue transformedValue)
=> new MaximumFormAggregatingValue(stat, form, transformedValue);

private static IValueCalculationContext MockContext(params int?[] values)
{
var nodeValues = values.Select(v => (NodeValue?) v);
return Mock.Of<IValueCalculationContext>(c => c.GetValues(Form.BaseSet, Paths) == nodeValues);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System.Linq;
using Moq;
using NUnit.Framework;
using PoESkillTree.Computation.Builders.Behaviors;
using PoESkillTree.Computation.Builders.Stats;
using PoESkillTree.Computation.Common;
using PoESkillTree.GameModel.Items;

namespace PoESkillTree.Computation.Builders.Tests.Behaviors
{
[TestFixture]
public class RequirementUncappedSubtotalValueTest
{
[TestCase(-1.0, null, 1.0)]
[TestCase(42.0, 43.0, 41.0, 43.0, 40.0)]
public void CalculateReturnsCorrectResult(params double?[] values)
{
var expected = (NodeValue?) values.Max();
var transformedStat = new Stat("transformed");
var paths = values
.Select((_, i) => new PathDefinition(new ModifierSource.Local.Gem(ItemSlot.Helm, i)))
.ToList();
var contextMock = new Mock<IValueCalculationContext>();
contextMock.Setup(c => c.GetPaths(transformedStat)).Returns(paths);
for (var i = 0; i < paths.Count; i++)
{
var path = paths[i];
contextMock.Setup(c => c.GetValue(transformedStat, NodeType.PathTotal, path))
.Returns((NodeValue?) values[i]);
}
var transformedValue = new FunctionalValue(c => c.GetValues(transformedStat, NodeType.PathTotal).Sum(), "");
var sut = new RequirementUncappedSubtotalValue(transformedStat, transformedValue);

var actual = sut.Calculate(contextMock.Object);

Assert.AreEqual(expected, actual);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
using PoESkillTree.Computation.Common;
using PoESkillTree.Computation.Common.Builders.Buffs;
using PoESkillTree.Computation.Common.Builders.Skills;
using PoESkillTree.GameModel;
using PoESkillTree.GameModel.Skills;

namespace PoESkillTree.Computation.Builders.Tests.Buffs
{
Expand All @@ -22,7 +24,7 @@ public void EffectBuildsToCorrectResults()
{
var sut = CreateSut(3);

var stats = sut.Effect.BuildToSingleResult().Stats;
var stats = sut.Effect.BuildToStats();

Assert.That(stats, Has.Exactly(3).Items);
Assert.AreEqual($"b0.EffectOn({default(Entity)})", stats[0].Identity);
Expand All @@ -36,7 +38,7 @@ public void AddStatBuildsToCorrectResult()
var addedStat = StatBuilderUtils.FromIdentity(StatFactory, "s", null);
var sut = CreateSut(3);

var stats = sut.AddStat(addedStat).BuildToSingleResult().Stats;
var stats = sut.AddStat(addedStat).BuildToStats();

Assert.That(stats, Has.Exactly(3).Items);
Assert.AreEqual("s", stats[0].Identity);
Expand All @@ -61,7 +63,7 @@ public void WithBuildsToCorrectResult()
var keyword = KeywordBuilder(1);
var sut = CreateSut(3);

var stats = sut.With(keyword).Effect.BuildToSingleResult().Stats;
var stats = sut.With(keyword).Effect.BuildToStats();

Assert.That(stats, Has.Exactly(2).Items);
Assert.AreEqual($"b0.EffectOn({default(Entity)})", stats[0].Identity);
Expand Down Expand Up @@ -99,7 +101,7 @@ public void WithResolveEffectBuildsToCorrectResult()
var sut = CreateSut(3);

var resolved = (IBuffBuilderCollection) sut.With(unresolved).Resolve(null);
var stats = resolved.Effect.BuildToSingleResult().Stats;
var stats = resolved.Effect.BuildToStats();

Assert.That(stats, Has.Exactly(2).Items);
}
Expand All @@ -125,7 +127,7 @@ public void WithEffectResolveBuildsToCorrectResult()
var sut = CreateSut(3);

var resolved = sut.With(unresolved).Effect.Resolve(null);
var stats = resolved.BuildToSingleResult().Stats;
var stats = resolved.BuildToStats();

Assert.That(stats, Has.Exactly(2).Items);
}
Expand Down
16 changes: 16 additions & 0 deletions PoESkillTree.Computation.Builders.Tests/Buffs/BuffBuilderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
using NUnit.Framework;
using PoESkillTree.Computation.Builders.Buffs;
using PoESkillTree.Computation.Builders.Entities;
using PoESkillTree.Computation.Builders.Resolving;
using PoESkillTree.Computation.Builders.Stats;
using PoESkillTree.Computation.Builders.Tests.Stats;
using PoESkillTree.Computation.Builders.Values;
using PoESkillTree.Computation.Common;
using PoESkillTree.Computation.Common.Builders.Resolving;
using PoESkillTree.GameModel;

namespace PoESkillTree.Computation.Builders.Tests.Buffs
{
Expand Down Expand Up @@ -78,6 +81,19 @@ public void AddStatBuildsToCorrectResultIfNotAsBuffActive()
Assert.AreEqual(expectedValue, actualValue);
}

[Test]
public void AddStatResolveResolvesIdentity()
{
var expectedIdentity = "buff";
var statBuilder = StatBuilderUtils.FromIdentity(StatFactory, "stat", null);
var resolveContext = new ResolveContext(null, null);
var sut = new BuffBuilder(StatFactory, new UnresolvedCoreBuilder<string>("unresolved",
c => CoreBuilder.Create(expectedIdentity)));

var builder = sut.AddStat(statBuilder).Resolve(resolveContext);
var value = builder.BuildToSingleResult().ValueConverter(new ValueBuilderImpl(1));
}

private static BuffBuilder CreateSut() =>
new BuffBuilder(StatFactory, CoreBuilder.Create("test"));

Expand Down
Loading

0 comments on commit fe131c6

Please sign in to comment.