Skip to content
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

Sync SiegeEvent fields/props #999

Closed
wants to merge 8 commits into from
Closed
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
161 changes: 161 additions & 0 deletions source/E2E.Tests/Services/SiegeEvents/SiegeEventFieldTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
using E2E.Tests.Environment;
using E2E.Tests.Environment.Instance;
using E2E.Tests.Util;
using HarmonyLib;
using System.Reflection;
using Xunit.Abstractions;
using static Common.Extensions.ReflectionExtensions;
using Common.Util;
using TaleWorlds.CampaignSystem.Siege;
using TaleWorlds.CampaignSystem.Settlements;

namespace E2E.Tests.Services.SiegeEvents;

public class SiegeEventFieldTests : IDisposable
{
private readonly List<MethodBase> disabledMethods;
private E2ETestEnvironment TestEnvironment { get; }
private EnvironmentInstance Server => TestEnvironment.Server;
private IEnumerable<EnvironmentInstance> Clients => TestEnvironment.Clients;
private IEnumerable<EnvironmentInstance> AllEnvironmentInstances => Clients.Append(Server);

private readonly string siegeEventId;

public SiegeEventFieldTests(ITestOutputHelper output)
{
TestEnvironment = new E2ETestEnvironment(output);

disabledMethods = new List<MethodBase> {
//Add your disabled methods
};

// Create SiegeEvent on the server
siegeEventId = TestEnvironment.CreateRegisteredObject<SiegeEvent>();

// Create SiegeEvent on all clients
foreach (var client in Clients)
{
var clientSiegeEvent = ObjectHelper.SkipConstructor<SiegeEvent>();
Assert.True(client.ObjectManager.AddExisting(siegeEventId, clientSiegeEvent));
}
}

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


[Fact]
public void ServerChangeSiegeEventBesiegedSettlement_SyncAllClients()
{
// Arrange
var field = AccessTools.Field(typeof(SiegeEvent), nameof(SiegeEvent.BesiegedSettlement));
var intercept = TestEnvironment.GetIntercept(field);

/// Create instances on server
Assert.True(Server.ObjectManager.AddNewObject(ObjectHelper.SkipConstructor<Settlement>(), out var besiegedSettlementId));

/// Create instances on all clients
foreach (var client in Clients)
{
var clientBesiegedSettlement = ObjectHelper.SkipConstructor<Settlement>();
Assert.True(client.ObjectManager.AddExisting(besiegedSettlementId, clientBesiegedSettlement));
}

// Act
Server.Call(() =>
{
Assert.True(Server.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var SiegeEvent));
Assert.True(Server.ObjectManager.TryGetObject<Settlement>(besiegedSettlementId, out var serverBesiegedSettlement));

Assert.Null(SiegeEvent.BesiegedSettlement);

/// Simulate the field changing
intercept.Invoke(null, new object[] { SiegeEvent, serverBesiegedSettlement});

Assert.Same(serverBesiegedSettlement, SiegeEvent.BesiegedSettlement);
});

// Assert
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var SiegeEvent));

Assert.True(client.ObjectManager.TryGetObject<Settlement>(besiegedSettlementId, out var clientBesiegedSettlement));

Assert.True(clientBesiegedSettlement == SiegeEvent.BesiegedSettlement);
}
}

[Fact]
public void ServerChangeSiegeEventBesiegerCamp_SyncAllClients()
{
// Arrange
var field = AccessTools.Field(typeof(SiegeEvent), nameof(SiegeEvent.BesiegerCamp));
var intercept = TestEnvironment.GetIntercept(field);

/// Create instances on server
Assert.True(Server.ObjectManager.AddNewObject(ObjectHelper.SkipConstructor<BesiegerCamp>(), out var besiegerCampId));

/// Create instances on all clients
foreach (var client in Clients)
{
var clientBesiegerCamp = ObjectHelper.SkipConstructor<BesiegerCamp>();
Assert.True(client.ObjectManager.AddExisting(besiegerCampId, clientBesiegerCamp));
}

// Act
Server.Call(() =>
{
Assert.True(Server.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var SiegeEvent));
Assert.True(Server.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var serverBesiegerCamp));

Assert.Null(SiegeEvent.BesiegerCamp);

/// Simulate the field changing
intercept.Invoke(null, new object[] { SiegeEvent, serverBesiegerCamp});

Assert.Same(serverBesiegerCamp, SiegeEvent.BesiegerCamp);
});

// Assert
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var SiegeEvent));

Assert.True(client.ObjectManager.TryGetObject<BesiegerCamp>(besiegerCampId, out var clientBesiegerCamp));

Assert.True(clientBesiegerCamp == SiegeEvent.BesiegerCamp);
}
}


[Fact]
public void ServerChangeSiegeEventIsBesiegerDefeated_SyncAllClients()
{
// Arrange
var field = AccessTools.Field(typeof(BesiegerCamp), nameof(BesiegerCamp._leaderParty));
var intercept = TestEnvironment.GetIntercept(field);
Assert.True(Server.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var serverSiegeEvent));
var newValue=Random<Boolean>();

// Act
Server.Call(() =>
{
/// Simulate the field changing
intercept.Invoke(null, new object[] { serverSiegeEvent, newValue });

Assert.Same(newValue, serverSiegeEvent._isBesiegerDefeated);
});

// Assert
foreach (var client in TestEnvironment.Clients)
{
Assert.True(client.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var clientSiegeEvent));
Assert.Equal(serverSiegeEvent._isBesiegerDefeated, clientSiegeEvent._isBesiegerDefeated);
}
}
}


77 changes: 29 additions & 48 deletions source/E2E.Tests/Services/SiegeEvents/SiegeEventLifetimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,52 @@
using E2E.Tests.Environment;
using E2E.Tests.Environment;
using E2E.Tests.Environment.Instance;
using E2E.Tests.Util;
using HarmonyLib;
using Common.Util;
using System.Reflection;
using TaleWorlds.CampaignSystem.MapEvents;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Siege;
using Xunit.Abstractions;
using static Common.Extensions.ReflectionExtensions;
using TaleWorlds.CampaignSystem.Siege;

namespace E2E.Tests.Services.SiegeEvents;

public class SiegeEventLifetimeTests : IDisposable
{
E2ETestEnvironment TestEnvironment { get; }
private readonly List<MethodBase> disabledMethods;
private E2ETestEnvironment TestEnvironment { get; }
private EnvironmentInstance Server => TestEnvironment.Server;
private IEnumerable<EnvironmentInstance> Clients => TestEnvironment.Clients;
private IEnumerable<EnvironmentInstance> AllEnvironmentInstances => Clients.Append(Server);

private List<MethodBase> disabledMethods;
private readonly string siegeEventId;

public SiegeEventLifetimeTests(ITestOutputHelper output)
{
TestEnvironment = new E2ETestEnvironment(output);

disabledMethods = new List<MethodBase>
{
AccessTools.Method(typeof(MobileParty), nameof(MobileParty.OnPartyJoinedSiegeInternal)),
disabledMethods = new List<MethodBase> {
//Add your disabled methods
};

disabledMethods.AddRange(AccessTools.GetDeclaredConstructors(typeof(SiegeEvent)));

}

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

[Fact]
public void ServerCreate_SiegeEvent_SyncAllClients()
[Fact]
public void ServerCreateSiegeEvent_SyncAllClients()
{
// Arrange
var server = TestEnvironment.Server;
string? siegeEventId = null;

// Act
string? siegeEventId = null;
server.Call((Action)(() =>
Server.Call(() =>
{
var siegeEvent = GameObjectCreator.CreateInitializedObject<SiegeEvent>();

Assert.True(server.ObjectManager.TryGetId(siegeEvent, out siegeEventId));
}),
disabledMethods: disabledMethods);
Assert.True(Server.ObjectManager.TryGetId(siegeEvent, out siegeEventId));
}, disabledMethods
);

// Assert
Assert.NotNull(siegeEventId);
Expand All @@ -59,41 +58,23 @@ public void ServerCreate_SiegeEvent_SyncAllClients()
}

[Fact]
public void ClientCreate_SiegeEvent_DoesNothing()
public void ClientCreateSiegeEvent_DoesNothing()
{
// Arrange
var server = TestEnvironment.Server;

string? settlementId = null;
string? mobilePartyId = null;
server.Call(() =>
{
var settlement = GameObjectCreator.CreateInitializedObject<Settlement>();
var mobileParty = GameObjectCreator.CreateInitializedObject<MobileParty>();

Assert.True(server.ObjectManager.TryGetId(settlement, out settlementId));
Assert.True(server.ObjectManager.TryGetId(mobileParty, out mobilePartyId));
});

Assert.NotNull(settlementId);
Assert.NotNull(mobilePartyId);
string? clientSiegeEventId = null;

// Act
string? clientBeseigerCampId = null;

var firstClient = TestEnvironment.Clients.First();
firstClient.Call(() =>
{
Assert.True(firstClient.ObjectManager.TryGetObject<Settlement>(settlementId, out var settlement));
Assert.True(firstClient.ObjectManager.TryGetObject<MobileParty>(mobilePartyId, out var mobileParty));

var SiegeEvent = new SiegeEvent(settlement, mobileParty);
var SiegeEvent = ObjectHelper.SkipConstructor<SiegeEvent>();

Assert.False(firstClient.ObjectManager.TryGetId(SiegeEvent, out clientBeseigerCampId));
},
disabledMethods: disabledMethods);
Assert.False(firstClient.ObjectManager.TryGetId(SiegeEvent, out clientSiegeEventId));
});

// Assert
Assert.Null(clientBeseigerCampId);
Assert.Null(clientSiegeEventId);
}
}


70 changes: 70 additions & 0 deletions source/E2E.Tests/Services/SiegeEvents/SiegeEventPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using E2E.Tests.Environment;
using E2E.Tests.Environment.Instance;
using E2E.Tests.Util;
using HarmonyLib;
using System.Reflection;
using Xunit.Abstractions;
using Common.Util;
using static Common.Extensions.ReflectionExtensions;
using TaleWorlds.CampaignSystem.Siege;
using TaleWorlds.CampaignSystem;

namespace E2E.Tests.Services.SiegeEvents;

public class SiegeEventPropertyTests : IDisposable
{
private readonly List<MethodBase> disabledMethods;
private E2ETestEnvironment TestEnvironment { get; }
private EnvironmentInstance Server => TestEnvironment.Server;
private IEnumerable<EnvironmentInstance> Clients => TestEnvironment.Clients;
private IEnumerable<EnvironmentInstance> AllEnvironmentInstances => Clients.Append(Server);

private readonly string siegeEventId;

public SiegeEventPropertyTests(ITestOutputHelper output)
{
TestEnvironment = new E2ETestEnvironment(output);

disabledMethods = new List<MethodBase> {
//Add your disabled methods
};

// Create SiegeEvent on the server
siegeEventId = TestEnvironment.CreateRegisteredObject<SiegeEvent>();

// Create SiegeEvent on all clients
foreach (var client in Clients)
{
var clientSiegeEvent = ObjectHelper.SkipConstructor<SiegeEvent>();
Assert.True(client.ObjectManager.AddExisting(siegeEventId, clientSiegeEvent));
}
}

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


[Fact]
public void ServerChangeSiegeEventSiegeStartTime_SyncAllClients()
{
// Arrange
Assert.True(Server.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var serverSiegeEvent));
var newValue=Random<CampaignTime>();

// Act
Server.Call(() =>
{
serverSiegeEvent.SiegeStartTime = newValue;
});

// Assert
foreach (var client in TestEnvironment.Clients)
{
Assert.True(client.ObjectManager.TryGetObject<SiegeEvent>(siegeEventId, out var clientSiegeEvent));
Assert.Equal(serverSiegeEvent.SiegeStartTime, clientSiegeEvent.SiegeStartTime);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.ObjectSystem;
Expand All @@ -10,6 +11,7 @@ namespace GameInterface.Services.Settlements;
internal class SettlementRegistry : RegistryBase<Settlement>
{
public static readonly string SettlementStringIdPrefix = "CoopSettlement";
private static int SettlementCounter = 0;

public SettlementRegistry(IRegistryCollection collection) : base(collection) { }

Expand Down Expand Up @@ -40,8 +42,7 @@ public override bool RegisterExistingObject(string id, object obj)

protected override string GetNewId(Settlement settlement)
{
settlement.StringId = Campaign.Current.CampaignObjectManager.FindNextUniqueStringId<Settlement>(SettlementStringIdPrefix);
return settlement.StringId;
return $"{SettlementStringIdPrefix}_{Interlocked.Increment(ref SettlementCounter)}";
}

private void AddToCampaignObjectManager(object obj)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
using Common;
using Common.Messaging;
using Common.Messaging;
using Common.Network;
using Common.Util;
using GameInterface.Services.ObjectManager;
using GameInterface.Services.Sieges.Messages;
using TaleWorlds.CampaignSystem.Party;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Siege;

namespace GameInterface.Services.Sieges.Handlers;
Expand Down
Loading
Loading