Skip to content

Commit

Permalink
Merge pull request #728 from fospas/fix_pda_message
Browse files Browse the repository at this point in the history
NanoChatUpdate
  • Loading branch information
CrimeMoot authored Jan 1, 2025
2 parents 84b0740 + 71ed523 commit a3e919c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,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 +39,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 +64,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 @@ -381,8 +399,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/_Cats/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/_Cats/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 a3e919c

Please sign in to comment.