Сообщения в ПДА (#564)

* add: Сообщения в ПДА

* Search bar and colors in messages on the PDA
This commit is contained in:
Spatison
2024-08-06 16:58:30 +03:00
committed by GitHub
parent ea1ed76465
commit d023d29e54
28 changed files with 929 additions and 13 deletions

View File

@@ -56,7 +56,7 @@ public sealed class SingletonDeviceNetServerSystem : EntitySystem
if (!server.Available)
{
DisconnectServer(uid,server, device);
DisconnectServer(uid, server, device);
continue;
}

View File

@@ -88,7 +88,7 @@ public sealed class RenameCommand : IConsoleCommand
{
if (pda.OwnerName == oldName)
{
pdaSystem.SetOwner(uid, pda, name);
pdaSystem.SetOwnerName(uid, pda, name);
}
}
}

View File

@@ -14,6 +14,7 @@ using Content.Shared.CartridgeLoader;
using Content.Shared.Chat;
using Content.Shared.Light.Components;
using Content.Shared.PDA;
using Content.Shared.Roles;
using Robust.Server.Containers;
using Robust.Server.GameObjects;
using Robust.Shared.Containers;
@@ -90,12 +91,24 @@ namespace Content.Server.PDA
UpdatePdaUi(uid, pda);
}
public void SetOwner(EntityUid uid, PdaComponent pda, string ownerName)
public void SetOwnerName(EntityUid uid, PdaComponent pda, string ownerName)
{
pda.OwnerName = ownerName;
UpdatePdaUi(uid, pda);
}
public void SetOwnerJob(EntityUid uid, PdaComponent pda, string ownerJob)
{
pda.OwnerJob = ownerJob;
UpdatePdaUi(uid, pda);
}
public void SetOwnerDepartment(EntityUid uid, PdaComponent pda, string ownerDepartment)
{
pda.OwnerDepartment = ownerDepartment;
UpdatePdaUi(uid, pda);
}
private void OnStationRenamed(StationRenamedEvent ev)
{
UpdateAllPdaUisOnStation();
@@ -172,6 +185,7 @@ namespace Content.Server.PDA
new PdaIdInfoText
{
ActualOwnerName = pda.OwnerName,
ActualOwnerJob = pda.OwnerJob, // WD EDIT
IdOwner = id?.FullName,
JobTitle = id?.JobTitle,
StationAlertLevel = pda.StationAlertLevel,

View File

@@ -12,6 +12,7 @@ using Content.Shared.Access.Systems;
using Content.Shared.CCVar;
using Content.Shared.Clothing;
using Content.Shared.Humanoid;
using Content.Server.Roles.Jobs;
using Content.Shared.Humanoid.Prototypes;
using Content.Shared.PDA;
using Content.Shared.Preferences;
@@ -49,6 +50,7 @@ public sealed class StationSpawningSystem : SharedStationSpawningSystem
[Dependency] private readonly SharedAccessSystem _accessSystem = default!;
[Dependency] private readonly IdentitySystem _identity = default!;
[Dependency] private readonly MetaDataSystem _metaSystem = default!;
[Dependency] private readonly JobSystem _jobs = default!; // WD EDIT
[Dependency] private readonly ArrivalsSystem _arrivalsSystem = default!;
[Dependency] private readonly ContainerSpawnPointSystem _containerSpawnPointSystem = default!;
@@ -337,7 +339,14 @@ public sealed class StationSpawningSystem : SharedStationSpawningSystem
_accessSystem.SetAccessToJob(cardId, jobPrototype, extendedAccess);
if (pdaComponent != null)
_pdaSystem.SetOwner(idUid.Value, pdaComponent, characterName);
{
_pdaSystem.SetOwnerName(idUid.Value, pdaComponent, characterName);
//WD EDIT START
_pdaSystem.SetOwnerJob(idUid.Value, pdaComponent, jobPrototype.LocalizedName);
_jobs.TryGetDepartment(jobPrototype.ID, out var department);
_pdaSystem.SetOwnerDepartment(idUid.Value, pdaComponent, department?.ID ?? "Specific");
//WD EDIT END
}
}

View File

@@ -0,0 +1,20 @@
namespace Content.Server._White.CartridgeLoader.Cartridges;
[RegisterComponent]
public sealed partial class MessagesCartridgeComponent : Component
{
/// <summary>
/// The component of the last contacted server
/// </summary>
[DataField]
public EntityUid? LastServer;
/// <summary>
/// The message system user id of the crew the user is chatting with
/// </summary>
[DataField]
public int? ChatUid;
[DataField]
public int? UserUid;
}

View File

@@ -0,0 +1,290 @@
using Content.Server._White.Radio.Components;
using Content.Server._White.Radio.EntitySystems;
using Content.Server.Administration.Commands;
using Content.Server.CartridgeLoader;
using Content.Shared.CartridgeLoader;
using Content.Shared.PDA;
using Robust.Shared.Map;
using Content.Server.GameTicking;
using Robust.Shared.Timing;
using Content.Server.DeviceNetwork.Systems;
using Content.Shared.DeviceNetwork;
using Content.Server.Station.Systems;
using Content.Shared._White.CartridgeLoader.Cartridges;
using Content.Shared.Access.Components;
using Content.Shared.Roles;
using Robust.Shared.Prototypes;
namespace Content.Server._White.CartridgeLoader.Cartridges;
public sealed class MessagesCartridgeSystem : EntitySystem
{
[Dependency] private readonly CartridgeLoaderSystem _cartridgeLoaderSystem = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
[Dependency] private readonly GameTicker _gameTicker = default!;
[Dependency] private readonly MessagesServerSystem _messagesServerSystem = default!;
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly SingletonDeviceNetServerSystem _singletonServerSystem = default!;
[Dependency] private readonly StationSystem _stationSystem = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MessagesCartridgeComponent, CartridgeMessageEvent>(OnUiMessage);
SubscribeLocalEvent<MessagesCartridgeComponent, CartridgeUiReadyEvent>(OnUiReady);
SubscribeLocalEvent<MessagesCartridgeComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
SubscribeLocalEvent<MessagesCartridgeComponent, CartridgeActivatedEvent>(OnCartActivation);
SubscribeLocalEvent<MessagesCartridgeComponent, CartridgeDeactivatedEvent>(OnCartDeactivation);
SubscribeLocalEvent<MessagesCartridgeComponent, CartridgeAddedEvent>(OnCartInsertion);
SubscribeLocalEvent<MessagesCartridgeComponent, ComponentInit>(OnInit);
SubscribeLocalEvent<MessagesCartridgeComponent, ComponentRemove>(OnRemove);
}
private void OnInit(EntityUid uid, MessagesCartridgeComponent component, ComponentInit args)
{
var stationId = _stationSystem.GetOwningStation(uid);
if (stationId.HasValue &&
_singletonServerSystem.TryGetActiveServerAddress<MessagesServerComponent>(stationId.Value,
out var address) && TryComp(uid, out CartridgeComponent? cartComponent))
{
SendName(uid, component, cartComponent, address);
component.UserUid = cartComponent.LoaderUid?.Id;
}
}
private void OnRemove(EntityUid uid, MessagesCartridgeComponent component, ComponentRemove args)
{
if (component.LastServer == null || !TryComp<MessagesServerComponent>(component.LastServer, out var messagesServerComponent) || component.UserUid == null)
return;
messagesServerComponent.NameDict.Remove(component.UserUid.Value);
}
/// <summary>
/// This gets called when the ui fragment needs to be updated for the first time after activating
/// </summary>
private void OnUiReady(EntityUid uid, MessagesCartridgeComponent component, CartridgeUiReadyEvent args)
{
var stationId = _stationSystem.GetOwningStation(uid);
if (stationId.HasValue && _singletonServerSystem.TryGetActiveServerAddress<MessagesServerComponent>(stationId.Value, out var address) && TryComp(uid, out CartridgeComponent? cartComponent))
SendName(uid, component, cartComponent, address);
UpdateUiState(uid, component);
}
/// <summary>
/// The ui messages received here get wrapped by a CartridgeMessageEvent and are relayed from the <see cref="CartridgeLoaderSystem"/>
/// </summary>
/// <remarks>
/// The cartridge specific ui message event needs to inherit from the CartridgeMessageEvent
/// </remarks>
private void OnUiMessage(EntityUid uid, MessagesCartridgeComponent component, CartridgeMessageEvent args)
{
if (args is not MessagesUiMessageEvent messageEvent)
return;
if (messageEvent.Action == MessagesUiAction.Send && TryComp(uid, out CartridgeComponent? cartComponent) && component.UserUid is { } userId && component.ChatUid != null && messageEvent.StringInput != null)
{
var stationId = _stationSystem.GetOwningStation(uid);
if (!stationId.HasValue)
return;
MessagesMessageData messageData = new()
{
SenderId = userId,
ReceiverId = component.ChatUid.Value,
Content = messageEvent.StringInput,
Time = _gameTiming.CurTime.Subtract(_gameTicker.RoundStartTimeSpan)
};
var packet = new NetworkPayload()
{
[MessagesNetworkKeys.Message] = messageData
};
_singletonServerSystem.TryGetActiveServerAddress<MessagesServerComponent>(stationId.Value, out var address);
_deviceNetworkSystem.QueuePacket(uid, address, packet);
}
else
{
if (messageEvent.Action == MessagesUiAction.ChangeChat)
component.ChatUid = messageEvent.TargetChatUid;
}
UpdateUiState(uid, component);
}
/// <summary>
/// On cart insertion, register as background process.
/// </summary>
private void OnCartInsertion(EntityUid uid, MessagesCartridgeComponent component, CartridgeAddedEvent args)
{
_cartridgeLoaderSystem.RegisterBackgroundProgram(args.Loader, uid);
component.UserUid = args.Loader.Id;
}
/// <summary>
/// On cartridge activation, connect to messages network.
/// </summary>
private void OnCartActivation(EntityUid uid, MessagesCartridgeComponent component, CartridgeActivatedEvent args)
{
_deviceNetworkSystem.ConnectDevice(uid);
var stationId = _stationSystem.GetOwningStation(uid);
if (stationId.HasValue && _singletonServerSystem.TryGetActiveServerAddress<MessagesServerComponent>(stationId.Value, out var address) && TryComp(uid, out CartridgeComponent? cartComponent))
SendName(uid, component, cartComponent, address);
}
/// <summary>
/// On cartridge deactivation, disconnect from messages network.
/// </summary>
private void OnCartDeactivation(EntityUid uid, MessagesCartridgeComponent component, CartridgeDeactivatedEvent args)
{
_deviceNetworkSystem.DisconnectDevice(uid, null);
}
/// <summary>
/// React and respond to packets from the server
/// </summary>
private void OnPacketReceived(EntityUid uid, MessagesCartridgeComponent component, DeviceNetworkPacketEvent args)
{
if (!TryComp(uid, out CartridgeComponent? cartComponent))
return;
component.LastServer = args.Sender;
if (args.Data.TryGetValue<MessagesMessageData>(MessagesNetworkKeys.Message, out var message) && cartComponent.LoaderUid != null)
{
if (message.ReceiverId == cartComponent.LoaderUid.Value.Id)
{
TryGetName(message.SenderId, component, out var name);
var subtitleString = Loc.GetString("messages-pda-notification-header", ("name", name));
_cartridgeLoaderSystem.SendNotification(
cartComponent.LoaderUid.Value,
subtitleString,
message.Content);
}
}
UpdateUiState(uid, component);
}
/// <summary>
/// Updates the user's name in the storage component.
/// </summary>
private void SendName(EntityUid uid, MessagesCartridgeComponent component, CartridgeComponent cartComponent, string? address)
{
TryGetMessagesUser(cartComponent, out var messagesUser);
var packet = new NetworkPayload()
{
[MessagesNetworkKeys.UserId] = component.UserUid,
[MessagesNetworkKeys.NewUser] = messagesUser
};
_deviceNetworkSystem.QueuePacket(uid, address, packet);
}
/// <summary>
/// Retrieves the name of the given user from the last contacted server
/// </summary>
private bool TryGetName(int key, MessagesCartridgeComponent component, out string name)
{
if (component.LastServer != null && _messagesServerSystem.TryGetUserFromDict(component.LastServer, key, out var messagesUser))
{
name = messagesUser.Name;
return true;
}
name = Loc.GetString("messages-pda-connection-error");
return false;
}
/// <summary>
/// Returns the user's name, job title and job department
/// </summary>
public bool TryGetMessagesUser(CartridgeComponent component, out MessagesUser messagesUser)
{
var pda = component.LoaderUid;
if (pda == null)
{
messagesUser = new MessagesUser(Loc.GetString("messages-pda-unknown-name"), Loc.GetString("messages-pda-unknown-job"), "Specific");
return false;
}
var pdaComponent = CompOrNull<PdaComponent>(pda);
if (pdaComponent?.OwnerName == null)
{
messagesUser = new MessagesUser(Loc.GetString("messages-pda-unknown-name"), Loc.GetString("messages-pda-unknown-job"), "Specific");
return false;
}
messagesUser = new MessagesUser(pdaComponent.OwnerName, pdaComponent.OwnerJob ?? Loc.GetString("messages-pda-unknown-job"), pdaComponent.OwnerDepartment ?? "Specific");
return true;
}
private void UpdateUiState(EntityUid uid, MessagesCartridgeComponent? component = null)
{
if (!Resolve(uid, ref component))
return;
if (!TryComp(uid, out CartridgeComponent? cartComponent))
return;
if (cartComponent.LoaderUid == null)
return;
var loaderUid = cartComponent.LoaderUid.Value;
MessagesUiState state;
MapId mapId = Transform(uid).MapID;
int? currentUserId = component.UserUid;
if (currentUserId == null || component.LastServer == null)
{
state = new MessagesUiState(MessagesUiStateMode.Error, [], null);
_cartridgeLoaderSystem.UpdateCartridgeUiState(loaderUid, state);
return;
}
if (component.ChatUid == null) //if no chat is loaded, list users
{
List<(MessagesUser, int?)> userList = [];
var nameDict = _messagesServerSystem.GetNameDict(component.LastServer);
foreach (var nameEntry in nameDict.Keys)
{
if (nameEntry == currentUserId)
continue;
userList.Add((nameDict[nameEntry], nameEntry));
}
state = new MessagesUiState(MessagesUiStateMode.UserList, userList, null);
}
else
{
List<MessagesMessageData> messageList = []; //Else, list messages from the current chat
foreach (var message in _messagesServerSystem.GetMessages(component.LastServer, component.ChatUid.Value, currentUserId.Value))
{
if (message.SenderId == component.ChatUid && message.ReceiverId == currentUserId || message.ReceiverId == component.ChatUid && message.SenderId == currentUserId)
{
messageList.Add(message);
}
}
messageList.Sort
(
delegate (MessagesMessageData a, MessagesMessageData b)
{
return TimeSpan.Compare(a.Time, b.Time);
}
);
List<(MessagesUser, int?)> formattedMessageList = [];
foreach (var message in messageList)
{
TryGetName(message.SenderId, component, out var name);
var stationTime = message.Time.Subtract(_gameTicker.RoundStartTimeSpan);
var content = $"{stationTime.ToString("\\[hh\\:mm\\:ss\\]")} {name}: {message.Content}";
formattedMessageList.Add((new MessagesUser(content, Loc.GetString("messages-pda-unknown-job"), "Specific"), null));
}
TryGetName(component.ChatUid.Value, component, out var user);
state = new MessagesUiState(MessagesUiStateMode.Chat, formattedMessageList, user);
}
_cartridgeLoaderSystem.UpdateCartridgeUiState(loaderUid, state);
}
}

View File

@@ -58,7 +58,7 @@ public sealed class RandomHumanSystem : EntitySystem
var cardId = pdaComponent.ContainedId.Value;
_card.TryChangeFullName(cardId, newProfile.Name, card);
_pda.SetOwner(idUid.Value, pdaComponent, newProfile.Name);
_pda.SetOwnerName(idUid.Value, pdaComponent, newProfile.Name);
if (EntityManager.TryGetComponent(cardId, out StationRecordKeyStorageComponent? keyStorage)
&& keyStorage.Key is { } key)

View File

@@ -0,0 +1,26 @@
using Content.Server.Power.Components;
using Content.Shared._White.CartridgeLoader.Cartridges;
namespace Content.Server._White.Radio.Components;
/// <summary>
/// Entities with <see cref="MessagesServerComponent"/> are needed to transmit messages using PDAs.
/// They also need to be powered by <see cref="ApcPowerReceiverComponent"/>
/// in order for them to work on the same map as server.
/// </summary>
[RegisterComponent]
public sealed partial class MessagesServerComponent : Component
{
/// <summary>
/// The list of messages cached by the server.
/// </summary>
[DataField]
public List<MessagesMessageData> Messages = [];
/// <summary>
/// Dictionary translating uids to readable names
/// </summary>
[DataField]
public Dictionary<int, MessagesUser> NameDict = [];
}

View File

@@ -0,0 +1,128 @@
using System.Linq;
using Content.Server._White.CartridgeLoader.Cartridges;
using Content.Server._White.Radio.Components;
using Content.Server.DeviceNetwork.Systems;
using Content.Shared._White.CartridgeLoader.Cartridges;
using Content.Shared.CartridgeLoader;
using Content.Shared.DeviceNetwork;
namespace Content.Server._White.Radio.EntitySystems;
public sealed class MessagesServerSystem : EntitySystem
{
[Dependency] private readonly DeviceNetworkSystem _deviceNetworkSystem = default!;
[Dependency] private readonly SingletonDeviceNetServerSystem _singletonServerSystem = default!;
[Dependency] private readonly MessagesCartridgeSystem _messagesSystem = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<MessagesServerComponent, DeviceNetworkPacketEvent>(OnPacketReceived);
SubscribeLocalEvent<MessagesServerComponent, ComponentInit>(OnInit);
}
private void OnInit(EntityUid uid, MessagesServerComponent component, ComponentInit args)
{
var query = EntityQueryEnumerator<MessagesCartridgeComponent>();
while (query.MoveNext(out var entityUid, out var cartridge))
{
if (!TryComp(entityUid, out CartridgeComponent? cartComponent))
continue;
_messagesSystem.TryGetMessagesUser(cartComponent, out var messagesUser);
if (cartridge.UserUid == null || messagesUser.Name == Loc.GetString("messages-pda-unknown-name"))
continue;
component.NameDict[cartridge.UserUid.Value] = messagesUser;
cartridge.LastServer = uid;
}
}
/// <summary>
/// Reacts to packets received from clients
/// </summary>
private void OnPacketReceived(EntityUid uid, MessagesServerComponent component, DeviceNetworkPacketEvent args)
{
if (!_singletonServerSystem.IsActiveServer(uid))
return;
if (args.Data.TryGetValue<MessagesUser>(MessagesNetworkKeys.NewUser, out var messagesUser) && args.Data.TryGetValue<int>(MessagesNetworkKeys.UserId, out var userId))
{
component.NameDict[userId] = messagesUser;
var packet = new NetworkPayload();
_deviceNetworkSystem.QueuePacket(uid, args.SenderAddress, packet);
}
if (args.Data.TryGetValue<MessagesMessageData>(MessagesNetworkKeys.Message, out var message))
SendMessage(uid, component, message);
}
/// <summary>
/// Broadcasts a message into the network
/// </summary>
private void SendMessage(EntityUid uid, MessagesServerComponent component, MessagesMessageData message)
{
component.Messages.Add(message);
var packet = new NetworkPayload()
{
[MessagesNetworkKeys.Message] = message
};
_deviceNetworkSystem.QueuePacket(uid, null, packet);
}
/// <summary>
/// Returns user
/// </summary>
public bool TryGetUserFromDict(EntityUid? uid, int key, out MessagesUser messagesUser)
{
if (!TryComp(uid, out MessagesServerComponent? component))
{
messagesUser = new MessagesUser(Loc.GetString("messages-pda-connection-error"), Loc.GetString("messages-pda-unknown-job"), "Specific");
return false;
}
if (component.NameDict.TryGetValue(key, out var keyValue))
{
messagesUser = keyValue;
return true;
}
messagesUser = new MessagesUser(Loc.GetString("messages-pda-user-missing"), Loc.GetString("messages-pda-unknown-job"), "Specific");
return false;
}
/// <summary>
/// Returns the name dictionary cache
/// </summary>
public Dictionary<int, MessagesUser> GetNameDict(EntityUid? uid)
{
if (!TryComp(uid, out MessagesServerComponent? component))
return new Dictionary<int, MessagesUser>();
return component.NameDict;
}
/// <summary>
/// Returns list of messages between the two users
/// </summary>
public List<MessagesMessageData> GetMessages(EntityUid? uid, int id1, int id2)
{
if (!TryComp(uid, out MessagesServerComponent? component))
return [];
return
[
..component.Messages.Where(message =>
message.SenderId == id1 && message.ReceiverId == id2 ||
message.SenderId == id2 && message.ReceiverId == id1)
];
}
}
public static class MessagesNetworkKeys
{
public const string NewUser = "new_user";
public const string UserId = "user_id";
public const string Message = "message";
}