-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update system and apply review changes
- Loading branch information
1 parent
f300e8a
commit e200bba
Showing
7 changed files
with
138 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 91 additions & 54 deletions
145
Content.Server/Corvax/StationGoal/StationGoalPaperSystem.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
5 changes: 4 additions & 1 deletion
5
Resources/Locale/en-US/corvax/station-goal/station-goal-command.ftl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.