Refactor AccentManager to be an entity system, makes accents ECS. (#4825)

This commit is contained in:
Vera Aguilera Puerto
2021-10-11 20:18:39 +02:00
committed by GitHub
parent 55b4edb895
commit 4c80fb9586
16 changed files with 226 additions and 234 deletions

View File

@@ -375,6 +375,7 @@ namespace Content.Server.Chat.Managers
public void RegisterChatTransform(TransformChat handler)
{
// TODO: Literally just make this an event...
_chatTransformHandlers.Add(handler);
}
}

View File

@@ -72,7 +72,6 @@ namespace Content.Server.Entry
IoCManager.Resolve<IServerDbManager>().Init();
IoCManager.Resolve<IServerPreferencesManager>().Init();
IoCManager.Resolve<INodeGroupFactory>().Initialize();
IoCManager.Resolve<IAccentManager>().Initialize();
_voteManager.Initialize();
}

View File

@@ -44,7 +44,6 @@ namespace Content.Server.IoC
IoCManager.Register<INodeGroupFactory, NodeGroupFactory>();
IoCManager.Register<BlackboardManager, BlackboardManager>();
IoCManager.Register<ConsiderationsManager, ConsiderationsManager>();
IoCManager.Register<IAccentManager, AccentManager>();
IoCManager.Register<IConnectionManager, ConnectionManager>();
IoCManager.Register<IObjectivesManager, ObjectivesManager>();
IoCManager.Register<IAdminManager, AdminManager>();

View File

@@ -1,39 +0,0 @@
using System.Text.RegularExpressions;
using Content.Server.Chat.Managers;
using Content.Server.Speech.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Speech
{
public interface IAccentManager
{
public void Initialize();
}
public class AccentManager : IAccentManager
{
[Dependency] private readonly IChatManager _chatManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
public static readonly Regex SentenceRegex = new(@"(?<=[\.!\?])");
public void Initialize()
{
IoCManager.InjectDependencies(this);
_chatManager.RegisterChatTransform(AccentHandler);
}
public string AccentHandler(IEntity player, string message)
{
//TODO: give accents a prio?
var accents = _entityManager.GetComponents<IAccentComponent>(player.Uid);
foreach (var accent in accents)
{
message = accent.Accentuate(message);
}
return message;
}
}
}

View File

@@ -0,0 +1,49 @@
using System.Text.RegularExpressions;
using Content.Server.Chat.Managers;
using Content.Server.Speech.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Speech
{
public class AccentSystem : EntitySystem
{
[Dependency] private readonly IChatManager _chatManager = default!;
public static readonly Regex SentenceRegex = new(@"(?<=[\.!\?])");
public override void Initialize()
{
_chatManager.RegisterChatTransform(AccentHandler);
}
public string AccentHandler(IEntity player, string message)
{
var accentEvent = new AccentGetEvent(player.Uid, message);
RaiseLocalEvent(player.Uid, accentEvent);
return accentEvent.Message;
}
}
public class AccentGetEvent : EntityEventArgs
{
/// <summary>
/// The entity to apply the accent to.
/// </summary>
public EntityUid Entity { get; }
/// <summary>
/// The message to apply the accent transformation to.
/// Modify this to apply the accent.
/// </summary>
public string Message { get; set; }
public AccentGetEvent(EntityUid entity, string message)
{
Entity = entity;
Message = message;
}
}
}

View File

@@ -1,80 +0,0 @@
using System.Linq;
using System.Text;
using Content.Server.Administration;
using Content.Server.Speech.Components;
using Content.Shared.Administration;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Speech
{
[AdminCommand(AdminFlags.Fun)]
public class AddAccent : IConsoleCommand
{
public string Command => "addaccent";
public string Description => "Add a speech component to the current player";
public string Help => $"{Command} <component>/?";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var player = shell.Player as IPlayerSession;
if (player?.AttachedEntity == null)
{
shell.WriteLine("You don't have an entity!");
return;
}
if (args.Length == 0)
{
shell.WriteLine(Help);
return;
}
var compFactory = IoCManager.Resolve<IComponentFactory>();
if (args[0] == "?")
{
// Get all components that implement the ISpeechComponent except
var speeches = compFactory.GetAllRefTypes()
.Where(c => typeof(IAccentComponent).IsAssignableFrom(c) && c.IsClass);
var msg = new StringBuilder();
foreach(var s in speeches)
{
msg.Append($"{compFactory.GetRegistration(s).Name}\n");
}
shell.WriteLine(msg.ToString());
}
else
{
var name = args[0];
// Try to get the Component
if (!compFactory.TryGetRegistration(name, out var registration, true))
{
shell.WriteLine($"Accent {name} not found. Try {Command} ? to get a list of all applicable accents.");
return;
}
var type = registration.Type;
// Check if that already exists
if (player.AttachedEntity.HasComponent(type))
{
shell.WriteLine("You already have this accent!");
return;
}
// Generic fuckery
var ensure = typeof(IEntity).GetMethod("AddComponent");
if (ensure == null)
return;
var method = ensure.MakeGenericMethod(type);
method.Invoke(player.AttachedEntity, null);
}
}
}
}

View File

@@ -1,18 +1,10 @@
using System;
using Robust.Shared.GameObjects;
using Robust.Shared.GameObjects;
namespace Content.Server.Speech.Components
{
[RegisterComponent]
public class BackwardsAccentComponent : Component, IAccentComponent
public class BackwardsAccentComponent : Component
{
public override string Name => "BackwardsAccent";
public string Accentuate(string message)
{
var arr = message.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
}
}

View File

@@ -1,12 +0,0 @@
namespace Content.Server.Speech.Components
{
internal interface IAccentComponent
{
/// <summary>
/// Transforms a message with the given Accent
/// </summary>
/// <param name="message">The spoken message</param>
/// <returns>The message after the transformation</returns>
public string Accentuate(string message);
}
}

View File

@@ -1,23 +1,10 @@
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Random;
namespace Content.Server.Speech.Components
{
[RegisterComponent]
public class MouseAccentComponent : Component, IAccentComponent
public class MouseAccentComponent : Component
{
[Dependency] private readonly IRobustRandom _random = default!;
public override string Name => "MouseAccent";
private static readonly IReadOnlyList<string> Squeek = new List<string>{
"Squeak!", "Piep!", "Chuu!"
}.AsReadOnly();
public string Accentuate(string message)
{
return _random.Pick(Squeek);
}
}
}

View File

@@ -1,37 +1,10 @@
using System.Collections.Generic;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Random;
using Robust.Shared.GameObjects;
namespace Content.Server.Speech.Components
{
[RegisterComponent]
public class OwOAccentComponent : Component, IAccentComponent
public class OwOAccentComponent : Component
{
[Dependency] private readonly IRobustRandom _random = default!;
public override string Name => "OwOAccent";
private static readonly IReadOnlyList<string> Faces = new List<string>{
" (・`ω´・)", " ;;w;;", " owo", " UwU", " >w<", " ^w^"
}.AsReadOnly();
private string RandomFace => _random.Pick(Faces);
private static readonly Dictionary<string, string> SpecialWords = new()
{
{ "you", "wu" },
};
public string Accentuate(string message)
{
foreach (var (word, repl) in SpecialWords)
{
message = message.Replace(word, repl);
}
return message.Replace("!", RandomFace)
.Replace("r", "w").Replace("R", "W")
.Replace("l", "w").Replace("L", "W");
}
}
}

View File

@@ -3,55 +3,8 @@
namespace Content.Server.Speech.Components
{
[RegisterComponent]
public class SpanishAccentComponent : Component, IAccentComponent
public class SpanishAccentComponent : Component
{
public override string Name => "SpanishAccent";
public string Accentuate(string message)
{
// Insert E before every S
message = InsertS(message);
// If a sentence ends with ?, insert a reverse ? at the beginning of the sentence
message = ReplaceQuestionMark(message);
return message;
}
private string InsertS(string message)
{
// Replace every new Word that starts with s/S
var msg = message.Replace(" s", " es").Replace(" S", " Es");
// Still need to check if the beginning of the message starts
if (msg.StartsWith("s"))
{
return msg.Remove(0, 1).Insert(0, "es");
}
else if (msg.StartsWith("S"))
{
return msg.Remove(0, 1).Insert(0, "Es");
}
return msg;
}
private string ReplaceQuestionMark(string message)
{
var sentences = AccentManager.SentenceRegex.Split(message);
var msg = "";
foreach (var s in sentences)
{
if (s.EndsWith("?")) // We've got a question => add ¿ to the beginning
{
// Because we don't split by whitespace, we may have some spaces in front of the sentence.
// So we add the symbol before the first non space char
msg += s.Insert(s.Length - s.TrimStart().Length, "¿");
}
else
{
msg += s;
}
}
return msg;
}
}
}

View File

@@ -0,0 +1,26 @@
using System;
using Content.Server.Speech.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Speech.EntitySystems
{
public class BackwardsAccentSystem : EntitySystem
{
public override void Initialize()
{
SubscribeLocalEvent<BackwardsAccentComponent, AccentGetEvent>(OnAccent);
}
public string Accentuate(string message)
{
var arr = message.ToCharArray();
Array.Reverse(arr);
return new string(arr);
}
private void OnAccent(EntityUid uid, BackwardsAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message);
}
}
}

View File

@@ -0,0 +1,32 @@
using System.Collections.Generic;
using Content.Server.Speech.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Random;
namespace Content.Server.Speech.EntitySystems
{
// TODO: Code in-game languages and make this a language
public class MouseAccentSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
private static readonly IReadOnlyList<string> Squeek = new List<string>{ "Squeak!", "Piep!", "Chuu!" };
public override void Initialize()
{
SubscribeLocalEvent<MouseAccentComponent, AccentGetEvent>(OnAccent);
}
public string Accentuate(string message)
{
// TODO: Maybe add more than one squeek when there are more words?
return _random.Pick(Squeek);
}
private void OnAccent(EntityUid uid, MouseAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message);
}
}
}

View File

@@ -0,0 +1,46 @@
using System.Collections.Generic;
using Content.Server.Speech.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Random;
namespace Content.Server.Speech.EntitySystems
{
public class OwOAccentSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
private static readonly IReadOnlyList<string> Faces = new List<string>{
" (・`ω´・)", " ;;w;;", " owo", " UwU", " >w<", " ^w^"
}.AsReadOnly();
private static readonly IReadOnlyDictionary<string, string> SpecialWords = new Dictionary<string, string>()
{
{ "you", "wu" },
};
private string RandomFace => _random.Pick(Faces);
public override void Initialize()
{
SubscribeLocalEvent<OwOAccentComponent, AccentGetEvent>(OnAccent);
}
public string Accentuate(string message)
{
foreach (var (word, repl) in SpecialWords)
{
message = message.Replace(word, repl);
}
return message.Replace("!", RandomFace)
.Replace("r", "w").Replace("R", "W")
.Replace("l", "w").Replace("L", "W");
}
private void OnAccent(EntityUid uid, OwOAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message);
}
}
}

View File

@@ -0,0 +1,65 @@
using Content.Server.Speech.Components;
using Robust.Shared.GameObjects;
namespace Content.Server.Speech.EntitySystems
{
public class SpanishAccentSystem : EntitySystem
{
public override void Initialize()
{
SubscribeLocalEvent<SpanishAccentComponent, AccentGetEvent>(OnAccent);
}
public string Accentuate(string message)
{
// Insert E before every S
message = InsertS(message);
// If a sentence ends with ?, insert a reverse ? at the beginning of the sentence
message = ReplaceQuestionMark(message);
return message;
}
private string InsertS(string message)
{
// Replace every new Word that starts with s/S
var msg = message.Replace(" s", " es").Replace(" S", " Es");
// Still need to check if the beginning of the message starts
if (msg.StartsWith("s"))
{
return msg.Remove(0, 1).Insert(0, "es");
}
else if (msg.StartsWith("S"))
{
return msg.Remove(0, 1).Insert(0, "Es");
}
return msg;
}
private string ReplaceQuestionMark(string message)
{
var sentences = AccentSystem.SentenceRegex.Split(message);
var msg = "";
foreach (var s in sentences)
{
if (s.EndsWith("?")) // We've got a question => add ¿ to the beginning
{
// Because we don't split by whitespace, we may have some spaces in front of the sentence.
// So we add the symbol before the first non space char
msg += s.Insert(s.Length - s.TrimStart().Length, "¿");
}
else
{
msg += s;
}
}
return msg;
}
private void OnAccent(EntityUid uid, SpanishAccentComponent component, AccentGetEvent args)
{
args.Message = Accentuate(args.Message);
}
}
}

View File

@@ -266,6 +266,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=Spawner/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Spillable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Spirate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=squeek/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Strippable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=stunnable/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=subfloor/@EntryIndexedValue">True</s:Boolean>