166 lines
6.1 KiB
C#
166 lines
6.1 KiB
C#
using Content.Server.Power.EntitySystems;
|
|
using Content.Server.Research.Components;
|
|
using Content.Shared.UserInterface;
|
|
using Content.Shared.Access.Components;
|
|
using Content.Shared.Emag.Components;
|
|
using Content.Shared.Emag.Systems;
|
|
using Content.Shared.IdentityManagement;
|
|
using Content.Shared.Research.Components;
|
|
using Content.Shared.Research.Prototypes;
|
|
|
|
namespace Content.Server.Research.Systems;
|
|
|
|
public sealed partial class ResearchSystem
|
|
{
|
|
[Dependency] private readonly EmagSystem _emag = default!;
|
|
|
|
private void InitializeConsole()
|
|
{
|
|
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleUnlockTechnologyMessage>(OnConsoleUnlock);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleRediscoverTechnologyMessage>(OnRediscoverTechnology);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, BeforeActivatableUIOpenEvent>(OnConsoleBeforeUiOpened);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, ResearchServerPointsChangedEvent>(OnPointsChanged);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, ResearchRegistrationChangedEvent>(OnConsoleRegistrationChanged);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, TechnologyDatabaseModifiedEvent>(OnConsoleDatabaseModified);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, TechnologyDatabaseSynchronizedEvent>(OnConsoleDatabaseSynchronized);
|
|
SubscribeLocalEvent<ResearchConsoleComponent, GotEmaggedEvent>(OnEmagged);
|
|
}
|
|
|
|
private void OnRediscoverTechnology(
|
|
EntityUid uid,
|
|
ResearchConsoleComponent console,
|
|
ConsoleRediscoverTechnologyMessage args
|
|
)
|
|
{
|
|
var act = args.Actor;
|
|
|
|
if (!this.IsPowered(uid, EntityManager))
|
|
return;
|
|
|
|
if (!HasAccess(uid, act))
|
|
{
|
|
_popup.PopupEntity(Loc.GetString("research-console-no-access-popup"), act);
|
|
return;
|
|
}
|
|
|
|
if (!TryGetClientServer(uid, out var serverEnt, out var serverComponent))
|
|
return;
|
|
|
|
if(serverComponent.NextRediscover > _timing.CurTime)
|
|
return;
|
|
|
|
var rediscoverCost = serverComponent.RediscoverCost;
|
|
if (rediscoverCost > serverComponent.Points)
|
|
return;
|
|
|
|
serverComponent.NextRediscover = _timing.CurTime + serverComponent.RediscoverInterval;
|
|
|
|
ModifyServerPoints(serverEnt.Value, -rediscoverCost);
|
|
UpdateTechnologyCards(serverEnt.Value);
|
|
SyncClientWithServer(uid);
|
|
UpdateConsoleInterface(uid);
|
|
}
|
|
|
|
private void OnConsoleUnlock(EntityUid uid, ResearchConsoleComponent component, ConsoleUnlockTechnologyMessage args)
|
|
{
|
|
var act = args.Actor;
|
|
|
|
if (!this.IsPowered(uid, EntityManager))
|
|
return;
|
|
|
|
if (!PrototypeManager.TryIndex<TechnologyPrototype>(args.Id, out var technologyPrototype))
|
|
return;
|
|
|
|
if (!HasAccess(uid, act))
|
|
{
|
|
_popup.PopupEntity(Loc.GetString("research-console-no-access-popup"), act);
|
|
return;
|
|
}
|
|
|
|
if (!UnlockTechnology(uid, args.Id, act))
|
|
return;
|
|
|
|
if (!_emag.CheckFlag(uid, EmagType.Interaction))
|
|
{
|
|
var getIdentityEvent = new TryGetIdentityShortInfoEvent(uid, act);
|
|
RaiseLocalEvent(getIdentityEvent);
|
|
|
|
var message = Loc.GetString(
|
|
"research-console-unlock-technology-radio-broadcast",
|
|
("technology", Loc.GetString(technologyPrototype.Name)),
|
|
("amount", technologyPrototype.Cost),
|
|
("approver", getIdentityEvent.Title ?? string.Empty)
|
|
);
|
|
_radio.SendRadioMessage(uid, message, component.AnnouncementChannel, uid, escapeMarkup: false);
|
|
}
|
|
|
|
SyncClientWithServer(uid);
|
|
UpdateConsoleInterface(uid, component);
|
|
}
|
|
|
|
private void OnConsoleBeforeUiOpened(EntityUid uid, ResearchConsoleComponent component, BeforeActivatableUIOpenEvent args)
|
|
{
|
|
SyncClientWithServer(uid);
|
|
}
|
|
|
|
private void UpdateConsoleInterface(EntityUid uid, ResearchConsoleComponent? component = null, ResearchClientComponent? clientComponent = null)
|
|
{
|
|
if (!Resolve(uid, ref component, ref clientComponent, false))
|
|
return;
|
|
|
|
|
|
var points = 0;
|
|
var nextRediscover = TimeSpan.MaxValue;
|
|
var rediscoverCost = 0;
|
|
if (TryGetClientServer(uid, out _, out var serverComponent, clientComponent) && clientComponent.ConnectedToServer)
|
|
{
|
|
points = serverComponent.Points;
|
|
nextRediscover = serverComponent.NextRediscover;
|
|
rediscoverCost = serverComponent.RediscoverCost;
|
|
}
|
|
var state = new ResearchConsoleBoundInterfaceState(points, nextRediscover, rediscoverCost);
|
|
|
|
_uiSystem.SetUiState(uid, ResearchConsoleUiKey.Key, state);
|
|
}
|
|
|
|
private void OnPointsChanged(EntityUid uid, ResearchConsoleComponent component, ref ResearchServerPointsChangedEvent args)
|
|
{
|
|
if (!_uiSystem.IsUiOpen(uid, ResearchConsoleUiKey.Key))
|
|
return;
|
|
UpdateConsoleInterface(uid, component);
|
|
}
|
|
|
|
private void OnConsoleRegistrationChanged(EntityUid uid, ResearchConsoleComponent component, ref ResearchRegistrationChangedEvent args)
|
|
{
|
|
SyncClientWithServer(uid);
|
|
UpdateConsoleInterface(uid, component);
|
|
}
|
|
|
|
private void OnConsoleDatabaseModified(EntityUid uid, ResearchConsoleComponent component, ref TechnologyDatabaseModifiedEvent args)
|
|
{
|
|
SyncClientWithServer(uid);
|
|
UpdateConsoleInterface(uid, component);
|
|
}
|
|
|
|
private void OnConsoleDatabaseSynchronized(EntityUid uid, ResearchConsoleComponent component, ref TechnologyDatabaseSynchronizedEvent args)
|
|
{
|
|
UpdateConsoleInterface(uid, component);
|
|
}
|
|
|
|
private void OnEmagged(Entity<ResearchConsoleComponent> ent, ref GotEmaggedEvent args)
|
|
{
|
|
if (!_emag.CompareFlag(args.Type, EmagType.Interaction))
|
|
return;
|
|
|
|
if (_emag.CheckFlag(ent, EmagType.Interaction))
|
|
return;
|
|
|
|
args.Handled = true;
|
|
}
|
|
|
|
private bool HasAccess(EntityUid uid, EntityUid act)
|
|
{
|
|
return TryComp<AccessReaderComponent>(uid, out var access) && _accessReader.IsAllowed(act, uid, access);
|
|
}
|
|
}
|