Skip to content

Commit

Permalink
NanoChat Bugfix (#212)
Browse files Browse the repository at this point in the history
* Update NanoChatCartridgeSystem.cs

* Update NanoChatCardComponent.cs

* Update SharedNanoChatSystem.cs

* Update SharedNanoChatSystem.cs

* Update NanoChatCartridgeSystem.cs

* Sending PDA messages is map based

* Update NanoChatCartridgeSystem.cs
  • Loading branch information
Vonsant authored Jan 3, 2025
1 parent 5c5f03a commit b00335e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Content.Shared._CorvaxNext.NanoChat;
using Content.Shared.PDA;
using Content.Shared.Radio.Components;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;

Expand All @@ -24,6 +25,7 @@ public sealed class NanoChatCartridgeSystem : EntitySystem
[Dependency] private readonly IGameTiming _timing = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly SharedNanoChatSystem _nanoChat = default!;
[Dependency] private readonly SharedUserInterfaceSystem _ui = default!;
[Dependency] private readonly StationSystem _station = default!;

// Messages in notifications get cut off after this point
Expand All @@ -38,6 +40,20 @@ public override void Initialize()
SubscribeLocalEvent<NanoChatCartridgeComponent, CartridgeMessageEvent>(OnMessage);
}

private void UpdateClosed(Entity<NanoChatCartridgeComponent> ent)
{
if (!TryComp<CartridgeComponent>(ent, out var cartridge) ||
cartridge.LoaderUid is not {} pda ||
!TryComp<CartridgeLoaderComponent>(pda, out var loader) ||
!GetCardEntity(pda, out var card))
{
return;
}

// if you switch to another program or close the pda UI, allow notifications for the selected chat
_nanoChat.SetClosed((card, card.Comp), loader.ActiveProgram != ent.Owner || !_ui.IsUiOpen(pda, PdaUiKey.Key));
}

public override void Update(float frameTime)
{
base.Update(frameTime);
Expand All @@ -49,6 +65,9 @@ public override void Update(float frameTime)
if (cartridge.LoaderUid == null)
continue;

// keep it up to date without handling ui open/close events on the pda or adding code when changing active program
UpdateClosed((uid, nanoChat));

// Check if we need to update our card reference
if (!TryComp<PdaComponent>(cartridge.LoaderUid, out var pda))
continue;
Expand Down Expand Up @@ -239,7 +258,7 @@ private void HandleSendMessage(Entity<NanoChatCartridgeComponent> cartridge,

// Log message attempt
var recipientsText = recipients.Count > 0
? string.Join(", ", recipients.Select(r => ToPrettyString(r)))
? string.Join(", ", recipients.Select((Entity<NanoChatCardComponent> r) => ToPrettyString(r)))
: $"#{msg.RecipientNumber:D4}";

_adminLogger.Add(LogType.Chat,
Expand Down Expand Up @@ -312,21 +331,19 @@ private bool EnsureRecipientExists(Entity<NanoChatCardComponent> card, uint reci
if (receiverCart.Card != recipient.Owner)
continue;

// Check if devices are on same station/map
var recipientStation = _station.GetOwningStation(receiverUid);
var senderStation = _station.GetOwningStation(sender);

// Both entities must be on a station
if (recipientStation == null || senderStation == null)
continue;
// Check if devices are on same map
var recipientMap = Transform(receiverUid).MapID;
var senderMap = Transform(sender).MapID;

// Must be on the same map/station unless long-range is allowed
if (!channel.LongRange && recipientMap != senderMap)
{
break;
}

// Must be on same map/station unless long range allowed
if (!channel.LongRange && recipientStation != senderStation)
continue;

// Needs telecomms
if (!HasActiveServer(senderStation.Value) || !HasActiveServer(recipientStation.Value))
continue;
/* Must have an active common server
if (HasActiveServer(senderMap))
continue;*/

// Check if recipient can receive
var receiveAttemptEv = new RadioReceiveAttemptEvent(channel, sender, receiverUid);
Expand All @@ -346,15 +363,15 @@ private bool EnsureRecipientExists(Entity<NanoChatCardComponent> card, uint reci
/// <summary>
/// Checks if there are any active telecomms servers on the given station
/// </summary>
private bool HasActiveServer(EntityUid station)
private bool HasActiveServer(MapId mapId)
{
// I have no idea why this isn't public in the RadioSystem
var query =
EntityQueryEnumerator<TelecomServerComponent, EncryptionKeyHolderComponent, ApcPowerReceiverComponent>();

while (query.MoveNext(out var uid, out _, out _, out var power))
{
if (_station.GetOwningStation(uid) == station && power.Powered)
if (Transform(uid).MapID == mapId && power.Powered)
return true;
}

Expand All @@ -381,8 +398,7 @@ private void DeliverMessageToRecipient(Entity<NanoChatCardComponent> sender,

_nanoChat.AddMessage((recipient, recipient.Comp), senderNumber.Value, message with { DeliveryFailed = false });


if (_nanoChat.GetCurrentChat((recipient, recipient.Comp)) != senderNumber)
if (recipient.Comp.IsClosed || _nanoChat.GetCurrentChat((recipient, recipient.Comp)) != senderNumber)
HandleUnreadNotification(recipient, message);

var msgEv = new NanoChatMessageReceivedEvent(recipient);
Expand Down
7 changes: 7 additions & 0 deletions Content.Shared/_CorvaxNext/NanoChat/NanoChatCardComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ public sealed partial class NanoChatCardComponent : Component
[DataField, AutoNetworkedField]
public uint? Number;

/// <summary>
/// Whether a PDA has this card's UI closed.
/// Used for notifications.
/// </summary>
[DataField]
public bool IsClosed;

/// <summary>
/// All chat recipients stored on this card.
/// </summary>
Expand Down
15 changes: 13 additions & 2 deletions Content.Shared/_CorvaxNext/NanoChat/SharedNanoChatSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,24 @@ private void OnExamined(Entity<NanoChatCardComponent> ent, ref ExaminedEvent arg
/// </summary>
public void SetNumber(Entity<NanoChatCardComponent?> card, uint number)
{
if (!Resolve(card, ref card.Comp))
if (!Resolve(card, ref card.Comp) || card.Comp.Number == number)
return;

card.Comp.Number = number;
Dirty(card);
}

/// <summary>
/// Sets IsClosed for a card.
/// </summary>
public void SetClosed(Entity<NanoChatCardComponent?> card, bool closed)
{
if (!Resolve(card, ref card.Comp))
return;

card.Comp.IsClosed = closed;
}

/// <summary>
/// Gets the recipients dictionary from a card.
/// </summary>
Expand Down Expand Up @@ -170,7 +181,7 @@ public bool GetNotificationsMuted(Entity<NanoChatCardComponent?> card)
/// </summary>
public void SetNotificationsMuted(Entity<NanoChatCardComponent?> card, bool muted)
{
if (!Resolve(card, ref card.Comp))
if (!Resolve(card, ref card.Comp) || card.Comp.NotificationsMuted == muted)
return;

card.Comp.NotificationsMuted = muted;
Expand Down

0 comments on commit b00335e

Please sign in to comment.