Files
tbd-station-14/Content.Server/Speech/EntitySystems/ScrambledAccentSystem.cs
Princess Cheeseballs 367ff79006 Accents Event to Shared (#38948)
* 27 file diff

* 27 file diff 2

* Apply suggestions from code review

---------

Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com>
Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com>
2025-07-12 22:52:51 +02:00

50 lines
1.5 KiB
C#

using System.Linq;
using System.Text.RegularExpressions;
using Content.Server.Speech.Components;
using Content.Shared.Speech;
using Robust.Shared.Random;
namespace Content.Server.Speech.EntitySystems
{
public sealed class ScrambledAccentSystem : EntitySystem
{
private static readonly Regex RegexLoneI = new(@"(?<=\ )i(?=[\ \.\?]|$)");
[Dependency] private readonly IRobustRandom _random = default!;
public override void Initialize()
{
SubscribeLocalEvent<ScrambledAccentComponent, AccentGetEvent>(OnAccent);
}
public string Accentuate(string message)
{
var words = message.ToLower().Split();
if (words.Length < 2)
{
var pick = _random.Next(1, 8);
// If they try to weasel out of it by saying one word at a time we give them this.
return Loc.GetString($"accent-scrambled-words-{pick}");
}
// Scramble the words
var scrambled = words.OrderBy(x => _random.Next()).ToArray();
var msg = string.Join(" ", scrambled);
// First letter should be capital
msg = msg[0].ToString().ToUpper() + msg.Remove(0, 1);
// Capitalize lone i's
msg = RegexLoneI.Replace(msg, "I");
return msg;
}
private void OnAccent(EntityUid uid, ScrambledAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message);
}
}
}