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 #1027

Open
wants to merge 6 commits into
base: development
Choose a base branch
from
Open
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
159 changes: 159 additions & 0 deletions source/E2E.Tests/Services/SiegeEvents/SiegeEventFieldTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
using TaleWorlds.CampaignSystem.Siege;
using TaleWorlds.CampaignSystem.Settlements;
using System;
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.Party;

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>
{
AccessTools.Method(typeof(MobileParty), nameof(MobileParty.OnPartyJoinedSiegeInternal)),
};

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

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

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assert.True(clientBesiegedSettlement == SiegeEvent.BesiegedSettlement);
Assert.Same(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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Assert.True(clientBesiegerCamp == SiegeEvent.BesiegerCamp);
Assert.Same(clientBesiegerCamp == SiegeEvent.BesiegerCamp);

}
}


[Fact]
public void ServerChangeSiegeEventIsBesiegerDefeated_SyncAllClients()
{
// Arrange
var field = AccessTools.Field(typeof(SiegeEvent), nameof(SiegeEvent._isBesiegerDefeated));
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.Equal(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);
}
}
}


71 changes: 27 additions & 44 deletions source/E2E.Tests/Services/SiegeEvents/SiegeEventLifetimeTests.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using E2E.Tests.Environment;
using TaleWorlds.CampaignSystem.Siege;
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.Party;

namespace E2E.Tests.Services.SiegeEvents;

public class SiegeEventLifetimeTests : IDisposable
{
E2ETestEnvironment TestEnvironment { get; }

private List<MethodBase> disabledMethods;
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);

public SiegeEventLifetimeTests(ITestOutputHelper output)
{
Expand All @@ -25,29 +29,26 @@ public SiegeEventLifetimeTests(ITestOutputHelper output)
};

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 +60,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);
}
}


67 changes: 67 additions & 0 deletions source/E2E.Tests/Services/SiegeEvents/SiegeEventPropertyTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using TaleWorlds.CampaignSystem.Siege;
using TaleWorlds.CampaignSystem;
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.Party;

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>
{
AccessTools.Method(typeof(MobileParty), nameof(MobileParty.OnPartyJoinedSiegeInternal)),
};

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

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

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 @@ -4,7 +4,6 @@
using Common.Util;
using GameInterface.Services.ObjectManager;
using GameInterface.Services.Settlements.Messages;
using GameInterface.Services.Sieges.Messages;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Siege;

Expand Down
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
Loading