-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
…er (#30446) * Initial commit * more like bYE * Fix exception during test
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
namespace Content.Server.Traitor.Components; | ||
|
||
/// <summary> | ||
/// Paper with written traitor codewords on it. | ||
/// </summary> | ||
[RegisterComponent] | ||
public sealed partial class TraitorCodePaperComponent : Component | ||
{ | ||
/// <summary> | ||
/// The number of codewords that should be generated on this paper. | ||
/// Will not extend past the max number of available codewords. | ||
/// </summary> | ||
[DataField] | ||
public int CodewordAmount = 1; | ||
|
||
/// <summary> | ||
/// Whether the codewords should be faked if there is no traitor gamerule set. | ||
/// </summary> | ||
[DataField] | ||
public bool FakeCodewords = true; | ||
|
||
/// <summary> | ||
/// Whether all codewords added to the round should be used. Overrides CodewordAmount if true. | ||
/// </summary> | ||
[DataField] | ||
public bool CodewordShowAll = false; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
using Content.Server.GameTicking; | ||
using Content.Server.GameTicking.Rules; | ||
using Content.Server.GameTicking.Rules.Components; | ||
using Content.Server.Paper; | ||
using Content.Server.Traitor.Components; | ||
using Robust.Shared.Random; | ||
using Robust.Shared.Utility; | ||
using System.Linq; | ||
|
||
namespace Content.Server.Traitor.Systems; | ||
|
||
public sealed class TraitorCodePaperSystem : EntitySystem | ||
{ | ||
[Dependency] private readonly GameTicker _gameTicker = default!; | ||
[Dependency] private readonly TraitorRuleSystem _traitorRuleSystem = default!; | ||
[Dependency] private readonly IRobustRandom _random = default!; | ||
[Dependency] private readonly PaperSystem _paper = default!; | ||
Check failure on line 18 in Content.Server/Traitor/Systems/TraitorCodePaperSystem.cs
|
||
|
||
public override void Initialize() | ||
{ | ||
base.Initialize(); | ||
SubscribeLocalEvent<TraitorCodePaperComponent, MapInitEvent>(OnMapInit); | ||
} | ||
|
||
private void OnMapInit(EntityUid uid, TraitorCodePaperComponent component, MapInitEvent args) | ||
{ | ||
SetupPaper(uid, component); | ||
} | ||
|
||
private void SetupPaper(EntityUid uid, TraitorCodePaperComponent? component = null) | ||
{ | ||
if (!Resolve(uid, ref component)) | ||
return; | ||
|
||
if (HasComp<PaperComponent>(uid)) | ||
{ | ||
if (TryGetTraitorCode(out var paperContent, component)) | ||
{ | ||
_paper.SetContent(uid, paperContent); | ||
} | ||
} | ||
} | ||
|
||
private bool TryGetTraitorCode([NotNullWhen(true)] out string? traitorCode, TraitorCodePaperComponent component) | ||
{ | ||
traitorCode = null; | ||
|
||
var codesMessage = new FormattedMessage(); | ||
List<string> codeList = new(); | ||
// Find the first nuke that matches the passed location. | ||
if (_gameTicker.IsGameRuleAdded<TraitorRuleComponent>()) | ||
{ | ||
var ruleEnts = _gameTicker.GetAddedGameRules(); | ||
foreach (var ruleEnt in ruleEnts) | ||
{ | ||
if (TryComp(ruleEnt, out TraitorRuleComponent? traitorComp)) | ||
{ | ||
codeList.AddRange(traitorComp.Codewords.ToList()); | ||
} | ||
} | ||
} | ||
if (codeList.Count == 0) | ||
{ | ||
if (component.FakeCodewords) | ||
codeList = _traitorRuleSystem.GenerateTraitorCodewords(new TraitorRuleComponent()).ToList(); | ||
else | ||
codeList = [Loc.GetString("traitor-codes-none")]; | ||
} | ||
|
||
_random.Shuffle(codeList); | ||
|
||
int i = 0; | ||
foreach (var code in codeList) | ||
{ | ||
i++; | ||
if (i > component.CodewordAmount && !component.CodewordShowAll) | ||
break; | ||
|
||
codesMessage.PushNewline(); | ||
codesMessage.AddMarkup(code); | ||
} | ||
|
||
if (!codesMessage.IsEmpty) | ||
{ | ||
if (i == 1) | ||
traitorCode = Loc.GetString("traitor-codes-message-singular") + codesMessage; | ||
else | ||
traitorCode = Loc.GetString("traitor-codes-message-plural") + codesMessage; | ||
} | ||
return !codesMessage.IsEmpty; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
traitor-codes-message-singular = syndicate codeword: | ||
traitor-codes-message-plural = syndicate codewords: | ||
traitor-codes-none = no known codewords |