2017-09-15 00:01:00 +02:00
|
|
|
|
using System;
|
2017-09-24 22:45:49 +02:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2017-09-15 00:01:00 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.Commands;
|
2017-10-12 16:34:10 +02:00
|
|
|
|
using Geekbot.net.Lib;
|
2017-09-15 00:01:00 +02:00
|
|
|
|
|
2017-10-02 21:57:48 +02:00
|
|
|
|
namespace Geekbot.net.Commands
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
|
|
|
|
public class Dice : ModuleBase
|
|
|
|
|
{
|
|
|
|
|
private readonly Random rnd;
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-09-15 00:01:00 +02:00
|
|
|
|
public Dice(Random RandomClient)
|
|
|
|
|
{
|
|
|
|
|
rnd = RandomClient;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("dice", RunMode = RunMode.Async)]
|
2017-10-12 16:34:10 +02:00
|
|
|
|
[Remarks(CommandCategories.Randomness)]
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Summary("Roll a dice.")]
|
2017-09-24 22:45:49 +02:00
|
|
|
|
public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d20")
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
2017-09-24 22:45:49 +02:00
|
|
|
|
var splitedDices = diceType.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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-15 22:56:03 +02:00
|
|
|
|
|
2017-09-24 22:45:49 +02:00
|
|
|
|
if (!dices.Any())
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
2017-09-24 22:45:49 +02:00
|
|
|
|
await ReplyAsync(
|
|
|
|
|
"That is not a valid dice, examples are: 1d20, 1d6, 2d6, 1d6+2, 1d6+2d8+1d20+6, etc...");
|
2017-09-15 00:01:00 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
2017-09-24 22:45:49 +02:00
|
|
|
|
|
2017-12-24 13:11:16 +01:00
|
|
|
|
|
|
|
|
|
if (dices.Any(d => d.times > 20))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("You can't throw more than 20 dices");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (dices.Any(d => d.sides > 120))
|
|
|
|
|
{
|
|
|
|
|
await ReplyAsync("A dice can't have more than 120 sides");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-09-24 22:45:49 +02:00
|
|
|
|
var rep = new StringBuilder();
|
|
|
|
|
rep.AppendLine($":game_die: {Context.User.Mention}");
|
|
|
|
|
rep.Append("**Result:** ");
|
|
|
|
|
var resultStrings = new List<string>();
|
|
|
|
|
var total = 0;
|
|
|
|
|
var extraText = "";
|
|
|
|
|
foreach (var dice in dices)
|
2017-09-18 23:31:11 +02:00
|
|
|
|
{
|
2017-09-24 22:45:49 +02:00
|
|
|
|
var results = new List<int>();
|
|
|
|
|
for (var i = 0; i < dice.times; i++)
|
|
|
|
|
{
|
|
|
|
|
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)})");
|
2017-09-18 23:31:11 +02:00
|
|
|
|
}
|
2017-09-24 22:45:49 +02:00
|
|
|
|
rep.Append(string.Join(" + ", resultStrings));
|
|
|
|
|
if (mod != 0)
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
2017-09-24 22:45:49 +02:00
|
|
|
|
rep.Append($" + {mod}");
|
|
|
|
|
total += mod;
|
2017-09-15 00:01:00 +02:00
|
|
|
|
}
|
2017-09-24 22:45:49 +02:00
|
|
|
|
rep.AppendLine();
|
|
|
|
|
rep.AppendLine($"**Total:** {total}");
|
|
|
|
|
if (extraText != "")
|
2017-09-15 00:01:00 +02:00
|
|
|
|
{
|
2017-09-24 22:45:49 +02:00
|
|
|
|
rep.AppendLine(extraText);
|
2017-09-15 00:01:00 +02:00
|
|
|
|
}
|
2017-09-24 22:45:49 +02:00
|
|
|
|
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))
|
2017-09-18 23:31:11 +02:00
|
|
|
|
{
|
2017-09-24 22:45:49 +02:00
|
|
|
|
return new DiceTypeDto()
|
|
|
|
|
{
|
|
|
|
|
mod = mod
|
|
|
|
|
};
|
2017-09-18 23:31:11 +02:00
|
|
|
|
}
|
2017-09-24 22:45:49 +02:00
|
|
|
|
return new DiceTypeDto();
|
2017-09-15 00:01:00 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-24 22:45:49 +02:00
|
|
|
|
|
|
|
|
|
class DiceTypeDto
|
|
|
|
|
{
|
|
|
|
|
public string diceType { get; set; }
|
|
|
|
|
public int times { get; set; }
|
|
|
|
|
public int sides { get; set; }
|
|
|
|
|
public int mod { get; set; }
|
|
|
|
|
}
|
|
|
|
|
}
|