Skip to content

Commit

Permalink
update system and apply review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DEATHB4DEFEAT committed Oct 29, 2023
1 parent f300e8a commit e200bba
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 180 deletions.
6 changes: 3 additions & 3 deletions Content.Server/Corvax/StationGoal/StationGoalCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,18 @@ public void Execute(IConsoleShell shell, string argStr, string[] args)
var prototypeManager = IoCManager.Resolve<IPrototypeManager>();
if (!prototypeManager.TryIndex<StationGoalPrototype>(protoId, out var proto))
{
shell.WriteError($"No station goal found with ID {protoId}!");
shell.WriteError(Loc.GetString("send-station-goal-command-error-no-goal-proto", ("id", protoId)));
return;
}

var stationGoalPaper = IoCManager.Resolve<IEntityManager>().System<StationGoalPaperSystem>();
if (!stationGoalPaper.SendStationGoal(proto))
{
shell.WriteError("Station goal was not sent");
shell.WriteError(Loc.GetString("send-station-goal-command-error-couldnt-fax"));
return;
}
}

public CompletionResult GetCompletion(IConsoleShell shell, string[] args)
{
if (args.Length == 1)
Expand Down
145 changes: 91 additions & 54 deletions Content.Server/Corvax/StationGoal/StationGoalPaperSystem.cs
Original file line number Diff line number Diff line change
@@ -1,79 +1,116 @@
using System.Linq;
using System.Data;
using System.Text.RegularExpressions;
using Content.Server.Fax;
using Content.Server.Station.Systems;
using Content.Shared.Corvax.CCCVars;
using Content.Shared.GameTicking;
using Content.Shared.Paper;
using Content.Shared.Random;
using Content.Shared.Random.Helpers;
using Robust.Shared.Configuration;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;

namespace Content.Server.Corvax.StationGoal
namespace Content.Server.Corvax.StationGoal;

/// <summary>
/// System to spawn paper with station goal.
/// </summary>
public sealed partial class StationGoalPaperSystem : EntitySystem
{
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly FaxSystem _fax = default!;
[Dependency] private readonly IConfigurationManager _config = default!;
[Dependency] private readonly StationSystem _station = default!;

private static readonly Regex StationIdRegex = new(@".*-(\d+)$");

private const string RandomPrototype = "StationGoals";

public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<RoundStartedEvent>(OnRoundStarted);
}

private void OnRoundStarted(RoundStartedEvent ev)
{
if (_config.GetCVar(CCCVars.StationGoalsEnabled) != true)
return;

SendRandomGoal();
}

/// <summary>
/// System to spawn paper with station goal.
/// Send a random station goal to all faxes which are authorized to receive it
/// </summary>
public sealed class StationGoalPaperSystem : EntitySystem
/// <returns>If the fax was successful</returns>
/// <exception cref="ConstraintException">Raised when station goal types in the prototype is invalid</exception>
public bool SendRandomGoal()
{
[Dependency] private readonly IPrototypeManager _prototype = default!;
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly FaxSystem _fax = default!;
[Dependency] private readonly StationSystem _station = default!;
// Get the random station goal list
if (!_prototype.TryIndex<WeightedRandomPrototype>(RandomPrototype, out var goals))
return false;

private static readonly Regex StationIdRegex = new(@".*-(\d+)$");
// Get a random goal
var goal = RecursiveRandom(goals);

public override void Initialize()
{
base.Initialize();
// Send the goal
return SendStationGoal(goal);
}

SubscribeLocalEvent<RoundStartedEvent>(OnRoundStarted);
}
private StationGoalPrototype RecursiveRandom(WeightedRandomPrototype random)
{
var goal = random.Pick(_random);

private void OnRoundStarted(RoundStartedEvent ev)
if (_prototype.TryIndex<StationGoalPrototype>(goal, out var goalPrototype))
{
SendRandomGoal();
return goalPrototype;
}

public bool SendRandomGoal()
if (_prototype.TryIndex<WeightedRandomPrototype>(goal, out var goalRandom))
{
var availableGoals = _prototypeManager.EnumeratePrototypes<StationGoalPrototype>().ToList();
var goal = _random.Pick(availableGoals);
return SendStationGoal(goal);
return RecursiveRandom(goalRandom);
}

/// <summary>
/// Send a station goal to all faxes which are authorized to receive it.
/// </summary>
/// <returns>True if at least one fax received paper</returns>
public bool SendStationGoal(StationGoalPrototype goal)
throw new Exception($"StationGoalPaperSystem: Random station goal could not be found from origin prototype {RandomPrototype}");
}

/// <summary>
/// Send a station goal to all faxes which are authorized to receive it
/// </summary>
/// <returns>True if at least one fax received paper</returns>
public bool SendStationGoal(StationGoalPrototype goal)
{
var enumerator = EntityManager.EntityQueryEnumerator<FaxMachineComponent>();
var wasSent = false;

while (enumerator.MoveNext(out var uid, out var fax))
{
var enumerator = EntityManager.EntityQueryEnumerator<FaxMachineComponent>();
var wasSent = false;
while (enumerator.MoveNext(out var uid, out var fax))
{
if (!fax.ReceiveStationGoal) continue;

if (!TryComp<MetaDataComponent>(_station.GetOwningStation(uid), out var meta))
continue;

var stationId = StationIdRegex.Match(meta.EntityName).Groups[1].Value;

var printout = new FaxPrintout(
Loc.GetString(goal.Text,
("date", DateTime.Now.AddYears(1000).ToString("dd.MM.yyyy")),
("station", string.IsNullOrEmpty(stationId) ? "???" : stationId)),
Loc.GetString("station-goal-fax-paper-name"),
null,
"paper_stamp-centcom",
new List<StampDisplayInfo>
{
new() { StampedName = Loc.GetString("stamp-component-stamped-name-centcom"), StampedColor = Color.FromHex("#BB3232") },
});
_faxSystem.Receive(uid, printout, null, fax);

wasSent = true;
}

return wasSent;
if (!fax.ReceiveStationGoal)
continue;

if (!TryComp<MetaDataComponent>(_station.GetOwningStation(uid), out var meta))
continue;

var stationId = StationIdRegex.Match(meta.EntityName).Groups[1].Value;

var printout = new FaxPrintout(
Loc.GetString("station-goal-fax-paper-header",
("date", DateTime.Now.AddYears(1000).ToString("yyyy MMMM dd")),
("station", string.IsNullOrEmpty(stationId) ? "???" : stationId),
("content", goal.Text)
),
Loc.GetString("station-goal-fax-paper-name"),
"StationGoalPaper"
);

_fax.Receive(uid, printout, null, fax);

wasSent = true;
}

return wasSent;
}
}
2 changes: 1 addition & 1 deletion Content.Server/Corvax/StationGoal/StationGoalPrototype.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ public sealed class StationGoalPrototype : IPrototype
{
[IdDataFieldAttribute] public string ID { get; } = default!;

[DataField("text")] public string Text { get; set; } = string.Empty;
public string Text => Loc.GetString($"station-goal-{ID.ToLower()}");
}
}
18 changes: 18 additions & 0 deletions Content.Shared/Corvax/CCVars/CCCVars.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Robust.Shared.Configuration;

namespace Content.Shared.Corvax.CCCVars;

[CVarDefs]
// ReSharper disable once InconsistentNaming
public sealed class CCCVars
{
/*
* Station Goals
*/

/// <summary>
/// Enables station goals
/// </summary>
public static readonly CVarDef<bool> StationGoalsEnabled =
CVarDef.Create("game.station_goals", false, CVar.SERVERONLY);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
send-station-goal-command-description = Sends the selected station target to all faxes that can receive it
send-station-goal-command-help-text = Usage: { $command } <target-id>
send-station-goal-command-help-text = Usage: { $command } <Goal Prototype ID>
send-station-goal-command-arg-id = Goal Prototype ID
send-station-goal-command-error-no-goal-proto = No station goal found with ID {$id}
send-station-goal-command-error-couldnt-fax = Couldn't send station goal, probably due to a lack of fax machines that are able to recieve it
Loading

0 comments on commit e200bba

Please sign in to comment.