Skip to content

Commit

Permalink
added negative rolls and cleaned up spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronpenner committed Aug 9, 2016
1 parent 2c0b79e commit ee9c2b9
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions DiscordBot/RollCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Discord.Commands;
using System;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DiscordBot
Expand All @@ -14,6 +15,8 @@ public string Roll(string parameters)
_message = "";
string errorMessage = "There was an error, try the format: !roll 5d3 -3";

Regex.Replace(parameters, @"(?<=-|\+)\s+", "");

string[] tokens = parameters.Split(' ');

int total = 0;
Expand All @@ -23,8 +26,8 @@ public string Roll(string parameters)
foreach(var token in tokens)
{
total += GetValue(token);
_message += $"\n\n**Total: {total}**";
}
_message += $"\n**Total: {total}**";
}
catch(Exception e)
{
Expand All @@ -40,8 +43,7 @@ private int GetValue(string roll)

if(int.TryParse(roll, out result))
{
string sign = result > 1 ? "+" : "";
_message += $" {sign}{result}";
_message += $" {result}\n";
}
else
{
Expand All @@ -61,6 +63,7 @@ private int GetRoll(string roll)

int mult;
int sides;
int sign = 1;

if(numbers.Length != 2)
{
Expand All @@ -79,11 +82,17 @@ private int GetRoll(string roll)

int output = 0;

if(mult < 0)
{
sign = -1;
mult *= -1;
}

_message += $" {roll}[";
Random rand = new Random();
while(mult > 0)
{
int rollResult = rand.Next(1, sides + 1);
int rollResult = rand.Next(1, sides + 1) * sign;

_message += rollResult;
if(mult > 1)
Expand All @@ -94,7 +103,7 @@ private int GetRoll(string roll)
output += rollResult;
mult--;
}
_message += "]";
_message += "]\n";

return output;
}
Expand Down

0 comments on commit ee9c2b9

Please sign in to comment.