diff --git a/ChaosBot/Discord/Modules/DiceCommands.cs b/ChaosBot/Discord/Modules/DiceCommands.cs index fbea25d..828a2a9 100644 --- a/ChaosBot/Discord/Modules/DiceCommands.cs +++ b/ChaosBot/Discord/Modules/DiceCommands.cs @@ -1,16 +1,122 @@ +using System; +using System.Linq; +using System.Text; using System.Threading.Tasks; using Dice; +using Discord; using Discord.Commands; using Microsoft.VisualBasic; +using NLog; namespace ChaosBot.Discord.Modules { public class DiceCommands : ModuleBase { + private static Logger _logger = Program._logger; + [Command("roll")] public async Task Roll(params string[] args) { - await ReplyAsync($"You rolled {Roller.Roll(Strings.Join(args, " ")).Value}"); + string diceRecipe = Strings.Join(args, " "); + + try + { + var sb = new StringBuilder(); + var embed = new EmbedBuilder(); + + RollResult rollResult = Roller.Roll(diceRecipe); + + embed.WithColor(new Color(255, 255,0)); + // embed.Title = $"Dice Roll"; + sb.AppendLine($"{Context.User.Mention} :game_die:"); + sb.AppendLine($"Result: {diceRecipe} ({DiceOutput(rollResult)})"); + sb.AppendLine($"Total: {rollResult.Value}"); + + /* + * Add the string to the Embed + */ + embed.Description = sb.ToString(); + + /* + * Reply with the Embed created above + */ + await ReplyAsync(null, false, embed.Build()); + } + catch (Exception ex) + { + _logger.Error($"diceCommands.RollCommand: Exception [{ex}] thrown, <[{ex.Message}]>."); + } + } + + private string DiceOutput(RollResult rollResult) + { + if (rollResult.RollRoot == null) return ""; + StringBuilder sb = new StringBuilder(); + + foreach (DieResult dieResult in rollResult.RollRoot.Values) + { + string result = ParseDiceResult(dieResult); + + sb.Append(DieFormatting(dieResult.Flags)); + sb.Append(result); + sb.Append(DieFormatting(dieResult.Flags, true)); + } + + return sb.ToString(); + } + + private string ParseDiceResult(DieResult dieResult) + { + switch (dieResult.DieType) + { + case DieType.Normal: + case DieType.Literal: + return dieResult.Value.ToString(); + case DieType.Special: + switch (dieResult.SpecialDie) + { + case SpecialDie.Add: + return "+"; + case SpecialDie.Comma: + return ","; + case SpecialDie.Divide: + return "/"; + case SpecialDie.Multiply: + return "*"; + case SpecialDie.Negate: + return "~"; + case SpecialDie.Subtract: + return "-"; + case SpecialDie.Text: + return dieResult.Data; + case SpecialDie.CloseParen: + return ")"; + case SpecialDie.OpenParen: + return "("; + } + + break; + } + + return ""; + } + + private string DieFormatting(DieFlags flags, bool reverse=false) + { + StringBuilder sb = new StringBuilder(); + if ((flags & (DieFlags.Critical ^ DieFlags.Success ^ DieFlags.Failure ^ DieFlags.Fumble)) != 0) + sb.Append("**"); + + if ((flags & DieFlags.Dropped) != 0) + sb.Append("~~"); + + if ((flags & DieFlags.Extra) != 0) + sb.Append("*"); + + if (reverse) + return new string(sb.ToString().Reverse().ToArray()); + + return sb.ToString(); } } } \ No newline at end of file