Skip to content

Commit

Permalink
chore: add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
nwesterhausen committed Jun 7, 2024
1 parent 06bf64d commit 79f12b6
Showing 1 changed file with 120 additions and 17 deletions.
137 changes: 120 additions & 17 deletions src/MessageTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ internal static class MessageTransformer
private const string DAY_NUMBER = "%DAY_NUMBER%";
private const string NUM_PLAYERS = "%NUM_PLAYERS%";

private static Regex OpenCaretRegex = new Regex(@"<[\w=]+>");
private static Regex CloseCaretRegex = new Regex(@"</[\w]+>");
private static readonly Regex OpenCaretRegex = new(@"<[\w=]+>");
private static readonly Regex CloseCaretRegex = new(@"</[\w]+>");

/// <summary>
/// Replace the static variables in the message. These are variables that are set in the config file.
/// Replaces:
/// - `%VAR1%` through `%VAR10%` with the user variables
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
private static string ReplaceVariables(string rawMessage)
{
return ReplaceDynamicVariables(rawMessage)
Expand All @@ -48,8 +54,15 @@ private static string ReplaceVariables(string rawMessage)

/// <summary>
/// Replace dynamic variables in the message. These are variables that are not static and need to be calculated at runtime.
/// Replaces:
/// - `%PUBLICIP%` with the public IP of the server
/// - `%WORLD_NAME%` with the name of the world
/// - `%DAY_NUMBER%` with the current day number
/// - `%NUM_PLAYERS%` with the number of players in the server
/// </summary>
private static string ReplaceDynamicVariables(string rawMessage) {
/// <param name="rawMessage">Raw message to format</param>
private static string ReplaceDynamicVariables(string rawMessage)
{
// additionally add any other dynamic variables here..
string dynamicReplacedMessage = ReplacePublicIp(rawMessage);
dynamicReplacedMessage = ReplaceWorldName(dynamicReplacedMessage);
Expand All @@ -61,7 +74,9 @@ private static string ReplaceDynamicVariables(string rawMessage) {

/// <summary>
/// Replace the day number in the message. Uses the EnvMan instance to get the current day.
/// This will only replace `%DAY_NUMBER%` with the current day if the EnvMan instance is available.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
private static string ReplaceDayNumber(string rawMessage)
{
// as written, if no EnvMan instance is available, it will return the raw message with `%DAY_NUMBER%` still in it.
Expand All @@ -71,11 +86,13 @@ private static string ReplaceDayNumber(string rawMessage)

/// <summary>
/// Replace the number of players in the message. Uses the ZNet instance to get the number of players.
/// This will only replace `%NUM_PLAYERS%` with the number of players if the ZNet instance is available.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
private static string ReplaceNumPlayers(string rawMessage)
{
return rawMessage
.Replace(NUM_PLAYERS, ZNet.instance.GetNrOfPlayers().ToString());
.Replace(NUM_PLAYERS, ZNet.instance != null ? ZNet.instance.GetNrOfPlayers().ToString() : NUM_PLAYERS);
}

/// <summary>
Expand All @@ -85,36 +102,48 @@ private static string ReplaceNumPlayers(string rawMessage)
/// the public IP address. This is out of scope for this plugin, to avoid making our own assumptions about the
/// network configuration of the server.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
private static string ReplacePublicIp(string rawMessage)
{
string publicIp = "";
try
{
publicIp = ZNet.GetPublicIP();
}
catch (System.Exception e)
{
Plugin.StaticLogger.LogError($"Unable to get Public IP from ZNet. {e.Message}");
}
return rawMessage
.Replace(PUBLIC_IP, ZNet.GetPublicIP());
.Replace(PUBLIC_IP, publicIp);
}

/// <summary>
/// Replace the world name in the message. Uses the ZNet instance to get the world name.
/// This will only replace `%WORLD_NAME%` with the world name if the ZNet instance is available.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
private static string ReplaceWorldName(string rawMessage)
{
string world_name = "";
try
{
world_name = ZNet.instance.GetWorldName();
}
catch (System.Exception e)
{
Plugin.StaticLogger.LogError($"Unable to get World Name from ZNet. {e.Message}");
}

return rawMessage
.Replace(WORLD_NAME, world_name);
.Replace(WORLD_NAME, ZNet.instance != null ? ZNet.instance.GetWorldName() : WORLD_NAME);
}

/// <summary>
/// Format a server message using the config values.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
public static string FormatServerMessage(string rawMessage)
{
return MessageTransformer.ReplaceVariables(rawMessage);
}

/// <summary>
/// Format a server message using the config values, with extra player information.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="playerName">Name of the player</param>
/// <param name="playerId">ID of the player</param>
public static string FormatPlayerMessage(string rawMessage, string playerName, string playerId)
{
return MessageTransformer.ReplaceVariables(rawMessage)
Expand All @@ -123,58 +152,132 @@ public static string FormatPlayerMessage(string rawMessage, string playerName, s
.Replace(PLAYER_NAME, playerName);
}

/// <summary>
/// Format a server message using the config values, with extra player information.
/// Specifically, this version includes the player's position in the message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="playerName">Name of the player</param>
/// <param name="playerId">ID of the player</param>
/// <param name="pos">Position of the player</param>
public static string FormatPlayerMessage(string rawMessage, string playerName, string playerId, UnityEngine.Vector3 pos)
{
return MessageTransformer.FormatPlayerMessage(rawMessage, playerName, playerId)
.Replace(POS, $"{pos}");
}
/// <summary>
/// Format a server message using the config values, with extra player information.
/// Specifically, this version includes the shout message in the message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="playerName">Name of the player</param>
/// <param name="playerId">ID of the player</param>
/// <param name="shout">Shout message</param>
public static string FormatPlayerMessage(string rawMessage, string playerName, string playerId, string shout)
{
return MessageTransformer.FormatPlayerMessage(rawMessage, playerName, playerId)
.Replace(SHOUT, shout);
}
/// <summary>
/// Format a server message using the config values, with extra player information.
/// Specifically, this version includes the shout message and the player's position in the message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="playerName">Name of the player</param>
/// <param name="playerSteamId">Steam ID of the player</param>
/// <param name="shout">Shout message</param>
/// <param name="pos">Position of the player</param>
public static string FormatPlayerMessage(string rawMessage, string playerName, string playerSteamId, string shout, UnityEngine.Vector3 pos)
{
return MessageTransformer.FormatPlayerMessage(rawMessage, playerName, playerSteamId, pos)
.Replace(SHOUT, shout);
}
/// <summary>
/// Format an event message using the config values, with extra event information.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="eventStartMsg">Event start message</param>
/// <param name="eventEndMsg">Event end message</param>
public static string FormatEventMessage(string rawMessage, string eventStartMsg, string eventEndMsg)
{
return MessageTransformer.ReplaceVariables(rawMessage)
.Replace(EVENT_START_MSG, eventStartMsg)
.Replace(EVENT_END_MSG, eventEndMsg);
//.Replace(EVENT_PLAYERS, players); //! Removed until re can reliably poll player locations
}
/// <summary>
/// Format an event message using the config values, with extra event information.
/// Specifically, this version includes the event position in the message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="eventStartMsg">Event start message</param>
/// <param name="eventEndMsg">Event end message</param>
/// <param name="pos">Position of the event</param>
public static string FormatEventMessage(string rawMessage, string eventStartMsg, string eventEndMsg, UnityEngine.Vector3 pos)
{
return MessageTransformer.FormatEventMessage(rawMessage, eventStartMsg, eventEndMsg)
.Replace(POS, $"{pos}");
}
/// <summary>
/// Format an event start message. This will only replace the event message with the start message.
/// </summary>
public static string FormatEventStartMessage(string rawMessage, string eventStartMsg, string eventEndMsg)
{
return MessageTransformer.FormatEventMessage(rawMessage, eventStartMsg, eventEndMsg)
.Replace(EVENT_MSG, eventStartMsg);
}
/// <summary>
/// Format an event end message. This will only replace the event message with the end message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="eventStartMsg">Event start message</param>
/// <param name="eventEndMsg">Event end message</param>
/// <param name="pos">Position of the event</param>
public static string FormatEventEndMessage(string rawMessage, string eventStartMsg, string eventEndMsg)
{
return MessageTransformer.FormatEventMessage(rawMessage, eventStartMsg, eventEndMsg)
.Replace(EVENT_MSG, eventEndMsg);
}
/// <summary>
/// Format an event start message. This will only replace the event message with the start message.
/// Specifically, this version includes the event position in the message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="eventStartMsg">Event start message</param>
/// <param name="eventEndMsg">Event end message</param>
/// <param name="pos">Position of the event</param>
public static string FormatEventStartMessage(string rawMessage, string eventStartMsg, string eventEndMsg, UnityEngine.Vector3 pos)
{
return MessageTransformer.FormatEventMessage(rawMessage, eventStartMsg, eventEndMsg, pos)
.Replace(EVENT_MSG, eventStartMsg);
}
/// <summary>
/// Format an event end message. This will only replace the event message with the end message.
/// Specifically, this version includes the event position in the message.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="eventStartMsg">Event start message</param>
/// <param name="eventEndMsg">Event end message</param>
/// <param name="pos">Position of the event</param>
public static string FormatEventEndMessage(string rawMessage, string eventStartMsg, string eventEndMsg, UnityEngine.Vector3 pos)
{
return MessageTransformer.FormatEventMessage(rawMessage, eventStartMsg, eventEndMsg, pos)
.Replace(EVENT_MSG, eventEndMsg);
}
/// <summary>
/// Format the header of the leaderboard header using the config values.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
public static string FormatLeaderBoardHeader(string rawMessage)
{
return MessageTransformer.ReplaceVariables(rawMessage);
}

/// <summary>
/// Format the header of the leaderboard header using the config values.
/// Specifically, this version includes the number of items in the leaderboard.
/// </summary>
/// <param name="rawMessage">Raw message to format</param>
/// <param name="n">Number of items in the leaderboard</param>
public static string FormatLeaderBoardHeader(string rawMessage, int n)
{
return MessageTransformer.ReplaceVariables(rawMessage)
Expand Down

0 comments on commit 79f12b6

Please sign in to comment.