Split codewords into its own system (#37928)
* Split codewords into its own system * Fix admin log * Nuke unused code * Fix formatting errors * Fix tests * Make the codeword system add itself if called when not active * Put comment in right place. * Review: Rename prototypes * Review: Make codewords serializable * Fix build * Reviews: Change the system to not be a gamerule. * Fix YAML Linter * Fix test fail * Remove unused import
This commit is contained in:
14
Content.Server/Codewords/CodewordComponent.cs
Normal file
14
Content.Server/Codewords/CodewordComponent.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
namespace Content.Server.Codewords;
|
||||
|
||||
/// <summary>
|
||||
/// Container for generated codewords.
|
||||
/// </summary>
|
||||
[RegisterComponent, Access(typeof(CodewordSystem))]
|
||||
public sealed partial class CodewordComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The codewords that were generated.
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public string[] Codewords = [];
|
||||
}
|
||||
20
Content.Server/Codewords/CodewordFactionPrototype.cs
Normal file
20
Content.Server/Codewords/CodewordFactionPrototype.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Codewords;
|
||||
|
||||
/// <summary>
|
||||
/// This is a prototype for easy access to codewords using identifiers instead of magic strings.
|
||||
/// </summary>
|
||||
[Prototype]
|
||||
public sealed partial class CodewordFactionPrototype : IPrototype
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[IdDataField]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// The generator to use for this faction.
|
||||
/// </summary>
|
||||
[DataField(required:true)]
|
||||
public ProtoId<CodewordGeneratorPrototype> Generator { get; } = default!;
|
||||
}
|
||||
32
Content.Server/Codewords/CodewordGeneratorPrototype.cs
Normal file
32
Content.Server/Codewords/CodewordGeneratorPrototype.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using Content.Shared.Dataset;
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Codewords;
|
||||
|
||||
/// <summary>
|
||||
/// This is a prototype for specifying codeword generation
|
||||
/// </summary>
|
||||
[Prototype]
|
||||
public sealed partial class CodewordGeneratorPrototype : IPrototype
|
||||
{
|
||||
/// <inheritdoc/>
|
||||
[IdDataField]
|
||||
public string ID { get; } = default!;
|
||||
|
||||
/// <summary>
|
||||
/// List of datasets to use for word generation. All values will be concatenated into one list and then randomly chosen from
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public List<ProtoId<LocalizedDatasetPrototype>> Words { get; } =
|
||||
[
|
||||
"Adjectives",
|
||||
"Verbs",
|
||||
];
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// How many codewords should be generated?
|
||||
/// </summary>
|
||||
[DataField]
|
||||
public int Amount = 3;
|
||||
}
|
||||
17
Content.Server/Codewords/CodewordManagerComponent.cs
Normal file
17
Content.Server/Codewords/CodewordManagerComponent.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using Robust.Shared.Prototypes;
|
||||
|
||||
namespace Content.Server.Codewords;
|
||||
|
||||
/// <summary>
|
||||
/// Component that defines <see cref="CodewordGeneratorPrototype"/> to use and keeps track of generated codewords.
|
||||
/// </summary>
|
||||
[RegisterComponent, Access(typeof(CodewordSystem))]
|
||||
public sealed partial class CodewordManagerComponent : Component
|
||||
{
|
||||
/// <summary>
|
||||
/// The generated codewords. The value contains the entity that has the <see cref="CodewordComponent"/>
|
||||
/// </summary>
|
||||
[DataField]
|
||||
[ViewVariables(VVAccess.ReadOnly)]
|
||||
public Dictionary<ProtoId<CodewordFactionPrototype>, EntityUid> Codewords = new();
|
||||
}
|
||||
90
Content.Server/Codewords/CodewordSystem.cs
Normal file
90
Content.Server/Codewords/CodewordSystem.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Linq;
|
||||
using Content.Server.Administration.Logs;
|
||||
using Content.Server.GameTicking.Events;
|
||||
using Content.Shared.Database;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Prototypes;
|
||||
using Robust.Shared.Random;
|
||||
|
||||
namespace Content.Server.Codewords;
|
||||
|
||||
/// <summary>
|
||||
/// Gamerule that provides codewords for other gamerules that rely on them.
|
||||
/// </summary>
|
||||
public sealed class CodewordSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
|
||||
[Dependency] private readonly IAdminLogManager _adminLogger = default!;
|
||||
[Dependency] private readonly IRobustRandom _random = default!;
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
|
||||
SubscribeLocalEvent<RoundStartingEvent>(OnRoundStart);
|
||||
}
|
||||
|
||||
private void OnRoundStart(RoundStartingEvent ev)
|
||||
{
|
||||
var manager = Spawn();
|
||||
AddComp<CodewordManagerComponent>(manager);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves codewords for the faction specified.
|
||||
/// </summary>
|
||||
public string[] GetCodewords(ProtoId<CodewordFactionPrototype> faction)
|
||||
{
|
||||
var query = EntityQueryEnumerator<CodewordManagerComponent>();
|
||||
while (query.MoveNext(out _, out var manager))
|
||||
{
|
||||
if (!manager.Codewords.TryGetValue(faction, out var codewordEntity))
|
||||
return GenerateForFaction(faction, ref manager);
|
||||
|
||||
return Comp<CodewordComponent>(codewordEntity).Codewords;
|
||||
}
|
||||
|
||||
Log.Warning("Codeword system not initialized. Returning empty array.");
|
||||
// While throwing in this situation would be cool, that causes a test fail (in SpawnAndDeleteEntityCountTest)
|
||||
// as the traitor codewords paper gets spawned in and calls this method,
|
||||
// but the "start round" event never gets called in this test case.
|
||||
return [];
|
||||
}
|
||||
|
||||
private string[] GenerateForFaction(ProtoId<CodewordFactionPrototype> faction, ref CodewordManagerComponent manager)
|
||||
{
|
||||
var factionProto = _prototypeManager.Index<CodewordFactionPrototype>(faction.Id);
|
||||
|
||||
var codewords = GenerateCodewords(factionProto.Generator);
|
||||
var codewordsContainer = EntityManager.Spawn(protoName:null, MapCoordinates.Nullspace);
|
||||
EnsureComp<CodewordComponent>(codewordsContainer)
|
||||
.Codewords = codewords;
|
||||
manager.Codewords[faction] = codewordsContainer;
|
||||
_adminLogger.Add(LogType.EventStarted, LogImpact.Low, $"Codewords generated for faction {faction}: {string.Join(", ", codewords)}");
|
||||
|
||||
return codewords;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Generates codewords as specified by the <see cref="CodewordGeneratorPrototype"/> codeword generator.
|
||||
/// </summary>
|
||||
public string[] GenerateCodewords(ProtoId<CodewordGeneratorPrototype> generatorId)
|
||||
{
|
||||
var generator = _prototypeManager.Index(generatorId);
|
||||
|
||||
var codewordPool = new List<string>();
|
||||
foreach (var dataset in generator.Words
|
||||
.Select(datasetPrototype => _prototypeManager.Index(datasetPrototype)))
|
||||
{
|
||||
codewordPool.AddRange(dataset.Values);
|
||||
}
|
||||
|
||||
var finalCodewordCount = Math.Min(generator.Amount, codewordPool.Count);
|
||||
var codewords = new string[finalCodewordCount];
|
||||
for (var i = 0; i < finalCodewordCount; i++)
|
||||
{
|
||||
codewords[i] = Loc.GetString(_random.PickAndTake(codewordPool));
|
||||
}
|
||||
return codewords;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user