Skip to content

Commit 81f0f23

Browse files
Merge pull request #325 from UchuServer/enhancement/random-missions-pool
Random Mission Pool
2 parents 977900b + 3ec7570 commit 81f0f23

File tree

5 files changed

+314
-69
lines changed

5 files changed

+314
-69
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
using System;
2+
using Moq;
3+
using NUnit.Framework;
4+
using Uchu.Core;
5+
using Uchu.Core.Client;
6+
using Uchu.World.Systems.Missions;
7+
8+
namespace Uchu.World.Test.Objects.Components.Server
9+
{
10+
public class MissionGiverComponentTest
11+
{
12+
/// <summary>
13+
/// Mission giver component used with the tests.
14+
/// </summary>
15+
private MissionGiverComponent _missionGiverComponent;
16+
17+
/// <summary>
18+
/// Player mission inventory used with the tests.
19+
/// </summary>
20+
private MissionInventoryComponent _missionInventoryComponent;
21+
22+
/// <summary>
23+
/// Mission NPC component used with the tests.
24+
/// </summary>
25+
private MissionNPCComponent _missionNpcComponent;
26+
27+
/// <summary>
28+
/// Sets up the tests.
29+
/// </summary>
30+
[SetUp]
31+
public void SetUp()
32+
{
33+
this._missionGiverComponent = new MissionGiverComponent();
34+
this._missionInventoryComponent = new MissionInventoryComponent();
35+
this._missionNpcComponent = new MissionNPCComponent()
36+
{
37+
AcceptsMission = true,
38+
OffersMission = true,
39+
};
40+
}
41+
42+
/// <summary>
43+
/// Tests GetIdMissionToOffer with no stored missions.
44+
/// </summary>
45+
[Test]
46+
public void TestGetIdMissionToOfferEmpty()
47+
{
48+
this._missionGiverComponent.Missions = Array.Empty<(Missions, MissionNPCComponent)>();
49+
Assert.AreEqual(0, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
50+
}
51+
52+
/// <summary>
53+
/// Tests GetIdMissionToOffer with a stored mission with the default id.
54+
/// </summary>
55+
[Test]
56+
public void TestGetIdMissionToOfferDefaultMissionId()
57+
{
58+
this._missionGiverComponent.Missions = new[]
59+
{
60+
(new Missions() { Id = default, }, this._missionNpcComponent)
61+
};
62+
Assert.AreEqual(0, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
63+
}
64+
65+
/// <summary>
66+
/// Tests GetIdMissionToOffer with a stored mission but missions aren't offered.
67+
/// </summary>
68+
[Test]
69+
public void TestGetIdMissionToOfferDoesntOfferMission()
70+
{
71+
this._missionNpcComponent.OffersMission = false;
72+
this._missionGiverComponent.Missions = new[]
73+
{
74+
(new Missions() { Id = 1, }, this._missionNpcComponent)
75+
};
76+
Assert.AreEqual(0, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
77+
}
78+
79+
/// <summary>
80+
/// Tests GetIdMissionToOffer with a mission ready to complete taking priority.
81+
/// </summary>
82+
[Test]
83+
public void TestGetIdMissionToOfferReadyToCompletePriority()
84+
{
85+
var mockMission1 = new Mock<MissionInstance>(1, null);
86+
mockMission1.SetupGet(mission => mission.State).Returns(MissionState.Available);
87+
var mockMission2 = new Mock<MissionInstance>(2, null);
88+
mockMission2.SetupGet(mission => mission.State).Returns(MissionState.ReadyToComplete);
89+
90+
this._missionGiverComponent.Missions = new[]
91+
{
92+
(new Missions() { Id = 1, }, this._missionNpcComponent),
93+
(new Missions() { Id = 2, }, this._missionNpcComponent),
94+
};
95+
this._missionInventoryComponent.AddTestMission(mockMission1.Object);
96+
this._missionInventoryComponent.AddTestMission(mockMission2.Object);
97+
Assert.AreEqual(2, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
98+
}
99+
100+
/// <summary>
101+
/// Tests GetIdMissionToOffer with a mission active taking priority over available.
102+
/// </summary>
103+
[Test]
104+
public void TestGetIdMissionToOfferActivePriority()
105+
{
106+
var mockMission1 = new Mock<MissionInstance>(1, null);
107+
mockMission1.SetupGet(mission => mission.State).Returns(MissionState.Available);
108+
var mockMission2 = new Mock<MissionInstance>(2, null);
109+
mockMission2.SetupGet(mission => mission.State).Returns(MissionState.Active);
110+
var mockMission3 = new Mock<MissionInstance>(1, null);
111+
mockMission3.SetupGet(mission => mission.State).Returns(MissionState.Available);
112+
113+
this._missionGiverComponent.Missions = new[]
114+
{
115+
(new Missions() { Id = 1, }, this._missionNpcComponent),
116+
(new Missions() { Id = 2, }, this._missionNpcComponent),
117+
(new Missions() { Id = 3, }, this._missionNpcComponent),
118+
};
119+
this._missionInventoryComponent.AddTestMission(mockMission1.Object);
120+
this._missionInventoryComponent.AddTestMission(mockMission2.Object);
121+
this._missionInventoryComponent.AddTestMission(mockMission3.Object);
122+
Assert.AreEqual(2, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
123+
}
124+
125+
/// <summary>
126+
/// Tests GetIdMissionToOffer with a mission completed active (repeating) taking priority over available.
127+
/// </summary>
128+
[Test]
129+
public void TestGetIdMissionToOfferCompletedActivePriority()
130+
{
131+
var mockMission1 = new Mock<MissionInstance>(1, null);
132+
mockMission1.SetupGet(mission => mission.State).Returns(MissionState.Available);
133+
var mockMission2 = new Mock<MissionInstance>(2, null);
134+
mockMission2.SetupGet(mission => mission.State).Returns(MissionState.CompletedActive);
135+
var mockMission3 = new Mock<MissionInstance>(1, null);
136+
mockMission3.SetupGet(mission => mission.State).Returns(MissionState.Available);
137+
138+
this._missionGiverComponent.Missions = new[]
139+
{
140+
(new Missions() { Id = 1, }, this._missionNpcComponent),
141+
(new Missions() { Id = 2, }, this._missionNpcComponent),
142+
(new Missions() { Id = 3, }, this._missionNpcComponent),
143+
};
144+
this._missionInventoryComponent.AddTestMission(mockMission1.Object);
145+
this._missionInventoryComponent.AddTestMission(mockMission2.Object);
146+
this._missionInventoryComponent.AddTestMission(mockMission3.Object);
147+
Assert.AreEqual(2, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
148+
}
149+
150+
/// <summary>
151+
/// Tests GetIdMissionToOffer with an empty random mission pool.
152+
/// </summary>
153+
[Test]
154+
public void TestGetIdMissionToOfferEmptyRandomPool()
155+
{
156+
var mockMission = new Mock<MissionInstance>(1, null);
157+
mockMission.SetupGet(mission => mission.State).Returns(MissionState.Available);
158+
159+
this._missionGiverComponent.Missions = new[]
160+
{
161+
(new Missions()
162+
{
163+
Id = 1,
164+
RandomPool = "",
165+
}, this._missionNpcComponent),
166+
};
167+
this._missionInventoryComponent.AddTestMission(mockMission.Object);
168+
Assert.AreEqual(1, this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent));
169+
}
170+
171+
/// <summary>
172+
/// Tests GetIdMissionToOffer with a random mission pool.
173+
/// </summary>
174+
[Test]
175+
public void TestGetIdMissionToOfferRandomPool()
176+
{
177+
var mockMission = new Mock<MissionInstance>(1, null);
178+
mockMission.SetupGet(mission => mission.State).Returns(MissionState.Available);
179+
180+
this._missionGiverComponent.Missions = new[]
181+
{
182+
(new Missions()
183+
{
184+
Id = 1,
185+
IsRandom = true,
186+
RandomPool = "2,3",
187+
}, this._missionNpcComponent),
188+
};
189+
this._missionInventoryComponent.AddTestMission(mockMission.Object);
190+
var randomMission = this._missionGiverComponent.GetIdMissionToOffer(this._missionInventoryComponent);
191+
Assert.IsTrue(randomMission == 2 || randomMission == 3);
192+
}
193+
}
194+
}
195+

Uchu.World.Test/Uchu.World.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<PackageReference Include="Moq" Version="4.16.1" />
1011
<PackageReference Include="NUnit" Version="3.12.0" />
1112
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1" />
1213
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />

Uchu.World/Objects/Components/Player/MissionInventoryComponent.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public MissionInventoryComponent()
4343
/// <summary>
4444
/// Complete list of missions this player has, either active or completed
4545
/// </summary>
46-
private List<MissionInstance> Missions { get; set; }
46+
private List<MissionInstance> Missions { get; set; } = new List<MissionInstance>();
4747

4848
/// <summary>
4949
/// Missions and achievements that the player has that are currently not completed. Provided as an array for
@@ -108,8 +108,6 @@ private async Task LoadAsync()
108108
var missions = await uchuContext.Missions.Where(
109109
m => m.CharacterId == GameObject.Id
110110
).ToArrayAsync();
111-
112-
Missions = new List<MissionInstance>();
113111

114112
foreach (var mission in missions)
115113
{
@@ -324,6 +322,17 @@ public async Task<MissionInstance> AddMissionAsync(int missionId, GameObject gam
324322
return mission;
325323
}
326324

325+
/// <summary>
326+
/// Adds a mission for unit testing.
327+
/// </summary>
328+
/// <param name="mission">The test mission to add.</param>
329+
public void AddTestMission(MissionInstance mission)
330+
{
331+
lock (Missions) {
332+
Missions.Add(mission);
333+
}
334+
}
335+
327336
/// <summary>
328337
/// Checks if the player can accept a mission based on whether it's repeatable, already started and if the
329338
/// requirements are met.

0 commit comments

Comments
 (0)