using System.Linq;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
namespace Content.Shared.FeedbackSystem;
///
/// SharedFeedbackManager handles feedback popup management and distribution across sessions.
/// It manages the state of displayed popups and provides mechanisms for opening, displaying, removing,
/// and sending popups to specified sessions or all sessions.
///
public interface ISharedFeedbackManager
{
///
/// An event that is triggered whenever the set of displayed feedback popups changes.
/// The boolean parameter is true if new popups have been added
///
event Action? DisplayedPopupsChanged;
///
/// Adds the specified popup prototypes to the displayed popups on the client..
///
/// A list of popup prototype IDs to be added to the displayed prototypes
///
/// This does nothing on the server.
///
/// Use this if you want to add a popup from a shared or client-side entity system.
///
void Display(List>? prototypes) {}
///
/// Removes the specified popup prototypes from the displayed popups on the client.
///
/// A list of popup prototype IDs to be removed from the displayed prototypes.
/// If null, all displayed popups will be cleared.
/// This does nothing on the server.
void Remove(List>? prototypes) {}
///
/// Sends a list of feedback popup prototypes to a specific entity, identified by its EntityUid.
///
/// The unique identifier of the entity to send the feedback popups to.
/// The list of feedback popup prototypes to send to the entity.
/// Returns true if the feedback popups were successfully sent, otherwise false.
/// This does nothing on the client.
bool Send(EntityUid uid, List> popupPrototypes)
{
return false;
}
///
/// Sends a list of feedback popup prototypes to the specified session.
///
/// The session to which the feedback popups will be sent.
/// A list of feedback popup prototype IDs to send to the session.
/// When true, removes the specified prototypes instead of adding them
/// This does nothing on the client.
void SendToSession(ICommonSession session, List> popupPrototypes, bool remove = false) {}
///
/// Sends the specified feedback popup prototypes to all connected client sessions.
///
/// A list of popup prototype IDs to be sent to all connected sessions.
/// When true, removes the specified prototypes instead of adding them
/// This does nothing on the client.
void SendToAllSessions(List> popupPrototypes, bool remove = false) {}
///
/// Opens the feedback popup for a specific session.
///
/// The session for which the feedback popup should be opened.
/// This does nothing on the client.
void OpenForSession(ICommonSession session) {}
///
/// Opens the feedback popup for all connected sessions.
///
/// This does nothing on the client.
void OpenForAllSessions() {}
}
///
public abstract partial class SharedFeedbackManager : ISharedFeedbackManager
{
[Dependency] private readonly IPrototypeManager _proto = null!;
[Dependency] protected readonly INetManager NetManager = null!;
public virtual IReadOnlySet>? DisplayedPopups => null;
//
public event Action? DisplayedPopupsChanged;
///
/// List of valid origns of the feedback popup that is filled from the CCVar. See
/// FeedbackValidOrigins
///
private List _validOrigins = [];
[MustCallBase]
public virtual void Initialize()
{
InitSubscriptions();
}
///
public virtual void Display(List>? prototypes) {}
///
public virtual void Remove(List>? prototypes) {}
///
public virtual bool Send(EntityUid uid, List> popupPrototypes)
{
return false;
}
///
public virtual void SendToSession(ICommonSession session, List> popupPrototypes, bool remove = false) {}
///
public virtual void SendToAllSessions(List> popupPrototypes, bool remove = false) {}
///
public virtual void OpenForSession(ICommonSession session) {}
///
public virtual void OpenForAllSessions() {}
///
/// Get a list of feedback prototypes that match the current valid origins.
///
/// If true, only retrieve pop-ups with ShowRoundEnd set to true.
/// If true, only retrieve pop-ups with a non-null RuleWhitelist.
/// If false, only retrieve pop-ups with a null RuleWhitelist.
/// Returns a list of protoIds; possibly empty.
public List> GetOriginFeedbackPrototypes(bool roundEndOnly, bool ruleSpecific = false)
{
var feedbackProtypes = _proto.EnumeratePrototypes()
.Where(x => (!roundEndOnly || x.ShowRoundEnd) && ruleSpecific == (x.RuleWhitelist != null) && _validOrigins.Contains(x.PopupOrigin))
.Select(x => new ProtoId(x.ID))
.OrderBy(x => x.Id)
.ToList();
return feedbackProtypes;
}
protected void InvokeDisplayedPopupsChanged(bool show)
{
DisplayedPopupsChanged?.Invoke(show);
}
}