CatStation/Content.Shared/Codewords/CodewordSystem.cs
Princess Cheeseballs 2c6ffecf62
Move GameTicker to Shared (#45834)
* Scope creep my ass

* why was this shit not pushed????

* gone...

* fix that

* review plus buildd

* asfsafsafsafsafas

* grug

* burber

* I'll fix this later

* shrimply die

* also this

* it's snerf or snuthin

* assaads

* round snend

* aaaa

* fix this shit for real

* actually, don't start GameRules until MapInit now that I think aboot it.

* forgot u

* don't actually need this part nullable

* Deffer comp removal

* one liner

* do that too

* chungus

* Honestly makes more sense here, plus WHY are we doing all that shit on "GameRuleAdded" rather than GameRuleStarted(?)

* deferred removal :V

* aaaaaaaaaaaaaaa

* whhop

* Whoop

* better

* landmine that I placed for myself then forgot about lmao

* cleanup

* FUCKING `!`

* gotovo

* amogus

* please don't check by metadata I beg

* waawsasfdfsasfa

* sfafsasfafsas

* fdsaasffsasfa

* code

* fix

* fasasffasfasfsad

* chungaas

* I may be stupid

* gra

* graaaaaaaaaaaaaaaaa

* comment

* gra

* clearing is not predicted and would require major refactors, so don't cache on client.

* lame shit :P

* last holes

* nukeopsfsafsasafsfafsa

* why  was that shit public bruh

* bubrbgwahrghfaka

* dynamic

* revert change that did nothing lmao
2026-09-16 19:34:19 +00:00

83 lines
3.1 KiB
C#

using System.Linq;
using Content.Shared.Administration.Logs;
using Content.Shared.Database;
using Content.Shared.GameTicking.Events;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Random;
namespace Content.Shared.Codewords;
/// <summary>
/// Gamerule that provides codewords for other gamerules that rely on them.
/// </summary>
public sealed partial class CodewordSystem : EntitySystem
{
[Dependency] private ISharedAdminLogManager _adminLogger = default!;
[Dependency] private IRobustRandom _random = default!;
[SubscribeLocalEvent]
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 = ProtoMan.Index<CodewordFactionPrototype>(faction.Id);
var codewords = GenerateCodewords(factionProto.Generator);
var codewordsContainer = Spawn(prototype: 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 = ProtoMan.Index(generatorId);
var codewordPool = new List<string>();
foreach (var dataset in generator.Words
.Select(datasetPrototype => ProtoMan.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;
}
}