Skip to content

Commit

Permalink
Add commands and E2E
Browse files Browse the repository at this point in the history
  • Loading branch information
brodrigz committed Oct 4, 2024
1 parent eb884a5 commit 63c3acf
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 12 deletions.
176 changes: 176 additions & 0 deletions source/E2E.Tests/Services/BesiegerCamps/BesiegerCampCollectionTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using E2E.Tests.Environment;
using E2E.Tests.Environment.Instance;
using E2E.Tests.Util;
using HarmonyLib;
using System.Reflection;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Siege;
using Xunit.Abstractions;
using GameInterface.Services.BesiegerCamps.Patches;

namespace E2E.Tests.Services.BesiegerCamps
{
public class BesiegerCampCollectionTests : IDisposable
{
private List<MethodBase> disabledMethods = new();

private E2ETestEnvironment TestEnvironment { get; }

private EnvironmentInstance Server => TestEnvironment.Server;
private IEnumerable<EnvironmentInstance> Clients => TestEnvironment.Clients;

public BesiegerCampCollectionTests(ITestOutputHelper output)
{
TestEnvironment = new E2ETestEnvironment(output);
DisableMethods();
}

public void Dispose()
{
TestEnvironment.Dispose();
}

private void DisableMethods()
{
disabledMethods = new List<MethodBase> {
AccessTools.Method(typeof (MobileParty), nameof(MobileParty.OnPartyJoinedSiegeInternal)),
AccessTools.Method(typeof (BesiegerCamp), nameof(BesiegerCamp.SetSiegeCampPartyPosition)),
AccessTools.Method(typeof (BesiegerCamp), nameof(BesiegerCamp.InitializeSiegeEventSide))
};
}

private T ServerCreateObject<T>(out string objectId)
{
string? id = null;
T? obj = default;

Server.Call(() =>
{
obj = GameObjectCreator.CreateInitializedObject<T>();
Assert.True(Server.ObjectManager.TryGetId(obj, out id));
}, disabledMethods);

objectId = id!;
return obj ?? throw new InvalidOperationException("Failed to create object.");
}

[Fact]
void ServerAddBesiegerParty_SyncAllClients()
{
//Arrange
var besiegerCamp = ServerCreateObject<BesiegerCamp>(out string besiegerCampId);
var besiegerParty = ServerCreateObject<MobileParty>(out string besiegerPartyId);

//Act
Server.Call(() =>
{
//besiegerCamp._besiegerParties.Add(besiegerParty); // Doesn't call override, must call it directly
BesiegerCampCollectionPatches.ListAddOverride(besiegerCamp._besiegerParties, besiegerParty, besiegerCamp);
});

//Assert
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));
Assert.True(client.ObjectManager.TryGetObject<MobileParty>(besiegerPartyId, out var clientBesiegerParty));
Assert.Contains<MobileParty>(clientBesiegerParty, clientBesiegerCamp._besiegerParties);
}
}

[Fact]
void ServerRemoveBesiegerParty_SyncAllClients()
{
// Arrange
var besiegerCamp = ServerCreateObject<BesiegerCamp>(out string besiegerCampId);
var besiegerParty = ServerCreateObject<MobileParty>(out string besiegerPartyId);
Server.Call(() =>
{
BesiegerCampCollectionPatches.ListAddOverride(besiegerCamp._besiegerParties, besiegerParty, besiegerCamp);
});
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));
Assert.True(client.ObjectManager.TryGetObject<MobileParty>(besiegerPartyId, out var clientBesiegerParty));
Assert.Contains(clientBesiegerParty, clientBesiegerCamp._besiegerParties);
}

// Act
Server.Call(() =>
{
BesiegerCampCollectionPatches.ListRemoveOverride(besiegerCamp._besiegerParties, besiegerParty, besiegerCamp);
});

// Assert
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));
Assert.True(client.ObjectManager.TryGetObject<MobileParty>(besiegerPartyId, out var clientBesiegerParty));
Assert.Empty(clientBesiegerCamp._besiegerParties);
}
}

[Fact]
void ClientAddBesiegerParty_DoesNothing()
{
// Arrange
var besiegerCamp = ServerCreateObject<BesiegerCamp>(out string besiegerCampId);
var besiegerParty = ServerCreateObject<MobileParty>(out string besiegerPartyId);
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var syncedCamp));
}

// Act
var firstClient = Clients.First();
firstClient.Call(() =>
{
BesiegerCampCollectionPatches.ListAddOverride(besiegerCamp._besiegerParties, besiegerParty, besiegerCamp);
});

// Assert
foreach (var client in Clients.Where(c => c != firstClient))
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));
Assert.True(client.ObjectManager.TryGetObject<MobileParty>(besiegerPartyId, out var clientBesiegerParty));
Assert.DoesNotContain<MobileParty>(clientBesiegerParty, clientBesiegerCamp._besiegerParties);
}
}

[Fact]
void ClientRemoveBesiegerParty_DoesNothing()
{
// Arrange
var besiegerCamp = ServerCreateObject<BesiegerCamp>(out string besiegerCampId);
var besiegerParty = ServerCreateObject<MobileParty>(out string besiegerPartyId);
Server.Call(() =>
{
BesiegerCampCollectionPatches.ListAddOverride(besiegerCamp._besiegerParties, besiegerParty, besiegerCamp);
});
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));
Assert.True(client.ObjectManager.TryGetObject<MobileParty>(besiegerPartyId, out var clientBesiegerParty));
Assert.Contains<MobileParty>(clientBesiegerParty, clientBesiegerCamp._besiegerParties);
}

// Act
var firstClient = Clients.First();
firstClient.Call(() =>
{
BesiegerCampCollectionPatches.ListRemoveOverride(besiegerCamp._besiegerParties, besiegerParty, besiegerCamp);
});

// Assert
foreach (var client in Clients.Where(c => c != firstClient))
{
Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));
Assert.True(client.ObjectManager.TryGetObject<MobileParty>(besiegerPartyId, out var clientBesiegerParty));
Assert.Contains<MobileParty>(clientBesiegerParty, clientBesiegerCamp._besiegerParties);
}
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void ServerChangeBesiegerCampSiegeEvent_SyncAllClients()
// Act
Server.Call(() =>
{
//serverBesiegerCamp.SiegeEvent = serverSiegeEvent;
serverBesiegerCamp.SiegeEvent = serverSiegeEvent;
});

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ private static bool TryGetObjectManager(out IObjectManager objectManager)
/// Set the number of tropps killed
/// </summary>
/// <param name="args">first arg : besiegerCampId ; second arg : value</param>
/// <returns></returns>
[CommandLineArgumentFunction("set_number_of_troops_killed_on_side", "coop.debug.BesiegerCamp")]
/// <returns>Result of the operation as a string</returns>
[CommandLineArgumentFunction("set_number_of_troops_killed_on_side", "coop.debug.besiegercamp")]
public static string SetBesiegerCampNumberOfTroopsKilledOnSide(List<string> args)
{
if (args.Count != 2)
{
return "Usage: coop.debug.besiegerCamp.set_number_of_troops_killed_on_side <besiegerCampId> <value> ";
return "Usage: coop.debug.besiegercamp.set_number_of_troops_killed_on_side <besiegerCampId> <value> ";
}

string besiegerCampId = args[0];
Expand Down Expand Up @@ -67,13 +67,13 @@ public static string SetBesiegerCampNumberOfTroopsKilledOnSide(List<string> args
/// Set siege preparations progress
/// </summary>
/// <param name="args">first arg : besiegerCampId ; second arg : value</param>
/// <returns></returns>
[CommandLineArgumentFunction("coop.debug.besiegercamp.set_progress", "coop.debug.BesiegerCamp")]
/// <returns>Result of the operation as a string</returns>
[CommandLineArgumentFunction("coop.debug.besiegercamp.set_progress", "coop.debug.besiegercamp")]
public static string SetBesiegerCampPreparationsProgress(List<string> args)
{
if (args.Count != 2)
{
return "Usage: coop.debug.besiegerCamp.set_progress <besiegerCampId> <progress> ";
return "Usage: coop.debug.besiegercamp.set_progress <besiegerCampId> <progress> ";
}

string besiegerCampId = args[0];
Expand Down Expand Up @@ -105,15 +105,15 @@ public static string SetBesiegerCampPreparationsProgress(List<string> args)
/// </summary>
/// <param name="args">first arg: besiegerCampId; second arg: strategyId</param>
/// <returns>Result of the operation as a string</returns>
[CommandLineArgumentFunction("set_siege_strategy", "coop.debug.BesiegerCamp")]
[CommandLineArgumentFunction("set_siege_strategy", "coop.debug.besiegercamp")]
public static string SetBesiegerCampSiegeStrategy(List<string> args)
{
string getPossibleStragegyIds() => string.Join(Environment.NewLine, SiegeStrategy.All.Select(x => x.StringId));
string idTipMsg = $"{Environment.NewLine}SiegeStrategy Id must be one of the following:{getPossibleStragegyIds()}";

if (args.Count != 2)
{
return "Usage: coop.debug.besiegerCamp.set_siege_strategy <besiegerCampId> <strategyId>" + idTipMsg;
return "Usage: coop.debug.besiegercamp.set_siege_strategy <besiegerCampId> <strategyId>" + idTipMsg;
}

string besiegerCampId = args[0];
Expand Down Expand Up @@ -147,13 +147,13 @@ public static string SetBesiegerCampSiegeStrategy(List<string> args)
/// Sets the leader party field of a specific besieger camp.
/// </summary>
/// <param name="args">besiegerCampId and the partyId to set</param>
/// <returns>information if it changed</returns>
[CommandLineArgumentFunction("set_leader_party", "coop.debug.besiegerCamp")]
/// <returns>Result of the operation as a string</returns>
[CommandLineArgumentFunction("set_leader_party", "coop.debug.besiegercamp")]
public static string SetBesiegerCampLeaderParty(List<string> args)
{
if (args.Count != 2)
{
return "Usage: coop.debug.besiegerCamp.set_leader_party <besiegerCampId> <partyId> ";
return "Usage: coop.debug.besiegercamp.set_leader_party <besiegerCampId> <partyId> ";
}

string besiegerCampId = args[0];
Expand All @@ -178,4 +178,84 @@ public static string SetBesiegerCampLeaderParty(List<string> args)

return $"{nameof(BesiegerCamp._leaderParty)} has changed to: {camp._leaderParty.Name} party with ID: {camp._leaderParty.StringId}";
}


// coop.debug.besiegercamp.add_party
/// <summary>
/// Adds a party as a besieger party to a besieger camp.
/// </summary>
/// <param name="args">besiegerCampId and the partyId to add</param>
/// <returns>Result of the operation as a string</returns>
[CommandLineArgumentFunction("add_besiegerparty", "coop.debug.besiegercamp")]
public static string AddPartyToBesiegerCamp(List<string> args)
{
if (args.Count != 2)
{
return "Usage: coop.debug.besiegercamp.add_party <besiegerCampId> <partyId>";
}

string besiegerCampId = args[0];
string partyId = args[1];

if (!TryGetObjectManager(out var objectManager))
{
return "Unable to resolve ObjectManager";
}

if (!objectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var besiegerCamp))
{
return $"BesiegerCamp with ID: '{besiegerCampId}' not found";
}

if (!objectManager.TryGetObject<MobileParty>(partyId, out var mobileParty))
{
return $"MobileParty with ID: '{partyId}' not found";
}

besiegerCamp._besiegerParties.Add(mobileParty);

return $"MobileParty {partyId} added to BesiegerCamp {besiegerCampId}";
}

// coop.debug.besiegercamp.remove_party
/// <summary>
/// Removes a besieger party from a besieger camp.
/// </summary>
/// <param name="args">besiegerCampId and the partyId to remove</param>
/// <returns>Result of the operation as a string</returns>
[CommandLineArgumentFunction("remove_party", "coop.debug.besiegercamp")]
public static string RemovePartyFromBesiegerCamp(List<string> args)
{
if (args.Count != 2)
{
return "Usage: coop.debug.besiegercamp.remove_party <besiegerCampId> <partyId>";
}

string besiegerCampId = args[0];
string partyId = args[1];

if (!TryGetObjectManager(out var objectManager))
{
return "Unable to resolve ObjectManager";
}

if (!objectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var besiegerCamp))
{
return $"BesiegerCamp with ID: '{besiegerCampId}' not found";
}

if (!objectManager.TryGetObject<MobileParty>(partyId, out var mobileParty))
{
return $"MobileParty with ID: '{partyId}' not found";
}

if (!besiegerCamp._besiegerParties.Remove(mobileParty))
{
return $"MobileParty {partyId} not found in BesiegerCamp {besiegerCampId}";
}

return $"MobileParty {partyId} removed from BesiegerCamp {besiegerCampId}";
}


}

0 comments on commit 63c3acf

Please sign in to comment.