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

UI Range Check #702

Merged
merged 1 commit into from
Nov 20, 2018
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
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using SS14.Shared.GameObjects;
using SS14.Shared.GameObjects.Components.UserInterface;
Expand Down Expand Up @@ -61,7 +61,7 @@ public override void HandleMessage(ComponentMessage message, INetChannel netChan
break;

case CloseBoundInterfaceMessage _:
Close(wrapped.UiKey);
Close(wrapped.UiKey, true);
break;

default:
Expand All @@ -86,15 +86,16 @@ private void OpenInterface(BoundInterfaceMessageWrapMessage wrapped)
boundInterface.Open();
_openInterfaces[wrapped.UiKey] = boundInterface;
}

internal void Close(object uiKey)
internal void Close(object uiKey, bool remoteCall)
{
if (!_openInterfaces.TryGetValue(uiKey, out var boundUserInterface))
{
return;
}

SendMessage(new CloseBoundInterfaceMessage(), uiKey);
if(!remoteCall)
SendMessage(new CloseBoundInterfaceMessage(), uiKey);
_openInterfaces.Remove(uiKey);
boundUserInterface.Dispose();
}
Expand Down Expand Up @@ -151,7 +152,7 @@ protected virtual void ReceiveMessage(BoundUserInterfaceMessage message)
/// </summary>
protected void Close()
{
Owner.Close(UiKey);
Owner.Close(UiKey, false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using SS14.Server.Interfaces.Player;
using SS14.Shared.Enums;
Expand All @@ -22,6 +22,11 @@ public sealed class ServerUserInterfaceComponent : SharedUserInterfaceComponent
private readonly Dictionary<object, BoundUserInterface> _interfaces =
new Dictionary<object, BoundUserInterface>();

/// <summary>
/// Enumeration of all the interfaces this component provides.
/// </summary>
public IEnumerable<BoundUserInterface> Interfaces => _interfaces.Values;

public override void ExposeData(ObjectSerializer serializer)
{
base.ExposeData(serializer);
Expand Down Expand Up @@ -95,6 +100,11 @@ public sealed class BoundUserInterface
private readonly HashSet<IPlayerSession> _subscribedSessions = new HashSet<IPlayerSession>();
private BoundUserInterfaceState _lastState;

/// <summary>
/// All of the sessions currently subscribed to this UserInterface.
/// </summary>
public IEnumerable<IPlayerSession> SubscribedSessions => _subscribedSessions;

public event Action<BoundUserInterfaceMessage> OnReceiveMessage;

public BoundUserInterface(object uiKey, ServerUserInterfaceComponent owner)
Expand Down Expand Up @@ -172,7 +182,7 @@ public void Close(IPlayerSession session)
throw new ArgumentNullException(nameof(session));
}

if (_subscribedSessions.Contains(session))
if (!_subscribedSessions.Contains(session))
{
return;
}
Expand Down
74 changes: 74 additions & 0 deletions SS14.Server/GameObjects/EntitySystems/UserInterfaceSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System.Collections.Generic;
using SS14.Server.GameObjects.Components.UserInterface;
using SS14.Server.Interfaces.Player;
using SS14.Shared.GameObjects;
using SS14.Shared.GameObjects.Systems;
using SS14.Shared.Interfaces.GameObjects.Components;

namespace SS14.Server.GameObjects.EntitySystems
{
internal class UserInterfaceSystem : EntitySystem
{
private const float MaxWindowRange = 2;
private const float MaxWindowRangeSquared = MaxWindowRange * MaxWindowRange;

private readonly List<IPlayerSession> _sessionCache = new List<IPlayerSession>();

/// <inheritdoc />
public override void Initialize()
{
EntityQuery = new TypeEntityQuery(typeof(ServerUserInterfaceComponent));
}

/// <inheritdoc />
public override void Update(float frameTime)
{
foreach (var entity in RelevantEntities)
{
var uiComp = entity.GetComponent<ServerUserInterfaceComponent>();

CheckRange(entity.Transform, uiComp);
}
}

/// <summary>
/// Verify that the subscribed clients are still in range of the entity.
/// </summary>
/// <param name="transformComp">Transform Component of the entity being checked.</param>
/// <param name="uiComp">UserInterface Component of entity being checked.</param>
private void CheckRange(ITransformComponent transformComp, ServerUserInterfaceComponent uiComp)
{
foreach (var ui in uiComp.Interfaces)
{
// We have to cache the set of sessions because Unsubscribe modifies the original.
_sessionCache.Clear();
_sessionCache.AddRange(ui.SubscribedSessions);

if (_sessionCache.Count == 0)
continue;

var uiPos = transformComp.WorldPosition;
var uiMap = transformComp.MapID;

foreach (var session in _sessionCache)
{
var attachedEntity = session.AttachedEntity;

// The component manages the set of sessions, so this invalid session should be removed soon.
if (attachedEntity == null || !attachedEntity.IsValid())
continue;

if (uiMap != attachedEntity.Transform.MapID)
{
ui.Close(session);
continue;
}

var distanceSquared = (uiPos - attachedEntity.Transform.WorldPosition).LengthSquared;
if (distanceSquared > MaxWindowRangeSquared)
ui.Close(session);
}
}
}
}
}
3 changes: 2 additions & 1 deletion SS14.Server/SS14.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
<Compile Include="GameObjects\Components\UserInterface\ServerUserInterfaceComponent.cs" />
<Compile Include="GameObjects\EntitySystems\InputSystem.cs" />
<Compile Include="GameObjects\EntitySystems\MoverSystem.cs" />
<Compile Include="GameObjects\EntitySystems\UserInterfaceSystem.cs" />
<Compile Include="Interfaces\Player\IPlayerData.cs" />
<Compile Include="Player\PlayerData.cs" />
<Compile Include="Prototypes\ServerPrototypeManager.cs" />
Expand Down Expand Up @@ -210,4 +211,4 @@
<PostBuildEvent>
</PostBuildEvent>
</PropertyGroup>
</Project>
</Project>