Skip to content

Random Mission Pool #325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 195 additions & 0 deletions Uchu.World.Test/Objects/Components/Server/MissionGiverComponentTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
using System;
using Moq;
using NUnit.Framework;
using Uchu.Core;
using Uchu.Core.Client;
using Uchu.World.Systems.Missions;

namespace Uchu.World.Test.Objects.Components.Server
{
public class MissionGiverComponentTest
{
/// <summary>
/// Mission giver component used with the tests.
/// </summary>
private MissionGiverComponent _missionGiverComponent;

/// <summary>
/// Player mission inventory used with the tests.
/// </summary>
private MissionInventoryComponent _missionInventoryComponent;

/// <summary>
/// Mission NPC component used with the tests.
/// </summary>
private MissionNPCComponent _missionNpcComponent;

/// <summary>
/// Sets up the tests.
/// </summary>
[SetUp]
public void SetUp()
{
this._missionGiverComponent = new MissionGiverComponent();
this._missionInventoryComponent = new MissionInventoryComponent();
this._missionNpcComponent = new MissionNPCComponent()
{
AcceptsMission = true,
OffersMission = true,
};
}

/// <summary>
/// Tests GetIdMissionToOffer with no stored missions.
/// </summary>
[Test]
public void TestGetIdMissionToOfferEmpty()
{
this._missionGiverComponent.Missions = Array.Empty<(Missions, MissionNPCComponent)>();
Assert.AreEqual(0, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with a stored mission with the default id.
/// </summary>
[Test]
public void TestGetIdMissionToOfferDefaultMissionId()
{
this._missionGiverComponent.Missions = new[]
{
(new Missions() { Id = default, }, this._missionNpcComponent)
};
Assert.AreEqual(0, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with a stored mission but missions aren't offered.
/// </summary>
[Test]
public void TestGetIdMissionToOfferDoesntOfferMission()
{
this._missionNpcComponent.OffersMission = false;
this._missionGiverComponent.Missions = new[]
{
(new Missions() { Id = 1, }, this._missionNpcComponent)
};
Assert.AreEqual(0, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with a mission ready to complete taking priority.
/// </summary>
[Test]
public void TestGetIdMissionToOfferReadyToCompletePriority()
{
var mockMission1 = new Mock<MissionInstance>(1, null);
mockMission1.SetupGet(mission => mission.State).Returns(MissionState.Available);
var mockMission2 = new Mock<MissionInstance>(2, null);
mockMission2.SetupGet(mission => mission.State).Returns(MissionState.ReadyToComplete);

this._missionGiverComponent.Missions = new[]
{
(new Missions() { Id = 1, }, this._missionNpcComponent),
(new Missions() { Id = 2, }, this._missionNpcComponent),
};
this._missionInventoryComponent.AddTestMission(mockMission1.Object);
this._missionInventoryComponent.AddTestMission(mockMission2.Object);
Assert.AreEqual(2, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with a mission active taking priority over available.
/// </summary>
[Test]
public void TestGetIdMissionToOfferActivePriority()
{
var mockMission1 = new Mock<MissionInstance>(1, null);
mockMission1.SetupGet(mission => mission.State).Returns(MissionState.Available);
var mockMission2 = new Mock<MissionInstance>(2, null);
mockMission2.SetupGet(mission => mission.State).Returns(MissionState.Active);
var mockMission3 = new Mock<MissionInstance>(1, null);
mockMission3.SetupGet(mission => mission.State).Returns(MissionState.Available);

this._missionGiverComponent.Missions = new[]
{
(new Missions() { Id = 1, }, this._missionNpcComponent),
(new Missions() { Id = 2, }, this._missionNpcComponent),
(new Missions() { Id = 3, }, this._missionNpcComponent),
};
this._missionInventoryComponent.AddTestMission(mockMission1.Object);
this._missionInventoryComponent.AddTestMission(mockMission2.Object);
this._missionInventoryComponent.AddTestMission(mockMission3.Object);
Assert.AreEqual(2, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with a mission completed active (repeating) taking priority over available.
/// </summary>
[Test]
public void TestGetIdMissionToOfferCompletedActivePriority()
{
var mockMission1 = new Mock<MissionInstance>(1, null);
mockMission1.SetupGet(mission => mission.State).Returns(MissionState.Available);
var mockMission2 = new Mock<MissionInstance>(2, null);
mockMission2.SetupGet(mission => mission.State).Returns(MissionState.CompletedActive);
var mockMission3 = new Mock<MissionInstance>(1, null);
mockMission3.SetupGet(mission => mission.State).Returns(MissionState.Available);

this._missionGiverComponent.Missions = new[]
{
(new Missions() { Id = 1, }, this._missionNpcComponent),
(new Missions() { Id = 2, }, this._missionNpcComponent),
(new Missions() { Id = 3, }, this._missionNpcComponent),
};
this._missionInventoryComponent.AddTestMission(mockMission1.Object);
this._missionInventoryComponent.AddTestMission(mockMission2.Object);
this._missionInventoryComponent.AddTestMission(mockMission3.Object);
Assert.AreEqual(2, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with an empty random mission pool.
/// </summary>
[Test]
public void TestGetIdMissionToOfferEmptyRandomPool()
{
var mockMission = new Mock<MissionInstance>(1, null);
mockMission.SetupGet(mission => mission.State).Returns(MissionState.Available);

this._missionGiverComponent.Missions = new[]
{
(new Missions()
{
Id = 1,
RandomPool = "",
}, this._missionNpcComponent),
};
this._missionInventoryComponent.AddTestMission(mockMission.Object);
Assert.AreEqual(1, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
}

/// <summary>
/// Tests GetIdMissionToOffer with a random mission pool.
/// </summary>
[Test]
public void TestGetIdMissionToOfferRandomPool()
{
var mockMission = new Mock<MissionInstance>(1, null);
mockMission.SetupGet(mission => mission.State).Returns(MissionState.Available);

this._missionGiverComponent.Missions = new[]
{
(new Missions()
{
Id = 1,
IsRandom = true,
RandomPool = "2,3",
}, this._missionNpcComponent),
};
this._missionInventoryComponent.AddTestMission(mockMission.Object);
var randomMission = this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent);
Assert.IsTrue(randomMission == 2 || randomMission == 3);
}
}
}

1 change: 1 addition & 0 deletions Uchu.World.Test/Uchu.World.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
Expand Down
15 changes: 12 additions & 3 deletions Uchu.World/Objects/Components/Player/MissionInventoryComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public MissionInventoryComponent()
/// <summary>
/// Complete list of missions this player has, either active or completed
/// </summary>
private List<MissionInstance> Missions { get; set; }
private List<MissionInstance> Missions { get; set; } = new List<MissionInstance>();

/// <summary>
/// Missions and achievements that the player has that are currently not completed. Provided as an array for
Expand Down Expand Up @@ -108,8 +108,6 @@ private async Task LoadAsync()
var missions = await uchuContext.Missions.Where(
m => m.CharacterId == GameObject.Id
).ToArrayAsync();

Missions = new List<MissionInstance>();

foreach (var mission in missions)
{
Expand Down Expand Up @@ -324,6 +322,17 @@ public async Task<MissionInstance> AddMissionAsync(int missionId, GameObject gam
return mission;
}

/// <summary>
/// Adds a mission for unit testing.
/// </summary>
/// <param name="mission">The test mission to add.</param>
public void AddTestMission(MissionInstance mission)
{
lock (Missions) {
Missions.Add(mission);
}
}

/// <summary>
/// Checks if the player can accept a mission based on whether it's repeatable, already started and if the
/// requirements are met.
Expand Down
Loading