geekbot/Geekbot.net/Commands/Dice.cs

123 lines
3.7 KiB
C#
Raw Normal View History

2017-09-15 00:01:00 +02:00
using System;
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
namespace Geekbot.net.Commands
2017-09-15 00:01:00 +02:00
{
public class Dice : ModuleBase
{
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.")]
public async Task RollCommand([Remainder] [Summary("diceType")] string diceType = "1d20")
2017-09-15 00:01:00 +02:00
{
var splitedDices = diceType.Split("+");
var dices = new List<DiceTypeDto>();
var mod = 0;
foreach (var i in splitedDices)
{
2018-04-30 23:44:19 +02:00
var dice = ToDice(i);
if (dice.Sides != 0 && dice.Times != 0)
{
dices.Add(dice);
}
2018-04-30 23:44:19 +02:00
else if (dice.Mod != 0)
{
if (mod != 0)
{
await ReplyAsync("You can only have one mod");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
mod = dice.Mod;
}
}
2017-09-15 22:56:03 +02:00
if (!dices.Any())
2017-09-15 00:01:00 +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-12-24 13:11:16 +01:00
2018-04-30 23:44:19 +02:00
if (dices.Any(d => d.Times > 20))
2017-12-24 13:11:16 +01:00
{
await ReplyAsync("You can't throw more than 20 dices");
return;
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
if (dices.Any(d => d.Sides > 120))
2017-12-24 13:11:16 +01:00
{
await ReplyAsync("A dice can't have more than 120 sides");
return;
}
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)
{
var results = new List<int>();
2018-04-30 23:44:19 +02:00
for (var i = 0; i < dice.Times; i++)
{
2018-04-30 23:44:19 +02:00
var roll = new Random().Next(1, dice.Sides);
total += roll;
results.Add(roll);
2018-04-30 23:44:19 +02:00
if (roll == dice.Sides) extraText = "**Critical Hit!**";
2017-12-29 01:53:50 +01:00
if (roll == 1) extraText = "**Critical Fail!**";
}
2017-12-29 01:53:50 +01:00
2018-04-30 23:44:19 +02:00
resultStrings.Add($"{dice.DiceType} ({string.Join(",", results)})");
}
2017-12-29 01:53:50 +01:00
rep.Append(string.Join(" + ", resultStrings));
if (mod != 0)
2017-09-15 00:01:00 +02:00
{
rep.Append($" + {mod}");
total += mod;
2017-09-15 00:01:00 +02:00
}
2017-12-29 01:53:50 +01:00
rep.AppendLine();
rep.AppendLine($"**Total:** {total}");
2017-12-29 01:53:50 +01:00
if (extraText != "") rep.AppendLine(extraText);
await ReplyAsync(rep.ToString());
}
2018-04-30 23:44:19 +02:00
private DiceTypeDto ToDice(string dice)
{
var diceParts = dice.Split('d');
if (diceParts.Length == 2
2017-12-29 01:53:50 +01:00
&& int.TryParse(diceParts[0], out var times)
&& int.TryParse(diceParts[1], out var max))
return new DiceTypeDto
{
2018-04-30 23:44:19 +02:00
DiceType = dice,
Times = times,
Sides = max
};
if (dice.Length == 1
2017-12-29 01:53:50 +01:00
&& int.TryParse(diceParts[0], out var mod))
return new DiceTypeDto
{
2018-04-30 23:44:19 +02:00
Mod = mod
};
return new DiceTypeDto();
2017-09-15 00:01:00 +02:00
}
}
2017-12-29 01:53:50 +01:00
internal class DiceTypeDto
{
2018-04-30 23:44:19 +02:00
public string DiceType { get; set; }
public int Times { get; set; }
public int Sides { get; set; }
public int Mod { get; set; }
}
}