Advertisement
LaughingMan

Dice Roller Class

May 13th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using Library.Enumerators;
  2. using Library.Extensions;
  3. using Microsoft.AspNetCore.Http;
  4. using System;
  5. using System.Collections.Generic;
  6.  
  7. namespace Library.Classes
  8. {
  9.     public class Dice
  10.     {
  11.         private static volatile CryptoRandom random = new CryptoRandom();
  12.  
  13.         public Dictionary<DiceType, int[]> DiceRoll { get; } = new Dictionary<DiceType, int[]>
  14.         {
  15.             {DiceType.D2, Array.Empty<int>()},
  16.             {DiceType.D4, Array.Empty<int>()},
  17.             {DiceType.D6, Array.Empty<int>()},
  18.             {DiceType.D8, Array.Empty<int>()},
  19.             {DiceType.D10, Array.Empty<int>()},
  20.             {DiceType.D12, Array.Empty<int>()},
  21.             {DiceType.D20, Array.Empty<int>()},
  22.             {DiceType.D69, Array.Empty<int>()},
  23.             {DiceType.D100, Array.Empty<int>()},
  24.             {DiceType.D1000, Array.Empty<int>()}
  25.         };
  26.  
  27.         public Dice()
  28.         {
  29.             DiceRoll[DiceType.D2] = Array.Empty<int>();
  30.             DiceRoll[DiceType.D4] = Array.Empty<int>();
  31.             DiceRoll[DiceType.D6] = Array.Empty<int>();
  32.             DiceRoll[DiceType.D8] = Array.Empty<int>();
  33.             DiceRoll[DiceType.D10] = Array.Empty<int>();
  34.             DiceRoll[DiceType.D12] = Array.Empty<int>();
  35.             DiceRoll[DiceType.D20] = Array.Empty<int>();
  36.             DiceRoll[DiceType.D69] = Array.Empty<int>();
  37.             DiceRoll[DiceType.D100] = Array.Empty<int>();
  38.             DiceRoll[DiceType.D1000] = Array.Empty<int>();
  39.         }
  40.  
  41.         public Dice(int? d2 = 0, int? d4 = 0, int? d6 = 0, int? d8 = 0, int? d10 = 0, int? d12 = 0, int? d20 = 0, int? d69 = 0, int? d100 = 0, int? d1000 = 0)
  42.         {
  43.             ProcessInput(d2, d4, d6, d8, d10, d12, d20, d69, d100, d1000);
  44.         }
  45.  
  46.         public Dice(HttpRequest request)
  47.         {
  48.             if (request == null) // Check for nulls... "Object reference not set to an instance of an object" is a beginner error to get... Always make sure you have a value to work with
  49.                 throw new Exception("No request was passed. Unable to retrieve dice roll from request.");
  50.  
  51.             IQueryCollection query = request.Query;
  52.  
  53.             if (query == null || !(query.Count > 0))
  54.                 throw new Exception("No query arguments were passed. Unable to retrieve dice roll from request.");
  55.  
  56.             ProcessInput(query.ContainsKey("d2") ? ((string)query["d2"]).ToInt() : 0, // These are 'ternary operators'. {Boolean condition} ? {True Result} : {False Result}; Very similar to CASE WHEN THEN ELSE END
  57.                         query.ContainsKey("d4") ? ((string)query["d4"]).ToInt() : 0,
  58.                         query.ContainsKey("d6") ? ((string)query["d6"]).ToInt() : 0,
  59.                         query.ContainsKey("d8") ? ((string)query["d8"]).ToInt() : 0,
  60.                         query.ContainsKey("d10") ? ((string)query["d10"]).ToInt() : 0,
  61.                         query.ContainsKey("d12") ? ((string)query["d12"]).ToInt() : 0,
  62.                         query.ContainsKey("d20") ? ((string)query["d20"]).ToInt() : 0,
  63.                         query.ContainsKey("d69") ? ((string)query["d69"]).ToInt() : 0,
  64.                         query.ContainsKey("d100") ? ((string)query["d100"]).ToInt() : 0,
  65.                         query.ContainsKey("d1000") ? ((string)query["d1000"]).ToInt() : 0);
  66.         }
  67.  
  68.         private void ProcessInput(int? d2 = 0, int? d4 = 0, int? d6 = 0, int? d8 = 0, int? d10 = 0, int? d12 = 0, int? d20 = 0, int? d69 = 0, int? d100 = 0, int? d1000 = 0)
  69.         {
  70.             // int? is a nullable integer. Normally int cannot be set to null but int? can...
  71.             // This allows us to very easily specify optional parameters for integers in method overloads
  72.  
  73.             if (d2 != null && d2.HasValue)
  74.                 DiceRoll[DiceType.D2] = new int[d2.Value];
  75.  
  76.             if (d4 != null && d4.HasValue)
  77.                 DiceRoll[DiceType.D4] = new int[d4.Value];
  78.  
  79.             if (d6 != null && d6.HasValue)
  80.                 DiceRoll[DiceType.D6] = new int[d6.Value];
  81.  
  82.             if (d8 != null && d8.HasValue)
  83.                 DiceRoll[DiceType.D8] = new int[d8.Value];
  84.  
  85.             if (d10 != null && d10.HasValue)
  86.                 DiceRoll[DiceType.D10] = new int[d10.Value];
  87.  
  88.             if (d12 != null && d12.HasValue)
  89.                 DiceRoll[DiceType.D12] = new int[d12.Value];
  90.  
  91.             if (d20 != null && d20.HasValue)
  92.                 DiceRoll[DiceType.D20] = new int[d20.Value];
  93.  
  94.             if (d69 != null && d69.HasValue)
  95.                 DiceRoll[DiceType.D69] = new int[d69.Value];
  96.  
  97.             if (d100 != null && d100.HasValue)
  98.                 DiceRoll[DiceType.D100] = new int[d100.Value];
  99.  
  100.             if (d1000 != null && d1000.HasValue)
  101.                 DiceRoll[DiceType.D1000] = new int[d1000.Value];
  102.         }
  103.  
  104.         public void Roll()
  105.         {
  106.             foreach (var diceRoll in DiceRoll)
  107.             {
  108.                 for (int i = 0; i < diceRoll.Value.Length; i++)
  109.                 {
  110.                     diceRoll.Value[i] = random.Next(1, (int)diceRoll.Key + 1);
  111.                 }
  112.             }
  113.         }
  114.  
  115.         public void Roll(int? d2 = 0, int? d4 = 0, int? d6 = 0, int? d8 = 0, int? d10 = 0, int? d12 = 0, int? d20 = 0, int? d69 = 0, int? d100 = 0, int? d1000 = 0)
  116.         {
  117.             ProcessInput(d2, d4, d6, d8, d10, d12, d20, d69, d100, d1000);
  118.  
  119.             Roll();
  120.         }
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement