#nullable enable
using System;
using System.Collections.Generic;
+using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading.Tasks;
using Content.Client.Construction;
using Content.Server.Construction.Components;
+using Content.Server.Power.Components;
using Content.Server.Tools.Components;
using Content.Shared.Construction.Prototypes;
using Content.Shared.Item;
using NUnit.Framework;
+using Robust.Client.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.Log;
using Robust.Shared.Map;
await DeleteHeldEntity();
- if (entity == null)
+ if (entity == null || string.IsNullOrWhiteSpace(entity.Prototype))
{
await RunTicks(1);
Assert.That(Hands.ActiveHandEntity == null);
/// <summary>
/// Place an entity prototype into the players hand and interact with the given entity (or target position)
/// </summary>
- protected async Task Interact(string? id, int quantity = 1, bool shouldSucceed = true, bool awaitDoAfters = true)
- => await Interact(id == null ? null : (id, quantity), shouldSucceed, awaitDoAfters);
+ /// <remarks>
+ /// Empty strings imply empty hands.
+ /// </remarks>
+ protected async Task Interact(string id, int quantity = 1, bool shouldSucceed = true, bool awaitDoAfters = true)
+ => await Interact((id, quantity), shouldSucceed, awaitDoAfters);
/// <summary>
/// Place an entity prototype into the players hand and interact with the given entity (or target position)
/// </summary>
- protected async Task Interact(EntitySpecifier? entity, bool shouldSucceed = true, bool awaitDoAfters = true)
+ /// <remarks>
+ /// Empty strings imply empty hands.
+ /// </remarks>
+ protected async Task Interact(EntitySpecifier entity, bool shouldSucceed = true, bool awaitDoAfters = true)
{
// For every interaction, we will also examine the entity, just in case this breaks something, somehow.
// (e.g., servers attempt to assemble construction examine hints).
/// <summary>
/// Variant of <see cref="InteractUsing"/> that performs several interactions using different entities.
/// </summary>
- protected async Task Interact(params EntitySpecifier?[] specifiers)
+ /// <remarks>
+ /// Empty strings imply empty hands.
+ /// </remarks>
+ protected async Task Interact(params EntitySpecifier[] specifiers)
{
foreach (var spec in specifiers)
{
#region Asserts
- protected void AssertPrototype(string? prototype)
+ protected void AssertPrototype(string? prototype, EntityUid? target = null)
{
- var meta = Comp<MetaDataComponent>();
+ target ??= Target;
+ if (target == null)
+ {
+ Assert.Fail("No target specified");
+ return;
+ }
+
+ var meta = SEntMan.GetComponent<MetaDataComponent>(target.Value);
Assert.That(meta.EntityPrototype?.ID, Is.EqualTo(prototype));
}
- protected void AssertAnchored(bool anchored = true)
+ protected void AssertAnchored(bool anchored = true, EntityUid? target = null)
{
- var sXform = SEntMan.GetComponent<TransformComponent>(Target!.Value);
- var cXform = CEntMan.GetComponent<TransformComponent>(Target.Value);
+ target ??= Target;
+ if (target == null)
+ {
+ Assert.Fail("No target specified");
+ return;
+ }
+
+ var sXform = SEntMan.GetComponent<TransformComponent>(target.Value);
+ var cXform = CEntMan.GetComponent<TransformComponent>(target.Value);
Assert.That(sXform.Anchored, Is.EqualTo(anchored));
Assert.That(cXform.Anchored, Is.EqualTo(anchored));
}
- protected void AssertDeleted(bool deleted = true)
+ protected void AssertDeleted(bool deleted = true, EntityUid? target = null)
{
- Assert.That(SEntMan.Deleted(Target), Is.EqualTo(deleted));
- Assert.That(CEntMan.Deleted(Target), Is.EqualTo(deleted));
+ target ??= Target;
+ if (target == null)
+ {
+ Assert.Fail("No target specified");
+ return;
+ }
+
+ Assert.That(SEntMan.Deleted(target), Is.EqualTo(deleted));
+ Assert.That(CEntMan.Deleted(target), Is.EqualTo(deleted));
}
/// <summary>
/// Assert whether or not the target has the given component.
/// </summary>
- protected void AssertComp<T>(bool hasComp = true)
+ protected void AssertComp<T>(bool hasComp = true, EntityUid? target = null)
{
- Assert.That(SEntMan.HasComponent<T>(Target), Is.EqualTo(hasComp));
+ target ??= Target;
+ if (target == null)
+ {
+ Assert.Fail("No target specified");
+ return;
+ }
+
+ Assert.That(SEntMan.HasComponent<T>(target), Is.EqualTo(hasComp));
}
/// <summary>
#endregion
-
/// <summary>
/// List of currently active DoAfters on the player.
/// </summary>
/// <summary>
/// Convenience method to get components on the target. Returns SERVER-SIDE components.
/// </summary>
- protected T Comp<T>() => SEntMan.GetComponent<T>(Target!.Value);
+ protected T Comp<T>(EntityUid? target = null)
+ {
+ target ??= Target;
+ if (target == null)
+ Assert.Fail("No target specified");
+
+ return SEntMan.GetComponent<T>(target!.Value);
+ }
/// <summary>
/// Set the tile at the target position to some prototype.
protected async Task RunSeconds(float seconds)
=> await RunTicks((int) Math.Ceiling(seconds / TickPeriod));
+
+ #region BUI
+ /// <summary>
+ /// Sends a bui message using the given bui key.
+ /// </summary>
+ protected async Task SendBui(Enum key, BoundUserInterfaceMessage msg, EntityUid? target = null)
+ {
+ if (!TryGetBui(key, out var bui))
+ return;
+
+ await Client.WaitPost(() => bui.SendMessage(msg));
+
+ // allow for client -> server and server -> client messages to be sent.
+ await RunTicks(15);
+ }
+
+ /// <summary>
+ /// Sends a bui message using the given bui key.
+ /// </summary>
+ protected async Task CloseBui(Enum key, EntityUid? target = null)
+ {
+ if (!TryGetBui(key, out var bui))
+ return;
+
+ await Client.WaitPost(() => bui.Close());
+
+ // allow for client -> server and server -> client messages to be sent.
+ await RunTicks(15);
+ }
+
+ protected bool TryGetBui(Enum key, [NotNullWhen(true)] out BoundUserInterface? bui, EntityUid? target = null, bool shouldSucceed = true)
+ {
+ bui = null;
+ target ??= Target;
+ if (target == null)
+ {
+ Assert.Fail("No target specified");
+ return false;
+ }
+
+ if (!CEntMan.TryGetComponent(target, out ClientUserInterfaceComponent? ui))
+ {
+ if (shouldSucceed)
+ Assert.Fail($"Entity {SEntMan.ToPrettyString(target.Value)} does not have a bui component");
+ return false;
+ }
+
+ var first = ui.Interfaces.First();
+
+
+ bui = ui.Interfaces.FirstOrDefault(x => x.UiKey.Equals(key));
+ if (bui == null)
+ {
+ if (shouldSucceed)
+ Assert.Fail($"Entity {SEntMan.ToPrettyString(target.Value)} does not have an open bui with key {key.GetType()}.{key}.");
+ return false;
+ }
+
+ Assert.That(shouldSucceed, Is.True);
+ return true;
+ }
+
+ #endregion
+
+ #region Power
+
+ protected void ToggleNeedPower(EntityUid? target = null)
+ {
+ var comp = Comp<ApcPowerReceiverComponent>(target);
+ comp.NeedsPower = !comp.NeedsPower;
+ }
+
+ #endregion
}