Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,109 @@
#nullable enable
using Content.Server.UserInterface;
using Content.Shared.Interaction;
using Content.Shared.Research.Components;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
namespace Content.Server.Research.Components
{
[RegisterComponent]
public class ResearchClientComponent : SharedResearchClientComponent, IActivate
{
[Dependency] private readonly IEntitySystemManager _entitySystemManager = default!;
// TODO: Create GUI for changing RD server.
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ResearchClientUiKey.Key);
public bool ConnectedToServer => Server != null;
[ViewVariables(VVAccess.ReadOnly)]
public ResearchServerComponent? Server { get; set; }
public bool RegisterServer(ResearchServerComponent? server)
{
var result = server != null && server.RegisterClient(this);
return result;
}
public void UnregisterFromServer()
{
Server?.UnregisterClient(this);
}
public override void Initialize()
{
base.Initialize();
// For now it just registers on the first server it can find.
var servers = _entitySystemManager.GetEntitySystem<ResearchSystem>().Servers;
if (servers.Count > 0)
RegisterServer(servers[0]);
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
}
public void OpenUserInterface(IPlayerSession session)
{
UpdateUserInterface();
UserInterface?.Open(session);
}
void IActivate.Activate(ActivateEventArgs eventArgs)
{
if (!eventArgs.User.TryGetComponent(out ActorComponent? actor))
return;
OpenUserInterface(actor.PlayerSession);
}
public void UpdateUserInterface()
{
UserInterface?.SetState(GetNewUiState());
}
private ResearchClientBoundInterfaceState GetNewUiState()
{
var rd = _entitySystemManager.GetEntitySystem<ResearchSystem>();
return new ResearchClientBoundInterfaceState(rd.Servers.Count, rd.GetServerNames(),
rd.GetServerIds(), ConnectedToServer ? Server!.Id : -1);
}
private void UserInterfaceOnOnReceiveMessage(ServerBoundUserInterfaceMessage msg)
{
switch (msg.Message)
{
case ResearchClientSyncMessage _:
UpdateUserInterface();
break;
case ResearchClientServerSelectedMessage selectedMessage:
UnregisterFromServer();
RegisterServer(_entitySystemManager.GetEntitySystem<ResearchSystem>().GetServerById(selectedMessage.ServerId));
UpdateUserInterface();
break;
case ResearchClientServerDeselectedMessage _:
UnregisterFromServer();
UpdateUserInterface();
break;
}
}
/// <inheritdoc />
protected override void Shutdown()
{
base.Shutdown();
UnregisterFromServer();
}
}
}

View File

@@ -0,0 +1,131 @@
#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 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.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!;
private const string SoundCollectionName = "keyboard";
[ViewVariables] private bool Powered => !Owner.TryGetComponent(out PowerReceiverComponent? receiver) || receiver.Powered;
[ViewVariables] private BoundUserInterface? UserInterface => Owner.GetUIOrNull(ResearchConsoleUiKey.Key);
public override void Initialize()
{
base.Initialize();
Owner.EnsureComponentWarn<ServerUserInterfaceComponent>();
if (UserInterface != null)
{
UserInterface.OnReceiveMessage += UserInterfaceOnOnReceiveMessage;
}
Owner.EnsureComponent<ResearchClientComponent>();
}
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;
}
}
/// <summary>
/// Method to update the user interface on the clients.
/// </summary>
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);
}
/// <summary>
/// Open the user interface on a certain player session.
/// </summary>
/// <param name="session">Session where the UI will be shown</param>
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()
{
var soundCollection = _prototypeManager.Index<SoundCollectionPrototype>(SoundCollectionName);
var file = _random.Pick(soundCollection.PickFiles);
SoundSystem.Play(Filter.Pvs(Owner), file,Owner,AudioParams.Default);
}
}
}

View File

@@ -0,0 +1,48 @@
using Content.Server.Power.Components;
using Content.Shared.Interaction;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Research.Components
{
[RegisterComponent]
[ComponentReference(typeof(IActivate))]
public class ResearchPointSourceComponent : ResearchClientComponent
{
public override string Name => "ResearchPointSource";
[DataField("pointspersecond")]
private int _pointsPerSecond;
[DataField("active")]
private bool _active;
private PowerReceiverComponent? _powerReceiver;
[ViewVariables(VVAccess.ReadWrite)]
public int PointsPerSecond
{
get => _pointsPerSecond;
set => _pointsPerSecond = value;
}
[ViewVariables(VVAccess.ReadWrite)]
public bool Active
{
get => _active;
set => _active = value;
}
/// <summary>
/// Whether this can be used to produce research points.
/// </summary>
/// <remarks>If no <see cref="PowerReceiverComponent"/> is found, it's assumed power is not required.</remarks>
[ViewVariables]
public bool CanProduce => Active && (_powerReceiver is null || _powerReceiver.Powered);
public override void Initialize()
{
base.Initialize();
Owner.TryGetComponent(out _powerReceiver);
}
}
}

View File

@@ -0,0 +1,169 @@
using System.Collections.Generic;
using Content.Server.Power.Components;
using Content.Shared.Research.Prototypes;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Research.Components
{
[RegisterComponent]
public class ResearchServerComponent : Component
{
public static int ServerCount = 0;
public override string Name => "ResearchServer";
[ViewVariables(VVAccess.ReadWrite)] public string ServerName => _serverName;
[DataField("servername")]
private string _serverName = "RDSERVER";
private float _timer = 0f;
public TechnologyDatabaseComponent? Database { get; private set; }
[ViewVariables(VVAccess.ReadWrite)] [DataField("points")] private int _points = 0;
[ViewVariables(VVAccess.ReadOnly)] public int Id { get; private set; }
// You could optimize research by keeping a list of unlocked recipes too.
[ViewVariables(VVAccess.ReadOnly)]
public IReadOnlyList<TechnologyPrototype>? UnlockedTechnologies => Database?.Technologies;
[ViewVariables(VVAccess.ReadOnly)]
public List<ResearchPointSourceComponent> PointSources { get; } = new();
[ViewVariables(VVAccess.ReadOnly)]
public List<ResearchClientComponent> Clients { get; } = new();
public int Point => _points;
/// <summary>
/// How many points per second this R&D server gets.
/// The value is calculated from all point sources connected to it.
/// </summary>
[ViewVariables(VVAccess.ReadOnly)]
public int PointsPerSecond
{
// This could be changed to PointsPerMinute quite easily for optimization.
get
{
var points = 0;
if (CanRun)
{
foreach (var source in PointSources)
{
if (source.CanProduce) points += source.PointsPerSecond;
}
}
return points;
}
}
/// <remarks>If no <see cref="PowerReceiverComponent"/> is found, it's assumed power is not required.</remarks>
[ViewVariables]
public bool CanRun => _powerReceiver is null || _powerReceiver.Powered;
private PowerReceiverComponent? _powerReceiver;
public override void Initialize()
{
base.Initialize();
Id = ServerCount++;
EntitySystem.Get<ResearchSystem>()?.RegisterServer(this);
Database = Owner.EnsureComponent<TechnologyDatabaseComponent>();
Owner.TryGetComponent(out _powerReceiver);
}
/// <inheritdoc />
protected override void Shutdown()
{
base.Shutdown();
EntitySystem.Get<ResearchSystem>()?.UnregisterServer(this);
}
public bool CanUnlockTechnology(TechnologyPrototype technology)
{
if (Database == null)
return false;
if (!Database.CanUnlockTechnology(technology) ||
_points < technology.RequiredPoints ||
Database.IsTechnologyUnlocked(technology))
return false;
return true;
}
/// <summary>
/// Unlocks a technology, but only if there are enough research points for it.
/// If there are, it subtracts the amount of points from the total.
/// </summary>
/// <param name="technology"></param>
/// <returns></returns>
public bool UnlockTechnology(TechnologyPrototype technology)
{
if (!CanUnlockTechnology(technology)) return false;
var result = Database?.UnlockTechnology(technology) ?? false;
if (result)
_points -= technology.RequiredPoints;
return result;
}
/// <summary>
/// Check whether a technology is unlocked or not.
/// </summary>
/// <param name="technology"></param>
/// <returns></returns>
public bool IsTechnologyUnlocked(TechnologyPrototype technology)
{
return Database?.IsTechnologyUnlocked(technology) ?? false;
}
/// <summary>
/// Registers a remote client on this research server.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
public bool RegisterClient(ResearchClientComponent client)
{
if (client is ResearchPointSourceComponent source)
{
if (PointSources.Contains(source)) return false;
PointSources.Add(source);
source.Server = this;
return true;
}
if (Clients.Contains(client)) return false;
Clients.Add(client);
client.Server = this;
return true;
}
/// <summary>
/// Unregisters a remote client from this server.
/// </summary>
/// <param name="client"></param>
public void UnregisterClient(ResearchClientComponent client)
{
if (client is ResearchPointSourceComponent source)
{
PointSources.Remove(source);
return;
}
Clients.Remove(client);
}
public void Update(float frameTime)
{
if (!CanRun) return;
_timer += frameTime;
if (_timer < 1f) return;
_timer = 0f;
_points += PointsPerSecond;
}
}
}

View File

@@ -0,0 +1,75 @@
using Content.Shared.Research.Components;
using Content.Shared.Research.Prototypes;
using Robust.Shared.GameObjects;
using Robust.Shared.Players;
namespace Content.Server.Research.Components
{
[RegisterComponent]
public class TechnologyDatabaseComponent : SharedTechnologyDatabaseComponent
{
public override ComponentState GetComponentState(ICommonSession player)
{
return new TechnologyDatabaseState(_technologies);
}
/// <summary>
/// Synchronizes this database against other,
/// adding all technologies from the other that
/// this one doesn't have.
/// </summary>
/// <param name="otherDatabase">The other database</param>
/// <param name="twoway">Whether the other database should be synced against this one too or not.</param>
public void Sync(TechnologyDatabaseComponent otherDatabase, bool twoway = true)
{
foreach (var tech in otherDatabase.Technologies)
{
if (!IsTechnologyUnlocked(tech)) AddTechnology(tech);
}
if (twoway)
otherDatabase.Sync(this, false);
Dirty();
}
/// <summary>
/// If there's a research client component attached to the owner entity,
/// and the research client is connected to a research server, this method
/// syncs against the research server, and the server against the local database.
/// </summary>
/// <returns>Whether it could sync or not</returns>
public bool SyncWithServer()
{
if (!Owner.TryGetComponent(out ResearchClientComponent? client)) return false;
if (client.Server?.Database == null) return false;
Sync(client.Server.Database);
return true;
}
/// <summary>
/// If possible, unlocks a technology on this database.
/// </summary>
/// <param name="technology"></param>
/// <returns></returns>
public bool UnlockTechnology(TechnologyPrototype technology)
{
if (!CanUnlockTechnology(technology)) return false;
AddTechnology(technology);
Dirty();
return true;
}
/// <summary>
/// Adds a technology to the database without checking if it could be unlocked.
/// </summary>
/// <param name="technology"></param>
public void AddTechnology(TechnologyPrototype technology)
{
_technologies.Add(technology);
}
}
}

View File

@@ -0,0 +1,83 @@
using System.Collections.Generic;
using Content.Server.Research.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Content.Server.Research
{
[UsedImplicitly]
public class ResearchSystem : EntitySystem
{
private const float ResearchConsoleUIUpdateTime = 30f;
private float _timer = ResearchConsoleUIUpdateTime;
private readonly List<ResearchServerComponent> _servers = new();
public IReadOnlyList<ResearchServerComponent> Servers => _servers;
public bool RegisterServer(ResearchServerComponent server)
{
if (_servers.Contains(server)) return false;
_servers.Add(server);
return true;
}
public void UnregisterServer(ResearchServerComponent server)
{
_servers.Remove(server);
}
public ResearchServerComponent? GetServerById(int id)
{
foreach (var server in Servers)
{
if (server.Id == id) return server;
}
return null;
}
public string[] GetServerNames()
{
var list = new string[Servers.Count];
for (var i = 0; i < Servers.Count; i++)
{
list[i] = Servers[i].ServerName;
}
return list;
}
public int[] GetServerIds()
{
var list = new int[Servers.Count];
for (var i = 0; i < Servers.Count; i++)
{
list[i] = Servers[i].Id;
}
return list;
}
public override void Update(float frameTime)
{
_timer += frameTime;
foreach (var server in _servers)
{
server.Update(frameTime);
}
if (_timer >= ResearchConsoleUIUpdateTime)
{
foreach (var console in ComponentManager.EntityQuery<ResearchConsoleComponent>())
{
console.UpdateUserInterface();
}
_timer = 0f;
}
}
}
}