feat: RnD tech research console now have reroll feature (#32931)

* feat: RnD tech research console now have reroll feature

* fix: disable Rediscover button when there is not enough currency or user have no access

* refactor: xml-doc, extract method, minor simplify xaml

* minor cleanup after review

* refactor: change sending research server points amount into BUI from state to  ResearchServerComponent (using AfterAutoHandleStateEvent)

* feat: now tech rerolls will have cooldown to ensure no one can spam-spend all dept budget instantly

* refactor: revert unneeded code

* refactor: whitespaces

---------

Co-authored-by: pa.pecherskij <pa.pecherskij@interfax.ru>
This commit is contained in:
Fildrance
2025-12-19 00:06:24 +03:00
committed by GitHub
parent fcf8207219
commit 1f2d80297c
10 changed files with 168 additions and 60 deletions

View File

@@ -1,8 +1,5 @@
using Content.Shared.Research.Systems;
using Content.Shared.Research.Systems;
namespace Content.Client.Research;
public sealed class ResearchSystem : SharedResearchSystem
{
}
public sealed class ResearchSystem : SharedResearchSystem;

View File

@@ -7,23 +7,22 @@ using Robust.Shared.Prototypes;
namespace Content.Client.Research.UI;
[UsedImplicitly]
public sealed class ResearchConsoleBoundUserInterface : BoundUserInterface
public sealed class ResearchConsoleBoundUserInterface(EntityUid owner, Enum uiKey) : BoundUserInterface(owner, uiKey)
{
[ViewVariables]
private ResearchConsoleMenu? _consoleMenu;
public ResearchConsoleBoundUserInterface(EntityUid owner, Enum uiKey) : base(owner, uiKey)
{
}
protected override void Open()
{
base.Open();
var owner = Owner;
_consoleMenu = this.CreateWindow<ResearchConsoleMenu>();
_consoleMenu.SetEntity(owner);
_consoleMenu.SetEntity(Owner);
_consoleMenu.OnTechnologyRediscoverPressed += () =>
{
SendMessage(new ConsoleRediscoverTechnologyMessage());
};
_consoleMenu.OnTechnologyCardPressed += id =>
{
@@ -56,6 +55,7 @@ public sealed class ResearchConsoleBoundUserInterface : BoundUserInterface
if (state is not ResearchConsoleBoundInterfaceState castState)
return;
_consoleMenu?.UpdatePanels(castState);
_consoleMenu?.UpdateInformationPanel(castState);
}

View File

@@ -1,4 +1,4 @@
<controls:FancyWindow xmlns="https://spacestation14.io"
<controls:FancyWindow xmlns="https://spacestation14.io"
xmlns:gfx="clr-namespace:Robust.Client.Graphics;assembly=Robust.Client"
xmlns:controls="clr-namespace:Content.Client.UserInterface.Controls"
xmlns:customControls="clr-namespace:Content.Client.Administration.UI.CustomControls"
@@ -22,6 +22,7 @@
</BoxContainer>
<BoxContainer Orientation="Vertical" VerticalExpand="True" HorizontalAlignment="Right">
<Button Name="ServerButton" Text="{Loc 'research-console-menu-server-selection-button'}" MinHeight="40"/>
<Button Name="RediscoverButton" Margin="0 5 0 0" ToolTip="{Loc 'research-console-menu-server-rediscover-tooltip'}" MinHeight="40"/>
</BoxContainer>
</BoxContainer>
<BoxContainer Orientation="Horizontal"

View File

@@ -12,6 +12,7 @@ using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
namespace Content.Client.Research.UI;
@@ -20,15 +21,26 @@ namespace Content.Client.Research.UI;
public sealed partial class ResearchConsoleMenu : FancyWindow
{
public Action<string>? OnTechnologyCardPressed;
public Action? OnTechnologyRediscoverPressed;
public Action? OnServerButtonPressed;
[Dependency] private readonly IEntityManager _entity = default!;
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IPlayerManager _player = default!;
[Dependency] private readonly IGameTiming _timing = default!;
private readonly ResearchSystem _research;
private readonly SpriteSystem _sprite;
private readonly AccessReaderSystem _accessReader;
// if set to null - we are waiting for server info and should not let rerolls
private TimeSpan? _nextRediscover;
private int _rediscoverCost;
private int _serverPoints;
private TimeSpan _nextUpdate;
private readonly TimeSpan _updateInterval = TimeSpan.FromMilliseconds(500);
public EntityUid Entity;
public ResearchConsoleMenu()
@@ -41,6 +53,7 @@ public sealed partial class ResearchConsoleMenu : FancyWindow
_accessReader = _entity.System<AccessReaderSystem>();
ServerButton.OnPressed += _ => OnServerButtonPressed?.Invoke();
RediscoverButton.OnPressed += OnRediscoverPressed;
}
public void SetEntity(EntityUid entity)
@@ -64,9 +77,7 @@ public sealed partial class ResearchConsoleMenu : FancyWindow
MinHeight = 10
});
var hasAccess = _player.LocalEntity is not { } local ||
!_entity.TryGetComponent<AccessReaderComponent>(Entity, out var access) ||
_accessReader.IsAllowed(local, Entity, access);
var hasAccess = HasAccess();
foreach (var techId in database.CurrentTechnologyCards)
{
var tech = _prototype.Index<TechnologyPrototype>(techId);
@@ -79,6 +90,12 @@ public sealed partial class ResearchConsoleMenu : FancyWindow
SyncTechnologyList(UnlockedCardsContainer, unlockedTech);
}
private void UpdateRediscoverButton()
{
RediscoverButton.Disabled = !HasAccess() || _serverPoints < _rediscoverCost || _timing.CurTime < _nextRediscover;
RediscoverButton.Text = Loc.GetString("research-console-menu-server-rediscover-button", ("cost", _rediscoverCost));
}
public void UpdateInformationPanel(ResearchConsoleBoundInterfaceState state)
{
var amountMsg = new FormattedMessage();
@@ -137,6 +154,27 @@ public sealed partial class ResearchConsoleMenu : FancyWindow
};
TierDisplayContainer.AddChild(control);
}
_serverPoints = state.Points;
_rediscoverCost = state.RediscoverCost;
_nextRediscover = state.NextRediscover;
UpdateRediscoverButton();
}
private void OnRediscoverPressed(BaseButton.ButtonEventArgs args)
{
RediscoverButton.Disabled = true;
_nextRediscover = null;
OnTechnologyRediscoverPressed?.Invoke();
}
private bool HasAccess()
{
return _player.LocalEntity is not { } local
|| !_entity.TryGetComponent<AccessReaderComponent>(Entity, out var access)
|| _accessReader.IsAllowed(local, Entity, access);
}
/// <summary>
@@ -179,5 +217,24 @@ public sealed partial class ResearchConsoleMenu : FancyWindow
container.Children.Remove(techControl);
}
}
/// <inheritdoc />
protected override void FrameUpdate(FrameEventArgs args)
{
base.FrameUpdate(args);
if(_nextUpdate > _timing.CurTime)
return;
_nextUpdate = _timing.CurTime + _updateInterval;
if (!RediscoverButton.Disabled)
return;
if (_nextRediscover == null || _nextRediscover > _timing.CurTime)
return;
UpdateRediscoverButton();
}
}

View File

@@ -17,6 +17,7 @@ public sealed partial class ResearchSystem
private void InitializeConsole()
{
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleUnlockTechnologyMessage>(OnConsoleUnlock);
SubscribeLocalEvent<ResearchConsoleComponent, ConsoleRediscoverTechnologyMessage>(OnRediscoverTechnology);
SubscribeLocalEvent<ResearchConsoleComponent, BeforeActivatableUIOpenEvent>(OnConsoleBeforeUiOpened);
SubscribeLocalEvent<ResearchConsoleComponent, ResearchServerPointsChangedEvent>(OnPointsChanged);
SubscribeLocalEvent<ResearchConsoleComponent, ResearchRegistrationChangedEvent>(OnConsoleRegistrationChanged);
@@ -25,6 +26,41 @@ public sealed partial class ResearchSystem
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;
@@ -35,7 +71,7 @@ public sealed partial class ResearchSystem
if (!PrototypeManager.TryIndex<TechnologyPrototype>(args.Id, out var technologyPrototype))
return;
if (TryComp<AccessReaderComponent>(uid, out var access) && !_accessReader.IsAllowed(act, uid, access))
if (!HasAccess(uid, act))
{
_popup.PopupEntity(Loc.GetString("research-console-no-access-popup"), act);
return;
@@ -72,17 +108,17 @@ public sealed partial class ResearchSystem
if (!Resolve(uid, ref component, ref clientComponent, false))
return;
ResearchConsoleBoundInterfaceState state;
if (TryGetClientServer(uid, out _, out var serverComponent, clientComponent))
var points = 0;
var nextRediscover = TimeSpan.MaxValue;
var rediscoverCost = 0;
if (TryGetClientServer(uid, out _, out var serverComponent, clientComponent) && clientComponent.ConnectedToServer)
{
var points = clientComponent.ConnectedToServer ? serverComponent.Points : 0;
state = new ResearchConsoleBoundInterfaceState(points);
}
else
{
state = new ResearchConsoleBoundInterfaceState(default);
points = serverComponent.Points;
nextRediscover = serverComponent.NextRediscover;
rediscoverCost = serverComponent.RediscoverCost;
}
var state = new ResearchConsoleBoundInterfaceState(points, nextRediscover, rediscoverCost);
_uiSystem.SetUiState(uid, ResearchConsoleUiKey.Key, state);
}
@@ -121,4 +157,9 @@ public sealed partial class ResearchSystem
args.Handled = true;
}
private bool HasAccess(EntityUid uid, EntityUid act)
{
return TryComp<AccessReaderComponent>(uid, out var access) && _accessReader.IsAllowed(act, uid, access);
}
}

View File

@@ -165,10 +165,10 @@ public sealed partial class ResearchSystem
if (args.Server != null)
return;
component.MainDiscipline = null;
component.CurrentTechnologyCards = new List<string>();
component.SupportedDisciplines = new List<ProtoId<TechDisciplinePrototype>>();
component.UnlockedTechnologies = new List<ProtoId<TechnologyPrototype>>();
component.UnlockedRecipes = new List<ProtoId<LatheRecipePrototype>>();
component.CurrentTechnologyCards = new();
component.SupportedDisciplines = new();
component.UnlockedTechnologies = new();
component.UnlockedRecipes = new();
Dirty(uid, component);
}
}

View File

@@ -1,26 +1,32 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Research.Components;
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState]
[RegisterComponent, NetworkedComponent, AutoGenerateComponentState, AutoGenerateComponentPause]
public sealed partial class ResearchServerComponent : Component
{
/// <summary>
/// The name of the server
/// </summary>
[AutoNetworkedField]
[DataField("serverName"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public string ServerName = "RDSERVER";
/// <summary>
/// The amount of points on the server.
/// </summary>
[AutoNetworkedField]
[DataField("points"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public int Points;
/// <summary>
/// Cost of technology research options reroll.
/// </summary>
[AutoNetworkedField]
[DataField]
public int RediscoverCost = 2000;
/// <summary>
/// A unique numeric id representing the server
/// </summary>
@@ -37,27 +43,34 @@ public sealed partial class ResearchServerComponent : Component
[ViewVariables(VVAccess.ReadOnly)]
public List<EntityUid> Clients = new();
[DataField("nextUpdateTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan NextUpdateTime = TimeSpan.Zero;
[DataField("researchConsoleUpdateTime"), ViewVariables(VVAccess.ReadWrite)]
[DataField]
public TimeSpan ResearchConsoleUpdateTime = TimeSpan.FromSeconds(1);
/// <summary>
/// Time when next reroll for tech to research will be available.
/// </summary>
[DataField(customTypeSerializer: typeof(TimeOffsetSerializer)), AutoPausedField]
public TimeSpan NextRediscover;
/// <summary>
/// Minimal interval between rediscover actions.
/// </summary>
[DataField]
public TimeSpan RediscoverInterval = TimeSpan.FromSeconds(1);
}
/// <summary>
/// Event raised on a server's clients when the point value of the server is changed.
/// </summary>
/// <param name="Server"></param>
/// <param name="Total"></param>
/// <param name="Delta"></param>
[ByRefEvent]
public readonly record struct ResearchServerPointsChangedEvent(EntityUid Server, int Total, int Delta);
/// <summary>
/// Event raised every second to calculate the amount of points added to the server.
/// </summary>
/// <param name="Server"></param>
/// <param name="Points"></param>
[ByRefEvent]
public record struct ResearchServerGetPointsPerSecondEvent(EntityUid Server, int Points);

View File

@@ -9,16 +9,14 @@ namespace Content.Shared.Research.Components
}
[Serializable, NetSerializable]
public sealed class ConsoleUnlockTechnologyMessage : BoundUserInterfaceMessage
public sealed class ConsoleUnlockTechnologyMessage(string id) : BoundUserInterfaceMessage
{
public string Id;
public ConsoleUnlockTechnologyMessage(string id)
{
Id = id;
}
public string Id = id;
}
[Serializable, NetSerializable]
public sealed class ConsoleRediscoverTechnologyMessage : BoundUserInterfaceMessage;
[Serializable, NetSerializable]
public sealed class ConsoleServerSelectionMessage : BoundUserInterfaceMessage
{
@@ -26,12 +24,12 @@ namespace Content.Shared.Research.Components
}
[Serializable, NetSerializable]
public sealed class ResearchConsoleBoundInterfaceState : BoundUserInterfaceState
public sealed class ResearchConsoleBoundInterfaceState(int points, TimeSpan nextRediscover, int rediscoverCost) : BoundUserInterfaceState
{
public int Points;
public ResearchConsoleBoundInterfaceState(int points)
{
Points = points;
}
public int Points = points;
public TimeSpan NextRediscover = nextRediscover;
public int RediscoverCost = rediscoverCost;
}
}

View File

@@ -3,7 +3,6 @@ using Content.Shared.Research.Prototypes;
using Content.Shared.Research.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Research.Components;
@@ -14,12 +13,12 @@ public sealed partial class TechnologyDatabaseComponent : Component
/// A main discipline that locks out other discipline technology past a certain tier.
/// </summary>
[AutoNetworkedField]
[DataField("mainDiscipline", customTypeSerializer: typeof(PrototypeIdSerializer<TechDisciplinePrototype>))]
public string? MainDiscipline;
[DataField]
public ProtoId<TechDisciplinePrototype>? MainDiscipline;
[AutoNetworkedField]
[DataField("currentTechnologyCards")]
public List<string> CurrentTechnologyCards = new();
[DataField]
public List<ProtoId<TechnologyPrototype>> CurrentTechnologyCards = new();
/// <summary>
/// Which research disciplines are able to be unlocked

View File

@@ -6,6 +6,8 @@ research-console-menu-main-discipline = Main Discipline: [color={$color}]{$name}
research-console-menu-server-selection-button = Server list
research-console-menu-server-sync-button = Sync
research-console-menu-server-research-button = Research
research-console-menu-server-rediscover-button = Rediscover ({$cost})
research-console-menu-server-rediscover-tooltip = Reroll list of technologies to research
research-console-available-text = Researchable Technologies
research-console-unlocked-text = Unlocked Technologies
research-console-tier-discipline-info = Tier {$tier}, [color={$color}]{$discipline}[/color]