#nullable enable using Content.Server.Power.Components; using Content.Server.UserInterface; using Content.Shared.Audio; using Content.Shared.Interaction; using Content.Shared.Research.Components; using Content.Shared.Research.Prototypes; using Content.Shared.Sound; using Robust.Server.GameObjects; using Robust.Server.Player; using Robust.Shared.Audio; using Robust.Shared.GameObjects; using Robust.Shared.IoC; using Robust.Shared.Player; using Robust.Shared.Prototypes; using Robust.Shared.Random; using Robust.Shared.Serialization.Manager.Attributes; using Robust.Shared.ViewVariables; namespace Content.Server.Research.Components { [RegisterComponent] [ComponentReference(typeof(IActivate))] public class ResearchConsoleComponent : SharedResearchConsoleComponent, IActivate { [Dependency] private readonly IPrototypeManager _prototypeManager = default!; [Dependency] private readonly IRobustRandom _random = default!; [DataField("sound")] private SoundSpecifier _soundCollectionName = new SoundCollectionSpecifier("keyboard"); [ViewVariables] private bool Powered => !Owner.TryGetComponent(out ApcPowerReceiverComponent? receiver) || receiver.Powered; [ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ResearchConsoleUiKey.Key); protected override void Initialize() { base.Initialize(); Owner.EnsureComponentWarn(); if (UserInterface != null) { UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage; } Owner.EnsureComponent(); } private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage message) { if (!Owner.TryGetComponent(out TechnologyDatabaseComponent? database)) return; if (!Owner.TryGetComponent(out ResearchClientComponent? client)) return; if (!Powered) return; switch (message.Message) { case ConsoleUnlockTechnologyMessage msg: if (!_prototypeManager.TryIndex(msg.Id, out TechnologyPrototype? tech)) break; if (client.Server == null) break; if (!client.Server.CanUnlockTechnology(tech)) break; if (client.Server.UnlockTechnology(tech)) { database.SyncWithServer(); database.Dirty(); UpdateUserInterface(); } break; case ConsoleServerSyncMessage _: database.SyncWithServer(); UpdateUserInterface(); break; case ConsoleServerSelectionMessage _: client.OpenUserInterface(message.Session); break; } } /// /// Method to update the user interface on the clients. /// public void UpdateUserInterface() { UserInterface?.SetState(GetNewUiState()); } private ResearchConsoleBoundInterfaceState GetNewUiState() { if (!Owner.TryGetComponent(out ResearchClientComponent? client) || client.Server == null) return new ResearchConsoleBoundInterfaceState(default, default); var points = client.ConnectedToServer ? client.Server.Point : 0; var pointsPerSecond = client.ConnectedToServer ? client.Server.PointsPerSecond : 0; return new ResearchConsoleBoundInterfaceState(points, pointsPerSecond); } /// /// Open the user interface on a certain player session. /// /// Session where the UI will be shown public void OpenUserInterface(IPlayerSession session) { UserInterface?.Open(session); } void IActivate.Activate(ActivateEventArgs eventArgs) { if (!eventArgs.User.TryGetComponent(out ActorComponent? actor)) return; if (!Powered) { return; } OpenUserInterface(actor.PlayerSession); PlayKeyboardSound(); } private void PlayKeyboardSound() { if (_soundCollectionName.TryGetSound(out var sound)) SoundSystem.Play(Filter.Pvs(Owner), sound, Owner, AudioParams.Default); } } }