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

@@ -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;
}
}
}