Skip to content

Commit

Permalink
Refactoring and adding new patterns
Browse files Browse the repository at this point in the history
+ made WordIsolator an Enumerator.
+ Turned AttendanceManager into the singleton pattern.
+ turned Commands into a singleton.
+ replaced string literal empties with string.Empty (where applicable).
+ fixed up comments.
+ removed unecessary interface.
+ turned FactoryReset into a new interface IResettable().
+ moved models to their own files.
  • Loading branch information
Samuel committed Feb 12, 2018
1 parent 515fd65 commit 29e8b6c
Show file tree
Hide file tree
Showing 12 changed files with 249 additions and 128 deletions.
30 changes: 21 additions & 9 deletions AttendanceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

namespace SETextToSpeechMod
{
public static class AttendanceManager
public class AttendanceManager
{
private static List <IMyPlayer> playersField = new List <IMyPlayer>();
public static IList <IMyPlayer> Players
private List <IMyPlayer> playersField = new List <IMyPlayer>();
public IList <IMyPlayer> Players
{
get
{
Expand All @@ -19,24 +19,36 @@ public static IList <IMyPlayer> Players
/// <summary>
/// CAN BE NULL
/// </summary>
public static IMyPlayer LocalPlayer { get; private set; }
public IMyPlayer LocalPlayer { get; private set; }

private static Dictionary <string, bool> muteStatusesField = new Dictionary <string, bool>();
public static IReadOnlyDictionary <string, bool> PlayersMuteStatuses
private Dictionary <string, bool> muteStatusesField = new Dictionary <string, bool>();
public IReadOnlyDictionary <string, bool> PlayersMuteStatuses
{
get
{
return muteStatusesField as IReadOnlyDictionary <string, bool>;
return (IReadOnlyDictionary<string, bool>)muteStatusesField;
}
}
}

private static AttendanceManager instance = new AttendanceManager();

private AttendanceManager()
{

}

public static AttendanceManager GetSingleton()
{
return instance;
}

/// <summary>
/// Mutes or unmutes the requests player based on your bool input.
/// </summary>
/// <param name="player"></param>
/// <param name="mutePlayer"></param>
/// <returns></returns>
public static void ChangeMuteStatusOfPlayer (string player, bool newMuteStatus)
public void ChangeMuteStatusOfPlayer (string player, bool newMuteStatus)
{
UpdatePlayers();

Expand Down
48 changes: 16 additions & 32 deletions ChatEntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using VRage.Game.Components; //location of MySessionComponentBase.
using System.Threading.Tasks;
using System;
using SETextToSpeechMod.LookUpTables;

namespace SETextToSpeechMod
{
Expand All @@ -18,6 +19,8 @@ public class ChatEntryPoint : MySessionComponentBase
private Encoding encode = Encoding.Unicode; //encoding is necessary to convert message into correct format.
private SoundPlayer soundPlayer;
public OutputManager OutputManager { get; private set; }
private AttendanceManager attendanceManager = AttendanceManager.GetSingleton();
private Commands commands = Commands.GetSingleton();

public ChatEntryPoint(){}

Expand All @@ -35,7 +38,7 @@ public override void UpdateBeforeSimulation()
OutputManager.Run();
}

public void Initialise() //this wouldnt work as a constructor because im guessing some assets arent available during load time.
public void Initialise() //this wouldnt work as a constructor because some assets arent available during load time.
{
initialised = true;
soundPlayer = new SoundPlayer (debugging, true);
Expand Down Expand Up @@ -67,11 +70,11 @@ public void OnMessageEntered (string messageText, ref bool sendToOthers) //even

if (MyAPIGateway.Multiplayer.MultiplayerActive)
{
for (int i = 0; i < AttendanceManager.Players.Count; i++)
for (int i = 0; i < attendanceManager.Players.Count; i++)
{
if (AttendanceManager.PlayersMuteStatuses[AttendanceManager.Players[i].DisplayName] == false)
if (attendanceManager.PlayersMuteStatuses[attendanceManager.Players[i].DisplayName] == false)
{
MyAPIGateway.Multiplayer.SendMessageTo(packet_ID, ConvertedToPacket, AttendanceManager.Players[i].SteamUserId, true); //everyone will get this trigger including you.
MyAPIGateway.Multiplayer.SendMessageTo(packet_ID, ConvertedToPacket, attendanceManager.Players[i].SteamUserId, true); //everyone will get this trigger including you.
}
}
}
Expand All @@ -84,37 +87,37 @@ public void OnMessageEntered (string messageText, ref bool sendToOthers) //even

private void ExecuteCommandIfValid (string upperCaseSentence)
{
for (int i = 0; i < COMMANDS.VoiceCollection.Length; i++)
for (int i = 0; i < commands.VoiceCollection.Length; i++)
{
if (upperCaseSentence.Contains (COMMANDS.VoiceCollection[i]))
if (upperCaseSentence.Contains (commands.VoiceCollection[i]))
{
OutputManager.LocalPlayersVoice = PossibleOutputs.Collection[i];
return;
}
}
string[] choppedCommand = upperCaseSentence.Split (' ');

if (choppedCommand.Length >= COMMANDS.MUTING_MIN_SIZE)
if (choppedCommand.Length >= commands.MUTING_MIN_SIZE)
{
int startOfNameIndex = COMMANDS.MUTING_MIN_SIZE - 1;
int startOfNameIndex = commands.MUTING_MIN_SIZE - 1;
string interpretedInput = choppedCommand[startOfNameIndex];

for (int i = startOfNameIndex; i < choppedCommand.Length; i++)
{
interpretedInput += " " + choppedCommand[i];
}

if (upperCaseSentence.Contains (COMMANDS.MUTE_PLAYER))
if (upperCaseSentence.Contains (commands.MUTE_PLAYER))
{
AttendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, true);
attendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, true);
}

else if (upperCaseSentence.Contains (COMMANDS.UNMUTE_PLAYER))
else if (upperCaseSentence.Contains (commands.UNMUTE_PLAYER))
{
AttendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, false);
attendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, false);
}

else if (upperCaseSentence.Contains (COMMANDS.CHANGE_VOLUME))
else if (upperCaseSentence.Contains (commands.CHANGE_VOLUME))
{
float attemptedConversion;

Expand Down Expand Up @@ -183,24 +186,5 @@ public void Dispose()
OutputManager.DisposeOfUnsafe();
}
}

public struct COMMANDS
{
public const string CHANGE_VOICE_TO_MAREK = "[ MAREK";
//public const string CHANGE_VOICE_TO_HAWKING = "[ JOHN MADDEN";
public const string CHANGE_VOICE_TO_GLADOS = "[ GLADOS";

public static readonly string[] VoiceCollection = {
CHANGE_VOICE_TO_MAREK,
//CHANGE_VOICE_TO_HAWKING,
CHANGE_VOICE_TO_GLADOS,
};

public const string MUTE_PLAYER = "[ MUTE";
public const string UNMUTE_PLAYER = "[ UNMUTE";
public const int MUTING_MIN_SIZE = 3;

public const string CHANGE_VOLUME = "[ VOLUME";
}
}

40 changes: 40 additions & 0 deletions LookUpTables/Commands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SETextToSpeechMod.LookUpTables
{
class Commands
{
public readonly string CHANGE_VOICE_TO_MAREK = "[ MAREK";
//public readonly string CHANGE_VOICE_TO_HAWKING {get; private set;} = "[ JOHN MADDEN";
public readonly string CHANGE_VOICE_TO_GLADOS = "[ GLADOS";

public readonly string[] VoiceCollection;

public readonly string MUTE_PLAYER = "[ MUTE";
public readonly string UNMUTE_PLAYER = "[ UNMUTE";
public readonly int MUTING_MIN_SIZE = 3;

public readonly string CHANGE_VOLUME = "[ VOLUME";

private static Commands instance = new Commands();

private Commands()
{
VoiceCollection = new string[]
{
CHANGE_VOICE_TO_MAREK,
//CHANGE_VOICE_TO_HAWKING,
CHANGE_VOICE_TO_GLADOS,
};
}

public static Commands GetSingleton()
{
return instance;
}
}
}
7 changes: 4 additions & 3 deletions Output/OutputManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using SETextToSpeechMod.Output;
using SETextToSpeechMod.Processing;
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace SETextToSpeechMod
{
public class OutputManager
public class OutputManager : ISentenceReset
{
public const int MAX_LETTERS = 100;
const int UPDATES_INTERVAL = 60;
Expand Down Expand Up @@ -56,7 +57,7 @@ public OutputManager (SoundPlayer inputEmitter, bool isDebugging)
FactoryReset();
}

public void FactoryReset()
public void FactoryReset(params string[] toProcess)
{
IsProcessingOutputs = false;
typeIndexes = new int[PossibleOutputs.Collection.Count];
Expand Down Expand Up @@ -97,7 +98,7 @@ public void Run()
{
IsProcessingOutputs = true;

if (speechesFields[i].ReturnInfo.Status == TaskStatus.Created) //assuming async calls has matching length to speechesField
if (speechesFields[i].ReturnInfo.Status == TaskStatus.Created) //assuming async calls have matching length to speechesField
{
int savedIndex = i; //fixed strange bug where i goes out of bounds even though the for loop prevents that; Weird!

Expand Down
13 changes: 13 additions & 0 deletions Processing/ISentenceReset.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SETextToSpeechMod.Processing
{
interface ISentenceReset
{
void FactoryReset(string newSentence);
}
}
2 changes: 1 addition & 1 deletion Processing/Intonation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public string GetPhonemesIntonation (string phoneme, string surroundingPhrase, b

else
{
throw new UnconventionalPhraseException ("surroundingPhrase must be " + Pronunciation.ALGORITHM_PHRASE_SIZE + " characters long.");
throw new UnconventionalPhraseException ("surroundingPhrase must be " + Pronunciation.ALGORITHM_PHRASE_SIZE + " characters long. Developers fault."); //an internal error that can only occur if I have broken something. not user input related
}
}

Expand Down
18 changes: 9 additions & 9 deletions Processing/Pronunciation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace SETextToSpeechMod.Processing
{
public class Pronunciation
public class Pronunciation : ISentenceReset
{
//Constants
/// <summary>
Expand Down Expand Up @@ -37,21 +37,21 @@ public Pronunciation (Intonation intonationType)
this.intonationGen = intonationType;
}

public void FactoryReset()
public void FactoryReset(string newSentence)
{
surroundingPhrase = "";
surroundingPhrase = string.Empty;
PreviousProcessUsedDictionary = false;
WrongFormatMatches = 0;
WrongFormatNonMatches = 0;
currentResults.Clear();
dictionaryMatch = null;

tempSentence = "";
tempSentence = string.Empty;
tempLetterIndex = 0;

pushingDictionaryWordOut = false;

WordIsolator.FactoryReset();
WordIsolator.FactoryReset(newSentence);
}

/// <summary>
Expand All @@ -65,19 +65,19 @@ public void FactoryReset()
{
pushingDictionaryWordOut = false;
currentResults = new List <string>();
WordIsolator.UpdateProperties (sentence, letterIndex); //Incrementing the WordIsolator must happen at the beginning of a new letter analysis. This is so optional debugger can pick up accurate properties after each letter analysis.
WordIsolator.MoveNext(); //Incrementing the WordIsolator must happen at the beginning of a new letter analysis. This is so optional debugger can pick up accurate properties after each letter analysis.
tempSentence = sentence;
tempLetterIndex = letterIndex;
currentResults.Clear();
surroundingPhrase = "";
surroundingPhrase = string.Empty;

if (WordIsolator.CurrentWord != SPACE)
if (WordIsolator.Current != SPACE)
{
if (WordIsolator.CurrentWordIsNew == true)
{
dictionaryMatch = null;
PreviousProcessUsedDictionary = false; //prevent false positives
PreviousProcessUsedDictionary = PrettyScaryDictionary.TTS_DICTIONARY.TryGetValue (WordIsolator.CurrentWord, out dictionaryMatch);
PreviousProcessUsedDictionary = PrettyScaryDictionary.TTS_DICTIONARY.TryGetValue (WordIsolator.Current, out dictionaryMatch);

if (PreviousProcessUsedDictionary)
{
Expand Down
20 changes: 20 additions & 0 deletions Processing/TimelineClip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SETextToSpeechMod.Processing
{
public struct TimelineClip
{
public int StartPoint { get; }
public string ClipsSound { get; }

internal TimelineClip (int inputPoint, string inputSound)
{
this.StartPoint = inputPoint;
this.ClipsSound = inputSound;
}
}
}
Loading

0 comments on commit 29e8b6c

Please sign in to comment.