Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Library.Enumerators;
- using Library.Extensions;
- using Microsoft.AspNetCore.Http;
- using System;
- using System.Collections.Generic;
- namespace Library.Classes
- {
- public class Dice
- {
- private static volatile CryptoRandom random = new CryptoRandom();
- public Dictionary<DiceType, int[]> DiceRoll { get; } = new Dictionary<DiceType, int[]>
- {
- {DiceType.D2, Array.Empty<int>()},
- {DiceType.D4, Array.Empty<int>()},
- {DiceType.D6, Array.Empty<int>()},
- {DiceType.D8, Array.Empty<int>()},
- {DiceType.D10, Array.Empty<int>()},
- {DiceType.D12, Array.Empty<int>()},
- {DiceType.D20, Array.Empty<int>()},
- {DiceType.D69, Array.Empty<int>()},
- {DiceType.D100, Array.Empty<int>()},
- {DiceType.D1000, Array.Empty<int>()}
- };
- public Dice()
- {
- DiceRoll[DiceType.D2] = Array.Empty<int>();
- DiceRoll[DiceType.D4] = Array.Empty<int>();
- DiceRoll[DiceType.D6] = Array.Empty<int>();
- DiceRoll[DiceType.D8] = Array.Empty<int>();
- DiceRoll[DiceType.D10] = Array.Empty<int>();
- DiceRoll[DiceType.D12] = Array.Empty<int>();
- DiceRoll[DiceType.D20] = Array.Empty<int>();
- DiceRoll[DiceType.D69] = Array.Empty<int>();
- DiceRoll[DiceType.D100] = Array.Empty<int>();
- DiceRoll[DiceType.D1000] = Array.Empty<int>();
- }
- 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)
- {
- ProcessInput(d2, d4, d6, d8, d10, d12, d20, d69, d100, d1000);
- }
- public Dice(HttpRequest request)
- {
- 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
- throw new Exception("No request was passed. Unable to retrieve dice roll from request.");
- IQueryCollection query = request.Query;
- if (query == null || !(query.Count > 0))
- throw new Exception("No query arguments were passed. Unable to retrieve dice roll from request.");
- 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
- query.ContainsKey("d4") ? ((string)query["d4"]).ToInt() : 0,
- query.ContainsKey("d6") ? ((string)query["d6"]).ToInt() : 0,
- query.ContainsKey("d8") ? ((string)query["d8"]).ToInt() : 0,
- query.ContainsKey("d10") ? ((string)query["d10"]).ToInt() : 0,
- query.ContainsKey("d12") ? ((string)query["d12"]).ToInt() : 0,
- query.ContainsKey("d20") ? ((string)query["d20"]).ToInt() : 0,
- query.ContainsKey("d69") ? ((string)query["d69"]).ToInt() : 0,
- query.ContainsKey("d100") ? ((string)query["d100"]).ToInt() : 0,
- query.ContainsKey("d1000") ? ((string)query["d1000"]).ToInt() : 0);
- }
- 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)
- {
- // int? is a nullable integer. Normally int cannot be set to null but int? can...
- // This allows us to very easily specify optional parameters for integers in method overloads
- if (d2 != null && d2.HasValue)
- DiceRoll[DiceType.D2] = new int[d2.Value];
- if (d4 != null && d4.HasValue)
- DiceRoll[DiceType.D4] = new int[d4.Value];
- if (d6 != null && d6.HasValue)
- DiceRoll[DiceType.D6] = new int[d6.Value];
- if (d8 != null && d8.HasValue)
- DiceRoll[DiceType.D8] = new int[d8.Value];
- if (d10 != null && d10.HasValue)
- DiceRoll[DiceType.D10] = new int[d10.Value];
- if (d12 != null && d12.HasValue)
- DiceRoll[DiceType.D12] = new int[d12.Value];
- if (d20 != null && d20.HasValue)
- DiceRoll[DiceType.D20] = new int[d20.Value];
- if (d69 != null && d69.HasValue)
- DiceRoll[DiceType.D69] = new int[d69.Value];
- if (d100 != null && d100.HasValue)
- DiceRoll[DiceType.D100] = new int[d100.Value];
- if (d1000 != null && d1000.HasValue)
- DiceRoll[DiceType.D1000] = new int[d1000.Value];
- }
- public void Roll()
- {
- foreach (var diceRoll in DiceRoll)
- {
- for (int i = 0; i < diceRoll.Value.Length; i++)
- {
- diceRoll.Value[i] = random.Next(1, (int)diceRoll.Key + 1);
- }
- }
- }
- 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)
- {
- ProcessInput(d2, d4, d6, d8, d10, d12, d20, d69, d100, d1000);
- Roll();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement