Skip to content

Commit

Permalink
Refactored static managers. Still needs to be review for bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
Roof Level Suite committed Dec 14, 2016
1 parent 8214026 commit 4c7b2c1
Show file tree
Hide file tree
Showing 23 changed files with 486 additions and 281 deletions.
Binary file modified .vs/SE TextToSpeechMod/v14/.suo
Binary file not shown.
2 changes: 1 addition & 1 deletion AdjacentResults.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Total Words: 203
Total Incorrect: 1
Previous Incorrect: 0
Previous Incorrect: 1
From Dictionary: 0
Lowercase Keys: 0
Wrong Format Matchers: 0
Expand Down
65 changes: 38 additions & 27 deletions AttendanceManager.cs
Original file line number Diff line number Diff line change
@@ -1,59 +1,49 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

using VRage.Game.ModAPI;
using Sandbox.ModAPI;

namespace SETextToSpeechMod
{
static class AttendanceManager
{
{
private static List <IMyPlayer> playersField = new List <IMyPlayer>();
public static IList <IMyPlayer> Players
{
get
{
playersField.Clear(); //GetPlayers() just adds without overwriting so list must be cleared every time.

if (OutputManager.Debugging == false)
{
MyAPIGateway.Multiplayer.Players.GetPlayers (playersField); //everytime the project needs to see all players, this will update. Little heavier on performance but its polymorphic.

for (int i = 0; i < playersField.Count; i++)
{
if (muteStatusesField.ContainsKey (Players[i].DisplayName) == false)
{
muteStatusesField.Add (Players[i].DisplayName, new bool());
}
}
}
return playersField.AsReadOnly();
}
}
}

/// <summary>
/// NOT GUARANTEED TO CONTAIN EVERY PLAYER. CHECK IF ITS THERE FIRST.
/// CAN BE NULL
/// </summary>
public static IMyPlayer LocalPlayer;

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

/// <summary>
/// Mutes or unmutes the requests player based on your bool input. prints outcome which depends on if the input player existed or not. (check for typos)
/// 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)
{
for (int i = 0; i < Players.Count; i++)
UpdatePlayers();

for (int i = 0; i < playersField.Count; i++)
{
if (Players[i].DisplayName == player)
if (playersField[i].DisplayName == player)
{
if (muteStatusesField.ContainsKey (player))
{
Expand All @@ -65,8 +55,6 @@ public static void ChangeMuteStatusOfPlayer (string player, bool newMuteStatus)
muteStatusesField.Add (player, newMuteStatus);
}

muteStatusesField.Add ("", true);

switch (newMuteStatus)
{
case true:
Expand All @@ -80,7 +68,30 @@ public static void ChangeMuteStatusOfPlayer (string player, bool newMuteStatus)
return;
}
}
MyAPIGateway.Utilities.ShowMessage ("", "Player: '" + player + "' does not exist.");
MyAPIGateway.Utilities.ShowMessage ("", "Player: '" + player + "' could not be found.");
}

public static void UpdatePlayers()
{
playersField.Clear(); //GetPlayers() just adds without overwriting so list must be cleared every time.

if (Debugging == false)
{
MyAPIGateway.Multiplayer.Players.GetPlayers (playersField);

for (int i = 0; i < playersField.Count; i++)
{
if (muteStatusesField.ContainsKey (playersField[i].DisplayName) == false)
{
muteStatusesField.Add (playersField[i].DisplayName, new bool());
}

if (playersField[i].SteamUserId == MyAPIGateway.Multiplayer.MyId)
{
LocalPlayer = playersField[i];
}
}
}
}
}
}
192 changes: 192 additions & 0 deletions ChatManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
using System;
using System.Text; //location of encoding/decoding.

using Sandbox.ModAPI; //location of MyAPIGateway.
using VRage.Game.Components; //location of MySessionComponentBase.

namespace SETextToSpeechMod
{
[MySessionComponentDescriptor (MyUpdateOrder.BeforeSimulation)] //adds an attribute tag telling the game to run my script.
class ChatManager : MySessionComponentBase
{
const ushort packet_ID = 60452; //the convention is to use the last 4-5 digits of your steam mod as packet ID

bool initialised;
private bool debugging;

private Encoding encode = Encoding.Unicode; //encoding is necessary to convert message into correct format.
private SoundPlayer soundPlayer;
public OutputManager OutputManager { get; private set; }

public ChatManager (bool isDebugging)
{
debugging = isDebugging;
}

public override void UpdateBeforeSimulation()
{
if (initialised == false)
{
Initialise();
}
OutputManager.Run();
}

public void Initialise() //this wouldnt work as a constructor because im guessing some assets arent available during load time.
{
initialised = true;
soundPlayer = new SoundPlayer (debugging);
OutputManager = new OutputManager (soundPlayer, debugging);

if (debugging == false)
{
MyAPIGateway.Utilities.MessageEntered += OnMessageEntered; //subscribes my method to the MessageEntered event.
MyAPIGateway.Multiplayer.RegisterMessageHandler (packet_ID, OnReceivedPacket); //registers a multiplayer packet receiver.
AttendanceManager.UpdatePlayers();
MyAPIGateway.Utilities.ShowMessage ("TextToSpeechMod:", "If you find a broken word, please tell the designer.");
}
}

public void OnMessageEntered (string messageText, ref bool sendToOthers) //event handler method will run when this client posts a chat message.
{
string noEscapes = string.Format (@"{0}", messageText);
string fixedCase = noEscapes.ToUpper(); //capitalize all letters of the input sentence so that comparison is made easier.
ExecuteCommandIfValid (fixedCase);
string signatureBuild = OutputManager.LocalPlayersVoice.ToString();
int leftoverSpace = POSSIBLE_OUTPUTS.AutoSignatureSize - OutputManager.LocalPlayersVoice.ToString().Length;

for (int i = 0; i < leftoverSpace; i++)
{
signatureBuild += " ";
}
fixedCase = signatureBuild + fixedCase;
byte[] ConvertedToPacket = encode.GetBytes (fixedCase);

if (MyAPIGateway.Multiplayer.MultiplayerActive)
{
for (int i = 0; i < AttendanceManager.Players.Count; i++)
{
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.
}
}
}

else
{
OnReceivedPacket (ConvertedToPacket);
}
}

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

if (choppedCommand.Length >= COMMANDS.MUTING_MIN_SIZE)
{
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))
{
AttendanceManager.ChangeMuteStatusOfPlayer (interpretedInput, true);
}

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

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

if (float.TryParse (interpretedInput, out attemptedConversion))
{
soundPlayer.UpdateVolume (attemptedConversion);
}

else
{
MyAPIGateway.Utilities.ShowMessage ("", "Could not determine a valid volume from: " + interpretedInput);
}
}
}
}

public void OnReceivedPacket (byte[] receivedPacket) //action type method which handles the received packets from other players.
{
string decoded = encode.GetString (receivedPacket);
string signature = ExtractSignatureFromPacket (ref decoded);

if (decoded.Length > OutputManager.MAX_LETTERS && //letter limit for mental health concerns.
debugging == false)
{
MyAPIGateway.Utilities.ShowMessage (OutputManager.MAX_LETTERS.ToString(), " LETTER LIMIT REACHED");
}

else
{
Type signatureConverted = Type.GetType (signature);
OutputManager.CreateNewSpeech (signatureConverted, decoded);
}
}

//dont blame me if your string gets cut the fuck up if it doesnt contain a signature!
private string ExtractSignatureFromPacket (ref string packet)
{
char[] dividedMessage = packet.ToCharArray();
char[] signatureChars = new char[POSSIBLE_OUTPUTS.AutoSignatureSize];

for (int i = 0; i < signatureChars.Length; i++)
{
signatureChars[i] = dividedMessage[i];
}
string voiceSignature = new string (signatureChars);

packet = packet.Remove (0, POSSIBLE_OUTPUTS.AutoSignatureSize);
return voiceSignature;
}

protected override void UnloadData() //will run when the session closes to prevent my assets from doubling up.
{
initialised = false;
MyAPIGateway.Utilities.MessageEntered -= OnMessageEntered;
MyAPIGateway.Multiplayer.UnregisterMessageHandler (packet_ID, OnReceivedPacket);
}
}

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[] VOICE_COLLECTION = {
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";
}
}

2 changes: 1 addition & 1 deletion GLADOSVoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public sealed class GLADOSVoice : SentenceFactory, VoiceTemplate
new int[] { 0, 1, 2, },
};

public GLADOSVoice() : base(){}
public GLADOSVoice (SoundPlayer inputEmitter) : base (inputEmitter){}
}
}
2 changes: 1 addition & 1 deletion HawkingVoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public sealed class HawkingVoice : SentenceFactory, VoiceTemplate
};
string sentenceEndPhonemeID = "-E";

public HawkingVoice() : base(){}
public HawkingVoice (SoundPlayer inputEmitter) : base (inputEmitter){}

//the point of extending this method is to create a special kind of phoneme at the end of every sentence.
protected override void AddIntonations (int timelineIndex)
Expand Down
2 changes: 1 addition & 1 deletion MarekVoice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public sealed class MarekVoice : SentenceFactory, VoiceTemplate
public override int ClipLength { get { return 4; } }
public override int SyllableSize { get { return 3; } }

public MarekVoice() : base(){}
public MarekVoice (SoundPlayer inputEmitter) : base (inputEmitter){}

protected override void AddIntonations (int timelineIndex) { }
}
Expand Down
Loading

0 comments on commit 4c7b2c1

Please sign in to comment.