Files
wwdpublic/Content.Shared/DeltaV/NanoChat/SharedNanoChatSystem.cs
Will-Oliver-Br d29faf2e85 Port Nanochat Lookup (#1978)
<!--
This is a semi-strict format, you can add/remove sections as needed but
the order/format should be kept the same
Remove these comments before submitting
-->

# Description

<!--
Explain this PR in as much detail as applicable

Some example prompts to consider:
How might this affect the game? The codebase?
What might be some alternatives to this?
How/Who does this benefit/hurt [the game/codebase]?
-->

Port nanochat lookup from Delta-v.
Pr: https://github.com/DeltaV-Station/Delta-v/pull/2794

---

# TODO

<!--
A list of everything you have to do before this PR is "complete"
You probably won't have to complete everything before merging but it's
good to leave future references
-->

- [x] Task
- [x] Completed Task

---

<!--
This is default collapsed, readers click to expand it and see all your
media
The PR media section can get very large at times, so this is a good way
to keep it clean
The title is written using HTML tags
The title must be within the <summary> tags or you won't see it
-->

<details><summary><h1>Media</h1></summary>
<p>

![Captura de tela 2025-03-16
183052](https://github.com/user-attachments/assets/2b844906-4117-4560-9045-69b9d5493cf3)
![Captura de tela 2025-03-16
183117](https://github.com/user-attachments/assets/b2546750-c808-468e-8fbf-de1d75402c06)
![Captura de tela 2025-03-16
183124](https://github.com/user-attachments/assets/01c5eb53-b1ba-4ca7-8a2e-f1813147b441)
![Captura de tela 2025-03-16
183147](https://github.com/user-attachments/assets/304ac291-b5b0-474f-84c3-8321386c02e2)

</p>
</details>

---

# Changelog

<!--
You can add an author after the `🆑` to change the name that appears
in the changelog (ex: `🆑 Death`)
Leaving it blank will default to your GitHub display name
This includes all available types for the changelog
-->

🆑 Toby222 and Will-Oliver-Br
add: Add Number lookup to NanoChat

---------

Co-authored-by: Tobias Berger <toby@tobot.dev>

(cherry picked from commit c3b6aeda090719cf82b132dcadebbe035a0da45a)
2025-03-21 16:50:22 +03:00

299 lines
8.7 KiB
C#

using Content.Shared.DeltaV.CartridgeLoader.Cartridges;
using Content.Shared.Examine;
using Content.Shared.PDA;
using Robust.Shared.Timing;
namespace Content.Shared.DeltaV.NanoChat;
/// <summary>
/// Base system for NanoChat functionality shared between client and server.
/// </summary>
public abstract class SharedNanoChatSystem : EntitySystem
{
[Dependency] private readonly IGameTiming _timing = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<NanoChatCardComponent, ExaminedEvent>(OnExamined);
}
private void OnExamined(Entity<NanoChatCardComponent> ent, ref ExaminedEvent args)
{
if (!args.IsInDetailsRange)
return;
if (ent.Comp.Number == null)
{
args.PushMarkup(Loc.GetString("nanochat-card-examine-no-number"));
return;
}
args.PushMarkup(Loc.GetString("nanochat-card-examine-number", ("number", $"{ent.Comp.Number:D4}")));
}
#region Public API Methods
/// <summary>
/// Gets the NanoChat number for a card.
/// </summary>
public uint? GetNumber(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return null;
return card.Comp.Number;
}
/// <summary>
/// Sets the NanoChat number for a card.
/// </summary>
public void SetNumber(Entity<NanoChatCardComponent?> card, uint number)
{
if (!Resolve(card, ref card.Comp))
return;
card.Comp.Number = number;
Dirty(card);
}
/// <summary>
/// Gets the recipients dictionary from a card.
/// </summary>
public IReadOnlyDictionary<uint, NanoChatRecipient> GetRecipients(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return new Dictionary<uint, NanoChatRecipient>();
return card.Comp.Recipients;
}
/// <summary>
/// Gets the messages dictionary from a card.
/// </summary>
public IReadOnlyDictionary<uint, List<NanoChatMessage>> GetMessages(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return new Dictionary<uint, List<NanoChatMessage>>();
return card.Comp.Messages;
}
/// <summary>
/// Sets a specific recipient in the card.
/// </summary>
public void SetRecipient(Entity<NanoChatCardComponent?> card, uint number, NanoChatRecipient recipient)
{
if (!Resolve(card, ref card.Comp))
return;
card.Comp.Recipients[number] = recipient;
Dirty(card);
}
/// <summary>
/// Gets a specific recipient from the card.
/// </summary>
public NanoChatRecipient? GetRecipient(Entity<NanoChatCardComponent?> card, uint number)
{
if (!Resolve(card, ref card.Comp) || !card.Comp.Recipients.TryGetValue(number, out var recipient))
return null;
return recipient;
}
/// <summary>
/// Gets all messages for a specific recipient.
/// </summary>
public List<NanoChatMessage>? GetMessagesForRecipient(Entity<NanoChatCardComponent?> card, uint recipientNumber)
{
if (!Resolve(card, ref card.Comp) || !card.Comp.Messages.TryGetValue(recipientNumber, out var messages))
return null;
return new List<NanoChatMessage>(messages);
}
/// <summary>
/// Adds a message to a recipient's conversation.
/// </summary>
public void AddMessage(Entity<NanoChatCardComponent?> card, uint recipientNumber, NanoChatMessage message)
{
if (!Resolve(card, ref card.Comp))
return;
if (!card.Comp.Messages.TryGetValue(recipientNumber, out var messages))
{
messages = new List<NanoChatMessage>();
card.Comp.Messages[recipientNumber] = messages;
}
messages.Add(message);
card.Comp.LastMessageTime = _timing.CurTime;
Dirty(card);
}
/// <summary>
/// Gets the currently selected chat recipient.
/// </summary>
public uint? GetCurrentChat(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return null;
return card.Comp.CurrentChat;
}
/// <summary>
/// Sets the currently selected chat recipient.
/// </summary>
public void SetCurrentChat(Entity<NanoChatCardComponent?> card, uint? recipient)
{
if (!Resolve(card, ref card.Comp))
return;
card.Comp.CurrentChat = recipient;
Dirty(card);
}
/// <summary>
/// Gets whether notifications are muted.
/// </summary>
public bool GetNotificationsMuted(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return false;
return card.Comp.NotificationsMuted;
}
/// <summary>
/// Sets whether notifications are muted.
/// </summary>
public void SetNotificationsMuted(Entity<NanoChatCardComponent?> card, bool muted)
{
if (!Resolve(card, ref card.Comp))
return;
card.Comp.NotificationsMuted = muted;
Dirty(card);
}
/// <summary>
/// Gets whether NanoChat number is listed.
/// </summary>
public bool GetListNumber(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return false;
return card.Comp.ListNumber;
}
/// <summary>
/// Sets whether NanoChat number is listed.
/// </summary>
public void SetListNumber(Entity<NanoChatCardComponent?> card, bool listNumber)
{
if (!Resolve(card, ref card.Comp) || card.Comp.ListNumber == listNumber)
return;
card.Comp.ListNumber = listNumber;
Dirty(card);
}
/// <summary>
/// Gets the time of the last message.
/// </summary>
public TimeSpan? GetLastMessageTime(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return null;
return card.Comp.LastMessageTime;
}
/// <summary>
/// Gets if there are unread messages from a recipient.
/// </summary>
public bool HasUnreadMessages(Entity<NanoChatCardComponent?> card, uint recipientNumber)
{
if (!Resolve(card, ref card.Comp) || !card.Comp.Recipients.TryGetValue(recipientNumber, out var recipient))
return false;
return recipient.HasUnread;
}
/// <summary>
/// Clears all messages and recipients from the card.
/// </summary>
public void Clear(Entity<NanoChatCardComponent?> card)
{
if (!Resolve(card, ref card.Comp))
return;
card.Comp.Messages.Clear();
card.Comp.Recipients.Clear();
card.Comp.CurrentChat = null;
Dirty(card);
}
/// <summary>
/// Deletes a chat conversation with a recipient from the card.
/// Optionally keeps message history while removing from active chats.
/// </summary>
/// <returns>True if the chat was deleted successfully</returns>
public bool TryDeleteChat(Entity<NanoChatCardComponent?> card, uint recipientNumber, bool keepMessages = false)
{
if (!Resolve(card, ref card.Comp))
return false;
// Remove from recipients list
var removed = card.Comp.Recipients.Remove(recipientNumber);
// Clear messages if requested
if (!keepMessages)
card.Comp.Messages.Remove(recipientNumber);
// Clear current chat if we just deleted it
if (card.Comp.CurrentChat == recipientNumber)
card.Comp.CurrentChat = null;
if (removed)
Dirty(card);
return removed;
}
/// <summary>
/// Ensures a recipient exists in the card's contacts and message lists.
/// If the recipient doesn't exist, they will be added with the provided info.
/// </summary>
/// <returns>True if the recipient was added or already existed</returns>
public bool EnsureRecipientExists(Entity<NanoChatCardComponent?> card,
uint recipientNumber,
NanoChatRecipient? recipientInfo = null)
{
if (!Resolve(card, ref card.Comp))
return false;
if (!card.Comp.Recipients.ContainsKey(recipientNumber))
{
// Only add if we have recipient info
if (recipientInfo == null)
return false;
card.Comp.Recipients[recipientNumber] = recipientInfo.Value;
}
// Ensure message list exists for this recipient
if (!card.Comp.Messages.ContainsKey(recipientNumber))
card.Comp.Messages[recipientNumber] = new List<NanoChatMessage>();
Dirty(card);
return true;
}
#endregion
}