Update dice to take multiple dices and take less space in the chat
This commit is contained in:
parent
7f833b05d3
commit
a7e3bb957f
1 changed files with 97 additions and 43 deletions
|
@ -1,6 +1,8 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Discord;
|
|
||||||
using Discord.Commands;
|
using Discord.Commands;
|
||||||
|
|
||||||
namespace Geekbot.net.Modules
|
namespace Geekbot.net.Modules
|
||||||
|
@ -16,55 +18,107 @@ namespace Geekbot.net.Modules
|
||||||
|
|
||||||
[Command("dice", RunMode = RunMode.Async)]
|
[Command("dice", RunMode = RunMode.Async)]
|
||||||
[Summary("Roll a dice.")]
|
[Summary("Roll a dice.")]
|
||||||
public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d6")
|
public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d20")
|
||||||
{
|
{
|
||||||
var dice = diceType.Split("d");
|
var splitedDices = diceType.Split("+");
|
||||||
var maxAndMod = dice[1].Split("+");
|
var dices = new List<DiceTypeDto>();
|
||||||
|
var mod = 0;
|
||||||
|
foreach (var i in splitedDices)
|
||||||
|
{
|
||||||
|
var dice = toDice(i);
|
||||||
|
if (dice.sides != 0 && dice.times != 0)
|
||||||
|
{
|
||||||
|
dices.Add(dice);
|
||||||
|
}
|
||||||
|
else if (dice.mod != 0)
|
||||||
|
{
|
||||||
|
if (mod != 0)
|
||||||
|
{
|
||||||
|
await ReplyAsync("You can only have one mod");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mod = dice.mod;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (dice.Length != 2
|
if (!dices.Any())
|
||||||
|| !int.TryParse(dice[0], out int times)
|
|
||||||
|| !int.TryParse(maxAndMod[0], out int max))
|
|
||||||
{
|
{
|
||||||
await ReplyAsync("That is not a valid dice, examples are: 1d20, 1d6, 2d10, 5d12, 1d20+5, 1d6+2, etc...");
|
await ReplyAsync(
|
||||||
|
"That is not a valid dice, examples are: 1d20, 1d6, 2d6, 1d6+2, 1d6+2d8+1d20+6, etc...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
int modifier = 0;
|
var rep = new StringBuilder();
|
||||||
if (maxAndMod.Length == 2 && !int.TryParse(maxAndMod[1], out modifier) || maxAndMod.Length > 2)
|
rep.AppendLine($":game_die: {Context.User.Mention}");
|
||||||
{
|
rep.Append("**Result:** ");
|
||||||
await ReplyAsync("That is not a valid dice, examples are: 1d20, 1d6, 2d10, 5d12, 1d20+5, 1d6+2, etc...");
|
var resultStrings = new List<string>();
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (times > 10 && !(times < 0))
|
|
||||||
{
|
|
||||||
await ReplyAsync("You can only roll between 1 and 10 dices");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (max > 100 && !(max < 1))
|
|
||||||
{
|
|
||||||
await ReplyAsync("The dice must have between 1 and 100 sides");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
var eb = new EmbedBuilder();
|
|
||||||
eb.WithAuthor(new EmbedAuthorBuilder()
|
|
||||||
.WithIconUrl(Context.User.GetAvatarUrl())
|
|
||||||
.WithName(Context.User.Username));
|
|
||||||
eb.WithColor(new Color(133, 189, 219));
|
|
||||||
eb.Title = $":game_die: Dice Roll - Type {diceType} :game_die:";
|
|
||||||
|
|
||||||
var total = 0;
|
var total = 0;
|
||||||
for (var i = 0; i < times; i++)
|
var extraText = "";
|
||||||
|
foreach (var dice in dices)
|
||||||
{
|
{
|
||||||
var roll = rnd.Next(1, max);
|
var results = new List<int>();
|
||||||
eb.AddInlineField($"Dice {i + 1}", roll);
|
for (var i = 0; i < dice.times; i++)
|
||||||
total = total + roll;
|
{
|
||||||
|
var roll = rnd.Next(1, dice.sides);
|
||||||
|
total += roll;
|
||||||
|
results.Add(roll);
|
||||||
|
if (roll == dice.sides)
|
||||||
|
{
|
||||||
|
extraText = "**Critical Hit!**";
|
||||||
|
}
|
||||||
|
if (roll == 1)
|
||||||
|
{
|
||||||
|
extraText = "**Critical Fail!**";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resultStrings.Add($"{dice.diceType} ({string.Join(",", results)})");
|
||||||
}
|
}
|
||||||
|
rep.Append(string.Join(" + ", resultStrings));
|
||||||
eb.AddField("Total", modifier == 0 ? $"{total}" : $"{total + modifier} ({total} +{modifier})");
|
if (mod != 0)
|
||||||
await ReplyAsync("", false, eb.Build());
|
{
|
||||||
|
rep.Append($" + {mod}");
|
||||||
|
total += mod;
|
||||||
|
}
|
||||||
|
rep.AppendLine();
|
||||||
|
rep.AppendLine($"**Total:** {total}");
|
||||||
|
if (extraText != "")
|
||||||
|
{
|
||||||
|
rep.AppendLine(extraText);
|
||||||
|
}
|
||||||
|
await ReplyAsync(rep.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
private DiceTypeDto toDice(string dice)
|
||||||
|
{
|
||||||
|
var diceParts = dice.Split('d');
|
||||||
|
if (diceParts.Length == 2
|
||||||
|
&& int.TryParse(diceParts[0], out int times)
|
||||||
|
&& int.TryParse(diceParts[1], out int max))
|
||||||
|
{
|
||||||
|
return new DiceTypeDto()
|
||||||
|
{
|
||||||
|
diceType = dice,
|
||||||
|
times = times,
|
||||||
|
sides = max
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (dice.Length == 1
|
||||||
|
&& int.TryParse(diceParts[0], out int mod))
|
||||||
|
{
|
||||||
|
return new DiceTypeDto()
|
||||||
|
{
|
||||||
|
mod = mod
|
||||||
|
};
|
||||||
|
}
|
||||||
|
return new DiceTypeDto();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
class DiceTypeDto
|
||||||
|
{
|
||||||
|
public string diceType { get; set; }
|
||||||
|
public int times { get; set; }
|
||||||
|
public int sides { get; set; }
|
||||||
|
public int mod { get; set; }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue