openac-mosswartmassacre/tests/OpenAC.MosswartMassacre.Tests/Fakes/FakeNetwork.cs

92 lines
3.2 KiB
C#

using AcDream.Plugin.Abstractions;
namespace OpenAC.MosswartMassacre.Tests.Fakes;
/// <summary>
/// A peer network that records what a plugin imports and hands back the casts
/// this client announced.
/// </summary>
public sealed class FakeNetwork : INetworkAutomation
{
public bool SupportsSubscriptions => true;
public List<PluginPeerCapabilities> Requests { get; } = [];
public int ActiveLeases { get; private set; }
public IDisposable Subscribe(PluginPeerCapabilities capabilities)
{ Requests.Add(capabilities); ActiveLeases++; return new Lease(this); }
private sealed class Lease(FakeNetwork owner) : IDisposable
{
private bool _disposed;
public void Dispose() { if (_disposed) return; _disposed = true; owner.ActiveLeases--; }
}
public bool IsAvailable { get; set; } = true;
public List<PluginNetworkClient> ImportedClients { get; } = [];
public List<(uint Caster, uint Target, uint Spell, int Skill, double Seconds, bool Landed)> ImportedCasts { get; } = [];
/// <summary>What this client announced, as <see cref="CaptureOwnCasts"/> reports it.</summary>
public List<PluginPeerCast> OwnCasts { get; } = [];
/// <summary>Whether an import is accepted.</summary>
public bool AcceptImports { get; set; } = true;
public IReadOnlyList<PluginNetworkClient> CaptureClients() => ImportedClients;
public IReadOnlyList<PluginPeerCast> CaptureOwnCasts(long afterSequence) =>
[.. OwnCasts.Where(cast => cast.Sequence > afterSequence)];
public bool ImportRemoteClient(PluginNetworkClient client)
{
ImportedClients.Add(client);
return AcceptImports;
}
public bool ImportRemoteCast(
uint casterObjectId,
uint targetObjectId,
uint spellId,
int effectiveSkill,
double secondsRemaining,
bool landed)
{
ImportedCasts.Add((casterObjectId, targetObjectId, spellId, effectiveSkill, secondsRemaining, landed));
return AcceptImports;
}
}
/// <summary>A dungeon-map surface whose sealed landblocks a test names.</summary>
public sealed class FakeDungeonMap : IDungeonMapAutomation
{
public HashSet<uint> SealedLandblocks { get; } = [];
public bool IsSealedDungeon(uint cellId) =>
(cellId & 0xFFFFu) >= 0x100u && SealedLandblocks.Contains(cellId & 0xFFFF0000u);
}
/// <summary>An allegiance surface a test fills in.</summary>
public sealed class FakeAllegiance : IAllegianceAutomation
{
public bool IsAvailable { get; set; } = true;
public PluginAllegianceSnapshot Snapshot { get; set; }
}
/// <summary>A status board whose lines a test sets.</summary>
public sealed class FakeStatusBoard : IPluginStatusBoard
{
public Dictionary<(string PluginId, string Key), string> Lines { get; } = [];
public bool IsAvailable => true;
public bool Publish(string key, string? value)
{
if (value is null)
Lines.Remove(("openac.mosswartmassacre", key));
else
Lines[("openac.mosswartmassacre", key)] = value;
return true;
}
public bool TryRead(string pluginId, string key, out string value) =>
Lines.TryGetValue((pluginId, key), out value!);
}