From b00335e9a7fcceb377b9aa817276a4a773776578 Mon Sep 17 00:00:00 2001 From: Kill_Me_I_Noobs <118206719+Vonsant@users.noreply.github.com> Date: Fri, 3 Jan 2025 21:21:21 +0300 Subject: [PATCH] NanoChat Bugfix (#212) * Update NanoChatCartridgeSystem.cs * Update NanoChatCardComponent.cs * Update SharedNanoChatSystem.cs * Update SharedNanoChatSystem.cs * Update NanoChatCartridgeSystem.cs * Sending PDA messages is map based * Update NanoChatCartridgeSystem.cs --- .../Cartridges/NanoChatCartridgeSystem.cs | 54 ++++++++++++------- .../NanoChat/NanoChatCardComponent.cs | 7 +++ .../NanoChat/SharedNanoChatSystem.cs | 15 +++++- 3 files changed, 55 insertions(+), 21 deletions(-) diff --git a/Content.Server/_CorvaxNext/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs b/Content.Server/_CorvaxNext/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs index 77c3e697d53..681f078f669 100644 --- a/Content.Server/_CorvaxNext/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs +++ b/Content.Server/_CorvaxNext/CartridgeLoader/Cartridges/NanoChatCartridgeSystem.cs @@ -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; @@ -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 @@ -38,6 +40,20 @@ public override void Initialize() SubscribeLocalEvent(OnMessage); } + private void UpdateClosed(Entity ent) + { + if (!TryComp(ent, out var cartridge) || + cartridge.LoaderUid is not {} pda || + !TryComp(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); @@ -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(cartridge.LoaderUid, out var pda)) continue; @@ -239,7 +258,7 @@ private void HandleSendMessage(Entity cartridge, // Log message attempt var recipientsText = recipients.Count > 0 - ? string.Join(", ", recipients.Select(r => ToPrettyString(r))) + ? string.Join(", ", recipients.Select((Entity r) => ToPrettyString(r))) : $"#{msg.RecipientNumber:D4}"; _adminLogger.Add(LogType.Chat, @@ -312,21 +331,19 @@ private bool EnsureRecipientExists(Entity 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); @@ -346,7 +363,7 @@ private bool EnsureRecipientExists(Entity card, uint reci /// /// Checks if there are any active telecomms servers on the given station /// - private bool HasActiveServer(EntityUid station) + private bool HasActiveServer(MapId mapId) { // I have no idea why this isn't public in the RadioSystem var query = @@ -354,7 +371,7 @@ private bool HasActiveServer(EntityUid station) 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; } @@ -381,8 +398,7 @@ private void DeliverMessageToRecipient(Entity 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); diff --git a/Content.Shared/_CorvaxNext/NanoChat/NanoChatCardComponent.cs b/Content.Shared/_CorvaxNext/NanoChat/NanoChatCardComponent.cs index 7830b902a09..138127c7180 100644 --- a/Content.Shared/_CorvaxNext/NanoChat/NanoChatCardComponent.cs +++ b/Content.Shared/_CorvaxNext/NanoChat/NanoChatCardComponent.cs @@ -14,6 +14,13 @@ public sealed partial class NanoChatCardComponent : Component [DataField, AutoNetworkedField] public uint? Number; + /// + /// Whether a PDA has this card's UI closed. + /// Used for notifications. + /// + [DataField] + public bool IsClosed; + /// /// All chat recipients stored on this card. /// diff --git a/Content.Shared/_CorvaxNext/NanoChat/SharedNanoChatSystem.cs b/Content.Shared/_CorvaxNext/NanoChat/SharedNanoChatSystem.cs index f40b84cbb92..f2639638ae1 100644 --- a/Content.Shared/_CorvaxNext/NanoChat/SharedNanoChatSystem.cs +++ b/Content.Shared/_CorvaxNext/NanoChat/SharedNanoChatSystem.cs @@ -49,13 +49,24 @@ private void OnExamined(Entity ent, ref ExaminedEvent arg /// public void SetNumber(Entity 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); } + /// + /// Sets IsClosed for a card. + /// + public void SetClosed(Entity card, bool closed) + { + if (!Resolve(card, ref card.Comp)) + return; + + card.Comp.IsClosed = closed; + } + /// /// Gets the recipients dictionary from a card. /// @@ -170,7 +181,7 @@ public bool GetNotificationsMuted(Entity card) /// public void SetNotificationsMuted(Entity card, bool muted) { - if (!Resolve(card, ref card.Comp)) + if (!Resolve(card, ref card.Comp) || card.Comp.NotificationsMuted == muted) return; card.Comp.NotificationsMuted = muted;