Skip to content

Commit

Permalink
1.0.1.0 - Added comments and documented code (still WIP)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lurchicus committed Apr 11, 2021
1 parent 637b335 commit aad0d40
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 112 deletions.
223 changes: 119 additions & 104 deletions DiceAPI/Controllers/DiesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ namespace DiceAPI.Controllers

public class DiesController : ControllerBase
{
//private readonly DiesContext _context;
//private readonly CultureInfo culture = new CultureInfo("en-US");

//public DiesController(DiesContext context)
//{
// _context context;
//}

/// <summary>
/// Create an roll dice and return the result.
/// </summary>
/// <param name="qty">int: Number of dice to "throw"</param>
/// <param name="sides">int: Number of sides on a die</param>
/// <param name="adj">int: Adjustment to apply to the total result</param>
/// <returns>ActionResult: List of Dies (one record in this case)</returns>
[HttpGet("qty/{qty}/sides/{sides}/adj/{adj}")]
public ActionResult<Dies> GetDies(int qty, int sides, int adj)
{
Expand Down Expand Up @@ -48,10 +47,17 @@ public ActionResult<Dies> GetDies(int qty, int sides, int adj)
return Ok(Dice[Dice.Count - 1]);
}

/// <summary>
/// Take a dice notation string to determine what dice we need to
/// throw
/// </summary>
/// <param name="cmd">string: Dice notation string (quantityDsides[[+|-}adjustment],
/// ie 1D6+1)</param>
/// <returns>ActionResult: List of Dies (one record in this case)</returns>
[HttpGet("dand/{cmd}")]
public ActionResult<Dies> GetDandD(string cmd)
{
int Quantity = 0; // Default 1D6
int Quantity = 0; // Default 1D6
int Sides = 6;
int Adjustment = 0;
List<Dies> Dice = new(); // List to hold return
Expand All @@ -74,7 +80,7 @@ public ActionResult<Dies> GetDandD(string cmd)
dies.Sides = Sides;
dies.Result = AResult;

// Dump into a list so it can be serialized json
// Dump into a list so it can be serialized by json
Dice.Add(dies);
}
catch (Exception e)
Expand All @@ -88,120 +94,129 @@ public ActionResult<Dies> GetDandD(string cmd)
return Ok(Dice[Dice.Count - 1]);
}

/// <summary>
/// Parse the nDx[[+|-]n] dice notation string and return Quantity, Sides and
/// Adjustment by reference to the caller
/// </summary>
/// <param name="arg">string: Dice notation to be parsed</param>
/// <param name="Quantity">int; The number of dice to roll (1:1000)</param>
/// <param name="Sides">int: The number of side a die has (0:1000) (0 allows for
/// a special case "coin flip" mode that returns 0 or 1)</param>
/// <param name="Adjustment">int: Adjustment that is applied to the total result
/// of the dice throws</param>
private void Parse(string arg, ref int Quantity, ref int Sides, ref int Adjustment)
{
string arrg = arg.Trim().ToUpper();
string[] parm1 = { "D" }; //Dies/sides delimiter
string[] parm2 = { "+", "-" }; //Adjustment delimiter
switch (arg)

// Parse individual dice roll command or default to 1d6+0

// No arguments, use 1D6 default
if (arrg.Length == 0)
{
Quantity = 1;
Sides = 6;
Adjustment = 0;
return;
}

// We started with a D (implies a singlr die)
if (arrg.StartsWith("D"))
{
// Just a "D"? Default with 1D6+0
if (arrg.Length == 1)
{
Quantity = 1;
Sides = 6;
Adjustment = 0;
return;
}
//More to do here... default to one die and continue
Quantity = 1;
arrg = arrg.Substring(1, arrg.Length - 1);
}
else
{
//Didn't start with a "D" split using "D" as a delimiter. The first argument
//should be the die count
string[] ary = arrg.Split(parm1, StringSplitOptions.RemoveEmptyEntries);
string sside = ary[0];
try
{
Quantity = Convert.ToInt32(sside);
}
catch
{
// Default to one die if dice count is bad
Quantity = 1;
}
if (ary.Length == 1)
{
// Only die count, default to 6 sides die with no adjustment
Sides = 6;
Adjustment = 0;
return;
}
// More to do after "D", pass it along
arrg = ary[1];
}

default: //Parse individual dice roll command or default to 1d6+0
if (arrg.Length == 0)
// Split the remaining parts using "+" or "-" as a delimiter
string[] ary2 = arrg.Split(parm2, StringSplitOptions.RemoveEmptyEntries);
if (ary2.Length == 1)
{
// We only got a single result which should be the number of sides
// on the die with no adjustment
try
{
Sides = Convert.ToInt32(ary2[0]);
}
catch
{
// Default to 6 sides if value is bogus
Sides = 6;
}
Adjustment = 0;
}
else
{
// More than a single result, should have die side count and adjustment
if (ary2.Length > 1)
{
try
{
Quantity = 1;
Sides = 6;
Adjustment = 0;
break;
Sides = Convert.ToInt32(ary2[0]);
}
if (arrg.StartsWith("D"))
catch
{
//Does the command start with "D"
if (arrg.Length == 1)
{
Quantity = 1;
Sides = 6;
Adjustment = 0;
break;
}
else
{
//More to do here, default to one die
Quantity = 1;
arrg = arrg.Substring(1, arrg.Length - 1);
}
// Thisis so a bogus adjustment will default to a 6 sided die
Sides = 6;
}
else
try
{
//Didn't start with a "D" split using "D" as a delimiter. The first argument
//should be the die count
string[] ary = arrg.Split(parm1, StringSplitOptions.RemoveEmptyEntries);
string sside = ary[0];
try
{
Quantity = Convert.ToInt32(sside);
}
catch
{
Quantity = 1;
}
if (ary.Length == 1)
{
// Only die count, default to 6 sides die with no adjustment
Sides = 6;
Adjustment = 0;
break;
}
else
{
//More to do after "D", pass it along
arrg = ary[1];
}
Adjustment = Convert.ToInt32(ary2[1]);
}
//Split the remaining parts using "+" or "-" as a delimiter
string[] ary2 = arrg.Split(parm2, StringSplitOptions.RemoveEmptyEntries);
if (ary2.Length == 1)
catch
{
//We only got a single result which should be the number of sides
//on the die with no adjustment
try
{
Sides = Convert.ToInt32(ary2[0]);
}
catch
{
Sides = 6;
}
// This is so a bogus adjustment will default to 0 adjustment
Adjustment = 0;
}
else
if (Adjustment > 0)
{
//More than a single result, should have die side count and adjustment
if (ary2.Length > 1)
{
try
{
Sides = Convert.ToInt32(ary2[0]);
}
catch
{
Sides = 6;
}
try
{
Adjustment = Convert.ToInt32(ary2[1]);
}
catch
{
Adjustment = 0;
}
if (Adjustment > 0)
{
if (arrg.Contains("-"))
{
//if the delimiter was a "-", negate the adjustment
Adjustment = 0 - Adjustment;
}
}
}
if (ary2.Length <= 0)
if (arrg.Contains("-"))
{
//Nothing to see here, default six sided die and no adjustment
Sides = 6;
Adjustment = 0;
// if the delimiter was a "-", negate the adjustment
Adjustment = 0 - Adjustment;
}
}
break;
}
if (ary2.Length <= 0)
{
// Nothing to see here, default six sided die and no adjustment
Sides = 6;
Adjustment = 0;
}
}
}
}
Expand Down
33 changes: 30 additions & 3 deletions DiceAPI/Dice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,20 @@

namespace DiceAPI
{
/// <summary>
/// Create and throw multiple dice using die class
/// </summary>
class Dice
{
public List<Die> DiceCup = new(); // A nice list object to hold our dies

private int _Quantity; // 1 to 1000 dies
private int _Sides; // 0 to 1000 sides (0 side is for a 1 or 0 coin toss)
private int _Sides; // 1 to 1000 sides (1 side is for a 1 or 0 coin toss)
private int _Adjustment; // Value to add to the sum of our results

/// <summary>
/// Default constructor throws a singler 6 sides die
/// </summary>
public Dice()
{
// Default to 1D6+0
Expand All @@ -22,6 +28,14 @@ public Dice()
DiceCup.Add(ADie);
}

/// <summary>
/// Constructor overload
/// Rolls 1 to 1000 dies (dice) with 0 to 1000 sides with an sdjustment to the
/// total result.
/// </summary>
/// <param name="DiceQuantity">int: 1 to 1000 dice</param>
/// <param name="DiceSides">int: 1 to 1000 sides</param>
/// <param name="DiceAdjustment">int: Adjustment to total result</param>
public Dice(int DiceQuantity, int DiceSides, int DiceAdjustment)
{
// Up to 1000 dies at a time
Expand All @@ -34,13 +48,13 @@ public Dice(int DiceQuantity, int DiceSides, int DiceAdjustment)
throw new Exception("Dice error: " + DiceQuantity.ToString() + " dies is out of range (1:1000).");
}
// Up to 1000 sides on a die
if (DiceSides >= 0 && DiceSides <= 1000)
if (DiceSides >= 1 && DiceSides <= 1000)
{
Sides = DiceSides;
}
else
{
throw new Exception("Dice error: " + DiceSides.ToString() + " die sides is out of range (0:1000).");
throw new Exception("Dice error: " + DiceSides.ToString() + " die sides is out of range (1:1000).");
}
Adjustment = DiceAdjustment;
//List<Die> DiceCup = new() { };
Expand All @@ -66,6 +80,10 @@ public Dice(int DiceQuantity, int DiceSides, int DiceAdjustment)
}
}

/// <summary>
/// Get and return the total throw results and adjust
/// </summary>
/// <returns>int: Adjusted sum of all throws</returns>
public int Results()
{
int Sum = 0;
Expand All @@ -76,18 +94,27 @@ public int Results()
return Sum + Adjustment;
}

/// <summary>
/// Dice Quantity (1:1000)
/// </summary>
public int Quantity
{
get => _Quantity;
set => _Quantity = value;
}

/// <summary>
/// Dice Sides (1:1000)
/// </summary>
public int Sides
{
get => _Sides;
set => _Sides = value;
}

/// <summary>
/// Dice Adjustment (MinInt:MaxInt)
/// </summary>
public int Adjustment
{
get => _Adjustment;
Expand Down
7 changes: 6 additions & 1 deletion DiceAPI/DiceAPI.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<Version>1.0.1.0</Version>
<Authors>Dan Rhea</Authors>
<Product>DiceAPI</Product>
<Description>A dice rolling API</Description>
<Copyright>Copyright 2021 by Dan Rhea</Copyright>
</PropertyGroup>

<ItemGroup>
Expand Down
Loading

0 comments on commit aad0d40

Please sign in to comment.