Add do_after to TryInsert (#1938)

Add delay to flushing

Co-authored-by: Julian Giebel <j.giebel@netrocks.info>
This commit is contained in:
Julian Giebel
2020-08-28 09:31:17 +02:00
committed by GitHub
parent 7b12d4e08c
commit c09c9176ef
2 changed files with 55 additions and 17 deletions

View File

@@ -22,14 +22,17 @@ namespace Content.IntegrationTests.Tests.Disposal
{ {
foreach (var entity in entities) foreach (var entity in entities)
{ {
var insertTask = unit.TryInsert(entity);
Assert.That(unit.CanInsert(entity), Is.EqualTo(result)); Assert.That(unit.CanInsert(entity), Is.EqualTo(result));
Assert.That(unit.TryInsert(entity), Is.EqualTo(result)); insertTask.ContinueWith(task =>
if (result)
{ {
// Not in a tube yet Assert.That(task.Result, Is.EqualTo(result));
Assert.That(entity.Transform.Parent == unit.Owner.Transform); if (result)
} {
// Not in a tube yet
Assert.That(entity.Transform.Parent, Is.EqualTo(unit.Owner.Transform));
}
});
} }
} }

View File

@@ -7,6 +7,7 @@ using System.Threading.Tasks;
using Content.Server.GameObjects.Components.GUI; using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage; using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.GameObjects.Components.Power.ApcNetComponents; using Content.Server.GameObjects.Components.Power.ApcNetComponents;
using Content.Server.GameObjects.EntitySystems.DoAfter;
using Content.Server.Interfaces; using Content.Server.Interfaces;
using Content.Server.Interfaces.GameObjects.Components.Items; using Content.Server.Interfaces.GameObjects.Components.Items;
using Content.Server.Utility; using Content.Server.Utility;
@@ -68,6 +69,12 @@ namespace Content.Server.GameObjects.Components.Disposal
[ViewVariables] [ViewVariables]
private TimeSpan _automaticEngageTime; private TimeSpan _automaticEngageTime;
[ViewVariables]
private TimeSpan _flushDelay;
[ViewVariables]
private float _entryDelay;
/// <summary> /// <summary>
/// Token used to cancel the automatic engage of a disposal unit /// Token used to cancel the automatic engage of a disposal unit
/// after an entity enters it. /// after an entity enters it.
@@ -174,13 +181,34 @@ namespace Content.Server.GameObjects.Components.Disposal
UpdateVisualState(); UpdateVisualState();
} }
public bool TryInsert(IEntity entity) public async Task<bool> TryInsert(IEntity entity, IEntity? user = default)
{ {
if (!CanInsert(entity) || !_container.Insert(entity)) if (!CanInsert(entity))
{
return false; return false;
if (user != null && _entryDelay > 0f)
{
var doAfterSystem = EntitySystem.Get<DoAfterSystem>();
var doAfterArgs = new DoAfterEventArgs(user, _entryDelay, default, Owner)
{
BreakOnDamage = true,
BreakOnStun = true,
BreakOnTargetMove = true,
BreakOnUserMove = true,
NeedHand = false,
};
var result = await doAfterSystem.DoAfter(doAfterArgs);
if (result == DoAfterStatus.Cancelled)
return false;
} }
if (!_container.Insert(entity))
return false;
AfterInsert(entity); AfterInsert(entity);
return true; return true;
@@ -225,9 +253,9 @@ namespace Content.Server.GameObjects.Components.Disposal
{ {
Engaged ^= true; Engaged ^= true;
if (Engaged) if (Engaged && CanFlush())
{ {
TryFlush(); Timer.Spawn(_flushDelay, () => TryFlush());
} }
} }
@@ -487,10 +515,16 @@ namespace Content.Server.GameObjects.Components.Disposal
() => (int) _automaticEngageTime.TotalSeconds); () => (int) _automaticEngageTime.TotalSeconds);
serializer.DataReadWriteFunction( serializer.DataReadWriteFunction(
"automaticEngageTime", "flushDelay",
30, 3,
seconds => _automaticEngageTime = TimeSpan.FromSeconds(seconds), seconds => _flushDelay = TimeSpan.FromSeconds(seconds),
() => (int) _automaticEngageTime.TotalSeconds); () => (int) _flushDelay.TotalSeconds);
serializer.DataReadWriteFunction(
"entryDelay",
0.5f,
seconds => _entryDelay = seconds,
() => (int) _entryDelay);
} }
public override void Initialize() public override void Initialize()
@@ -620,7 +654,8 @@ namespace Content.Server.GameObjects.Components.Disposal
bool IDragDropOn.DragDropOn(DragDropEventArgs eventArgs) bool IDragDropOn.DragDropOn(DragDropEventArgs eventArgs)
{ {
return TryInsert(eventArgs.Dropped); _ = TryInsert(eventArgs.Dropped, eventArgs.User);
return true;
} }
[Verb] [Verb]
@@ -642,7 +677,7 @@ namespace Content.Server.GameObjects.Components.Disposal
protected override void Activate(IEntity user, DisposalUnitComponent component) protected override void Activate(IEntity user, DisposalUnitComponent component)
{ {
component.TryInsert(user); _ = component.TryInsert(user, user);
} }
} }