clean up infinite researchsystem shitcode (#13136)

* clean up infinite researchsystem shitcode

* fml some more shit

* make syncing work logically

* naming naming naming
This commit is contained in:
Nemanja
2022-12-25 16:22:23 -05:00
committed by GitHub
parent 9afbdf6a17
commit 4eee1ee9b2
23 changed files with 497 additions and 293 deletions

View File

@@ -24,19 +24,10 @@ public sealed class LatheUpdateState : BoundUserInterfaceState
/// Sent to the server to sync material storage and the recipe queue.
/// </summary>
[Serializable, NetSerializable]
public sealed class LatheSyncRequestMessage : BoundUserInterfaceMessage { }
public sealed class LatheSyncRequestMessage : BoundUserInterfaceMessage
{
/// <summary>
/// Sent to the server to sync the lathe's technology database with the research server.
/// </summary>
[Serializable, NetSerializable]
public sealed class LatheServerSyncMessage : BoundUserInterfaceMessage { }
/// <summary>
/// Sent to the server to open the ResearchClient UI.
/// </summary>
[Serializable, NetSerializable]
public sealed class LatheServerSelectionMessage : BoundUserInterfaceMessage { }
}
/// <summary>
/// Sent to the server when a client queues a new recipe.

View File

@@ -2,15 +2,34 @@
namespace Content.Shared.Research.Components
{
/// <summary>
/// This is an entity that is able to connect to a <see cref="ResearchServerComponent"/>
/// </summary>
[RegisterComponent]
public sealed class ResearchClientComponent : Component
{
public bool ConnectedToServer => Server != null;
/// <summary>
/// The server the client is connected to
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public EntityUid? Server { get; set; }
}
/// <summary>
/// Raised on the client whenever its server is changed
/// </summary>
/// <param name="Server">Its new server</param>
[ByRefEvent]
public readonly record struct ResearchRegistrationChangedEvent(EntityUid? Server);
/// <summary>
/// Sent to the server when the client deselects a research server.
/// </summary>
[Serializable, NetSerializable]
public sealed class ResearchClientServerDeselectedMessage : BoundUserInterfaceMessage
{
public ResearchClientServerDeselectedMessage()
{
}
}
/// <summary>
@@ -33,10 +52,6 @@ namespace Content.Shared.Research.Components
[Serializable, NetSerializable]
public sealed class ResearchClientSyncMessage : BoundUserInterfaceMessage
{
public ResearchClientSyncMessage()
{
}
}
[NetSerializable, Serializable]

View File

@@ -0,0 +1,74 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom;
namespace Content.Shared.Research.Components
{
[RegisterComponent, NetworkedComponent]
public sealed class ResearchServerComponent : Component
{
/// <summary>
/// The name of the server
/// </summary>
[DataField("servername"), ViewVariables(VVAccess.ReadWrite)]
public string ServerName = "RDSERVER";
/// <summary>
/// The amount of points on the server.
/// </summary>
[DataField("points"), ViewVariables(VVAccess.ReadWrite)]
public int Points;
/// <summary>
/// A unique numeric id representing the server
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public int Id;
/// <summary>
/// Entities connected to the server
/// </summary>
/// <remarks>
/// This is not safe to read clientside
/// </remarks>
[ViewVariables(VVAccess.ReadOnly)]
public List<EntityUid> Clients = new();
[DataField("nextUpdateTime", customTypeSerializer: typeof(TimeOffsetSerializer))]
public TimeSpan NextUpdateTime = TimeSpan.Zero;
[DataField("researchConsoleUpdateTime"), ViewVariables(VVAccess.ReadWrite)]
public readonly TimeSpan ResearchConsoleUpdateTime = TimeSpan.FromSeconds(1);
}
[Serializable, NetSerializable]
public sealed class ResearchServerState : ComponentState
{
public string ServerName;
public int Points;
public int Id;
public ResearchServerState(string serverName, int points, int id)
{
ServerName = serverName;
Points = points;
Id = id;
}
}
/// <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

@@ -22,15 +22,13 @@ namespace Content.Shared.Research.Components
[Serializable, NetSerializable]
public sealed class ConsoleServerSyncMessage : BoundUserInterfaceMessage
{
public ConsoleServerSyncMessage()
{}
}
[Serializable, NetSerializable]
public sealed class ConsoleServerSelectionMessage : BoundUserInterfaceMessage
{
public ConsoleServerSelectionMessage()
{}
}
[Serializable, NetSerializable]

View File

@@ -8,13 +8,31 @@ namespace Content.Shared.Research.Components
[RegisterComponent, NetworkedComponent]
public sealed class TechnologyDatabaseComponent : Component
{
/// <summary>
/// The ids of all the technologies which have been unlocked.
/// </summary>
[DataField("technologyIds", customTypeSerializer: typeof(PrototypeIdListSerializer<TechnologyPrototype>))]
public List<string> TechnologyIds = new();
/// <summary>
/// The ids of all the lathe recipes which have been unlocked.
/// This is maintained alongside the TechnologyIds
/// </summary>
[DataField("recipeIds", customTypeSerializer: typeof(PrototypeIdListSerializer<LatheRecipePrototype>))]
public List<string> RecipeIds = new();
}
/// <summary>
/// Event raised on the database whenever its
/// technologies or recipes are modified.
/// </summary>
/// <remarks>
/// This event is forwarded from the
/// server to all of it's clients.
/// </remarks>
[ByRefEvent]
public readonly record struct TechnologyDatabaseModifiedEvent;
[Serializable, NetSerializable]
public sealed class TechnologyDatabaseState : ComponentState
{

View File

@@ -1,22 +1,35 @@
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
namespace Content.Shared.Research.Systems;
public abstract class SharedResearchSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ResearchServerComponent, ComponentGetState>(OnServerGetState);
SubscribeLocalEvent<ResearchServerComponent, ComponentHandleState>(OnServerHandleState);
SubscribeLocalEvent<TechnologyDatabaseComponent, ComponentGetState>(OnTechnologyGetState);
SubscribeLocalEvent<TechnologyDatabaseComponent, ComponentHandleState>(OnTechnologyHandleState);
}
private void OnServerGetState(EntityUid uid, ResearchServerComponent component, ref ComponentGetState args)
{
args.State = new ResearchServerState(component.ServerName, component.Points, component.Id);
}
private void OnServerHandleState(EntityUid uid, ResearchServerComponent component, ref ComponentHandleState args)
{
if (args.Current is not ResearchServerState state)
return;
component.ServerName = state.ServerName;
component.Points = state.Points;
component.Id = state.Id;
}
private void OnTechnologyHandleState(EntityUid uid, TechnologyDatabaseComponent component, ref ComponentHandleState args)
{
if (args.Current is not TechnologyDatabaseState state)
@@ -34,40 +47,35 @@ public abstract class SharedResearchSystem : EntitySystem
/// Returns whether a technology is unlocked on this database or not.
/// </summary>
/// <returns>Whether it is unlocked or not</returns>
public bool IsTechnologyUnlocked(EntityUid uid, string technologyId, TechnologyDatabaseComponent? component = null)
public bool IsTechnologyUnlocked(EntityUid uid, TechnologyPrototype technology, TechnologyDatabaseComponent? component = null)
{
return Resolve(uid, ref component) && component.TechnologyIds.Contains(technologyId);
return Resolve(uid, ref component) && IsTechnologyUnlocked(uid, technology.ID, component);
}
/// <summary>
/// Returns whether a technology is unlocked on this database or not.
/// </summary>
/// <returns>Whether it is unlocked or not</returns>
public bool IsTechnologyUnlocked(EntityUid uid, TechnologyPrototype technologyId, TechnologyDatabaseComponent? component = null)
public bool IsTechnologyUnlocked(EntityUid uid, string technologyId, TechnologyDatabaseComponent? component = null)
{
return Resolve(uid, ref component) && IsTechnologyUnlocked(uid, technologyId.ID, component);
return Resolve(uid, ref component, false) && component.TechnologyIds.Contains(technologyId);
}
/// <summary>
/// Returns whether a technology can be unlocked on this database,
/// taking parent technologies into account.
/// Returns whether or not all the prerequisite
/// technologies for a technology are unlocked.
/// </summary>
/// <returns>Whether it could be unlocked or not</returns>
public bool CanUnlockTechnology(EntityUid uid, TechnologyPrototype technology, TechnologyDatabaseComponent? component = null)
/// <param name="uid"></param>
/// <param name="prototype"></param>
/// <param name="component"></param>
/// <returns>Whether or not the prerequesites are present</returns>
public bool ArePrerequesitesUnlocked(EntityUid uid, TechnologyPrototype prototype, TechnologyDatabaseComponent? component = null)
{
if (!Resolve(uid, ref component))
return false;
if (IsTechnologyUnlocked(uid, technology.ID, component))
return false;
foreach (var technologyId in technology.RequiredTechnologies)
foreach (var technologyId in prototype.RequiredTechnologies)
{
_prototypeManager.TryIndex(technologyId, out TechnologyPrototype? requiredTechnology);
if (requiredTechnology == null)
return false;
if (!IsTechnologyUnlocked(uid, requiredTechnology.ID, component))
if (!IsTechnologyUnlocked(uid, technologyId, component))
return false;
}
return true;