Skip to content

Commit

Permalink
Added shortcut method ScpArgMsg
Browse files Browse the repository at this point in the history
  • Loading branch information
exectails committed Dec 3, 2023
1 parent bbf9e70 commit bb4bb2e
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions src/ZoneServer/Scripting/Shortcuts.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Melia.Shared.Configuration.Files;
Expand Down Expand Up @@ -417,6 +418,44 @@ public static void AddChatCommand(string command, string usage, string descripti
ZoneServer.Instance.ChatCommands.Add(command, usage, description, func);
ZoneServer.Instance.Conf.Commands.CommandLevels[command] = new CommandAuthLevels(auth, targetAuth);
}

/// <summary>
/// Formats the given message name and arguments in a way that the
/// client can recognize as a client message with arguments.
/// </summary>
/// <param name="messageName">Name of the client message.</param>
/// <param name="args">Optional list of arguments as key/value pairs.</param>
/// <returns></returns>
/// <exception cref="ArgumentException"></exception>
/// <example>
/// ScpArgMsg("KlaipedaCentralPlaza")
/// ScpArgMsg("InputPriceBetween{MIN}{MAX}", "MIN", 1, "MAX", 10)
/// </example>
public static string ScpArgMsg(string messageName, params object[] args)
{
if (args != null && args.Length % 2 != 0)
throw new ArgumentException("Expected an even amount of arguments for the key/value arguments.");

var result = new StringBuilder();

result.Append("!@#$" + messageName);

if (args != null)
{
for (var i = 0; i < args.Length; i += 2)
{
var key = args[i];
var value = args[i + 1];

result.Append("$*$" + key);
result.Append("$*$" + value);
}
}

result.Append("#@!");

return result.ToString();
}
}

/// <summary>
Expand Down

0 comments on commit bb4bb2e

Please sign in to comment.