Skip to content

Commit

Permalink
cleanup (Rxup#416)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rxup authored Jan 13, 2024
1 parent cb8b022 commit b3e4629
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 78 deletions.
12 changes: 0 additions & 12 deletions Content.Client/Backmen/GhostTheme/GhostThemeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@ namespace Content.Client.Backmen.GhostTheme;
public sealed class GhostThemeSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IConfigurationManager _cfg = default!;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GhostThemeComponent, AfterAutoHandleStateEvent>(OnInit);
SubscribeNetworkEvent<TickerJoinGameEvent>(JoinGame);
}

private void JoinGame(TickerJoinGameEvent ev)
{
var ghostTheme = _cfg.GetCVar(Shared.Backmen.CCVar.CCVars.SponsorsSelectedGhost);
if (string.IsNullOrEmpty(ghostTheme))
{
return;
}
RaiseNetworkEvent(new RequestGhostThemeEvent(ghostTheme));
}

private void OnInit(EntityUid uid, GhostThemeComponent component, ref AfterAutoHandleStateEvent args)
Expand Down
82 changes: 28 additions & 54 deletions Content.Server/Backmen/GhostTheme/GhostThemeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Content.Shared.GameTicking;
using Content.Shared.Ghost;
using Content.Shared.Players;
using Robust.Server.Configuration;
using Robust.Shared;
using Robust.Shared.Network;
using Robust.Shared.Player;
using Robust.Shared.Prototypes;
Expand All @@ -20,72 +22,44 @@ public sealed class GhostThemeSystem : EntitySystem
[Dependency] private readonly IServerSponsorsManager _sponsorsMgr = default!; // Corvax-Sponsors
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly ISerializationManager _serialization = default!;
[Dependency] private readonly MindSystem _mindSystem = default!;


private Dictionary<NetUserId, ProtoId<GhostThemePrototype>> _cachedGhosts = new();
[Dependency] private readonly IServerNetConfigurationManager _netConfigManager = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GhostComponent, PlayerAttachedEvent>(OnPlayerAttached);
SubscribeNetworkEvent<RequestGhostThemeEvent>(OnPlayerSelectGhost);
SubscribeLocalEvent<RoundRestartCleanupEvent>(OnCleanup);
}

private void OnCleanup(RoundRestartCleanupEvent ev)
{
_cachedGhosts.Clear();
}

private void OnPlayerSelectGhost(RequestGhostThemeEvent msg, EntitySessionEventArgs args)
private void OnPlayerAttached(EntityUid uid, GhostComponent component, PlayerAttachedEvent args)
{
#if DEBUG
if (!_sponsorsMgr.TryGetPrototypes(args.SenderSession.UserId, out var items))
var prefGhost = _netConfigManager.GetClientCVar(args.Player.Channel, Shared.Backmen.CCVar.CCVars.SponsorsSelectedGhost);
{
items = new List<string>();
items.Add("tier1");
items.Add("tier2");
items.Add("tier01");
items.Add("tier02");
items.Add("tier03");
items.Add("tier04");
items.Add("tier05");
}
#if DEBUG
if (!_sponsorsMgr.TryGetPrototypes(args.Player.UserId, out var items))
{
items = new List<string>();
items.Add("tier1");
items.Add("tier2");
items.Add("tier01");
items.Add("tier02");
items.Add("tier03");
items.Add("tier04");
items.Add("tier05");
}
if (!items.Contains(prefGhost))
{
prefGhost = "";
}
#else
if (!_sponsorsMgr.TryGetPrototypes(args.SenderSession.UserId, out var items))
return;

if (!items.Contains(msg.Ghost.Id))
return;
if (!_sponsorsMgr.TryGetPrototypes(args.Player.UserId, out var items) || !items.Contains(prefGhost))
{
prefGhost = "";
}
#endif

if (!_prototypeManager.TryIndex(msg.Ghost, out _))
return;

_cachedGhosts[args.SenderSession.UserId] = msg.Ghost;


if (args.SenderSession.AttachedEntity != null && args.SenderSession.AttachedEntity.Value.IsValid() && HasComp<GhostComponent>(args.SenderSession.AttachedEntity))
{
var comp = EnsureComp<GhostThemeComponent>(args.SenderSession.AttachedEntity.Value);
comp.GhostTheme = msg.Ghost.Id;
Dirty(args.SenderSession.AttachedEntity.Value, comp);
}

if (_mindSystem.TryGetMind(args.SenderSession, out var mindId, out var mind) && mind.IsVisitingEntity && HasComp<GhostComponent>(mind.VisitingEntity))
{
var comp = EnsureComp<GhostThemeComponent>(mind.VisitingEntity.Value);
comp.GhostTheme = msg.Ghost.Id;
Dirty(mind.VisitingEntity.Value, comp);
}

}


private void OnPlayerAttached(EntityUid uid, GhostComponent component, PlayerAttachedEvent args)
{
if (!(_cachedGhosts.TryGetValue(args.Player.UserId, out var value) && _prototypeManager.TryIndex(value, out var ghostThemePrototype)))
GhostThemePrototype? ghostThemePrototype = null;
if (string.IsNullOrEmpty(prefGhost) || !_prototypeManager.TryIndex<GhostThemePrototype>(prefGhost, out ghostThemePrototype))
{
if (!_sponsorsMgr.TryGetGhostTheme(args.Player.UserId, out var ghostTheme) ||
!_prototypeManager.TryIndex(ghostTheme, out ghostThemePrototype)
Expand All @@ -95,7 +69,7 @@ private void OnPlayerAttached(EntityUid uid, GhostComponent component, PlayerAtt
}
}

foreach (var entry in ghostThemePrototype!.Components.Values)
foreach (var entry in ghostThemePrototype.Components.Values)
{
var comp = (Component) _serialization.CreateCopy(entry.Component, notNullableOverride: true);
comp.Owner = uid;
Expand Down
2 changes: 1 addition & 1 deletion Content.Shared/Backmen/CCVar/CCVars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static readonly CVarDef<bool>
CVarDef.Create("sponsor.api_url", "", CVar.SERVERONLY);

public static readonly CVarDef<string> SponsorsSelectedGhost =
CVarDef.Create("sponsor.ghost", "", CVar.CLIENTONLY);
CVarDef.Create("sponsor.ghost", "", CVar.REPLICATED | CVar.CLIENT);


public static readonly CVarDef<bool>
Expand Down
11 changes: 0 additions & 11 deletions Content.Shared/Backmen/GhostTheme/GhostThemeComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,3 @@ public sealed partial class GhostThemeComponent: Component
[ViewVariables(VVAccess.ReadWrite), AutoNetworkedField, DataField("ghostTheme")]
public ProtoId<GhostThemePrototype>? GhostTheme;
}

[Serializable, NetSerializable]
public sealed class RequestGhostThemeEvent : EntityEventArgs
{
public ProtoId<GhostThemePrototype> Ghost { get; }

public RequestGhostThemeEvent(string ghostThemeId)
{
Ghost = ghostThemeId;
}
}

0 comments on commit b3e4629

Please sign in to comment.