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

Hideout sync #1074

Merged
merged 3 commits into from
Jan 26, 2025
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
85 changes: 85 additions & 0 deletions source/E2E.Tests/Services/Hideouts/HideoutSyncTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using E2E.Tests.Environment;
using E2E.Tests.Environment.Instance;
using E2E.Tests.Util;
using HarmonyLib;
using TaleWorlds.CampaignSystem;
using TaleWorlds.CampaignSystem.Settlements;
using TaleWorlds.CampaignSystem.Settlements.Buildings;
using Xunit.Abstractions;

namespace E2E.Tests.Services.Hideouts
{
public class HideoutSyncTests : IDisposable
{
E2ETestEnvironment TestEnvironment { get; }

EnvironmentInstance Server => TestEnvironment.Server;

IEnumerable<EnvironmentInstance> Clients => TestEnvironment.Clients;

private readonly string HideoutId;

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

var hideout = GameObjectCreator.CreateInitializedObject<Hideout>();

// Create objects on the server
Assert.True(Server.ObjectManager.AddNewObject(hideout, out HideoutId));

// Create objects on all clients
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.AddExisting(HideoutId, hideout));
}
}

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

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

var IsSpottedField = AccessTools.Field(typeof(Hideout), nameof(Hideout._isSpotted));
var NextPossibleAttackField = AccessTools.Field(typeof(Hideout), nameof(Hideout._nextPossibleAttackTime));

// Get field intercept to use on the server to simulate the field changing
var IsSpottedIntercept = TestEnvironment.GetIntercept(IsSpottedField);
var NextPossibleAttackIntercept = TestEnvironment.GetIntercept(NextPossibleAttackField);

// Act
server.Call(() =>
{
Assert.True(server.ObjectManager.TryGetObject<Hideout>(HideoutId, out var serverHideout));

// Simulate the field changing
IsSpottedIntercept.Invoke(null, new object[] { serverHideout, true });
NextPossibleAttackIntercept.Invoke(null, new object[] { serverHideout, new CampaignTime(99) });

serverHideout.SceneName = "testScene";

Assert.True(serverHideout._isSpotted);
Assert.Equal(99, serverHideout._nextPossibleAttackTime._numTicks);

Assert.Equal("testScene", serverHideout.SceneName);
});

// Assert
foreach (var client in Clients)
{
Assert.True(client.ObjectManager.TryGetObject<Hideout>(HideoutId, out var clientHideout));

Assert.Equal(99, clientHideout._nextPossibleAttackTime._numTicks);
Assert.True(clientHideout._isSpotted);

Assert.Equal("testScene", clientHideout.SceneName);
}
}
}
}
17 changes: 17 additions & 0 deletions source/GameInterface/Services/Hideouts/HideoutSync.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using GameInterface.AutoSync;
using HarmonyLib;
using TaleWorlds.CampaignSystem.Settlements;

namespace GameInterface.Services.Hideouts
{
internal class HideoutSync : IAutoSync
{
public HideoutSync(IAutoSyncBuilder autoSyncBuilder)
{
autoSyncBuilder.AddProperty(AccessTools.Property(typeof(Hideout), nameof(Hideout.SceneName)));

autoSyncBuilder.AddField(AccessTools.Field(typeof(Hideout), nameof(Hideout._isSpotted)));
autoSyncBuilder.AddField(AccessTools.Field(typeof(Hideout), nameof(Hideout._nextPossibleAttackTime)));
}
}
}
Loading