1
0

PirateAccentSystem.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System.Linq;
  2. using Content.Server.Speech.Components;
  3. using Robust.Shared.Random;
  4. using System.Text.RegularExpressions;
  5. namespace Content.Server.Speech.EntitySystems;
  6. public sealed class PirateAccentSystem : EntitySystem
  7. {
  8. private static readonly Regex FirstWordAllCapsRegex = new(@"^(\S+)");
  9. [Dependency] private readonly IRobustRandom _random = default!;
  10. [Dependency] private readonly ReplacementAccentSystem _replacement = default!;
  11. public override void Initialize()
  12. {
  13. base.Initialize();
  14. SubscribeLocalEvent<PirateAccentComponent, AccentGetEvent>(OnAccentGet);
  15. }
  16. // converts left word when typed into the right word. For example typing you becomes ye.
  17. public string Accentuate(string message, PirateAccentComponent component)
  18. {
  19. var msg = _replacement.ApplyReplacements(message, "pirate");
  20. if (!_random.Prob(component.YarrChance))
  21. return msg;
  22. //Checks if the first word of the sentence is all caps
  23. //So the prefix can be allcapped and to not resanitize the captial
  24. var firstWordAllCaps = !FirstWordAllCapsRegex.Match(msg).Value.Any(char.IsLower);
  25. var pick = _random.Pick(component.PirateWords);
  26. var pirateWord = Loc.GetString(pick);
  27. // Reverse sanitize capital
  28. if (!firstWordAllCaps)
  29. msg = msg[0].ToString().ToLower() + msg.Remove(0, 1);
  30. else
  31. pirateWord = pirateWord.ToUpper();
  32. msg = pirateWord + " " + msg;
  33. return msg;
  34. }
  35. private void OnAccentGet(EntityUid uid, PirateAccentComponent component, AccentGetEvent args)
  36. {
  37. args.Message = Accentuate(args.Message, component);
  38. }
  39. }