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:
@@ -1,97 +1,164 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Power.EntitySystems;
|
||||
using Content.Server.Research.Components;
|
||||
using Content.Shared.Research.Components;
|
||||
using Content.Shared.Research.Prototypes;
|
||||
|
||||
namespace Content.Server.Research.Systems;
|
||||
|
||||
public sealed partial class ResearchSystem
|
||||
{
|
||||
private bool CanRun(ResearchServerComponent component)
|
||||
private void InitializeServer()
|
||||
{
|
||||
return this.IsPowered(component.Owner, EntityManager);
|
||||
SubscribeLocalEvent<ResearchServerComponent, ComponentStartup>(OnServerStartup);
|
||||
SubscribeLocalEvent<ResearchServerComponent, ComponentShutdown>(OnServerShutdown);
|
||||
SubscribeLocalEvent<ResearchServerComponent, TechnologyDatabaseModifiedEvent>(OnServerDatabaseModified);
|
||||
}
|
||||
|
||||
private void UpdateServer(ResearchServerComponent component, int time)
|
||||
private void OnServerStartup(EntityUid uid, ResearchServerComponent component, ComponentStartup args)
|
||||
{
|
||||
if (!CanRun(component))
|
||||
var unusedId = EntityQuery<ResearchServerComponent>(true)
|
||||
.Max(s => s.Id) + 1;
|
||||
component.Id = unusedId;
|
||||
Dirty(component);
|
||||
}
|
||||
|
||||
private void OnServerShutdown(EntityUid uid, ResearchServerComponent component, ComponentShutdown args)
|
||||
{
|
||||
foreach (var client in new List<EntityUid>(component.Clients))
|
||||
{
|
||||
UnregisterClient(client, uid, serverComponent: component, dirtyServer: false);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnServerDatabaseModified(EntityUid uid, ResearchServerComponent component, ref TechnologyDatabaseModifiedEvent args)
|
||||
{
|
||||
foreach (var client in component.Clients)
|
||||
{
|
||||
RaiseLocalEvent(client, ref args);
|
||||
}
|
||||
}
|
||||
|
||||
private bool CanRun(EntityUid uid)
|
||||
{
|
||||
return this.IsPowered(uid, EntityManager);
|
||||
}
|
||||
|
||||
private void UpdateServer(EntityUid uid, int time, ResearchServerComponent? component = null)
|
||||
{
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
ChangePointsOnServer(component.Owner, PointsPerSecond(component) * time, component);
|
||||
|
||||
if (!CanRun(uid))
|
||||
return;
|
||||
AddPointsToServer(uid, PointsPerSecond(uid, component) * time, component);
|
||||
}
|
||||
|
||||
public bool RegisterServerClient(ResearchServerComponent component, EntityUid client, ResearchClientComponent? clientComponent = null)
|
||||
/// <summary>
|
||||
/// Registers a client to the specified server.
|
||||
/// </summary>
|
||||
/// <param name="client">The client being registered</param>
|
||||
/// <param name="server">The server the client is being registered to</param>
|
||||
/// <param name="clientComponent"></param>
|
||||
/// <param name="serverComponent"></param>
|
||||
/// <param name="dirtyServer">Whether or not to dirty the server component after registration</param>
|
||||
/// <returns>Whether or not the client was successfully registered to the server</returns>
|
||||
public bool RegisterClient(EntityUid client, EntityUid server, ResearchClientComponent? clientComponent = null,
|
||||
ResearchServerComponent? serverComponent = null, bool dirtyServer = true)
|
||||
{
|
||||
if (!Resolve(client, ref clientComponent))
|
||||
if (!Resolve(client, ref clientComponent) || !Resolve(server, ref serverComponent))
|
||||
return false;
|
||||
|
||||
if (component.Clients.Contains(client))
|
||||
if (serverComponent.Clients.Contains(client))
|
||||
return false;
|
||||
component.Clients.Add(client);
|
||||
clientComponent.Server = component;
|
||||
|
||||
serverComponent.Clients.Add(client);
|
||||
clientComponent.Server = server;
|
||||
|
||||
if (dirtyServer)
|
||||
Dirty(serverComponent);
|
||||
|
||||
var ev = new ResearchRegistrationChangedEvent(server);
|
||||
RaiseLocalEvent(client, ref ev);
|
||||
return true;
|
||||
}
|
||||
|
||||
public void UnregisterServerClient(ResearchServerComponent component, EntityUid client, ResearchClientComponent? clientComponent = null)
|
||||
/// <summary>
|
||||
/// Unregisterse a client from its server
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="clientComponent"></param>
|
||||
/// <param name="dirtyServer"></param>
|
||||
public void UnregisterClient(EntityUid client, ResearchClientComponent? clientComponent = null, bool dirtyServer = true)
|
||||
{
|
||||
if (!Resolve(client, ref clientComponent))
|
||||
return;
|
||||
|
||||
component.Clients.Remove(client);
|
||||
if (clientComponent.Server is not { } server)
|
||||
return;
|
||||
|
||||
UnregisterClient(client, server, clientComponent, dirtyServer: dirtyServer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters a client from its server
|
||||
/// </summary>
|
||||
/// <param name="client"></param>
|
||||
/// <param name="server"></param>
|
||||
/// <param name="clientComponent"></param>
|
||||
/// <param name="serverComponent"></param>
|
||||
/// <param name="dirtyServer"></param>
|
||||
public void UnregisterClient(EntityUid client, EntityUid server, ResearchClientComponent? clientComponent = null,
|
||||
ResearchServerComponent? serverComponent = null, bool dirtyServer = true)
|
||||
{
|
||||
if (!Resolve(client, ref clientComponent) || !Resolve(server, ref serverComponent))
|
||||
return;
|
||||
|
||||
serverComponent.Clients.Remove(client);
|
||||
clientComponent.Server = null;
|
||||
|
||||
if (dirtyServer)
|
||||
{
|
||||
Dirty(serverComponent);
|
||||
}
|
||||
|
||||
var ev = new ResearchRegistrationChangedEvent(null);
|
||||
RaiseLocalEvent(client, ref ev);
|
||||
}
|
||||
|
||||
public bool IsTechnologyUnlocked(ResearchServerComponent component, TechnologyPrototype prototype,
|
||||
TechnologyDatabaseComponent? databaseComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref databaseComponent, false))
|
||||
return false;
|
||||
return IsTechnologyUnlocked(databaseComponent.Owner, prototype.ID, databaseComponent);
|
||||
}
|
||||
|
||||
public bool CanUnlockTechnology(ResearchServerComponent component, TechnologyPrototype technology, TechnologyDatabaseComponent? databaseComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref databaseComponent, false))
|
||||
return false;
|
||||
|
||||
if (!CanUnlockTechnology(databaseComponent.Owner, technology, databaseComponent) ||
|
||||
component.Points < technology.RequiredPoints ||
|
||||
IsTechnologyUnlocked(component, technology, databaseComponent))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public bool UnlockTechnology(ResearchServerComponent component, TechnologyPrototype prototype,
|
||||
TechnologyDatabaseComponent? databaseComponent = null)
|
||||
{
|
||||
if (!Resolve(component.Owner, ref databaseComponent, false))
|
||||
return false;
|
||||
|
||||
if (!CanUnlockTechnology(component, prototype, databaseComponent))
|
||||
return false;
|
||||
var result = UnlockTechnology(databaseComponent, prototype);
|
||||
if (result)
|
||||
ChangePointsOnServer(component.Owner, -prototype.RequiredPoints, component);
|
||||
return result;
|
||||
}
|
||||
|
||||
public int PointsPerSecond(ResearchServerComponent component)
|
||||
/// <summary>
|
||||
/// Gets the amount of points generated by all the server's sources in a second.
|
||||
/// </summary>
|
||||
/// <param name="uid"></param>
|
||||
/// <param name="component"></param>
|
||||
/// <returns></returns>
|
||||
public int PointsPerSecond(EntityUid uid, ResearchServerComponent? component = null)
|
||||
{
|
||||
var points = 0;
|
||||
|
||||
if (!CanRun(component))
|
||||
if (!Resolve(uid, ref component))
|
||||
return points;
|
||||
|
||||
if (!CanRun(uid))
|
||||
return points;
|
||||
|
||||
var ev = new ResearchServerGetPointsPerSecondEvent(component.Owner, points);
|
||||
foreach (var client in component.Clients)
|
||||
{
|
||||
RaiseLocalEvent(client, ref ev);
|
||||
}
|
||||
|
||||
return ev.Points;
|
||||
}
|
||||
|
||||
public void ChangePointsOnServer(EntityUid uid, int points, ResearchServerComponent? component = null)
|
||||
/// <summary>
|
||||
/// Adds a specified number of points to a server.
|
||||
/// </summary>
|
||||
/// <param name="uid">The server</param>
|
||||
/// <param name="points">The amount of points being added</param>
|
||||
/// <param name="component"></param>
|
||||
public void AddPointsToServer(EntityUid uid, int points, ResearchServerComponent? component = null)
|
||||
{
|
||||
if (points == 0)
|
||||
return;
|
||||
|
||||
if (!Resolve(uid, ref component))
|
||||
return;
|
||||
component.Points += points;
|
||||
@@ -100,5 +167,6 @@ public sealed partial class ResearchSystem
|
||||
{
|
||||
RaiseLocalEvent(client, ref ev);
|
||||
}
|
||||
Dirty(component);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user