private void OnCommandRoll(string Source, string Cmd)
{
int PlusMinusIndex = Cmd.IndexOfAny(new char[] { '+', '-' });
string PlusMinus = null;
string[] CmdArgs;
if (PlusMinusIndex != -1)
{
PlusMinus = Cmd.Substring(PlusMinusIndex);
Cmd = Cmd.Substring(0, PlusMinusIndex);
}
CmdArgs = Cmd.Split(new char[] { 'd' });
if (CmdArgs.Length != 2)
{
SendMessage(SendType.Message, Source, "Usage: !roll <count>d<sides>[+/-delta].");
return;
}
try
{
string RollsMsg;
int[] Rolls;
int Dice = Convert.ToInt32(CmdArgs[0]);
int Sides = Convert.ToInt32(CmdArgs[1]);
int Sum = 0;
if (Dice < 1 || Sides < 1 || Dice > 100)
{
SendMessage(SendType.Message, Source, "Invalid arguments for !roll request.");
return;
}
Rolls = new int[Dice];
for (int i = 0; i < Dice; i += 1)
{
Rolls[i] = (Rng.Next() % Sides) + 1;
}
Sum = Rolls.Sum();
if (PlusMinus == null)
{
PlusMinus = "";
}
else
{
Sum += Convert.ToInt32(PlusMinus);
}
RollsMsg = string.Join(", ", Rolls);
if (RollsMsg.Length > 400)
RollsMsg = RollsMsg.Substring(0, 400);
if (Dice > 1)
{
SendMessage(SendType.Message, Source, String.Format(
"Rolled {0}d{1}{2} ({3}): {4}", Dice, Sides, PlusMinus, RollsMsg, Sum));
}
else
{
SendMessage(SendType.Message, Source, String.Format(
"Rolled {0}d{1}{2}: {3}", Dice, Sides, PlusMinus, Sum));
}
}
catch (Exception)
{
SendMessage(SendType.Message, Source, "Internal error processing !roll request.");
}
}